|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/buffers |
| 8 | +// |
| 9 | + |
| 10 | += HTTP Primer |
| 11 | + |
| 12 | +HTTP is a stream-oriented protocol between two connected programs: one acting |
| 13 | +as the client, the other as the server. While the connection is open, the client |
| 14 | +sends an HTTP request, which the server reads and answers with an HTTP response. |
| 15 | +These _messages_ are paired in order; each request has exactly one |
| 16 | +corresponding response. This exchange of structured messages continues until |
| 17 | +either peer closes the connection, whether normally or due to an error. |
| 18 | + |
| 19 | +HTTP messages consist of three parts: the start line, the headers, and the |
| 20 | +message body. The start line differs between requests and responses, while |
| 21 | +the headers and body share the same structure. Headers are made up of zero |
| 22 | +or more fields, each expressed as a name–value pair. Both the start line and |
| 23 | +the header fields use a line-oriented text format, with each line terminated |
| 24 | +by a CRLF sequence (carriage return followed by line feed, i.e. bytes |
| 25 | +`0x0D 0x0A`). The message body is a sequence of bytes of defined length, |
| 26 | +with content determined by the semantics of the start line and headers. |
| 27 | + |
| 28 | +This diagram shows an actual HTTP request and HTTP response |
| 29 | + |
| 30 | +[cols="1a,1a"] |
| 31 | +|=== |
| 32 | +|HTTP Request|HTTP Response |
| 33 | + |
| 34 | +| |
| 35 | +[source] |
| 36 | +---- |
| 37 | +GET /index.html HTTP/1.1\r\n |
| 38 | +User-Agent: Boost\r\n |
| 39 | +\r\n |
| 40 | +---- |
| 41 | +| |
| 42 | +[source] |
| 43 | +---- |
| 44 | +HTTP/1.1 200 OK\r\n |
| 45 | +Server: Boost.Http.Proto\r\n |
| 46 | +Content-Length: 13\r\n |
| 47 | +\r\n |
| 48 | +Hello, world! |
| 49 | +---- |
| 50 | + |
| 51 | +|=== |
| 52 | + |
| 53 | +More formally, the ABNF for HTTP messages is defined as follows: |
| 54 | + |
| 55 | +[cols="1a,4a"] |
| 56 | +|=== |
| 57 | +|Name|ABNF |
| 58 | + |
| 59 | +|message |
| 60 | +|[literal] |
| 61 | +HTTP-message = request-line / status-line |
| 62 | + *( header-field CRLF ) |
| 63 | + CRLF |
| 64 | + [ message-body ] |
| 65 | + |
| 66 | +|request-line |
| 67 | +|[literal] |
| 68 | +request-line = method SP request-target SP HTTP-version CRLF |
| 69 | + |
| 70 | +|status-line |
| 71 | +|[literal] |
| 72 | +status-line = HTTP-version SP status-code SP reason-phrase CRLF |
| 73 | + |
| 74 | +|=== |
| 75 | + |
| 76 | + |
| 77 | +Most HTTP header field values are domain-specific or application-defined, while |
| 78 | +certain fields commonly recur. The library understands these fields and takes |
| 79 | +appropriate action to ensure RFC compliance: |
| 80 | + |
| 81 | +[cols="1a,4a"] |
| 82 | +|=== |
| 83 | +|Field|Description |
| 84 | + |
| 85 | +a| |
| 86 | +https://tools.ietf.org/html/rfc7230#section-6.1[*Connection*] + |
| 87 | +https://tools.ietf.org/html/rfc7230#appendix-A.1.2[*Proxy-Connection*] |
| 88 | + |
| 89 | +|This field lets the sender specify control options for the current connection. |
| 90 | +Typical values include close, keep-alive, and upgrade. |
| 91 | + |
| 92 | +|https://tools.ietf.org/html/rfc7230#section-3.3.2[*Content-Length*] |
| 93 | +|When present, this field tells the recipient the exact size of the message |
| 94 | +body, measured in bytes, that follows the header. |
| 95 | + |
| 96 | +|https://tools.ietf.org/html/rfc7230#section-3.3.1[*Transfer-Encoding*] |
| 97 | +|This optional field specifies the sequence of transfer codings that have been, |
| 98 | +or will be, applied to the content payload to produce the message body. |
| 99 | + |
| 100 | +The library supports the |
| 101 | +chunked, |
| 102 | +gzip, |
| 103 | +deflate, and |
| 104 | +brotli |
| 105 | +encoding schemes, |
| 106 | +in any valid combination. Encodings can be automatically applied or removed |
| 107 | +as needed by the caller. |
| 108 | + |
| 109 | +|https://tools.ietf.org/html/rfc7230#section-6.7[*Upgrade*] |
| 110 | +|The Upgrade header field provides a mechanism to transition from HTTP/1.1 to |
| 111 | +another protocol on the same connection. For example, it is the mechanism used |
| 112 | +by WebSocket's initial HTTP handshake to establish a WebSocket connection. |
| 113 | + |
| 114 | +|=== |
| 115 | + |
0 commit comments