Beast Logo

PrevUpHomeNext

Message

The HTTP protocol defines the client and server roles: clients send messages called requests and servers send back messages called responses. A HTTP message (referred to hereafter as "message") contains request or response specific attributes (contained in the "Start Line"), a series of zero or more name/value pairs (collectively termed "Fields"), and an optional series of octets called the message body which may be zero in length. The start line for a HTTP request includes a string called the method, a string called the URL, and a version number indicating HTTP/1.0 or HTTP/1.1. For a response, the start line contains an integer status code and a string called the reason phrase. Alternatively, a HTTP message can be viewed as two parts: a header, followed by a body.

[Note] Note

The Reason-Phrase is obsolete as of rfc7230.

The header class template models the header for HTTP/1 and HTTP/2 messages. This class template is a family of specializations, one for requests and one for responses, depending on the isRequest template value. The Fields template type determines the type of associative container used to store the field values. The provided basic_fields class template and fields type alias are typical choices for the Fields type, but advanced applications may supply user defined types which meet the requirements. The message class template models the header and optional body for HTTP/1 and HTTP/2 requests and responses. It is derived from the header class template with the same shared template parameters, and adds the body data member. The message class template requires an additional template argument type Body. This type controls the container used to represent the body, if any, as well as the algorithms needed to serialize and parse bodies of that type.

This illustration shows the declarations and members of the header and message class templates, as well as the inheritance relationship:

For notational convenience, these template type aliases are provided which supply typical choices for the Fields type:

using request_header = header<true, fields>;
using response_header = header<false, fields>;

template<class Body, class Fields = fields>
using request = message<true, Body, Fields>;

template<class Body, class Fields = fields>
using response = message<false, Body, Fields>;

The code examples below show how to create and fill in a request and response object:

Table 1. Create Message

HTTP Request

HTTP Response

request<empty_body> req;
req.version = 11;   // HTTP/1.1
req.method = "GET";
req.url = "/index.htm"
req.fields.insert("Accept", "text/html");
req.fields.insert("Connection", "keep-alive");
req.fields.insert("User-Agent", "Beast");
response<string_body> res;
res.version = 11;   // HTTP/1.1
res.status = 200;
res.reason = "OK";
res.fields.insert("Sever", "Beast");
res.fields.insert("Content-Length", 4);
res.body = "****";

In the serialized format of a HTTP message, the header is represented as a series of text lines ending in CRLF ("\r\n"). The end of the header is indicated by a line containing only CRLF. Here are examples of serialized HTTP request and response objects. The objects created above will produce these results when serialized. Note that only the response has a body:

Table 2. Serialized HTTP Request and Response

HTTP Request

HTTP Response

GET /index.htm HTTP/1.1\r\n
Accept: text/html\r\n
Connection: keep-alive\r\n
User-Agent: Beast\r\n
\r\n
200 OK HTTP/1.1\r\n
Server: Beast\r\n
Content-Length: 4\r\n
\r\n
****


PrevUpHomeNext