0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-27 01:02:46 +01:00
construct/ircd/resource.cc

135 lines
3.6 KiB
C++
Raw Normal View History

2016-09-06 01:05:16 +02:00
/*
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace ircd {
2016-11-29 16:23:38 +01:00
IRCD_INIT_PRIORITY(STD_CONTAINER)
decltype(resource::resources)
resource::resources
{};
2016-09-06 01:05:16 +02:00
} // namespace ircd
2016-11-29 16:23:38 +01:00
ircd::resource::resource(const char *const &name,
const char *const &description)
2016-09-06 01:05:16 +02:00
:name{name}
2016-11-29 16:23:38 +01:00
,description{description}
,resources_it{[this, &name]
2016-09-06 01:05:16 +02:00
{
2016-11-29 16:23:38 +01:00
const auto iit(resources.emplace(this->name, this));
if(!iit.second)
throw error("resource \"%s\" already registered", name);
return iit.first;
}()}
{
log::info("Registered resource \"%s\" by handler @ %p", name, this);
2016-09-06 01:05:16 +02:00
}
2016-11-29 16:23:38 +01:00
ircd::resource::~resource()
2016-09-06 01:05:16 +02:00
noexcept
{
2016-11-29 16:23:38 +01:00
resources.erase(resources_it);
log::info("Unregistered resource \"%s\" by handler @ %p", name, this);
2016-09-06 01:05:16 +02:00
}
void
2016-11-29 16:23:38 +01:00
ircd::resource::operator()(client &client,
2017-03-13 23:24:42 +01:00
parse::capstan &pc,
2017-03-11 02:46:25 +01:00
const http::request::head &head)
2016-11-29 16:23:38 +01:00
const try
2016-09-06 01:05:16 +02:00
{
2017-03-11 02:46:25 +01:00
const auto &method(*methods.at(head.method));
http::request::content content{pc, head};
2017-03-11 02:46:25 +01:00
resource::request request
{
head, content
};
method(client, request);
2016-11-29 16:23:38 +01:00
}
catch(const std::out_of_range &e)
{
2017-03-11 02:46:25 +01:00
throw http::error(http::METHOD_NOT_ALLOWED);
2016-09-06 01:05:16 +02:00
}
2016-11-29 16:23:38 +01:00
ircd::resource::method::method(struct resource &resource,
const char *const &name,
const handler &handler,
const std::initializer_list<member *> &members)
:function{handler}
,name{name}
,resource{&resource}
,methods_it{[this, &name]
2016-09-06 01:05:16 +02:00
{
2016-11-29 16:23:38 +01:00
const auto iit(this->resource->methods.emplace(this->name, this));
2016-09-06 01:05:16 +02:00
if(!iit.second)
2016-11-29 16:23:38 +01:00
throw error("resource \"%s\" already registered", name);
2016-09-06 01:05:16 +02:00
2016-11-29 16:23:38 +01:00
return iit.first;
}()}
{
for(const auto &member : members)
this->members.emplace(member->name, member);
2016-09-06 01:05:16 +02:00
}
2016-11-29 16:23:38 +01:00
ircd::resource::method::~method()
noexcept
2016-09-06 01:05:16 +02:00
{
2016-11-29 16:23:38 +01:00
resource->methods.erase(methods_it);
2016-09-06 01:05:16 +02:00
}
2016-11-29 16:23:38 +01:00
2017-03-11 04:31:20 +01:00
ircd::resource::response::response(client &client,
const json::obj &doc,
const http::code &code)
2016-09-06 01:05:16 +02:00
{
2017-03-11 04:31:20 +01:00
char cbuf[1024];
response(client, serialize(doc, cbuf, cbuf + sizeof(cbuf)), code);
2017-03-11 04:31:20 +01:00
}
ircd::resource::response::response(client &client,
const json::doc &doc,
const http::code &code)
{
http::response
2017-03-11 04:31:20 +01:00
{
code, doc, write_closure(client),
{
{ "Content-Type", "application/json" }
}
2017-03-11 04:31:20 +01:00
};
2016-09-06 01:05:16 +02:00
}
2016-11-29 16:23:38 +01:00
ircd::resource::response::~response()
noexcept
2016-09-06 01:05:16 +02:00
{
}
2016-11-29 16:23:38 +01:00
ircd::resource::member::member(const char *const &name,
const enum json::type &type,
validator valid)
:name{name}
,type{type}
,valid{std::move(valid)}
2016-09-06 01:05:16 +02:00
{
}