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
|
|
|
|
2017-03-16 21:35:37 +01:00
|
|
|
#include <ircd/spirit.h>
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::http
|
|
|
|
{
|
|
|
|
namespace spirit = boost::spirit;
|
|
|
|
namespace qi = spirit::qi;
|
2017-09-30 02:11:23 +02:00
|
|
|
namespace karma = spirit::karma;
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ascii = qi::ascii;
|
|
|
|
|
|
|
|
using qi::lit;
|
|
|
|
using qi::string;
|
|
|
|
using qi::char_;
|
|
|
|
using qi::short_;
|
|
|
|
using qi::int_;
|
|
|
|
using qi::long_;
|
|
|
|
using qi::repeat;
|
|
|
|
using qi::omit;
|
|
|
|
using qi::raw;
|
|
|
|
using qi::attr;
|
|
|
|
using qi::eps;
|
2017-09-30 02:11:23 +02:00
|
|
|
using qi::attr_cast;
|
|
|
|
using karma::maxwidth;
|
2018-03-09 01:35:39 +01:00
|
|
|
using spirit::unused_type;
|
2017-08-28 23:51:22 +02:00
|
|
|
|
|
|
|
template<class it, class top = unused_type> struct grammar;
|
|
|
|
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
|
|
|
|
|
|
|
[[noreturn]] void throw_error(const qi::expectation_failure<const char *> &);
|
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
|
|
|
)
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
const 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" },
|
|
|
|
{ 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" },
|
|
|
|
{ code::REQUEST_TIMEOUT, "Request Time-out" },
|
|
|
|
{ code::CONFLICT, "Conflict" },
|
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" },
|
|
|
|
{ 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" },
|
|
|
|
{ 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" },
|
|
|
|
{ code::HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported" },
|
|
|
|
{ code::INSUFFICIENT_STORAGE, "Insufficient Storage" },
|
2017-03-21 03:25:10 +01:00
|
|
|
};
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class it,
|
2017-08-28 23:51:22 +02:00
|
|
|
class top>
|
|
|
|
struct ircd::http::grammar
|
2016-11-29 16:23:38 +01:00
|
|
|
:qi::grammar<it, top>
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
qi::uint_parser<size_t, 16, 1, 8>{} >> CRLF
|
|
|
|
,"chunk size"
|
|
|
|
};
|
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
2017-10-03 14:05:15 +02:00
|
|
|
grammar(const rule<top> &top_rule)
|
2017-03-21 03:25:10 +01:00
|
|
|
:grammar<it, top>::base_type
|
|
|
|
{
|
|
|
|
top_rule
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::parser
|
2016-11-29 16:23:38 +01:00
|
|
|
:grammar<const char *, unused_type>
|
|
|
|
{
|
2017-03-18 04:28:49 +01:00
|
|
|
static size_t content_length(const string_view &val);
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
using http::grammar<const char *, unused_type>::grammar;
|
2017-10-03 14:05:15 +02:00
|
|
|
parser(): grammar { grammar::ws } {}
|
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
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&host](const mutable_buffer &out) -> size_t
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2017-12-23 01:40:44 +01:00
|
|
|
assert(!host.empty());
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Host: %s", host
|
|
|
|
};
|
|
|
|
});
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
if(content_length)
|
|
|
|
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
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
writeline(out, [&content_length](const mutable_buffer &out) -> size_t
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-12-23 01:40:44 +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) }
|
|
|
|
}
|
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
|
|
|
{
|
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;
|
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;
|
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;
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "content-length"_sv))
|
2017-10-12 03:00:33 +02:00
|
|
|
this->content_length = parser.content_length(h.second);
|
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;
|
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;
|
2018-01-12 22:15:30 +01:00
|
|
|
else if(iequals(h.first, "content-type"_sv))
|
|
|
|
this->content_type = h.second;
|
|
|
|
else if(iequals(h.first, "user-agent"_sv))
|
|
|
|
this->user_agent = h.second;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
if(c)
|
|
|
|
c(h);
|
2017-10-12 03:00:33 +02:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
{
|
2017-03-13 22:07:58 +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,
|
2017-12-24 22:25:09 +01:00
|
|
|
const string_view &headers_string,
|
|
|
|
const vector_view<const header> &headers_vector,
|
2017-12-23 01:40:44 +01:00
|
|
|
const bool &termination)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
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
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
if(code >= 200 && code < 300)
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
if(code < 400)
|
|
|
|
writeline(out, [](const mutable_buffer &out) -> size_t
|
|
|
|
{
|
|
|
|
char date_buf[96];
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "Date: %s", timef(date_buf, ircd::localtime)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
if(code != NO_CONTENT && content_type && content_length)
|
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-12-24 22:25:09 +01:00
|
|
|
if(code != NO_CONTENT && content_length != std::numeric_limits<size_t>::max())
|
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
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
if(!headers_string.empty())
|
|
|
|
out([&headers_string](const mutable_buffer &out)
|
2017-12-23 01:40:44 +01:00
|
|
|
{
|
2017-12-24 22:25:09 +01:00
|
|
|
return copy(out, headers_string);
|
2017-12-23 01:40:44 +01:00
|
|
|
});
|
2017-12-24 22:25:09 +01:00
|
|
|
|
|
|
|
if(!headers_vector.empty())
|
|
|
|
write(out, headers_vector);
|
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-09-28 03:32:31 +02:00
|
|
|
ircd::http::response::chunked::chunked(const code &code,
|
|
|
|
const write_closure &closure,
|
2017-09-30 02:11:23 +02:00
|
|
|
const vector_view<const header> &user_headers)
|
2017-09-28 03:32:31 +02:00
|
|
|
:closure{closure}
|
|
|
|
{
|
|
|
|
const auto num_headers
|
|
|
|
{
|
2017-09-30 02:11:23 +02:00
|
|
|
user_headers.size() + 1
|
2017-09-28 03:32:31 +02:00
|
|
|
};
|
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
header headers[num_headers]
|
2017-09-28 03:32:31 +02:00
|
|
|
{
|
|
|
|
{ "Transfer-Encoding", "chunked" }
|
|
|
|
};
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
std::copy(begin(user_headers), end(user_headers), headers + 1);
|
2017-09-28 03:32:31 +02:00
|
|
|
|
2017-12-23 01:40:44 +01:00
|
|
|
//TODO: bitrot
|
|
|
|
assert(0);
|
|
|
|
/*
|
2017-09-28 03:32:31 +02:00
|
|
|
response
|
|
|
|
{
|
|
|
|
code, {}, closure, { headers, headers + num_headers }
|
|
|
|
};
|
2017-12-23 01:40:44 +01:00
|
|
|
*/
|
2017-09-28 03:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::http::response::chunked::~chunked()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
chunk(*this, null_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::http::response::chunked::chunk::chunk(chunked &chunked,
|
|
|
|
const const_buffer &buffer)
|
|
|
|
{
|
|
|
|
char size_buf[16];
|
|
|
|
const auto size_size
|
|
|
|
{
|
2017-09-30 02:11:23 +02:00
|
|
|
snprintf(size_buf, sizeof(size_buf), "%lx", size(buffer))
|
2017-09-28 03:32:31 +02:00
|
|
|
};
|
|
|
|
|
2018-01-07 06:36:17 +01:00
|
|
|
const ilist<const const_buffer> iov
|
2017-09-28 03:32:31 +02:00
|
|
|
{
|
|
|
|
{ size_buf, size_t(size_size) },
|
|
|
|
{ "\r\n", 2 },
|
2017-09-30 02:11:23 +02:00
|
|
|
{ buffer },
|
2017-09-28 03:32:31 +02:00
|
|
|
{ "\r\n", 2 },
|
|
|
|
};
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
assert(bool(chunked.closure));
|
2017-09-28 03:32:31 +02:00
|
|
|
chunked.closure(iov);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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
|
|
|
|
2018-02-26 13:00:36 +01:00
|
|
|
else if(iequals(h.first, "content-type"s))
|
|
|
|
this->content_type = h.second;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
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})
|
2017-03-13 22:07:58 +01:00
|
|
|
if(c)
|
|
|
|
c(h);
|
2017-10-12 03:00:33 +02:00
|
|
|
|
|
|
|
return { started, stopped };
|
|
|
|
}()}
|
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:00:12 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::http::query::string::at(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto ret(operator[](key));
|
|
|
|
if(ret.empty())
|
2017-12-30 09:07:23 +01:00
|
|
|
throw std::out_of_range{"Failed to find value for required query string key"};
|
2017-04-03 06:00:12 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::http::query::string::operator[](const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
const auto match([&key, &ret](const query &query) -> bool
|
|
|
|
{
|
|
|
|
if(query.first == key)
|
|
|
|
{
|
|
|
|
ret = query.second;
|
|
|
|
return false; // false to break out of until()
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
until(match);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::http::query::string::until(const std::function<bool (const query &)> &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto action([&closure](const auto &attribute, const auto &context, auto &halt)
|
|
|
|
{
|
|
|
|
halt = closure(attribute);
|
|
|
|
});
|
|
|
|
|
|
|
|
const parser::rule<unused_type> grammar
|
|
|
|
{
|
|
|
|
-parser.question >> (parser.query[action] % parser.ampersand)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &s(*this);
|
|
|
|
const char *start(s.data()), *const stop(s.data() + s.size());
|
|
|
|
return qi::parse(start, stop, grammar);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::http::query::string::for_each(const std::function<void (const query &)> &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto action([&closure](const auto &attribute, const auto &context, auto &halt)
|
|
|
|
{
|
|
|
|
closure(attribute);
|
|
|
|
});
|
|
|
|
|
|
|
|
const parser::rule<unused_type> grammar
|
|
|
|
{
|
|
|
|
-parser.question >> (parser.query[action] % parser.ampersand)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &s(*this);
|
|
|
|
const char *start(s.data()), *const stop(s.data() + s.size());
|
|
|
|
qi::parse(start, stop, grammar);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
const bool parsed(qi::parse(start, start + str.size(), grammar, ret));
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return copy(out, "\r\n"_sv);
|
|
|
|
}};
|
|
|
|
|
|
|
|
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-01-12 08:38:12 +01:00
|
|
|
void
|
|
|
|
ircd::http::throw_error(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
const auto rule
|
|
|
|
{
|
|
|
|
ircd::string(e.what_)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
code::BAD_REQUEST, fmt::snstringf
|
|
|
|
{
|
|
|
|
512, "I require a valid HTTP %s. You sent %zu invalid characters starting with `%s'.",
|
|
|
|
between(rule, "<", ">"),
|
|
|
|
size_t(e.last - e.first),
|
|
|
|
string_view{e.first, e.last}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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)
|
2017-10-12 03:00:33 +02:00
|
|
|
:ircd::error{generate_skip}
|
|
|
|
,content{std::move(content)}
|
2017-12-24 22:25:09 +01:00
|
|
|
,headers{std::move(headers)}
|
2018-03-05 09:37:05 +01:00
|
|
|
,code{code}
|
2017-10-12 03:00:33 +02:00
|
|
|
{
|
2017-12-23 01:40:44 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%d %s", int(code), status(code).c_str());
|
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
|
|
|
|
ircd::http::status(const enum 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
|
|
|
|
{
|
|
|
|
"No reason string for HTTP status code %d", int(code)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ""_sv;
|
|
|
|
}
|