HTTP Primer
HTTP is a stream-oriented protocol between two connected programs: one acting as the client, the other as the server. While the connection is open, the client sends an HTTP request, which the server reads and answers with an HTTP response. These messages are paired in order; each request has exactly one corresponding response. This exchange of structured messages continues until either peer closes the connection, whether normally or due to an error.
HTTP messages consist of three parts: the start line, the headers, and the
message body. The start line differs between requests and responses, while
the headers and body share the same structure. Headers are made up of zero
or more fields, each expressed as a name–value pair. Both the start line and
the header fields use a line-oriented text format, with each line terminated
by a CRLF sequence (carriage return followed by line feed, i.e. bytes
0x0D 0x0A
). The message body is a sequence of bytes of defined length,
with content determined by the semantics of the start line and headers.
This diagram shows an actual HTTP request and HTTP response
HTTP Request | HTTP Response |
---|---|
|
|
More formally, the ABNF for HTTP messages is defined as follows:
Name | ABNF |
---|---|
message |
HTTP-message = request-line / status-line *( header-field CRLF ) CRLF [ message-body ] |
request-line |
request-line = method SP request-target SP HTTP-version CRLF |
status-line |
status-line = HTTP-version SP status-code SP reason-phrase CRLF |
Most HTTP header field values are domain-specific or application-defined, while certain fields commonly recur. The library understands these fields and takes appropriate action to ensure RFC compliance:
Field | Description |
---|---|
This field lets the sender specify control options for the current connection. Typical values include close, keep-alive, and upgrade. |
|
When present, this field tells the recipient the exact size of the message body, measured in bytes, that follows the header. |
|
This optional field specifies the sequence of transfer codings that have been, or will be, applied to the content payload to produce the message body. The library supports the chunked, gzip, deflate, and brotli encoding schemes, in any valid combination. Encodings can be automatically applied or removed as needed by the caller. |
|
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. |