Request

class request : public std::streambuf

Represents a single request sent by a client. Inherits from std::streambuf, so it can be used as-is or with a std::istream.

See also

enum content_length_flag

A utility type for unknown_content_length() with the values:

Value Evaluates to
NO false
YES true
MAYBE true
const std::string &client_address() const

The IP address of the client that sent the request

const unsigned int client_port() const

The port of the client that sent the request

bool eof() const

Returns whether or not the request, acting as a std::streambuf, has reached the end of the request contents. Always returns false if the content length is unknown.

request(connection&)

Constructs a new request on a connection. Blocks until a connection is sent, the connection timeout is reached, or the client disconnects. May also throw request_parse_error if the data sent by the client cannot be understood as an HTTP request.

request(request&&)

Explicit move constructor as one can’t be generated for this class

void flush()

Flushes the request contents from the buffer, putting it in a state where the next request can be extracted. It is only safe to call this function if unknown_content_length() evaluates to false.

http_protocol protocol() const

The HTTP protocol used by the request. If NONE, it’s usually safe to assume HTTP/1.0. If UNKNOWN, typically either a 400 Bad Request should be returned, just assume HTTP/1.0 to be permissive, or try to interpret something from protocol_string().

const std::string &protocol_string() const

The raw protocol string sent in the request, useful if protocol() is UNKNOWN

const std::string &method() const

The request method as a capitalized ASCII string. While the HTTP protocol technically does not restrict the available methods, typically this will be one of the following:

GET Common methods
POST
PUT
DELETE
OPTIONS Useful for APIs
PATCH Relatively uncommon methods
TRACE
HEAD
CONNECT

See also

const std::vector<std::string> &path() const

The request path separated into its elements, each of which has been URL- or percent-decoded. For example:

/foo/bar/hello+world/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF

becomes:

{
    "foo",
    "bar"
    "hello world",
    "こんにちは"
}
const query_args_type &query_args() const

The request query arguments. SHOW is very permissive in how it parses query arguments:

Query string Interpreted as
?foo=1&bar=2 { { "foo", { "1" } }, { "bar", { "2" } } }
?foo=bar=baz { { "foo", { "baz" } }, { "bar", { "baz" } } }
?foo=&bar=baz { { "foo", { "" } }, { "bar", { "baz" } } }
?foo&bar=1&bar=2 { { "foo", { "" } }, { "bar", { "1", "2" } } }
const headers_type &headers() const

The request headers

content_length_flag unknown_content_length() const

Whether the content length of the request could be interpreted

This member may be a bit confusing because it is “un-known” rather than “know”. It’s convenient for content_length_flag to evaluate to a boolean value, but there are two possible reasons the content length would be unknown. Either

  1. the request did not send a Content-Length header, or
  2. the value supplied is not an integer or multiple Content-Length headers were sent.

In many languages (including C++), 0 is false and any other value is true; so the boolean value needs to be false for a known content length and true for anything else.

unsigned long long content_length() const

The number of bytes in the request content; only holds a meaningful value if unknown_content_length() is YES/true