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.
|
|
|
|
*/
|
|
|
|
|
2017-11-30 20:44:23 +01:00
|
|
|
#include <ircd/m/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
|
|
|
|
|
|
|
} // namespace ircd
|
|
|
|
|
2017-08-23 23:06:14 +02:00
|
|
|
ircd::resource &
|
|
|
|
ircd::resource::find(string_view path)
|
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
path = rstrip(path, '/');
|
2017-08-23 23:06:14 +02:00
|
|
|
auto it(resources.lower_bound(path));
|
2017-08-23 23:47:54 +02:00
|
|
|
if(it == end(resources)) try
|
|
|
|
{
|
2017-10-03 13:13:52 +02:00
|
|
|
--it;
|
|
|
|
if(it == begin(resources) || !startswith(path, rstrip(it->first, '/')))
|
2017-10-16 06:26:05 +02:00
|
|
|
return *resources.at("/");
|
2017-08-23 23:47:54 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw http::error(http::code::NOT_FOUND);
|
|
|
|
}
|
2016-09-06 01:05:16 +02:00
|
|
|
|
2017-08-23 23:06:14 +02:00
|
|
|
// Exact file or directory match
|
2017-10-03 13:13:52 +02:00
|
|
|
if(path == rstrip(it->first, '/'))
|
2017-08-23 23:06:14 +02:00
|
|
|
return *it->second;
|
|
|
|
|
|
|
|
// Directories handle all paths under them.
|
2017-10-03 13:13:52 +02:00
|
|
|
if(!startswith(path, rstrip(it->first, '/')))
|
2017-08-23 23:06:14 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2017-10-03 13:13:52 +02:00
|
|
|
if(!startswith(path, rstrip(it->first, '/')))
|
2017-08-23 23:06:14 +02:00
|
|
|
throw http::error(http::code::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the resource is a directory; if not, it can only
|
|
|
|
// handle exact path matches.
|
2017-10-03 13:13:52 +02:00
|
|
|
if(~it->second->flags & it->second->DIRECTORY && path != rstrip(it->first, '/'))
|
2017-08-23 23:06:14 +02:00
|
|
|
throw http::error(http::code::NOT_FOUND);
|
|
|
|
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:26:39 +01:00
|
|
|
ircd::resource::resource(const string_view &path)
|
2017-08-23 23:06:14 +02:00
|
|
|
:resource
|
|
|
|
{
|
2017-12-12 21:26:39 +01:00
|
|
|
path, opts{}
|
2017-08-23 23:06:14 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::resource::resource(const string_view &path,
|
2017-12-12 21:26:39 +01:00
|
|
|
const opts &opts)
|
2017-08-23 23:06:14 +02:00
|
|
|
:path{path}
|
2017-12-12 21:26:39 +01:00
|
|
|
,description{opts.description}
|
2017-08-23 23:06:14 +02:00
|
|
|
,flags{opts.flags}
|
|
|
|
,resources_it{[this, &path]
|
2016-09-06 01:05:16 +02:00
|
|
|
{
|
2017-08-23 23:06:14 +02:00
|
|
|
const auto iit(resources.emplace(this->path, this));
|
2016-11-29 16:23:38 +01:00
|
|
|
if(!iit.second)
|
2017-08-23 23:06:14 +02:00
|
|
|
throw error("resource \"%s\" already registered", path);
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:06:14 +02:00
|
|
|
return unique_const_iterator<decltype(resources)>
|
|
|
|
{
|
|
|
|
resources, iit.first
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
}()}
|
|
|
|
{
|
2017-09-19 08:21:24 +02:00
|
|
|
log::info("Registered resource \"%s\"", path.empty()? string_view{"/"} : path);
|
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
|
|
|
|
{
|
2017-09-19 08:21:24 +02:00
|
|
|
log::info("Unregistered resource \"%s\"", path.empty()? string_view{"/"} : path);
|
2016-09-06 01:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
static void verify_origin(client &client, resource::method &method, resource::request &request);
|
|
|
|
static void authenticate(client &client, resource::method &method, resource::request &request);
|
|
|
|
}
|
2017-08-23 23:06:14 +02:00
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
void
|
|
|
|
ircd::authenticate(client &client,
|
|
|
|
resource::method &method,
|
|
|
|
resource::request &request)
|
2017-08-23 23:06:14 +02:00
|
|
|
try
|
|
|
|
{
|
2017-09-25 05:47:13 +02:00
|
|
|
const string_view &access_token
|
2017-09-17 00:22:54 +02:00
|
|
|
{
|
|
|
|
request.query.at("access_token")
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
// Sets up the query to find the access_token in the sessions room
|
2017-11-16 02:48:25 +01:00
|
|
|
const m::vm::query<m::vm::where::equal> query
|
2017-09-17 00:22:54 +02:00
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
{ "type", "ircd.access_token" },
|
|
|
|
{ "state_key", access_token },
|
|
|
|
{ "room_id", m::user::sessions.room_id },
|
2017-09-17 00:22:54 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 05:47:13 +02:00
|
|
|
const bool result
|
|
|
|
{
|
2017-10-25 18:47:03 +02:00
|
|
|
m::vm::test(query, [&request, &access_token](const m::event &event)
|
2017-09-25 05:47:13 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
// Checks if the access token has expired. Tokens are expired when
|
|
|
|
// an m.room.redaction event is issued for the ircd.access_token
|
|
|
|
// event. Instead of making another query here for the redaction
|
|
|
|
// we expect the original event to be updated with the following
|
|
|
|
// key which must be part of the redaction process.
|
|
|
|
const json::object &unsigned_
|
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"unsigned"_>(event)
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(unsigned_.has("redacted_because"))
|
|
|
|
return false;
|
|
|
|
|
2017-10-05 01:40:02 +02:00
|
|
|
assert(at<"state_key"_>(event) == access_token);
|
2017-12-12 21:26:39 +01:00
|
|
|
request.user_id = m::user::id{at<"sender"_>(event)};
|
2017-09-25 05:47:13 +02:00
|
|
|
return true;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!result)
|
2017-08-23 23:06:14 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
2017-09-08 11:17:06 +02:00
|
|
|
// 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.
|
2017-08-23 23:06:14 +02:00
|
|
|
http::UNAUTHORIZED, "M_UNKNOWN_TOKEN", "Credentials for this method are required but invalid."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
2017-09-08 11:17:06 +02:00
|
|
|
// 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.
|
2017-08-23 23:06:14 +02:00
|
|
|
http::UNAUTHORIZED, "M_MISSING_TOKEN", "Credentials for this method are required but missing."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
void
|
|
|
|
ircd::verify_origin(client &client,
|
|
|
|
resource::method &method,
|
|
|
|
resource::request &request)
|
|
|
|
{
|
|
|
|
const auto &authorization
|
|
|
|
{
|
|
|
|
request.head.authorization
|
|
|
|
};
|
|
|
|
|
|
|
|
const fmt::bsprintf<1024> uri
|
|
|
|
{
|
|
|
|
"%s%s%s", request.head.path, request.query? "?" : "", request.query
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto verified
|
|
|
|
{
|
2017-10-25 18:47:03 +02:00
|
|
|
m::io::verify_x_matrix_authorization(authorization, method.name, uri, request.content)
|
2017-10-16 06:29:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!verified)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "M_INVALID_SIGNATURE", "The X-Matrix Authorization is invalid."
|
|
|
|
};
|
|
|
|
}
|
2017-08-23 23:06:14 +02:00
|
|
|
|
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
|
|
|
{
|
2017-08-23 23:06:14 +02:00
|
|
|
auto &method(operator[](head.method));
|
2017-03-13 22:07:58 +01:00
|
|
|
http::request::content content{pc, head};
|
2017-10-12 05:52:33 +02:00
|
|
|
|
|
|
|
const auto pathparm
|
|
|
|
{
|
|
|
|
lstrip(head.path, this->path)
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view param[8];
|
|
|
|
const vector_view<string_view> parv
|
|
|
|
{
|
|
|
|
param, tokens(pathparm, '/', param)
|
|
|
|
};
|
|
|
|
|
2017-03-11 02:46:25 +01:00
|
|
|
resource::request request
|
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
head, content, head.query, parv
|
2017-03-11 02:46:25 +01:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:06:14 +02:00
|
|
|
if(method.flags & method.REQUIRES_AUTH)
|
|
|
|
authenticate(client, method, request);
|
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
if(method.flags & method.VERIFY_ORIGIN)
|
|
|
|
verify_origin(client, method, request);
|
2017-08-23 23:06:14 +02:00
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
handle_request(client, method, request);
|
2017-03-21 03:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-23 23:06:14 +02:00
|
|
|
ircd::resource::handle_request(client &client,
|
|
|
|
method &method,
|
|
|
|
resource::request &request)
|
2017-03-21 03:30:07 +01:00
|
|
|
try
|
|
|
|
{
|
2017-09-08 11:17:06 +02:00
|
|
|
const auto response
|
|
|
|
{
|
|
|
|
method(client, request)
|
|
|
|
};
|
2017-03-21 03:30:07 +01:00
|
|
|
}
|
2017-09-15 21:07:07 +02:00
|
|
|
catch(const json::not_found &e)
|
2017-03-21 03:30:07 +01:00
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
2017-09-08 11:17:06 +02:00
|
|
|
http::BAD_REQUEST, "M_BAD_JSON", "Required JSON field: %s", e.what()
|
2017-03-21 03:30:07 +01:00
|
|
|
};
|
2016-09-06 01:05:16 +02:00
|
|
|
}
|
2017-09-15 21:07:07 +02:00
|
|
|
catch(const json::print_error &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::INTERNAL_SERVER_ERROR, "M_NOT_JSON", "Generator Protection: %s", e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const json::error &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST, "M_NOT_JSON", "%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2017-09-25 03:05:42 +02:00
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::NOT_FOUND, "M_NOT_FOUND", "%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2016-09-06 01:05:16 +02:00
|
|
|
|
2017-10-16 06:29:38 +02:00
|
|
|
ircd::resource::method &
|
|
|
|
ircd::resource::operator[](const string_view &name)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return *methods.at(name);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw http::error
|
|
|
|
{
|
|
|
|
http::METHOD_NOT_ALLOWED
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::resource::method::method(struct resource &resource,
|
2017-08-23 23:06:14 +02:00
|
|
|
const string_view &name,
|
2016-11-29 16:23:38 +01:00
|
|
|
const handler &handler,
|
2017-08-23 23:06:14 +02:00
|
|
|
opts opts)
|
|
|
|
:name{name}
|
2016-11-29 16:23:38 +01:00
|
|
|
,resource{&resource}
|
2017-08-23 23:06:14 +02:00
|
|
|
,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
|
|
|
|
2017-08-23 23:06:14 +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
|
|
|
{
|
2017-08-23 23:06:14 +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,
|
2017-10-12 05:52:33 +02:00
|
|
|
http::query::string query,
|
|
|
|
const vector_view<string_view> &parv)
|
2017-08-23 23:48:28 +02:00
|
|
|
:json::object{content}
|
2017-08-23 23:06:14 +02:00
|
|
|
,head{head}
|
|
|
|
,content{content}
|
|
|
|
,query{std::move(query)}
|
2017-10-12 05:52:33 +02:00
|
|
|
,parv{parv}
|
2017-08-23 23:06:14 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:00:05 +02:00
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const http::code &code)
|
|
|
|
:response{client, json::object{"{}"}, code}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:06:14 +02:00
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const http::code &code,
|
2017-09-09 21:20:00 +02:00
|
|
|
const json::iov &members)
|
2017-09-08 17:15:14 +02:00
|
|
|
:response{client, members, code}
|
2017-08-23 23:06:14 +02:00
|
|
|
{
|
2016-09-06 01:05:16 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-12 22:34:21 +02:00
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const json::members &members,
|
|
|
|
const http::code &code)
|
|
|
|
:response{client, code, members}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:00:05 +02:00
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const json::value &value,
|
|
|
|
const http::code &code)
|
|
|
|
:response{client, code, value}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const http::code &code,
|
|
|
|
const json::value &value)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
serialized(value)
|
|
|
|
};
|
|
|
|
|
2017-10-28 21:31:26 +02:00
|
|
|
const unique_buffer<mutable_buffer> buffer
|
2017-09-25 02:00:05 +02:00
|
|
|
{
|
2017-10-28 21:31:26 +02:00
|
|
|
size
|
2017-09-25 02:00:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
switch(type(value))
|
|
|
|
{
|
|
|
|
case json::ARRAY:
|
|
|
|
{
|
2017-10-28 21:31:26 +02:00
|
|
|
response(client, json::array{stringify(mutable_buffer{buffer}, value)}, code);
|
2017-09-25 02:00:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case json::OBJECT:
|
|
|
|
{
|
2017-10-28 21:31:26 +02:00
|
|
|
response(client, json::object{stringify(mutable_buffer{buffer}, value)}, code);
|
2017-09-25 02:00:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: throw m::error
|
|
|
|
{
|
|
|
|
http::INTERNAL_SERVER_ERROR, "M_NOT_JSON", "Cannot send json::%s as response content", type(value)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const json::error &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::INTERNAL_SERVER_ERROR, "M_NOT_JSON", "Generator Protection: %s", e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-12 22:34:21 +02:00
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const http::code &code,
|
|
|
|
const json::members &members)
|
2017-09-25 02:00:05 +02:00
|
|
|
try
|
2017-09-12 22:34:21 +02:00
|
|
|
{
|
2017-09-15 21:07:07 +02:00
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
serialized(members)
|
|
|
|
};
|
|
|
|
|
2017-10-28 21:31:26 +02:00
|
|
|
const unique_buffer<mutable_buffer> buffer
|
|
|
|
{
|
|
|
|
size
|
|
|
|
};
|
|
|
|
|
2017-09-15 21:07:07 +02:00
|
|
|
const json::object object
|
|
|
|
{
|
2017-10-28 21:31:26 +02:00
|
|
|
stringify(mutable_buffer{buffer}, members)
|
2017-09-15 21:07:07 +02:00
|
|
|
};
|
2017-09-12 22:34:21 +02:00
|
|
|
|
2017-09-15 21:07:07 +02:00
|
|
|
response(client, object, code);
|
2017-09-12 22:34:21 +02:00
|
|
|
}
|
2017-09-25 02:00:05 +02:00
|
|
|
catch(const json::error &e)
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2017-09-25 02:00:05 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::INTERNAL_SERVER_ERROR, "M_NOT_JSON", "Generator Protection: %s", e.what()
|
|
|
|
};
|
2017-09-09 21:20:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const json::iov &members,
|
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
|
|
|
{
|
2017-09-15 21:07:07 +02:00
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
serialized(members)
|
|
|
|
};
|
|
|
|
|
2017-10-28 21:31:26 +02:00
|
|
|
const unique_buffer<mutable_buffer> buffer
|
|
|
|
{
|
|
|
|
size
|
|
|
|
};
|
|
|
|
|
2017-09-15 21:07:07 +02:00
|
|
|
const json::object object
|
|
|
|
{
|
2017-10-28 21:31:26 +02:00
|
|
|
stringify(mutable_buffer{buffer}, members)
|
2017-09-15 21:07:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
response(client, object, code);
|
2017-03-21 03:30:07 +01:00
|
|
|
}
|
|
|
|
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,
|
2017-08-23 23:48:28 +02:00
|
|
|
const json::object &object,
|
2017-03-11 04:31:20 +01:00
|
|
|
const http::code &code)
|
|
|
|
{
|
2017-11-30 19:31:13 +01:00
|
|
|
static const auto content_type
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-08-23 23:06:14 +02:00
|
|
|
"application/json; charset=utf-8"
|
2017-03-11 04:31:20 +01:00
|
|
|
};
|
2016-09-06 01:05:16 +02:00
|
|
|
|
2017-11-30 19:31:13 +01:00
|
|
|
assert(json::valid(object, std::nothrow));
|
2017-08-23 23:48:28 +02:00
|
|
|
response(client, object, content_type, code);
|
2016-09-06 01:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:00:05 +02:00
|
|
|
ircd::resource::response::response(client &client,
|
|
|
|
const json::array &array,
|
|
|
|
const http::code &code)
|
|
|
|
{
|
2017-11-30 19:31:13 +01:00
|
|
|
static const auto content_type
|
2017-09-25 02:00:05 +02:00
|
|
|
{
|
|
|
|
"application/json; charset=utf-8"
|
|
|
|
};
|
|
|
|
|
2017-11-30 19:31:13 +01:00
|
|
|
assert(json::valid(array, std::nothrow));
|
2017-09-25 02:00:05 +02:00
|
|
|
response(client, array, content_type, code);
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:06:14 +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
|
|
|
{
|
2017-09-25 02:00:05 +02:00
|
|
|
const auto request_time
|
|
|
|
{
|
2017-09-30 08:09:03 +02:00
|
|
|
client.request_timer.at<microseconds>().count()
|
|
|
|
};
|
|
|
|
|
|
|
|
char rtime[64]; const auto rtime_len
|
|
|
|
{
|
|
|
|
snprintf(rtime, sizeof(rtime), "%zdus",
|
|
|
|
request_time)
|
2017-09-25 02:00:05 +02:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:06:14 +02:00
|
|
|
http::response
|
|
|
|
{
|
|
|
|
code, str, write_closure(client),
|
|
|
|
{
|
|
|
|
{ "Content-Type", content_type },
|
2017-09-30 08:09:03 +02:00
|
|
|
{ "Access-Control-Allow-Origin", "*" }, //TODO: XXX
|
|
|
|
{ "X-IRCd-Request-Timer", string_view{rtime, size_t(rtime_len)} }
|
2017-08-23 23:06:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:00:05 +02:00
|
|
|
log::debug("client[%s] HTTP %d %s in %ld$us; response in %ld$us (%s) content-length: %zu",
|
2017-09-30 08:09:03 +02:00
|
|
|
string(remote(client)),
|
2017-08-23 23:06:14 +02:00
|
|
|
int(code),
|
2017-09-30 08:09:03 +02:00
|
|
|
http::status(code),
|
|
|
|
request_time,
|
|
|
|
(client.request_timer.at<microseconds>().count() - request_time),
|
2017-09-24 04:46:17 +02:00
|
|
|
content_type,
|
2017-09-25 02:00:05 +02:00
|
|
|
str.size());
|
2016-09-06 01:05:16 +02:00
|
|
|
}
|