2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2019-04-17 02:12:17 +02:00
|
|
|
namespace ircd { namespace http
|
|
|
|
__attribute__((visibility("hidden")))
|
2017-08-28 23:51:22 +02:00
|
|
|
{
|
2018-03-17 18:44:40 +01:00
|
|
|
using namespace ircd::spirit;
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2018-12-21 22:56:20 +01:00
|
|
|
struct grammar;
|
2017-08-28 23:51:22 +02:00
|
|
|
struct parser extern const parser;
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
extern const std::unordered_map<ircd::http::code, ircd::string_view> reason;
|
2018-01-12 08:38:12 +01:00
|
|
|
|
2018-03-19 22:40:04 +01:00
|
|
|
[[noreturn]] void throw_error(const qi::expectation_failure<const char *> &, const bool &internal = false);
|
2019-04-17 02:12:17 +02:00
|
|
|
}}
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2017-12-12 21:38:16 +01:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
2017-12-23 01:40:44 +01:00
|
|
|
ircd::http::query,
|
|
|
|
( decltype(ircd::http::query::first), first )
|
|
|
|
( decltype(ircd::http::query::second), second )
|
2017-12-12 21:38:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
2017-12-24 22:25:09 +01:00
|
|
|
ircd::http::header,
|
|
|
|
( decltype(ircd::http::header::first), first )
|
|
|
|
( decltype(ircd::http::header::second), second )
|
2017-12-12 21:38:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
2017-12-23 01:40:44 +01:00
|
|
|
ircd::http::line::response,
|
|
|
|
( decltype(ircd::http::line::response::version), version )
|
|
|
|
( decltype(ircd::http::line::response::status), status )
|
|
|
|
( decltype(ircd::http::line::response::reason), reason )
|
2017-12-12 21:38:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
2017-12-23 01:40:44 +01:00
|
|
|
ircd::http::line::request,
|
|
|
|
( decltype(ircd::http::line::request::method), method )
|
|
|
|
( decltype(ircd::http::line::request::path), path )
|
|
|
|
( decltype(ircd::http::line::request::query), query )
|
|
|
|
( decltype(ircd::http::line::request::fragment), fragment )
|
|
|
|
( decltype(ircd::http::line::request::version), version )
|
2017-12-12 21:38:16 +01:00
|
|
|
)
|
|
|
|
|
2018-03-11 18:23:06 +01:00
|
|
|
decltype(ircd::http::reason)
|
|
|
|
ircd::http::reason
|
2017-03-21 03:25:10 +01:00
|
|
|
{
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::CONTINUE, "Continue" },
|
|
|
|
{ code::SWITCHING_PROTOCOLS, "Switching Protocols" },
|
2017-03-21 03:25:10 +01:00
|
|
|
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::OK, "OK" },
|
|
|
|
{ code::CREATED, "Created" },
|
|
|
|
{ code::ACCEPTED, "Accepted" },
|
|
|
|
{ code::NON_AUTHORITATIVE_INFORMATION, "Non-Authoritative Information" },
|
|
|
|
{ code::NO_CONTENT, "No Content" },
|
2017-04-07 01:54:20 +02:00
|
|
|
{ code::PARTIAL_CONTENT, "Partial Content" },
|
|
|
|
|
2017-10-12 03:00:33 +02:00
|
|
|
{ code::MULTIPLE_CHOICES, "Multiple Choices" },
|
2017-04-07 01:54:20 +02:00
|
|
|
{ code::MOVED_PERMANENTLY, "Moved Permanently" },
|
|
|
|
{ code::FOUND, "Found" },
|
|
|
|
{ code::SEE_OTHER, "See Other" },
|
|
|
|
{ code::NOT_MODIFIED, "Not Modified" },
|
2019-02-19 20:34:48 +01:00
|
|
|
{ code::USE_PROXY, "Use Proxy" },
|
|
|
|
{ code::SWITCH_PROXY, "Switch Proxy" },
|
2017-04-07 01:54:20 +02:00
|
|
|
{ code::TEMPORARY_REDIRECT, "Temporary Redirect" },
|
|
|
|
{ code::PERMANENT_REDIRECT, "Permanent Redirect" },
|
2017-03-21 03:25:10 +01:00
|
|
|
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::BAD_REQUEST, "Bad Request" },
|
|
|
|
{ code::UNAUTHORIZED, "Unauthorized" },
|
|
|
|
{ code::FORBIDDEN, "Forbidden" },
|
|
|
|
{ code::NOT_FOUND, "Not Found" },
|
|
|
|
{ code::METHOD_NOT_ALLOWED, "Method Not Allowed" },
|
2018-05-10 02:19:43 +02:00
|
|
|
{ code::NOT_ACCEPTABLE, "Not Acceptable" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::REQUEST_TIMEOUT, "Request Time-out" },
|
|
|
|
{ code::CONFLICT, "Conflict" },
|
2019-09-09 03:29:19 +02:00
|
|
|
{ code::GONE, "Gone" },
|
2018-03-22 07:01:23 +01:00
|
|
|
{ code::LENGTH_REQUIRED, "Length Required" },
|
2018-01-12 03:41:58 +01:00
|
|
|
{ code::PAYLOAD_TOO_LARGE, "Payload Too Large" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::REQUEST_URI_TOO_LONG, "Request URI Too Long" },
|
2018-05-10 02:19:43 +02:00
|
|
|
{ code::UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type" },
|
2019-06-15 21:50:53 +02:00
|
|
|
{ code::RANGE_NOT_SATISFIABLE, "Range Not Satisfiable" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::EXPECTATION_FAILED, "Expectation Failed" },
|
2017-04-07 01:54:20 +02:00
|
|
|
{ code::IM_A_TEAPOT, "Negative, I Am A Meat Popsicle" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::UNPROCESSABLE_ENTITY, "Unprocessable Entity" },
|
2019-03-08 01:01:19 +01:00
|
|
|
{ code::PRECONDITION_REQUIRED, "Precondition Required" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::TOO_MANY_REQUESTS, "Too Many Requests" },
|
|
|
|
{ code::REQUEST_HEADER_FIELDS_TOO_LARGE, "Request Header Fields Too Large" },
|
2017-03-21 03:25:10 +01:00
|
|
|
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::INTERNAL_SERVER_ERROR, "Internal Server Error" },
|
|
|
|
{ code::NOT_IMPLEMENTED, "Not Implemented" },
|
2018-02-07 04:33:33 +01:00
|
|
|
{ code::BAD_GATEWAY, "Bad Gateway" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::SERVICE_UNAVAILABLE, "Service Unavailable" },
|
2018-05-22 00:56:13 +02:00
|
|
|
{ code::GATEWAY_TIMEOUT, "Gateway Timeout" },
|
2017-04-03 05:59:18 +02:00
|
|
|
{ code::HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported" },
|
|
|
|
{ code::INSUFFICIENT_STORAGE, "Insufficient Storage" },
|
2019-06-07 09:09:58 +02:00
|
|
|
{ code::WEB_SERVER_IS_DOWN, "Web Server Is Down" },
|
2018-10-01 20:30:34 +02:00
|
|
|
{ code::A_TIMEOUT_OCCURRED, "A Timeout Occurred" },
|
2017-03-21 03:25:10 +01:00
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::grammar
|
2018-12-21 22:56:20 +01:00
|
|
|
:qi::grammar<const char *, unused_type>
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-12-21 22:56:20 +01:00
|
|
|
using it = const char *;
|
2017-09-30 02:11:23 +02:00
|
|
|
template<class R = unused_type, class... S> using rule = qi::rule<it, R, S...>;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-21 03:25:10 +01:00
|
|
|
rule<> NUL { lit('\0') ,"nul" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-21 03:25:10 +01:00
|
|
|
// insignificant whitespaces
|
|
|
|
rule<> SP { lit('\x20') ,"space" };
|
|
|
|
rule<> HT { lit('\x09') ,"horizontal tab" };
|
2017-08-23 23:00:21 +02:00
|
|
|
rule<> ws { SP | HT ,"whitespace" };
|
|
|
|
|
2017-03-21 03:25:10 +01:00
|
|
|
rule<> CR { lit('\x0D') ,"carriage return" };
|
|
|
|
rule<> LF { lit('\x0A') ,"line feed" };
|
2017-08-23 23:00:21 +02:00
|
|
|
rule<> CRLF { CR >> LF ,"carriage return, line feed" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-04-03 06:00:12 +02:00
|
|
|
rule<> illegal { NUL | CR | LF ,"illegal" };
|
2017-03-21 03:25:10 +01:00
|
|
|
rule<> colon { lit(':') ,"colon" };
|
2017-04-03 06:00:12 +02:00
|
|
|
rule<> slash { lit('/') ,"forward solidus" };
|
|
|
|
rule<> question { lit('?') ,"question mark" };
|
|
|
|
rule<> pound { lit('#') ,"pound sign" };
|
|
|
|
rule<> equal { lit('=') ,"equal sign" };
|
|
|
|
rule<> ampersand { lit('&') ,"ampersand" };
|
|
|
|
|
|
|
|
rule<string_view> token { raw[+(char_ - (illegal | ws))] ,"token" };
|
|
|
|
rule<string_view> string { raw[+(char_ - illegal)] ,"string" };
|
2017-03-21 03:25:10 +01:00
|
|
|
rule<string_view> line { *ws >> -string >> CRLF ,"line" };
|
|
|
|
|
|
|
|
rule<string_view> status { raw[repeat(3)[char_("0-9")]] ,"status" };
|
|
|
|
rule<short> status_code { short_ ,"status code" };
|
2017-04-03 06:00:12 +02:00
|
|
|
rule<string_view> reason { string ,"status" };
|
2017-03-21 03:25:10 +01:00
|
|
|
|
2017-04-03 06:00:12 +02:00
|
|
|
rule<string_view> head_key { raw[+(char_ - (illegal | ws | colon))] ,"head key" };
|
|
|
|
rule<string_view> head_val { string ,"head value" };
|
2017-12-24 22:25:09 +01:00
|
|
|
rule<http::header> header { head_key >> *ws >> colon >> *ws >> head_val ,"header" };
|
2017-03-21 03:25:10 +01:00
|
|
|
rule<unused_type> headers { (header % (*ws >> CRLF)) ,"headers" };
|
|
|
|
|
2017-04-03 06:00:12 +02:00
|
|
|
rule<> query_terminator { equal | question | ampersand | pound ,"query terminator" };
|
|
|
|
rule<> query_illegal { illegal | ws | query_terminator ,"query illegal" };
|
|
|
|
rule<string_view> query_key { raw[+(char_ - query_illegal)] ,"query key" };
|
|
|
|
rule<string_view> query_val { raw[*(char_ - query_illegal)] ,"query value" };
|
|
|
|
|
2017-12-12 21:38:16 +01:00
|
|
|
rule<string_view> method { token ,"method" };
|
|
|
|
rule<string_view> path { raw[-slash >> *(char_ - query_illegal)] ,"path" };
|
|
|
|
rule<string_view> fragment { pound >> -token ,"fragment" };
|
|
|
|
rule<string_view> version { token ,"version" };
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
rule<size_t> chunk_size
|
|
|
|
{
|
2018-03-19 22:42:40 +01:00
|
|
|
qi::uint_parser<size_t, 16, 1, 8>{}
|
2017-09-30 02:11:23 +02:00
|
|
|
,"chunk size"
|
|
|
|
};
|
|
|
|
|
2018-03-19 22:42:40 +01:00
|
|
|
rule<string_view> chunk_extensions
|
|
|
|
{
|
|
|
|
';' >> raw[string] //TODO: extensions
|
|
|
|
,"chunk extensions"
|
|
|
|
};
|
|
|
|
|
2017-04-03 06:00:12 +02:00
|
|
|
rule<http::query> query
|
|
|
|
{
|
|
|
|
query_key >> -(equal >> query_val)
|
|
|
|
,"query"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<string_view> query_string
|
|
|
|
{
|
2017-04-07 01:54:47 +02:00
|
|
|
question >> -raw[(query_key >> -(equal >> query_val)) % ampersand]
|
2017-04-03 06:00:12 +02:00
|
|
|
,"query string"
|
|
|
|
};
|
|
|
|
|
2017-03-21 03:25:10 +01:00
|
|
|
rule<line::request> request_line
|
|
|
|
{
|
2017-04-03 06:00:12 +02:00
|
|
|
method >> +SP >> path >> -query_string >> -fragment >> +SP >> version
|
2017-03-21 03:25:10 +01:00
|
|
|
,"request line"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<line::response> response_line
|
|
|
|
{
|
|
|
|
version >> +SP >> status >> -(+SP >> reason)
|
|
|
|
,"response line"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<unused_type> request
|
|
|
|
{
|
|
|
|
request_line >> *ws >> CRLF >> -headers >> CRLF
|
|
|
|
,"request"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<unused_type> response
|
|
|
|
{
|
|
|
|
response_line >> *ws >> CRLF >> -headers >> CRLF
|
|
|
|
,"response"
|
|
|
|
};
|
|
|
|
|
2018-12-21 22:56:20 +01:00
|
|
|
grammar()
|
|
|
|
:qi::grammar<const char *, unused_type>{rule<>{}}
|
2017-03-21 03:25:10 +01:00
|
|
|
{}
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::parser
|
2018-12-21 22:56:20 +01:00
|
|
|
:grammar
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-18 04:28:49 +01:00
|
|
|
static size_t content_length(const string_view &val);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2017-08-28 23:51:22 +02:00
|
|
|
const ircd::http::parser;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
/// Compose a request. This prints an HTTP head into the buffer. No real IO is
|
|
|
|
/// done here. After composing into the buffer, the user can then drive the
|
|
|
|
/// socket by sending the header and the content as specified.
|
|
|
|
///
|
|
|
|
/// If termination is false, no extra CRLF is printed to the buffer allowing
|
|
|
|
/// additional headers not specified to be appended later.
|
2018-02-08 06:30:20 +01:00
|
|
|
ircd::http::request::request(window_buffer &out,
|
2017-12-23 01:40:44 +01:00
|
|
|
const string_view &host,
|
2017-03-13 22:07:58 +01:00
|
|
|
const string_view &method,
|
2018-01-20 11:29:03 +01:00
|
|
|
const string_view &uri,
|
2017-12-23 01:40:44 +01:00
|
|
|
const size_t &content_length,
|
|
|
|
const string_view &content_type,
|
|
|
|
const vector_view<const header> &headers,
|
|
|
|
const bool &termination)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2018-01-20 11:29:03 +01:00
|
|
|
writeline(out, [&method, &uri](const mutable_buffer &out) -> size_t
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
2017-12-23 01:40:44 +01:00
|
|
|
assert(!method.empty());
|
2018-01-20 11:29:03 +01:00
|
|
|
assert(!uri.empty());
|
2017-12-23 01:40:44 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2018-01-20 11:29:03 +01:00
|
|
|
out, "%s %s HTTP/1.1", method, uri
|
2017-12-23 01:40:44 +01:00
|
|
|
};
|
|
|
|
});
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-12-31 03:03:40 +01:00
|
|
|
if(!has(headers, "host"))
|
|
|
|
writeline(out, [&host](const mutable_buffer &out) -> size_t
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
2018-12-31 03:03:40 +01:00
|
|
|
assert(!host.empty());
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Host: %s", host
|
|
|
|
};
|
|
|
|
});
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-12-31 03:03:40 +01:00
|
|
|
if(content_length && !has(headers, "content-type"))
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&content_type](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Content-Type: %s", content_type?: "text/plain; charset=utf-8"
|
|
|
|
};
|
|
|
|
});
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-12-31 03:03:40 +01:00
|
|
|
if(!has(headers, "content-length"))
|
|
|
|
writeline(out, [&content_length](const mutable_buffer &out) -> size_t
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
2018-12-31 03:03:40 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Content-Length: %zu", content_length
|
|
|
|
};
|
|
|
|
});
|
2017-04-03 06:00:12 +02:00
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
write(out, headers);
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
if(termination)
|
|
|
|
writeline(out);
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
ircd::http::request::head::head(parse::capstan &pc,
|
2017-03-13 22:07:58 +01:00
|
|
|
const headers::closure &c)
|
|
|
|
:line::request{pc}
|
2018-01-21 09:38:47 +01:00
|
|
|
,uri
|
|
|
|
{
|
|
|
|
fragment? string_view { begin(path), end(fragment) }:
|
|
|
|
query? string_view { begin(path), end(query) }:
|
|
|
|
string_view { begin(path), end(path) }
|
|
|
|
}
|
2019-03-09 22:49:14 +01:00
|
|
|
,headers{[this, &pc, &c]
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2019-04-12 17:35:13 +02:00
|
|
|
const bool version_compatible
|
|
|
|
{
|
|
|
|
this->version == "HTTP/1.0" ||
|
|
|
|
this->version == "HTTP/1.1"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!version_compatible)
|
2019-03-09 22:49:14 +01:00
|
|
|
throw error
|
|
|
|
{
|
2019-03-23 11:12:28 +01:00
|
|
|
HTTP_VERSION_NOT_SUPPORTED, fmt::snstringf
|
|
|
|
{
|
2019-04-12 17:35:13 +02:00
|
|
|
128, "Sorry, no speak '%s'. Try HTTP/1.1 here.",
|
2019-03-23 11:12:28 +01:00
|
|
|
this->version
|
|
|
|
}
|
2019-03-09 22:49:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return http::headers{pc, [this, &c](const auto &h)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2018-01-12 22:15:30 +01:00
|
|
|
if(iequals(h.first, "host"_sv))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->host = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "expect"_sv))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->expect = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "te"_sv))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->te = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "authorization"_sv))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->authorization = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "connection"_sv))
|
2017-10-25 18:26:50 +02:00
|
|
|
this->connection = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "content-type"_sv))
|
|
|
|
this->content_type = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "user-agent"_sv))
|
|
|
|
this->user_agent = h.second;
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2019-05-31 06:48:40 +02:00
|
|
|
else if(iequals(h.first, "upgrade"_sv))
|
|
|
|
this->upgrade = h.second;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2019-06-15 21:48:01 +02:00
|
|
|
else if(iequals(h.first, "range"_sv))
|
|
|
|
this->range = h.second;
|
|
|
|
|
|
|
|
else if(iequals(h.first, "if-range"_sv))
|
|
|
|
this->if_range = h.second;
|
|
|
|
|
|
|
|
else if(iequals(h.first, "content-length"_sv))
|
|
|
|
this->content_length = parser.content_length(h.second);
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
if(c)
|
|
|
|
c(h);
|
2019-03-09 22:49:14 +01:00
|
|
|
}};
|
|
|
|
}()}
|
2017-10-12 03:00:33 +02:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:52:56 +01:00
|
|
|
ircd::http::request::head::operator
|
|
|
|
string_view()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const string_view request_line
|
|
|
|
{
|
|
|
|
static_cast<const line::request &>(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request_line.empty() || headers.empty())
|
|
|
|
return request_line;
|
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
request_line.begin(), headers.end()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-08 06:30:20 +01:00
|
|
|
ircd::http::response::response(window_buffer &out,
|
2017-12-23 01:40:44 +01:00
|
|
|
const code &code,
|
|
|
|
const size_t &content_length,
|
|
|
|
const string_view &content_type,
|
2018-12-31 20:45:04 +01:00
|
|
|
const http::headers &headers_s,
|
|
|
|
const vector_view<const header> &headers_v,
|
2017-12-23 01:40:44 +01:00
|
|
|
const bool &termination)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2018-12-31 20:45:04 +01:00
|
|
|
const auto has_header{[&headers_s, &headers_v]
|
|
|
|
(const string_view &key) -> bool
|
|
|
|
{
|
|
|
|
return has(headers_v, key) || has(headers_s, key);
|
|
|
|
}};
|
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&code](const mutable_buffer &out) -> size_t
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2017-12-23 01:40:44 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "HTTP/1.1 %u %s", uint(code), status(code)
|
|
|
|
};
|
|
|
|
});
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
const bool write_server_header
|
|
|
|
{
|
|
|
|
code >= 200 && code < 300
|
|
|
|
&& !has_header("server")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(write_server_header)
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&code](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
2018-02-19 23:03:05 +01:00
|
|
|
size_t ret{0};
|
|
|
|
ret += copy(out, "Server: "_sv);
|
|
|
|
ret += copy(out + ret, ircd::info::server_agent);
|
|
|
|
return ret;
|
2017-12-23 01:40:44 +01:00
|
|
|
});
|
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
const bool write_date_header
|
|
|
|
{
|
|
|
|
code < 400
|
|
|
|
&& !has_header("date")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(write_date_header)
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
2018-05-20 04:58:37 +02:00
|
|
|
thread_local char date_buf[96];
|
2017-12-23 01:40:44 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Date: %s", timef(date_buf, ircd::localtime)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
const bool write_content_type_header
|
|
|
|
{
|
|
|
|
code != NO_CONTENT
|
|
|
|
&& content_type && content_length
|
|
|
|
&& !has_header("content-type")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(write_content_type_header)
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&content_type](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Content-Type: %s", content_type?: "text/plain; charset=utf-8"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
const bool write_content_length_header
|
|
|
|
{
|
|
|
|
code != NO_CONTENT
|
|
|
|
&& content_length != size_t(-1) // chunked encoding indication
|
|
|
|
&& !has_header("content-length")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(write_content_length_header)
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&content_length](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Content-Length: %zu", content_length
|
|
|
|
};
|
|
|
|
});
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
const bool write_transfer_encoding_chunked
|
|
|
|
{
|
|
|
|
content_length == size_t(-1)
|
|
|
|
&& !has_header("transfer-encoding")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(write_transfer_encoding_chunked)
|
2018-03-22 08:37:16 +01:00
|
|
|
writeline(out, [&content_length](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
return copy(out, "Transfer-Encoding: chunked"_sv);
|
|
|
|
});
|
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
if(!headers_s.empty())
|
|
|
|
out([&headers_s](const mutable_buffer &out)
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
2018-12-31 20:45:04 +01:00
|
|
|
return copy(out, headers_s);
|
2017-12-23 01:40:44 +01:00
|
|
|
});
|
2017-12-24 22:25:09 +01:00
|
|
|
|
2018-12-31 20:45:04 +01:00
|
|
|
if(!headers_v.empty())
|
|
|
|
write(out, headers_v);
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
if(termination)
|
|
|
|
writeline(out);
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
ircd::http::response::head::head(parse::capstan &pc,
|
2017-03-13 22:07:58 +01:00
|
|
|
const headers::closure &c)
|
|
|
|
:line::response{pc}
|
2017-10-12 03:00:33 +02:00
|
|
|
,headers
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2017-10-12 03:00:33 +02:00
|
|
|
http::headers{pc, [this, &c](const auto &h)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2019-06-15 21:48:01 +02:00
|
|
|
if(iequals(h.first, "content-type"s))
|
|
|
|
this->content_type = h.second;
|
|
|
|
|
|
|
|
else if(iequals(h.first, "content-length"s))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->content_length = parser.content_length(h.second);
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2019-06-15 21:48:01 +02:00
|
|
|
else if(iequals(h.first, "content-range"s))
|
|
|
|
this->content_range = h.second;
|
|
|
|
|
|
|
|
else if(iequals(h.first, "accept-range"s))
|
|
|
|
this->content_range = h.second;
|
2018-02-26 13:00:36 +01:00
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
else if(iequals(h.first, "transfer-encoding"s))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->transfer_encoding = h.second;
|
2017-09-30 02:11:23 +02:00
|
|
|
|
2018-01-24 18:15:16 +01:00
|
|
|
else if(iequals(h.first, "server"s))
|
|
|
|
this->server = h.second;
|
|
|
|
|
2017-03-13 22:07:58 +01:00
|
|
|
if(c)
|
|
|
|
c(h);
|
2017-10-12 03:00:33 +02:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:42:40 +01:00
|
|
|
ircd::http::response::chunk::chunk(parse::capstan &pc)
|
|
|
|
try
|
|
|
|
:line{pc}
|
|
|
|
{
|
|
|
|
static const parser::rule<size_t> grammar
|
|
|
|
{
|
2018-12-05 03:41:44 +01:00
|
|
|
eoi | (eps > (parser.chunk_size >> -parser.chunk_extensions))
|
2018-03-19 22:42:40 +01:00
|
|
|
,"chunk head"
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(line::begin());
|
2018-12-05 03:41:44 +01:00
|
|
|
const auto res
|
|
|
|
{
|
|
|
|
qi::parse(start, line::end(), grammar, this->size)
|
|
|
|
};
|
|
|
|
|
2018-03-19 22:42:40 +01:00
|
|
|
assert(res == true);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw_error(e, true);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:06:18 +01:00
|
|
|
//
|
|
|
|
// headers
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::http::has(const vector_view<const header> &headers,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
return end(headers) != std::find_if(begin(headers), end(headers), [&key]
|
|
|
|
(const header &header)
|
|
|
|
{
|
|
|
|
return header == key;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::http::has(const headers &headers,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
return headers.has(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// headers::headers
|
|
|
|
//
|
|
|
|
|
2019-06-29 03:23:43 +02:00
|
|
|
decltype(ircd::http::headers::terminator)
|
|
|
|
ircd::http::headers::terminator
|
|
|
|
{
|
|
|
|
"\r\n\r\n"
|
|
|
|
};
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
ircd::http::headers::headers(parse::capstan &pc,
|
2017-03-13 22:07:58 +01:00
|
|
|
const closure &c)
|
2018-12-31 20:06:18 +01:00
|
|
|
:headers
|
|
|
|
{
|
|
|
|
pc, closure_bool{[&c](const auto &header)
|
|
|
|
{
|
|
|
|
if(c)
|
|
|
|
c(header);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::http::headers::headers(parse::capstan &pc,
|
|
|
|
closure_bool c)
|
2017-10-12 03:00:33 +02:00
|
|
|
:string_view{[&pc, &c]
|
|
|
|
() -> string_view
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2017-12-24 22:25:09 +01:00
|
|
|
header h{pc};
|
2017-10-12 03:00:33 +02:00
|
|
|
const char *const &started{h.first.data()}, *stopped{started};
|
2017-12-24 22:25:09 +01:00
|
|
|
for(; !h.first.empty(); stopped = h.second.data() + h.second.size(), h = header{pc})
|
2018-12-31 20:06:18 +01:00
|
|
|
if(c && !c(h))
|
|
|
|
c = {};
|
2017-10-12 03:00:33 +02:00
|
|
|
|
|
|
|
return { started, stopped };
|
|
|
|
}()}
|
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:06:18 +01:00
|
|
|
bool
|
|
|
|
ircd::http::headers::has(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
// has header if break early from for_each
|
|
|
|
return !for_each([&key]
|
|
|
|
(const header &header)
|
|
|
|
{
|
|
|
|
// true to continue; false to break (for_each protocol)
|
2019-04-01 03:58:15 +02:00
|
|
|
return iequals(header.first, key)? false : true;
|
2018-12-31 20:06:18 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::http::headers::at(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
this->operator[](key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw std::out_of_range{key};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::http::headers::operator[](const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
for_each([&key, &ret](const auto &header)
|
|
|
|
{
|
2019-04-01 03:58:15 +02:00
|
|
|
if(iequals(header.first, key))
|
2018-12-31 20:06:18 +01:00
|
|
|
{
|
|
|
|
ret = header.second;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::http::headers::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
parse::buffer pb{const_buffer{*this}};
|
|
|
|
parse::capstan pc{pb};
|
|
|
|
header h{pc};
|
|
|
|
for(; !h.first.empty(); h = header{pc})
|
|
|
|
if(!closure(h))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// header
|
|
|
|
//
|
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
ircd::http::header::header(const line &line)
|
2016-11-29 16:23:38 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto grammar
|
|
|
|
{
|
|
|
|
eps > parser.header
|
|
|
|
};
|
|
|
|
|
|
|
|
if(line.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *start(line.data());
|
|
|
|
const char *const stop(line.data() + line.size());
|
|
|
|
qi::parse(start, stop, grammar, *this);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2018-01-12 08:38:12 +01:00
|
|
|
throw_error(e);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::http::line::response::response(const line &line)
|
|
|
|
{
|
|
|
|
static const auto grammar
|
|
|
|
{
|
2017-03-21 03:25:10 +01:00
|
|
|
eps > parser.response_line
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(line.data());
|
|
|
|
const char *const stop(line.data() + line.size());
|
|
|
|
qi::parse(start, stop, grammar, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::http::line::request::request(const line &line)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto grammar
|
|
|
|
{
|
|
|
|
eps > parser.request_line
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(line.data());
|
|
|
|
const char *const stop(line.data() + line.size());
|
|
|
|
qi::parse(start, stop, grammar, *this);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2018-01-12 08:38:12 +01:00
|
|
|
throw_error(e);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:52:56 +01:00
|
|
|
ircd::http::line::request::operator
|
|
|
|
string_view()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(method.empty())
|
|
|
|
return string_view{};
|
|
|
|
|
|
|
|
assert(!version.empty());
|
|
|
|
assert(method.begin() < version.end());
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
method.begin(), version.end()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-29 03:23:43 +02:00
|
|
|
//
|
|
|
|
// http::line
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::http::line::terminator)
|
|
|
|
ircd::http::line::terminator
|
|
|
|
{
|
|
|
|
"\r\n"
|
|
|
|
};
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
ircd::http::line::line(parse::capstan &pc)
|
2016-11-29 16:23:38 +01:00
|
|
|
:string_view{[&pc]
|
|
|
|
{
|
|
|
|
static const auto grammar
|
|
|
|
{
|
|
|
|
parser.line
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2017-03-13 22:07:58 +01:00
|
|
|
pc([&ret](const char *&start, const char *const &stop)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
if(!qi::parse(start, stop, grammar, ret))
|
|
|
|
{
|
|
|
|
ret = {};
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:17:59 +01:00
|
|
|
//
|
|
|
|
// query::string
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::http::query::string::has(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
for_each(key, [&ret]
|
|
|
|
(const auto &)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::http::query::string::count(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each(key, [&ret]
|
|
|
|
(const auto &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:00:12 +02:00
|
|
|
ircd::string_view
|
2019-03-07 02:17:59 +01:00
|
|
|
ircd::http::query::string::at(const string_view &key,
|
|
|
|
const size_t &idx)
|
2017-04-03 06:00:12 +02:00
|
|
|
const
|
|
|
|
{
|
2019-03-07 02:17:59 +01:00
|
|
|
const string_view &ret
|
|
|
|
{
|
|
|
|
_get(key, idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
2018-08-30 19:58:32 +02:00
|
|
|
{
|
|
|
|
thread_local char buf[1024];
|
|
|
|
const string_view msg{fmt::sprintf
|
|
|
|
{
|
2019-03-07 02:17:59 +01:00
|
|
|
buf, "Failed to find value for required query string key '%s' #%zu.",
|
|
|
|
key,
|
|
|
|
idx
|
2018-08-30 19:58:32 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
msg.c_str() // fmt::sprintf() will null terminate msg
|
|
|
|
};
|
|
|
|
}
|
2017-04-03 06:00:12 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::http::query::string::operator[](const string_view &key)
|
|
|
|
const
|
2019-03-07 02:17:59 +01:00
|
|
|
{
|
|
|
|
return _get(key, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::http::query::string::_get(const string_view &key,
|
|
|
|
size_t idx)
|
|
|
|
const
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
|
|
|
string_view ret;
|
2019-03-07 02:17:59 +01:00
|
|
|
for_each(key, [&idx, &ret]
|
|
|
|
(const auto &query)
|
|
|
|
{
|
|
|
|
if(!idx--)
|
|
|
|
{
|
|
|
|
ret = query.second;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::http::query::string::for_each(const string_view &key,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each([&key, &closure]
|
|
|
|
(const auto &query)
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
2018-08-31 03:58:59 +02:00
|
|
|
if(query.first != key)
|
|
|
|
return true;
|
|
|
|
|
2019-03-07 02:17:59 +01:00
|
|
|
return closure(query);
|
|
|
|
});
|
2017-04-03 06:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-08-31 03:58:59 +02:00
|
|
|
ircd::http::query::string::for_each(const closure &view)
|
2017-04-03 06:00:12 +02:00
|
|
|
const
|
|
|
|
{
|
2019-07-16 02:52:01 +02:00
|
|
|
bool ret{true};
|
|
|
|
const auto action{[&view, &ret]
|
2018-08-31 03:58:59 +02:00
|
|
|
(const auto &attribute, const auto &context, auto &continue_)
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
2019-07-16 02:52:01 +02:00
|
|
|
ret = view(attribute);
|
|
|
|
continue_ = ret;
|
2018-08-31 03:58:59 +02:00
|
|
|
}};
|
2017-04-03 06:00:12 +02:00
|
|
|
|
|
|
|
const parser::rule<unused_type> grammar
|
|
|
|
{
|
|
|
|
-parser.question >> (parser.query[action] % parser.ampersand)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &s(*this);
|
2018-08-31 03:58:59 +02:00
|
|
|
const char *start(s.begin()), *const stop(s.end());
|
2019-07-16 02:52:01 +02:00
|
|
|
qi::parse(start, stop, grammar);
|
|
|
|
return ret;
|
2017-04-03 06:00:12 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:17:59 +01:00
|
|
|
//
|
|
|
|
// parser util
|
|
|
|
//
|
|
|
|
|
2017-03-18 04:28:49 +01:00
|
|
|
size_t
|
|
|
|
ircd::http::parser::content_length(const string_view &str)
|
|
|
|
{
|
|
|
|
static const parser::rule<long> grammar
|
|
|
|
{
|
|
|
|
long_
|
|
|
|
};
|
|
|
|
|
|
|
|
long ret;
|
|
|
|
const char *start(str.data());
|
2019-08-16 12:00:51 +02:00
|
|
|
const bool parsed
|
|
|
|
{
|
|
|
|
qi::parse(start, start + str.size(), grammar, ret)
|
|
|
|
};
|
|
|
|
|
2017-03-18 04:28:49 +01:00
|
|
|
if(!parsed || ret < 0)
|
2017-12-30 09:07:23 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
BAD_REQUEST, "Invalid content-length value"
|
|
|
|
};
|
2017-03-18 04:28:49 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-30 02:11:23 +02:00
|
|
|
|
2019-03-07 02:17:59 +01:00
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
2018-03-19 05:09:42 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::http::writechunk(const mutable_buffer &buf,
|
2018-03-22 07:20:33 +01:00
|
|
|
const uint32_t &chunk_size)
|
2018-03-19 05:09:42 +01:00
|
|
|
{
|
|
|
|
window_buffer wb{buf};
|
|
|
|
writechunk(wb, chunk_size);
|
|
|
|
return wb.completed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::http::writechunk(window_buffer &buf,
|
2018-03-22 07:20:33 +01:00
|
|
|
const uint32_t &chunk_size)
|
2018-03-19 05:09:42 +01:00
|
|
|
{
|
|
|
|
writeline(buf, [&chunk_size](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
2018-03-22 07:20:33 +01:00
|
|
|
return ::snprintf(data(out), size(out), "%08x", chunk_size);
|
2018-03-19 05:09:42 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
std::string
|
|
|
|
ircd::http::strung(const vector_view<const header> &headers)
|
|
|
|
{
|
2018-03-05 08:45:23 +01:00
|
|
|
return ircd::string(serialized(headers), [&]
|
|
|
|
(window_buffer out)
|
|
|
|
{
|
|
|
|
write(out, headers);
|
|
|
|
return out.consumed();
|
|
|
|
});
|
2017-12-24 22:25:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates the buffer size required to write these headers. This size
|
|
|
|
/// may include room for a terminating null character which may be written
|
|
|
|
/// by write(headers). Only use write(headers) to know the actually written
|
|
|
|
/// string size (without null) not this.
|
|
|
|
size_t
|
|
|
|
ircd::http::serialized(const vector_view<const header> &headers)
|
|
|
|
{
|
|
|
|
// Because the write(header) functions use fmt::sprintf we have to
|
|
|
|
// indicate an extra space for a null string terminator to not overlof
|
2018-03-05 08:45:59 +01:00
|
|
|
const size_t initial{!headers.empty()};
|
2017-12-24 22:25:09 +01:00
|
|
|
return std::accumulate(std::begin(headers), std::end(headers), initial, []
|
|
|
|
(auto &ret, const auto &pair)
|
|
|
|
{
|
|
|
|
// key : SP value CRLF
|
|
|
|
return ret += pair.first.size() + 1 + 1 + pair.second.size() + 2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-08 06:30:20 +01:00
|
|
|
ircd::http::write(window_buffer &out,
|
2017-12-24 22:25:09 +01:00
|
|
|
const vector_view<const header> &headers)
|
|
|
|
{
|
|
|
|
for(const auto &header : headers)
|
|
|
|
write(out, header);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-08 06:30:20 +01:00
|
|
|
ircd::http::write(window_buffer &out,
|
2017-12-24 22:25:09 +01:00
|
|
|
const header &header)
|
|
|
|
{
|
|
|
|
if(header.second.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!header.first.empty());
|
|
|
|
if(unlikely(header.first.empty()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
writeline(out, [&header](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "%s: %s", header.first, header.second
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
/// Close over the user's closure to append a newline.
|
|
|
|
void
|
2018-02-08 06:30:20 +01:00
|
|
|
ircd::http::writeline(window_buffer &write,
|
|
|
|
const window_buffer::closure &closure)
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
2018-02-08 06:30:20 +01:00
|
|
|
// A new window_buffer is implicit constructed out of the mutable_buffer
|
2017-12-23 01:40:44 +01:00
|
|
|
// otherwise presented to this closure as its write window.
|
2018-02-08 06:30:20 +01:00
|
|
|
write([&closure](window_buffer write)
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
|
|
|
const auto newline{[](const mutable_buffer &out)
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
return copy(out, line::terminator);
|
2017-12-23 01:40:44 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
write(closure);
|
|
|
|
write(newline);
|
|
|
|
return write.consumed();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-08 06:30:20 +01:00
|
|
|
ircd::http::writeline(window_buffer &write)
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
|
|
|
writeline(write, [](const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:40:04 +01:00
|
|
|
/// Called to translate a grammar exception into an http::error within our
|
|
|
|
/// system. This will then usually propagate back to our client.
|
|
|
|
///
|
|
|
|
/// If we are a client to another server, set internal=true. Even though this
|
|
|
|
/// still generates an HTTP error, the code is 500 so if it propagates back to
|
|
|
|
/// a client it does not indicate to *that* client that *they* made a bad
|
|
|
|
/// request from a 400 back to them.
|
2018-01-12 08:38:12 +01:00
|
|
|
void
|
2018-03-19 22:40:04 +01:00
|
|
|
ircd::http::throw_error(const qi::expectation_failure<const char *> &e,
|
|
|
|
const bool &internal)
|
2018-01-12 08:38:12 +01:00
|
|
|
{
|
2018-03-19 22:40:04 +01:00
|
|
|
const auto &code_
|
|
|
|
{
|
|
|
|
internal?
|
|
|
|
code::INTERNAL_SERVER_ERROR:
|
|
|
|
code::BAD_REQUEST
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const &fmtstr
|
|
|
|
{
|
|
|
|
internal?
|
|
|
|
"I expected a valid HTTP %s. Server sent %zu invalid characters starting with `%s'.":
|
|
|
|
"I require a valid HTTP %s. You sent %zu invalid characters starting with `%s'."
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &rule
|
2018-01-12 08:38:12 +01:00
|
|
|
{
|
|
|
|
ircd::string(e.what_)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
2018-03-19 22:40:04 +01:00
|
|
|
code_, fmt::snstringf
|
2018-01-12 08:38:12 +01:00
|
|
|
{
|
2018-03-19 22:40:04 +01:00
|
|
|
512, fmtstr,
|
2018-01-12 08:38:12 +01:00
|
|
|
between(rule, "<", ">"),
|
|
|
|
size_t(e.last - e.first),
|
|
|
|
string_view{e.first, e.last}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-14 11:40:22 +02:00
|
|
|
//
|
|
|
|
// error
|
|
|
|
//
|
|
|
|
|
2018-03-05 09:37:05 +01:00
|
|
|
ircd::http::error::error(const http::code &code,
|
2017-12-24 22:25:09 +01:00
|
|
|
std::string content,
|
|
|
|
const vector_view<const header> &headers)
|
2018-03-05 09:37:05 +01:00
|
|
|
:http::error
|
2017-12-24 22:25:09 +01:00
|
|
|
{
|
|
|
|
code, std::move(content), strung(headers)
|
|
|
|
}
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-05 09:37:05 +01:00
|
|
|
ircd::http::error::error(const http::code &code,
|
2017-12-24 22:25:09 +01:00
|
|
|
std::string content,
|
|
|
|
std::string headers)
|
2018-12-05 00:20:12 +01:00
|
|
|
:ircd::error
|
|
|
|
{
|
|
|
|
generate_skip
|
|
|
|
}
|
|
|
|
,content
|
|
|
|
{
|
|
|
|
std::move(content)
|
|
|
|
}
|
|
|
|
,headers
|
|
|
|
{
|
|
|
|
std::move(headers)
|
|
|
|
}
|
2018-03-05 09:37:05 +01:00
|
|
|
,code{code}
|
2017-10-12 03:00:33 +02:00
|
|
|
{
|
2018-12-05 00:20:12 +01:00
|
|
|
auto &buf(ircd::error::buf);
|
|
|
|
::snprintf(buf, sizeof(buf), "%u %s", uint(code), status(code).c_str());
|
2017-10-12 03:00:33 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 11:40:22 +02:00
|
|
|
//
|
|
|
|
// status
|
|
|
|
//
|
|
|
|
|
2017-10-12 03:00:33 +02:00
|
|
|
ircd::http::code
|
|
|
|
ircd::http::status(const string_view &str)
|
|
|
|
{
|
|
|
|
static const auto grammar
|
|
|
|
{
|
|
|
|
parser.status_code
|
|
|
|
};
|
|
|
|
|
|
|
|
short ret;
|
|
|
|
const char *start(str.data());
|
|
|
|
const bool parsed(qi::parse(start, start + str.size(), grammar, ret));
|
|
|
|
if(!parsed || ret < 0 || ret >= 1000)
|
2017-12-30 09:07:23 +01:00
|
|
|
throw ircd::error{"Invalid HTTP status code"};
|
2017-10-12 03:00:33 +02:00
|
|
|
|
|
|
|
return http::code(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-03-11 18:23:06 +01:00
|
|
|
ircd::http::status(const http::code &code)
|
2018-02-07 04:28:37 +01:00
|
|
|
try
|
2017-10-12 03:00:33 +02:00
|
|
|
{
|
|
|
|
return reason.at(code);
|
|
|
|
}
|
2018-02-07 04:28:37 +01:00
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2018-03-11 18:23:06 +01:00
|
|
|
"No reason string for HTTP status code %u", uint(code)
|
2018-02-07 04:28:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return ""_sv;
|
|
|
|
}
|