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 astd::istream
.See also
std::streambuf
on cppreference.comstd::istream
on cppreference.com
-
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 returnsfalse
if the content length is unknown.See also
-
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.See also
-
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 tofalse
.
-
http_protocol
protocol
() const¶ The HTTP protocol used by the request. If
NONE
, it’s usually safe to assume HTTP/1.0. IfUNKNOWN
, typically either a 400 Bad Request should be returned, just assume HTTP/1.0 to be permissive, or try to interpret something fromprotocol_string()
.
-
const std::string &
protocol_string
() const¶ The raw protocol string sent in the request, useful if
protocol()
isUNKNOWN
-
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
- List of common HTTP methods on Wikipedia for descriptions of the methods
-
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- the request did not send a Content-Length header, or
- 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 istrue
; so the boolean value needs to befalse
for a known content length andtrue
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()
isYES
/true