...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The HTTP protocol defines the client and server roles: clients send requests and servers send back responses. When a client and server have established a connection, the client sends a series of requests while the server sends back at least one response for each received request in the order those requests were received.
A request or response is an HTTP
message (referred to hereafter as "message") having two
parts: a header with structured metadata and an optional variable-length
body holding arbitrary data. A serialized header is one or more text lines
where each line ends in a carriage return followed by linefeed ("\r\n"
). An empty line marks the
end of the header. The first line in the header is called the start-line.
The contents of the start line contents are different for requests and responses.
Every message contains a set of zero or more field name/value pairs, collectively
called "fields". The names and values are represented using text
strings with various requirements. A serialized field contains the field
name, then a colon followed by a space (":
"
), and finally the field value with a trailing CRLF.
Clients send requests, which contain a method and request-target, and HTTP-version. The method identifies the operation to be performed while the target identifies the object on the server to which the operation applies. The version is almost always 1.1, but older programs sometimes use 1.0.
Serialized Request |
Description |
---|---|
GET / HTTP/1.1\r\n User-Agent: Beast\r\n \r\n |
This request has a method of "GET", a target of "/", and indicates HTTP version 1.1. It contains a single field called "User-Agent" whose value is "Beast". There is no message body. |
Servers send responses, which contain a status-code, reason-phrase, and HTTP-version. The reason phrase is obsolete: clients SHOULD ignore the reason-phrase content. Here is a response which includes a body. The special Content-Length field informs the remote host of the size of the body which follows.
Serialized Response |
Description |
---|---|
HTTP/1.1 200 OK\r\n Server: Beast\r\n Content-Length: 13\r\n \r\n Hello, world! |
This response has a 200 status code meaning the operation requested completed successfully. The obsolete reason phrase is "OK". It specifies HTTP version 1.1, and contains a body 13 octets in size with the text "Hello, world!". |
Messages may optionally carry a body. The size of the message body is determined by the semantics of the message and the special fields Content-Length and Transfer-Encoding. rfc7230 section 3.3 provides a comprehensive description for how the body length is determined.
Certain fields appearing in messages are special. The library understands these fields when performing serialization and parsing, taking automatic action as needed when the fields are parsed in a message and also setting the fields if the caller requests it.
Table 1.14. Special Fields
Field |
Description |
---|---|
This field allows the sender to indicate desired control options for the current connection. Common values include "close", "keep-alive", and "upgrade". |
|
When present, this field informs the recipient about the exact size in bytes of the body which follows the message header. |
|
This optional field lists the names of the sequence of transfer codings that have been (or will be) applied to the content payload to form the message body. Beast understands the "chunked" coding scheme when it is the last (outermost) applied coding. The library will automatically apply chunked encoding when the content length is not known ahead of time during serialization, and the library will automatically remove chunked encoding from parsed messages when present. |
|
The Upgrade header field provides a mechanism to transition from HTTP/1.1 to another protocol on the same connection. For example, it is the mechanism used by WebSocket's initial HTTP handshake to establish a WebSocket connection. |