2016-11-29 16:23:38 +01:00
|
|
|
/*
|
|
|
|
* charybdis: 21st Century IRC++d
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_HTTP_H
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// HyperText TransPort: formal grammars & tools
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::http
|
|
|
|
{
|
|
|
|
enum code :int;
|
|
|
|
struct error;
|
|
|
|
struct line;
|
|
|
|
struct query;
|
2017-12-24 22:25:09 +01:00
|
|
|
struct header;
|
2017-08-28 23:51:22 +02:00
|
|
|
struct headers;
|
|
|
|
struct content;
|
|
|
|
struct request;
|
|
|
|
struct response;
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
string_view status(const enum code &);
|
2017-08-28 23:51:22 +02:00
|
|
|
enum code status(const string_view &);
|
2017-12-24 22:25:09 +01:00
|
|
|
|
|
|
|
void writeline(stream_buffer &);
|
|
|
|
void writeline(stream_buffer &, const stream_buffer::closure &);
|
|
|
|
|
|
|
|
void write(stream_buffer &out, const header &);
|
|
|
|
void write(stream_buffer &out, const vector_view<const header> &);
|
|
|
|
size_t serialized(const vector_view<const header> &);
|
|
|
|
std::string strung(const vector_view<const header> &);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 22:41:47 +02:00
|
|
|
//
|
|
|
|
// Add more as you go...
|
|
|
|
//
|
2017-08-28 23:51:22 +02:00
|
|
|
enum ircd::http::code
|
|
|
|
:int
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 20:02:24 +01:00
|
|
|
CONTINUE = 100,
|
|
|
|
SWITCHING_PROTOCOLS = 101,
|
|
|
|
|
|
|
|
OK = 200,
|
|
|
|
CREATED = 201,
|
|
|
|
ACCEPTED = 202,
|
|
|
|
NON_AUTHORITATIVE_INFORMATION = 203,
|
|
|
|
NO_CONTENT = 204,
|
2017-04-07 01:54:20 +02:00
|
|
|
PARTIAL_CONTENT = 206,
|
|
|
|
|
2017-10-12 03:00:33 +02:00
|
|
|
MULTIPLE_CHOICES = 300,
|
2017-04-07 01:54:20 +02:00
|
|
|
MOVED_PERMANENTLY = 301,
|
|
|
|
FOUND = 302,
|
|
|
|
SEE_OTHER = 303,
|
|
|
|
NOT_MODIFIED = 304,
|
|
|
|
TEMPORARY_REDIRECT = 305,
|
|
|
|
PERMANENT_REDIRECT = 306,
|
2017-03-13 20:02:24 +01:00
|
|
|
|
|
|
|
BAD_REQUEST = 400,
|
|
|
|
UNAUTHORIZED = 401,
|
|
|
|
FORBIDDEN = 403,
|
|
|
|
NOT_FOUND = 404,
|
|
|
|
METHOD_NOT_ALLOWED = 405,
|
|
|
|
REQUEST_TIMEOUT = 408,
|
2017-04-03 05:59:18 +02:00
|
|
|
CONFLICT = 409,
|
2018-01-12 03:41:58 +01:00
|
|
|
PAYLOAD_TOO_LARGE = 413,
|
2017-04-03 05:59:18 +02:00
|
|
|
REQUEST_URI_TOO_LONG = 414,
|
2017-03-13 20:02:24 +01:00
|
|
|
EXPECTATION_FAILED = 417,
|
2017-04-07 02:08:40 +02:00
|
|
|
IM_A_TEAPOT = 418,
|
2017-04-03 05:59:18 +02:00
|
|
|
UNPROCESSABLE_ENTITY = 422,
|
2017-03-13 20:02:24 +01:00
|
|
|
TOO_MANY_REQUESTS = 429,
|
|
|
|
REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
|
|
|
|
|
|
|
|
INTERNAL_SERVER_ERROR = 500,
|
|
|
|
NOT_IMPLEMENTED = 501,
|
|
|
|
SERVICE_UNAVAILABLE = 503,
|
|
|
|
HTTP_VERSION_NOT_SUPPORTED = 505,
|
2017-04-03 05:59:18 +02:00
|
|
|
INSUFFICIENT_STORAGE = 507,
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Root exception for HTTP.
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::error
|
2016-11-29 16:23:38 +01:00
|
|
|
:ircd::error
|
|
|
|
{
|
2017-03-11 02:46:25 +01:00
|
|
|
enum code code;
|
2016-11-29 16:23:38 +01:00
|
|
|
std::string content;
|
2017-12-24 22:25:09 +01:00
|
|
|
std::string headers;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-12-24 22:25:09 +01:00
|
|
|
error(const enum code &, std::string content = {}, std::string headers = {});
|
|
|
|
error(const enum code &, std::string content, const vector_view<const header> &);
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents a single \r\n delimited line used in HTTP.
|
|
|
|
///
|
|
|
|
/// This object is just a string_view of that line. The actual data backing
|
|
|
|
/// that view is the responsibility of the user. This object is constructed
|
|
|
|
/// with an ircd::parse::capstan argument which is used by the formal grammar
|
|
|
|
/// in the constructor.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::line
|
2016-11-29 16:23:38 +01:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
struct request;
|
|
|
|
struct response;
|
|
|
|
|
|
|
|
using string_view::string_view;
|
2017-03-13 23:24:42 +01:00
|
|
|
line(parse::capstan &);
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents a 'request line' or the first line a client sends to a server.
|
|
|
|
///
|
|
|
|
/// This is a dual-use class. For HTTP clients, one may simply connect the
|
|
|
|
/// members to the proper strings and then pass this structure to a function
|
|
|
|
/// making a client request. For HTTP servers, pass an http::line to the ctor
|
|
|
|
/// and the formal grammar will set the members appropriately. The actual data
|
|
|
|
/// behind these members is the responsibility of the user.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::line::request
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
string_view method;
|
2017-04-03 06:00:12 +02:00
|
|
|
string_view path;
|
|
|
|
string_view query;
|
|
|
|
string_view fragment;
|
2016-11-29 16:23:38 +01:00
|
|
|
string_view version;
|
|
|
|
|
|
|
|
request(const line &);
|
|
|
|
request() = default;
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents a 'response line' or the first line a server sends to a client.
|
|
|
|
///
|
|
|
|
/// This is a dual-use class and symmetric to the http::line::request class.
|
|
|
|
/// Servers may set the members and then use this object to respond to a client
|
|
|
|
/// while clients should provide an http::line to the constructor which will
|
|
|
|
/// fill in the members.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::line::response
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
string_view version;
|
|
|
|
string_view status;
|
|
|
|
string_view reason;
|
|
|
|
|
|
|
|
response(const line &);
|
|
|
|
response() = default;
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents a single key/value pair in a query string.
|
|
|
|
///
|
|
|
|
/// This is used by the ircd::http::query::string object when parsing query
|
|
|
|
/// strings.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::query
|
2017-04-03 06:00:12 +02:00
|
|
|
:std::pair<string_view, string_view>
|
|
|
|
{
|
|
|
|
struct string;
|
|
|
|
|
|
|
|
bool operator<(const string_view &s) const { return iless(first, s); }
|
|
|
|
bool operator==(const string_view &s) const { return iequals(first, s); }
|
|
|
|
|
|
|
|
using std::pair<string_view, string_view>::pair;
|
|
|
|
query() = default;
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Tool for parsing an HTTP query string.
|
|
|
|
///
|
|
|
|
/// Query string is read as a complete string off the tape (into request.query)
|
|
|
|
/// and not parsed further. To make queries into that string use this class to
|
|
|
|
/// view it. Once this object is constructed by viewing the whole query string,
|
|
|
|
/// the member functions invoke the formal grammar to get individual key/value
|
|
|
|
/// pairs.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::query::string
|
2017-04-03 06:00:12 +02:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
void for_each(const std::function<void (const query &)> &) const;
|
|
|
|
bool until(const std::function<bool (const query &)> &) const;
|
|
|
|
|
|
|
|
string_view at(const string_view &key) const;
|
|
|
|
string_view operator[](const string_view &key) const;
|
|
|
|
|
|
|
|
using string_view::string_view;
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents an HTTP header key/value pair.
|
|
|
|
///
|
|
|
|
/// This is a dual-use class. Those sending headers will simply fill in the
|
|
|
|
/// components of the std::pair. Those receiving headers can pass the ctor an
|
|
|
|
/// ircd::http::line which will construct the pair using the formal grammars.
|
|
|
|
///
|
2017-12-24 22:25:09 +01:00
|
|
|
struct ircd::http::header
|
2016-11-29 16:23:38 +01:00
|
|
|
:std::pair<string_view, string_view>
|
|
|
|
{
|
|
|
|
bool operator<(const string_view &s) const { return iless(first, s); }
|
|
|
|
bool operator==(const string_view &s) const { return iequals(first, s); }
|
|
|
|
|
|
|
|
using std::pair<string_view, string_view>::pair;
|
|
|
|
header(const line &);
|
|
|
|
header() = default;
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// This device allows parsing HTTP headers directly off the wire without state
|
|
|
|
///
|
|
|
|
/// The constructor of this object contains the grammar to read HTTP headers
|
|
|
|
/// from the capstan and then proffer them one by one to the provided closure,
|
|
|
|
/// that's all it does.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::headers
|
2017-10-12 03:00:33 +02:00
|
|
|
:string_view
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-30 02:11:23 +02:00
|
|
|
using closure = std::function<void (const header &)>;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
headers(parse::capstan &, const closure & = {});
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents the content of an HTTP request after the head.
|
|
|
|
///
|
|
|
|
/// Use the request::content / response::content wrappers. They ensure the
|
|
|
|
/// proper amount of content is read and the tape is in the right position
|
|
|
|
/// for the next request with exception safety. In other words, this object
|
|
|
|
/// ensures the capstan is in the proper place for the next request no matter
|
|
|
|
/// what happens; whether an exception happened, or whether the user simply
|
|
|
|
/// didn't care to read the content. The capstan MUST advance Content-Length
|
|
|
|
/// bytes in any case.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::content
|
2017-03-13 22:07:58 +01:00
|
|
|
:string_view
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
IRCD_OVERLOAD(discard)
|
2017-09-30 02:11:23 +02:00
|
|
|
IRCD_OVERLOAD(chunked)
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
content(parse::capstan &, const size_t &length, discard_t);
|
|
|
|
content(parse::capstan &, const size_t &length);
|
2017-09-30 02:11:23 +02:00
|
|
|
content(parse::capstan &, chunked_t);
|
2017-03-13 22:07:58 +01:00
|
|
|
content() = default;
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-12-12 21:38:16 +01:00
|
|
|
/// HTTP request suite. Functionality to send and receive requests.
|
|
|
|
///
|
|
|
|
struct ircd::http::request
|
|
|
|
{
|
|
|
|
struct head;
|
|
|
|
struct content;
|
|
|
|
|
|
|
|
using proffer = std::function<void (const head &)>;
|
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
// compose a request into buffer
|
2017-12-23 01:40:44 +01:00
|
|
|
request(stream_buffer &,
|
|
|
|
const string_view &host,
|
2017-12-12 21:38:16 +01:00
|
|
|
const string_view &method = "GET",
|
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 = 0,
|
|
|
|
const string_view &content_type = {},
|
|
|
|
const vector_view<const header> & = {},
|
|
|
|
const bool &termination = true);
|
2017-12-12 21:38:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Represents an HTTP request head. This is only for receiving requests.
|
|
|
|
///
|
|
|
|
struct ircd::http::request::head
|
|
|
|
:line::request
|
|
|
|
{
|
|
|
|
string_view host;
|
|
|
|
string_view expect;
|
|
|
|
string_view te;
|
|
|
|
string_view authorization;
|
|
|
|
string_view connection;
|
2018-01-12 22:15:30 +01:00
|
|
|
string_view content_type;
|
|
|
|
string_view user_agent;
|
2017-12-12 21:38:16 +01:00
|
|
|
size_t content_length {0};
|
|
|
|
|
2018-01-21 09:38:47 +01:00
|
|
|
string_view uri; // full view of (path, query, fragmet)
|
|
|
|
string_view headers; // full view of all headers
|
2017-12-12 21:38:16 +01:00
|
|
|
|
|
|
|
head(parse::capstan &pc, const headers::closure &c = {});
|
2018-01-12 22:15:30 +01:00
|
|
|
head() = default;
|
2017-12-12 21:38:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Represents an HTTP request content. This is only for receiving content.
|
|
|
|
///
|
|
|
|
struct ircd::http::request::content
|
|
|
|
:http::content
|
|
|
|
{
|
|
|
|
content(parse::capstan &pc, const head &h, discard_t)
|
|
|
|
:http::content{pc, h.content_length, discard}
|
|
|
|
{}
|
|
|
|
|
|
|
|
content(parse::capstan &pc, const head &h)
|
|
|
|
:http::content{pc, h.content_length}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// HTTP response suite. Functionality to send and receive responses.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::response
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
struct head;
|
2017-03-13 22:07:58 +01:00
|
|
|
struct content;
|
2017-09-28 03:32:31 +02:00
|
|
|
struct chunked;
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2018-01-07 06:36:17 +01:00
|
|
|
using write_closure = std::function<void (const ilist<const const_buffer> &)>;
|
2017-03-13 22:07:58 +01:00
|
|
|
using proffer = std::function<void (const head &)>;
|
|
|
|
|
2018-01-12 22:15:30 +01:00
|
|
|
// compose a response into buffer
|
2017-12-23 01:40:44 +01:00
|
|
|
response(stream_buffer &,
|
|
|
|
const code & = code::OK,
|
|
|
|
const size_t &content_length = 0,
|
|
|
|
const string_view &content_type = {},
|
2017-12-24 22:25:09 +01:00
|
|
|
const string_view &headers = {},
|
2017-12-23 01:40:44 +01:00
|
|
|
const vector_view<const header> & = {},
|
|
|
|
const bool &termination = true);
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-09-28 03:32:31 +02:00
|
|
|
response() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::http::response::chunked
|
|
|
|
:response
|
|
|
|
{
|
|
|
|
struct chunk;
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
write_closure closure;
|
2017-09-28 03:32:31 +02:00
|
|
|
|
|
|
|
chunked(const code &,
|
|
|
|
const write_closure &,
|
2017-09-30 02:11:23 +02:00
|
|
|
const vector_view<const header> &headers);
|
2017-09-28 03:32:31 +02:00
|
|
|
|
|
|
|
chunked(const chunked &) = delete;
|
|
|
|
~chunked() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::http::response::chunked::chunk
|
|
|
|
{
|
|
|
|
chunk(chunked &, const const_buffer &);
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents an HTTP response head. This is for receiving responses only.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::response::head
|
2017-03-13 22:07:58 +01:00
|
|
|
:line::response
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 22:07:58 +01:00
|
|
|
size_t content_length {0};
|
2017-09-30 02:11:23 +02:00
|
|
|
string_view transfer_encoding;
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-10-12 03:00:33 +02:00
|
|
|
string_view headers;
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
head(parse::capstan &pc, const headers::closure &c = {});
|
2018-01-14 03:03:26 +01:00
|
|
|
head() = default;
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:35:25 +02:00
|
|
|
/// Represents an HTTP response content. This is for receiving only.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::http::response::content
|
2017-03-13 22:07:58 +01:00
|
|
|
:http::content
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-13 23:24:42 +01:00
|
|
|
content(parse::capstan &pc, const head &h, discard_t)
|
2017-03-13 22:07:58 +01:00
|
|
|
:http::content{pc, h.content_length, discard}
|
|
|
|
{}
|
|
|
|
|
2017-09-30 02:11:23 +02:00
|
|
|
content(parse::capstan &pc, const head &h, chunked_t)
|
|
|
|
:http::content{pc, chunked}
|
|
|
|
{}
|
|
|
|
|
2017-03-13 23:24:42 +01:00
|
|
|
content(parse::capstan &pc, const head &h)
|
2017-03-13 22:07:58 +01:00
|
|
|
:http::content{pc, h.content_length}
|
2016-11-29 16:23:38 +01:00
|
|
|
{}
|
2017-03-13 22:07:58 +01:00
|
|
|
|
|
|
|
content() = default;
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|