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

305 lines
7.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.
*/
#include <ircd/m.h>
2016-09-06 01:05:16 +02:00
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
IRCD_INIT_PRIORITY(STD_CONTAINER)
decltype(resource::tokens)
resource::tokens
{};
2016-09-06 01:05:16 +02:00
} // namespace ircd
ircd::resource &
ircd::resource::find(string_view path)
{
path = lstrip(path, '/');
path = strip(path, '/');
auto it(resources.lower_bound(path));
if(it == end(resources))
return *resources.at(string_view{});
2016-09-06 01:05:16 +02:00
// Exact file or directory match
if(path == it->first)
return *it->second;
// Directories handle all paths under them.
if(!startswith(path, it->first))
{
// Walk the iterator back to find if there is a directory prefixing this path.
if(it == begin(resources))
throw http::error(http::code::NOT_FOUND);
--it;
if(!startswith(path, it->first))
throw http::error(http::code::NOT_FOUND);
}
// Check if the resource is a directory; if not, it can only
// handle exact path matches.
if(~it->second->flags & it->second->DIRECTORY && path != it->first)
throw http::error(http::code::NOT_FOUND);
return *it->second;
}
ircd::resource::resource(const string_view &path,
opts opts)
:resource
{
path, opts.description, opts
}
{
}
ircd::resource::resource(const string_view &path,
const string_view &description,
opts opts)
:path{path}
2016-11-29 16:23:38 +01:00
,description{description}
,flags{opts.flags}
,resources_it{[this, &path]
2016-09-06 01:05:16 +02:00
{
const auto iit(resources.emplace(this->path, this));
2016-11-29 16:23:38 +01:00
if(!iit.second)
throw error("resource \"%s\" already registered", path);
2016-11-29 16:23:38 +01:00
return unique_const_iterator<decltype(resources)>
{
resources, iit.first
};
2016-11-29 16:23:38 +01:00
}()}
{
log::info("Registered resource \"%s\" by handler @ %p", path, 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
{
log::info("Unregistered resource \"%s\" by handler @ %p", path, this);
2016-09-06 01:05:16 +02:00
}
namespace ircd {
static void
authenticate(client &client,
resource::method &method,
resource::request &request)
try
{
const auto &access_token(request.query.at("access_token"));
if(access_token == "charybdisLETMEIN")
return;
const auto it(resource::tokens.find(access_token));
if(it == end(resource::tokens) || it->second != &client)
{
throw m::error
{
// "When credentials are required but missing or invalid, the HTTP call will "
// "return with a status of 401 and the error code, M_MISSING_TOKEN or "
// "M_UNKNOWN_TOKEN respectively."
http::UNAUTHORIZED, "M_UNKNOWN_TOKEN", "Credentials for this method are required but invalid."
};
}
}
catch(const std::out_of_range &e)
{
throw m::error
{
// "When credentials are required but missing or invalid, the HTTP call will return "
// "with a status of 401 and the error code, M_MISSING_TOKEN or M_UNKNOWN_TOKEN "
// "respectively."
http::UNAUTHORIZED, "M_MISSING_TOKEN", "Credentials for this method are required but missing."
};
}
} // namespace ircd
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-09-06 01:05:16 +02:00
{
auto &method(operator[](head.method));
http::request::content content{pc, head};
2017-03-11 02:46:25 +01:00
resource::request request
{
head, content, head.query
2017-03-11 02:46:25 +01:00
};
if(method.flags & method.REQUIRES_AUTH)
authenticate(client, method, request);
handle_request(client, method, request);
}
ircd::resource::method &
ircd::resource::operator[](const string_view &name)
try
{
return *methods.at(name);
2016-11-29 16:23:38 +01:00
}
catch(const std::out_of_range &e)
{
2017-03-21 03:30:07 +01:00
throw http::error
{
http::METHOD_NOT_ALLOWED
};
}
void
ircd::resource::handle_request(client &client,
method &method,
resource::request &request)
2017-03-21 03:30:07 +01:00
try
{
method(client, request);
}
catch(const json::error &e)
{
throw m::error
{
"M_BAD_JSON", "Required JSON field: %s", e.what()
};
2016-09-06 01:05:16 +02:00
}
2016-11-29 16:23:38 +01:00
ircd::resource::method::method(struct resource &resource,
const string_view &name,
2016-11-29 16:23:38 +01:00
const handler &handler,
opts opts)
:name{name}
2016-11-29 16:23:38 +01:00
,resource{&resource}
,function{handler}
,flags{opts.flags}
2016-11-29 16:23:38 +01:00
,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
return unique_const_iterator<decltype(resource::methods)>
{
this->resource->methods,
iit.first
};
2016-11-29 16:23:38 +01:00
}()}
{
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
{
}
ircd::resource::response
ircd::resource::method::operator()(client &client,
request &request)
try
{
return function(client, request);
}
catch(const std::bad_function_call &e)
{
throw http::error
{
http::SERVICE_UNAVAILABLE
};
}
ircd::resource::request::request(const http::request::head &head,
http::request::content &content,
http::query::string query)
:json::doc{content}
,head{head}
,content{content}
,query{std::move(query)}
{
}
ircd::resource::response::response(client &client,
const http::code &code,
const json::obj &obj)
:response{client, obj, code}
{
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,
2017-03-21 03:30:07 +01:00
const json::obj &obj,
2017-03-11 04:31:20 +01:00
const http::code &code)
2017-03-21 03:30:07 +01:00
try
2016-09-06 01:05:16 +02:00
{
char cbuf[4096], *out(cbuf);
2017-03-21 03:30:07 +01:00
const auto doc(serialize(obj, out, cbuf + sizeof(cbuf)));
response(client, doc, code);
}
catch(const json::error &e)
{
throw m::error
{
http::INTERNAL_SERVER_ERROR, "M_NOT_JSON", "Generator Protection: %s", e.what()
};
2017-03-11 04:31:20 +01:00
}
ircd::resource::response::response(client &client,
const json::doc &doc,
const http::code &code)
{
const auto content_type
2017-03-11 04:31:20 +01:00
{
"application/json; charset=utf-8"
2017-03-11 04:31:20 +01:00
};
2016-09-06 01:05:16 +02:00
response(client, doc, content_type, code);
2016-09-06 01:05:16 +02:00
}
ircd::resource::response::response(client &client,
const string_view &str,
const string_view &content_type,
const http::code &code)
2016-09-06 01:05:16 +02:00
{
http::response
{
code, str, write_closure(client),
{
{ "Content-Type", content_type },
{ "Access-Control-Allow-Origin", "*" }
}
};
log::debug("client[%s] HTTP %d %s (%s) content-length: %zu %s...",
string(remote_addr(client)),
int(code),
http::reason[code],
content_type,
str.size(),
startswith(content_type, "text") ||
content_type == "application/json" ||
content_type == "application/javascript"? str.substr(0, 96) : string_view{});
2016-09-06 01:05:16 +02:00
}