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
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
namespace ircd::http
|
2017-08-28 23:51:22 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
using reason_codes = std::unordered_map<http::code, string_view>;
|
|
|
|
using expectation_failure = spirit::qi::expectation_failure<const char *>;
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
[[clang::internal_linkage, clang::always_destroy]]
|
|
|
|
extern const reason_codes reason;
|
2018-01-12 08:38:12 +01:00
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
[[noreturn]]
|
|
|
|
static void throw_error(const expectation_failure &, const bool & = false);
|
|
|
|
}
|
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" },
|
2020-03-12 02:18:53 +01:00
|
|
|
{ code::PROCESSING, "Processing" },
|
|
|
|
{ code::EARLY_HINTS, "Early Hints" },
|
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" },
|
2020-03-21 03:08:39 +01:00
|
|
|
|
|
|
|
{ code::CLOUDFLARE_REFUSED, "Cloudflare Customer Connection Refused" },
|
|
|
|
{ code::CLOUDFLARE_TIMEDOUT, "Cloudflare Customer Connection Timed-out" },
|
|
|
|
{ code::CLOUDFLARE_UNREACHABLE, "Cloudflare Customer Unreachable" },
|
|
|
|
{ code::CLOUDFLARE_REQUEST_TIMEOUT, "Cloudflare Customer Request Time-out" },
|
2017-03-21 03:25:10 +01:00
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
namespace ircd::http::parser
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
using namespace ircd::spirit;
|
2020-06-13 01:39:00 +02:00
|
|
|
|
|
|
|
template<class R = unused_type,
|
|
|
|
class... S>
|
2022-06-17 05:37:14 +02:00
|
|
|
struct [[clang::internal_linkage]] rule
|
|
|
|
:qi::rule<const char *, R, S...>
|
|
|
|
{
|
|
|
|
using qi::rule<const char *, R, S...>::rule;
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const expr NUL { lit('\0') ,"nul" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-21 03:25:10 +01:00
|
|
|
// insignificant whitespaces
|
2022-06-17 05:37:14 +02:00
|
|
|
const expr SP { lit('\x20') ,"space" };
|
|
|
|
const expr HT { lit('\x09') ,"horizontal tab" };
|
|
|
|
const expr ws { SP | HT ,"whitespace" };
|
|
|
|
|
|
|
|
const expr CR { lit('\x0D') ,"carriage return" };
|
|
|
|
const expr LF { lit('\x0A') ,"line feed" };
|
|
|
|
const expr CRLF { CR >> LF ,"carriage return, line feed" };
|
|
|
|
|
|
|
|
const expr illegal { NUL | CR | LF ,"illegal" };
|
|
|
|
const expr colon { lit(':') ,"colon" };
|
|
|
|
const expr slash { lit('/') ,"forward solidus" };
|
|
|
|
const expr question { lit('?') ,"question mark" };
|
|
|
|
const expr pound { lit('#') ,"pound sign" };
|
|
|
|
const expr equal { lit('=') ,"equal sign" };
|
|
|
|
const expr ampersand { lit('&') ,"ampersand" };
|
|
|
|
|
|
|
|
const rule<string_view> token { raw[+(char_ - (illegal | ws))] ,"token" };
|
|
|
|
const rule<string_view> str { raw[+(char_ - illegal)] ,"string" };
|
|
|
|
const rule<string_view> line { *ws >> -str >> CRLF ,"line" };
|
|
|
|
|
|
|
|
const rule<string_view> status { raw[repeat(3)[char_('0','9')]] ,"status" };
|
|
|
|
const rule<string_view> reason { str ,"reason" };
|
|
|
|
|
|
|
|
const rule<string_view> head_key { raw[+(char_ - (illegal | ws | colon))] ,"head key" };
|
2023-04-04 21:27:43 +02:00
|
|
|
const rule<string_view> head_val { -str ,"head value" };
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<http::header> header { head_key >> *ws >> colon >> *ws >> head_val ,"header" };
|
|
|
|
const rule<> headers { header % (*ws >> CRLF) ,"headers" };
|
|
|
|
|
|
|
|
const expr query_terminator { equal | question | ampersand | pound ,"query terminator" };
|
|
|
|
const expr query_illegal { illegal | ws | query_terminator ,"query illegal" };
|
|
|
|
const rule<string_view> query_key { raw[+(char_ - query_illegal)] ,"query key" };
|
|
|
|
const rule<string_view> query_val { raw[*(char_ - query_illegal)] ,"query value" };
|
|
|
|
|
|
|
|
const rule<string_view> method { token ,"method" };
|
|
|
|
const rule<string_view> path { raw[-slash >> *(char_ - query_illegal)] ,"path" };
|
|
|
|
const rule<string_view> fragment { pound >> -token ,"fragment" };
|
|
|
|
const rule<string_view> version { token ,"version" };
|
|
|
|
|
|
|
|
const rule<size_t> content_size { ulong_ ,"content length" };
|
|
|
|
const rule<uint32_t> chunk_size
|
2017-09-30 02:11:23 +02:00
|
|
|
{
|
2020-05-06 02:32:41 +02:00
|
|
|
qi::uint_parser<uint32_t, 16, 1, 8>{}
|
2017-09-30 02:11:23 +02:00
|
|
|
,"chunk size"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<string_view> chunk_extensions
|
2018-03-19 22:42:40 +01:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
';' >> raw[str] //TODO: extensions
|
2018-03-19 22:42:40 +01:00
|
|
|
,"chunk extensions"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<http::query> query
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
|
|
|
query_key >> -(equal >> query_val)
|
|
|
|
,"query"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<string_view> query_string
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
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"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<line::request> request_line
|
2017-03-21 03:25:10 +01:00
|
|
|
{
|
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"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<line::response> response_line
|
2017-03-21 03:25:10 +01:00
|
|
|
{
|
2022-06-17 23:04:39 +02:00
|
|
|
version >> +SP >> status >> -(+SP >> -reason)
|
2017-03-21 03:25:10 +01:00
|
|
|
,"response line"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<> request
|
2017-03-21 03:25:10 +01:00
|
|
|
{
|
|
|
|
request_line >> *ws >> CRLF >> -headers >> CRLF
|
|
|
|
,"request"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const rule<> response
|
2017-03-21 03:25:10 +01:00
|
|
|
{
|
|
|
|
response_line >> *ws >> CRLF >> -headers >> CRLF
|
|
|
|
,"response"
|
|
|
|
};
|
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);
|
2020-06-10 14:29:07 +02: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
|
|
|
|
2020-10-25 05:43:56 +01:00
|
|
|
// Note: pass your own user-agent with an empty value to mute this header.
|
|
|
|
if(!has(headers, "user-agent") && info::user_agent)
|
|
|
|
writeline(out, [](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "User-Agent: %s", info::user_agent
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-11 23:24:40 +01:00
|
|
|
namespace ircd::http
|
|
|
|
{
|
|
|
|
static void assign(request::head &, const header &);
|
|
|
|
}
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
ircd::http::request::head::head(parse::capstan &pc,
|
2020-03-13 19:07:39 +01:00
|
|
|
const headers::closure &closure)
|
2017-03-13 22:07:58 +01:00
|
|
|
: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) }
|
|
|
|
}
|
2020-03-13 19:07:39 +01:00
|
|
|
,headers{[this, &pc, &closure]
|
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"
|
|
|
|
};
|
|
|
|
|
2019-09-15 01:29:27 +02:00
|
|
|
if(unlikely(!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
|
|
|
};
|
|
|
|
|
2019-09-15 01:29:27 +02:00
|
|
|
return http::headers
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
pc, [this, &closure](const auto &header)
|
2020-03-11 23:24:40 +01:00
|
|
|
{
|
|
|
|
assign(*this, header);
|
2022-06-17 05:37:14 +02:00
|
|
|
if(likely(closure))
|
|
|
|
closure(header);
|
2020-03-11 23:24:40 +01:00
|
|
|
}
|
2019-09-15 01:29:27 +02:00
|
|
|
};
|
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()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:24:40 +01:00
|
|
|
void
|
|
|
|
ircd::http::assign(request::head &head,
|
|
|
|
const header &header)
|
|
|
|
{
|
2020-03-12 00:31:42 +01:00
|
|
|
char buf[64];
|
|
|
|
const auto &[key_, val] {header};
|
|
|
|
assert(size(key_) <= sizeof(buf));
|
|
|
|
const auto &key
|
2020-03-11 23:24:40 +01:00
|
|
|
{
|
2020-03-12 00:31:42 +01:00
|
|
|
tolower(buf, key_)
|
2020-03-11 23:24:40 +01:00
|
|
|
};
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
if(key == "content-length"_sv)
|
2022-06-17 05:37:14 +02:00
|
|
|
head.content_length = parser::content_length(val);
|
2020-03-12 00:31:42 +01:00
|
|
|
|
|
|
|
else if(key == "host"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.host = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "expect"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.expect = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "te"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.te = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "authorization"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.authorization = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "connection"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.connection = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "content-type"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.content_type = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "user-agent"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.user_agent = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "upgrade"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.upgrade = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "range"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.range = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "if-range"_sv)
|
2020-03-11 23:24:40 +01:00
|
|
|
head.if_range = val;
|
2020-04-23 08:37:53 +02:00
|
|
|
|
2023-02-23 23:29:27 +01:00
|
|
|
else if(key == "forwarded"_sv)
|
|
|
|
head.forwarded[0] = val;
|
|
|
|
|
2020-04-23 08:37:53 +02:00
|
|
|
else if(key == "x-forwarded-for"_sv)
|
2023-02-23 23:29:27 +01:00
|
|
|
head.forwarded_for[0] = val;
|
|
|
|
|
|
|
|
else if(key == "x-forwarded-host"_sv)
|
|
|
|
head.forwarded_host[0] = val;
|
2020-03-11 23:24:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2022-05-20 23:45:02 +02:00
|
|
|
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
|
|
|
{
|
2020-06-13 03:05:42 +02:00
|
|
|
assert(endswith(headers_s, "\r\n"));
|
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
|
|
|
}
|
|
|
|
|
2020-03-11 23:44:17 +01:00
|
|
|
namespace ircd::http
|
|
|
|
{
|
|
|
|
static void assign(response::head &, const header &);
|
|
|
|
}
|
|
|
|
|
2020-03-13 19:07:39 +01:00
|
|
|
ircd::http::response::head::head(parse::capstan &pc)
|
2022-06-17 05:37:14 +02:00
|
|
|
try
|
2017-03-13 22:07:58 +01:00
|
|
|
:line::response{pc}
|
2017-10-12 03:00:33 +02:00
|
|
|
,headers
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2023-02-23 04:46:52 +01:00
|
|
|
pc, [this](const auto &header)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2023-02-23 04:46:52 +01:00
|
|
|
assign(*this, header);
|
2020-03-13 19:07:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2022-06-17 05:37:14 +02:00
|
|
|
catch(const expectation_failure &e)
|
|
|
|
{
|
|
|
|
throw_error(e, true);
|
|
|
|
}
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2020-03-13 19:07:39 +01:00
|
|
|
ircd::http::response::head::head(parse::capstan &pc,
|
|
|
|
const headers::closure &closure)
|
2022-06-17 05:37:14 +02:00
|
|
|
try
|
2020-03-13 19:07:39 +01:00
|
|
|
:line::response{pc}
|
|
|
|
,headers
|
|
|
|
{
|
2023-02-23 04:46:52 +01:00
|
|
|
pc, [this, &closure](const auto &header)
|
2020-03-13 19:07:39 +01:00
|
|
|
{
|
2023-02-23 04:46:52 +01:00
|
|
|
assign(*this, header);
|
|
|
|
closure(header);
|
2020-03-11 23:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2022-06-17 05:37:14 +02:00
|
|
|
catch(const expectation_failure &e)
|
|
|
|
{
|
|
|
|
throw_error(e, true);
|
|
|
|
}
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2020-03-11 23:44:17 +01:00
|
|
|
void
|
|
|
|
ircd::http::assign(response::head &head,
|
|
|
|
const header &header)
|
|
|
|
{
|
2020-11-06 03:18:33 +01:00
|
|
|
char buf[96];
|
2020-03-12 00:31:42 +01:00
|
|
|
const auto &[key_, val] {header};
|
|
|
|
assert(size(key_) <= sizeof(buf));
|
2020-11-06 03:18:33 +01:00
|
|
|
if(unlikely(size(key_) > sizeof(buf)))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
REQUEST_HEADER_FIELDS_TOO_LARGE
|
|
|
|
};
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
const auto &key
|
2020-03-11 23:44:17 +01:00
|
|
|
{
|
2020-03-12 00:31:42 +01:00
|
|
|
tolower(buf, key_)
|
2020-03-11 23:44:17 +01:00
|
|
|
};
|
2019-06-15 21:48:01 +02:00
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
if(key == "content-length"_sv)
|
2022-06-17 05:37:14 +02:00
|
|
|
head.content_length = parser::content_length(val);
|
2017-09-30 02:11:23 +02:00
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "content-type"_sv)
|
|
|
|
head.content_type = val;
|
|
|
|
|
|
|
|
else if(key == "content-range"_sv)
|
2020-03-11 23:44:17 +01:00
|
|
|
head.content_range = val;
|
2018-01-24 18:15:16 +01:00
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "accept-range"_sv)
|
2020-03-11 23:44:17 +01:00
|
|
|
head.content_range = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "transfer-encoding"_sv)
|
2020-03-11 23:44:17 +01:00
|
|
|
head.transfer_encoding = val;
|
|
|
|
|
2020-03-12 00:31:42 +01:00
|
|
|
else if(key == "server"_sv)
|
2020-03-11 23:44:17 +01:00
|
|
|
head.server = val;
|
2020-03-12 00:32:55 +01:00
|
|
|
|
|
|
|
else if(key == "location"_sv)
|
|
|
|
head.location = val;
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
namespace ircd::http::parser
|
|
|
|
{
|
|
|
|
extern const rule<size_t> parse_chunk_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::http::parser::parse_chunk_head)
|
|
|
|
ircd::http::parser::parse_chunk_head
|
|
|
|
{
|
|
|
|
eoi | (eps > (chunk_size >> -chunk_extensions))
|
|
|
|
,"chunk head"
|
|
|
|
};
|
|
|
|
|
2018-03-19 22:42:40 +01:00
|
|
|
ircd::http::response::chunk::chunk(parse::capstan &pc)
|
|
|
|
try
|
|
|
|
:line{pc}
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
const char *start(line::begin()), *const stop(line::end());
|
2018-12-05 03:41:44 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, parser::parse_chunk_head, this->size)
|
2018-12-05 03:41:44 +01:00
|
|
|
};
|
|
|
|
|
2018-03-19 22:42:40 +01:00
|
|
|
assert(res == true);
|
|
|
|
}
|
2022-06-17 05:37:14 +02:00
|
|
|
catch(const expectation_failure &e)
|
2018-03-19 22:42:40 +01:00
|
|
|
{
|
|
|
|
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,
|
2023-02-23 21:53:33 +01:00
|
|
|
closure 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};
|
2022-06-23 00:33:38 +02:00
|
|
|
for(; !h.first.empty(); stopped = pc.parsed, 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
|
2023-02-23 21:53:33 +01:00
|
|
|
ircd::http::headers::for_each(const closure &closure)
|
2018-12-31 20:06:18 +01:00
|
|
|
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
|
|
|
|
//
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
namespace ircd::http::parser
|
|
|
|
{
|
|
|
|
extern const rule<http::header> parse_header;
|
|
|
|
extern const rule<http::line::request> parse_request;
|
|
|
|
extern const rule<http::line::response> parse_response;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::http::parser::parse_header)
|
|
|
|
ircd::http::parser::parse_header
|
|
|
|
{
|
2022-06-23 00:00:38 +02:00
|
|
|
eoi | expect[header]
|
2022-06-17 05:37:14 +02:00
|
|
|
,"header"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::http::parser::parse_request)
|
|
|
|
ircd::http::parser::parse_request
|
|
|
|
{
|
|
|
|
expect[request_line]
|
|
|
|
,"request head line"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::http::parser::parse_response)
|
|
|
|
ircd::http::parser::parse_response
|
|
|
|
{
|
|
|
|
expect[response_line]
|
|
|
|
,"response head line"
|
|
|
|
};
|
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
ircd::http::header::header(const line &line)
|
2016-11-29 16:23:38 +01:00
|
|
|
try
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
const char
|
|
|
|
*start(line.data()),
|
|
|
|
*const stop(line.data() + line.size());
|
|
|
|
const auto ok
|
|
|
|
{
|
|
|
|
ircd::parse(start, stop, parser::parse_header, *this)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(ok);
|
|
|
|
assert(start == stop);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2022-06-17 05:37:14 +02:00
|
|
|
catch(const expectation_failure &e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
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)
|
2022-06-17 05:37:14 +02:00
|
|
|
try
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
const char
|
|
|
|
*start(line.data()),
|
|
|
|
*const stop(line.data() + line.size());
|
|
|
|
const auto ok
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, parser::parse_response, *this)
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
assert(ok);
|
|
|
|
assert(start == stop);
|
|
|
|
}
|
|
|
|
catch(const expectation_failure &e)
|
|
|
|
{
|
|
|
|
throw_error(e, true);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::http::line::request::request(const line &line)
|
|
|
|
try
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
const char
|
|
|
|
*start(line.data()),
|
|
|
|
*const stop(line.data() + line.size());
|
|
|
|
const auto ok
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, parser::parse_request, *this)
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
assert(ok);
|
|
|
|
assert(start == stop);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2022-06-17 05:37:14 +02:00
|
|
|
catch(const expectation_failure &e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
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
|
|
|
|
//
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
namespace ircd::http::parser
|
|
|
|
{
|
|
|
|
extern const rule<string_view> parse_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::http::parser::parse_line)
|
|
|
|
ircd::http::parser::parse_line
|
|
|
|
{
|
|
|
|
expect[line]
|
|
|
|
,"line"
|
|
|
|
};
|
|
|
|
|
2019-06-29 03:23:43 +02:00
|
|
|
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
|
|
|
{
|
2022-06-23 00:00:38 +02:00
|
|
|
auto &ret(static_cast<string_view &>(*this));
|
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
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
if(start == stop)
|
2017-03-13 22:07:58 +01:00
|
|
|
return false;
|
2022-06-17 05:37:14 +02:00
|
|
|
|
|
|
|
const bool ok
|
|
|
|
{
|
|
|
|
ircd::parse(start, stop, parser::parse_line, ret)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(ok);
|
|
|
|
return ok;
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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 &)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2019-03-07 02:17:59 +01:00
|
|
|
{
|
|
|
|
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 &)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2019-03-07 02:17:59 +01:00
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-22 04:41:29 +02:00
|
|
|
ircd::vector_view<ircd::string_view>
|
|
|
|
ircd::http::query::string::array(const mutable_buffer &buf,
|
|
|
|
const string_view &key,
|
|
|
|
string_view *const &out,
|
|
|
|
const size_t &max)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(unlikely(!max))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
size_t ret(0);
|
2022-05-21 02:39:22 +02:00
|
|
|
window_buffer window(buf);
|
|
|
|
for_each(key, [&out, &max, &ret, &window]
|
2020-04-22 04:41:29 +02:00
|
|
|
(const auto &query)
|
|
|
|
{
|
2022-05-21 02:39:22 +02:00
|
|
|
window([&out, &max, &ret, &query]
|
2020-04-22 04:41:29 +02:00
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
assert(ret < max);
|
2020-11-22 03:03:34 +01:00
|
|
|
const auto &[_, val] {query};
|
|
|
|
out[ret] = url::decode(buf, val);
|
2020-04-22 04:41:29 +02:00
|
|
|
return out[ret];
|
|
|
|
});
|
|
|
|
|
|
|
|
return ++ret < max;
|
|
|
|
});
|
|
|
|
|
|
|
|
return vector_view<string_view>
|
|
|
|
{
|
|
|
|
out, 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)
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
if(unlikely(!ret))
|
2018-08-30 19:58:32 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
char buf[1024];
|
|
|
|
const string_view msg
|
2018-08-30 19:58:32 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
fmt::sprintf
|
|
|
|
{
|
|
|
|
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)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2019-03-07 02:17:59 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
const parser::rule<> grammar
|
2017-04-03 06:00:12 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
-parser::question >> (parser::query[action] % parser::ampersand)
|
|
|
|
,"query strings"
|
2017-04-03 06:00:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &s(*this);
|
2018-08-31 03:58:59 +02:00
|
|
|
const char *start(s.begin()), *const stop(s.end());
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, grammar);
|
2019-07-16 02:52:01 +02:00
|
|
|
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)
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
const char
|
|
|
|
*start(str.data()),
|
|
|
|
*const stop(start + str.size());
|
2017-03-18 04:28:49 +01:00
|
|
|
|
2020-05-06 02:39:45 +02:00
|
|
|
size_t ret;
|
2019-08-16 12:00:51 +02:00
|
|
|
const bool parsed
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, parser::content_size, ret)
|
2019-08-16 12:00:51 +02:00
|
|
|
};
|
|
|
|
|
2020-05-06 02:39:45 +02:00
|
|
|
if(!parsed || ret >= 256_GiB)
|
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
|
|
|
{
|
2022-05-21 02:39:22 +02:00
|
|
|
window_buffer window{buf};
|
|
|
|
writechunk(window, chunk_size);
|
|
|
|
return window.completed();
|
2018-03-19 05:09:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2019-09-24 02:09:51 +02:00
|
|
|
writeline(buf, [&chunk_size]
|
|
|
|
(const mutable_buffer &out) -> size_t
|
2018-03-19 05:09:42 +01:00
|
|
|
{
|
2020-08-28 10:42:08 +02:00
|
|
|
assert(size(out) >= (8 + 1));
|
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, []
|
2022-06-18 04:57:46 +02:00
|
|
|
(auto &&ret, const auto &pair)
|
2017-12-24 22:25:09 +01:00
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
2020-07-12 00:28:24 +02:00
|
|
|
writeline(write, []
|
|
|
|
(const mutable_buffer &out)
|
|
|
|
noexcept
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
|
|
|
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
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::http::throw_error(const expectation_failure &e,
|
2018-03-19 22:40:04 +01:00
|
|
|
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);
|
2022-06-29 22:20:52 +02:00
|
|
|
const auto msg(status(code));
|
|
|
|
::snprintf
|
|
|
|
(
|
|
|
|
buf, sizeof(buf),
|
|
|
|
"%u %s",
|
|
|
|
uint(code),
|
|
|
|
msg? msg.c_str(): ""
|
|
|
|
);
|
2017-10-12 03:00:33 +02:00
|
|
|
}
|
|
|
|
|
2020-02-28 21:12:51 +01:00
|
|
|
// Out-of-line placement.
|
|
|
|
ircd::http::error::~error()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-14 11:40:22 +02:00
|
|
|
//
|
|
|
|
// status
|
|
|
|
//
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
namespace ircd::http::parser
|
2017-10-12 03:00:33 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
extern const rule<uint8_t> status_codepoint;
|
|
|
|
extern const rule<enum http::category> status_category;
|
|
|
|
extern const rule<enum http::code> status_code;
|
2020-06-13 01:39:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
decltype(ircd::http::parser::status_codepoint)
|
|
|
|
ircd::http::parser::status_codepoint
|
2020-06-13 01:39:00 +02:00
|
|
|
{
|
|
|
|
qi::uint_parser<uint8_t, 10, 1, 1>{}
|
|
|
|
,"status codepoint"
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
decltype(ircd::http::parser::status_category)
|
|
|
|
ircd::http::parser::status_category
|
2020-06-13 01:39:00 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
&char_('1','9')
|
|
|
|
>> status_codepoint
|
|
|
|
>> omit[repeat(2)[char_('0','9')] >> (eoi | ws)]
|
|
|
|
,"status category"
|
2020-06-13 01:39:00 +02:00
|
|
|
};
|
|
|
|
|
2022-06-17 05:37:14 +02:00
|
|
|
decltype(ircd::http::parser::status_code)
|
|
|
|
ircd::http::parser::status_code
|
2020-06-13 01:39:00 +02:00
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
qi::uint_parser<uint16_t, 10, 3, 3>{}
|
|
|
|
>> omit[eoi | ws]
|
2020-06-13 01:39:00 +02:00
|
|
|
,"status code"
|
|
|
|
};
|
2017-10-12 03:00:33 +02:00
|
|
|
|
2020-06-13 01:39:00 +02:00
|
|
|
enum ircd::http::code
|
|
|
|
ircd::http::status(const string_view &str)
|
|
|
|
{
|
2017-10-12 03:00:33 +02:00
|
|
|
short ret;
|
2020-06-13 01:39:00 +02:00
|
|
|
const char *start(begin(str)), *const stop(end(str));
|
2020-06-10 14:29:07 +02:00
|
|
|
const bool parsed
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, parser::status_code, ret)
|
2020-06-10 14:29:07 +02:00
|
|
|
};
|
|
|
|
|
2020-06-13 01:39:00 +02:00
|
|
|
if(!parsed)
|
2020-03-13 22:20:41 +01:00
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"Invalid HTTP status code"
|
|
|
|
};
|
2017-10-12 03:00:33 +02:00
|
|
|
|
2020-06-13 01:39:00 +02:00
|
|
|
assert(ret >= 0 && ret < 1000);
|
2017-10-12 03:00:33 +02:00
|
|
|
return http::code(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2020-06-13 01:39:00 +02:00
|
|
|
ircd::http::status(const enum code &code)
|
|
|
|
noexcept 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)
|
|
|
|
{
|
2020-03-13 22:20:41 +01:00
|
|
|
log::dwarning
|
2018-02-07 04:28:37 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
2020-06-13 01:39:00 +02:00
|
|
|
|
2021-02-03 09:47:44 +01:00
|
|
|
enum ircd::log::level
|
|
|
|
ircd::http::severity(const enum category &category)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
switch(category)
|
|
|
|
{
|
|
|
|
case http::category::NONE: return log::level::DERROR;
|
|
|
|
case http::category::SUCCESS: return log::level::DEBUG;
|
|
|
|
case http::category::REDIRECT: return log::level::DWARNING;
|
|
|
|
case http::category::ERROR: return log::level::DERROR;
|
|
|
|
case http::category::SERVER: return log::level::DERROR;
|
|
|
|
case http::category::INTERNAL: return log::level::ERROR;
|
|
|
|
default:
|
|
|
|
case http::category::UNKNOWN: return log::level::DWARNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 01:39:00 +02:00
|
|
|
enum ircd::http::category
|
|
|
|
ircd::http::category(const string_view &str)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
enum category ret;
|
|
|
|
const char *start(begin(str)), *const stop(end(str));
|
|
|
|
const bool parsed
|
|
|
|
{
|
2022-06-17 05:37:14 +02:00
|
|
|
ircd::parse(start, stop, parser::status_category, ret)
|
2020-06-13 01:39:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!parsed || ret > category::UNKNOWN)
|
|
|
|
ret = category::UNKNOWN;
|
|
|
|
|
|
|
|
assert(uint8_t(ret) <= uint8_t(category::UNKNOWN));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::http::category
|
|
|
|
ircd::http::category(const enum code &code)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(ushort(code) == 0)
|
|
|
|
return category::NONE;
|
|
|
|
|
|
|
|
if(ushort(code) < 200)
|
|
|
|
return category::INFO;
|
|
|
|
|
|
|
|
if(ushort(code) < 300)
|
|
|
|
return category::SUCCESS;
|
|
|
|
|
|
|
|
if(ushort(code) < 400)
|
|
|
|
return category::REDIRECT;
|
|
|
|
|
|
|
|
if(ushort(code) < 500)
|
|
|
|
return category::ERROR;
|
|
|
|
|
2021-02-03 09:47:44 +01:00
|
|
|
if(ushort(code) == 500)
|
|
|
|
return category::INTERNAL;
|
|
|
|
|
2020-06-13 01:39:00 +02:00
|
|
|
if(ushort(code) < 600)
|
|
|
|
return category::SERVER;
|
|
|
|
|
|
|
|
return category::UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::http::category(const enum category &category)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
switch(category)
|
|
|
|
{
|
|
|
|
case category::NONE: return "NONE";
|
|
|
|
case category::INFO: return "INFO";
|
|
|
|
case category::SUCCESS: return "SUCCESS";
|
|
|
|
case category::REDIRECT: return "REDIRECT";
|
|
|
|
case category::ERROR: return "ERROR";
|
|
|
|
case category::SERVER: return "SERVER";
|
2021-02-03 09:47:44 +01:00
|
|
|
case category::INTERNAL: return "INTERNAL";
|
2022-06-14 01:29:23 +02:00
|
|
|
case category::UNKNOWN: break;
|
2020-06-13 01:39:00 +02:00
|
|
|
}
|
2022-06-14 01:29:23 +02:00
|
|
|
|
|
|
|
return "UNKNOWN";
|
2020-06-13 01:39:00 +02:00
|
|
|
}
|