HTTP Transaction Model:
The HTTP protocol is transaction-driven. This means that each request will lead to one and only one response. Traditionally, a TCP connection is established from the client to the server, a request is sent by the client on the connection, the server responds and the connection is closed. A new request will involve a new connection :
[CON1] [REQ1] ... [RESP1] [CLO1] [CON2] [REQ2] ... [RESP2] [CLO2] ...
In this mode, called the "HTTP close" mode, there are as many connection establishments as there are HTTP transactions. Since the connection is closed by the server after the response, the client does not need to know the content length.
Due to the transactional nature of the protocol, it was possible to improve it to avoid closing a connection between two subsequent transactions. In this mode however, it is mandatory that the server indicates the content length for each response so that the client does not wait indefinitely. For this, a special header is used: "Content-length". This mode is called the "keep-alive" mode :
[CON] [REQ1] ... [RESP1] [REQ2] ... [RESP2] [CLO] ...
Its advantages are a reduced latency between transactions, and less processing power required on the server side. It is generally better than the close mode, but not always because the clients often limit their concurrent connections to a smaller value.
A last improvement in the communications is the pipelining mode. It still uses keep-alive, but the client does not wait for the first response to send the second request. This is useful for fetching large number of images composing a page :
[CON] [REQ1] [REQ2] ... [RESP1] [RESP2] [CLO] ...
This can obviously have a tremendous benefit on performance because the network latency is eliminated between subsequent requests. Many HTTP agents do not correctly support pipelining since there is no way to associate a response with the corresponding request in HTTP. For this reason, it is mandatory for the server to reply in the exact same order as the requests were received.
First, let's consider this HTTP request :
|1||GET /serv/login.php?lang=en&profile=2 HTTP/1.1|
|3||User-agent: my small browser|
|4||Accept: image/jpeg, image/gif|
The Request line
Line 1 is the "request line". It is always composed of 3 fields :
All of them are delimited by what the standard calls LWS (linear white spaces), which are commonly spaces, but can also be tabs or line feeds/carriage returns followed by spaces/tabs. The method itself cannot contain any colon (':') and is limited to alphabetic letters.The URI itself can have several forms :
/serv/login.php?lang=en&profile=2) It is a complete URL without the host part. This is generally what is received by servers, reverse proxies and transparent proxies.
http://192.168.0.12:8080/serv/login.php?lang=en&profile=2) It is composed of a "scheme" (the protocol name followed by '://'), a host name or address, optionally a colon (':') followed by a port number, then a relative URI beginning at the first slash ('/') after the address part. This is generally what proxies receive, but a server supporting HTTP/1.1 must accept this form too.
('*')This form is only accepted in association with the OPTIONS method and is not relayable. It is used to inquiry a next hop's capabilities.
192.168.0.12:80) This is used with the CONNECT method, which is used to establish TCP tunnels through HTTP proxies, generally for HTTPS, but sometimes for other protocols too.
In a relative URI, two sub-parts are identified. The part before the question mark is called the "path". It is typically the relative path to static objects on the server. The part after the question mark is called the "query string". It is mostly used with GET requests sent to dynamic scripts and is very specific to the language, framework or application in use.
The request headers
The headers start at the second line and are composed of a name at the
beginning of the line, immediately followed by a colon
an LWS is added after the colon but that's not required. Then come the values.
Multiple identical headers may be folded into one single line, delimiting the
values with commas, provided that their order is respected. This is commonly
encountered in the "Cookie:" field. A header may span over multiple lines if
the subsequent lines begin with an LWS. In the example in 1.2, lines 4 and 5
define a total of 3 values for the "Accept:" header.
Contrary to a common mis-conception, header names are not case-sensitive, and their values are not either if they refer to other header names (such as the "Connection:" header).
The end of the headers is indicated by the first empty line. People often say that it's a double line feed, which is not exact, even if a double line feed is one valid form of empty line.
An HTTP response looks very much like an HTTP request. Both are called HTTP messages. Let's consider this HTTP response :
|1||HTTP/1.1 200 OK|
As a special case, HTTP supports so called "Informational responses" as status codes 1xx. These messages are special in that they don't convey any part of the response, they're just used as sort of a signaling message to ask a client to continue to post its request for instance.
In the case of a status 100 response the requested information will be carried by the next non-100 response message following the informational one. This implies that multiple responses may be sent to a single request, and that this only works when keep-alive is enabled (1xx messages are HTTP/1.1 only).
The Response line
Line 1 is the "response line". It is always composed of 3 fields :
|Name||Value(can be different at times)|
|1xx||informational message to be skipped (eg: 100, 101)|
|2xx||OK, content is following (eg: 200, 206)|
|3xx||OK, no content following (eg: 302, 304)|
|4xx||error caused by the client (eg: 401, 403, 404)|
|5xx||error caused by the server (eg: 500, 502, 503)|
|Code||When / reason|
|200||access to stats page, and when replying to monitoring requests|
|301||when performing a redirection, depending on the configured code|
|302||when performing a redirection, depending on the configured code|
|303||when performing a redirection, depending on the configured code|
|307||when performing a redirection, depending on the configured code|
|308||when performing a redirection, depending on the configured code|
|400||for an invalid or too large request|
|401||when an authentication is required to perform the action (when accessing the stats page)|
|403||when a request is forbidden by a "block" ACL or "reqdeny" filter|
|408||when the request timeout strikes before the request is complete|
|500||when haproxy encounters an unrecoverable internal error, such as a memory allocation failure, which should never happen|
|502||when the server returns an empty, invalid or incomplete response, or when an "rspdeny" filter blocks the response.|
|503||when no server was available to handle the request, or in response to monitoring requests which match the "monitor fail" condition|
|504||when the response timeout strikes before the server responds|
The response headers
Response headers work exactly like request headers.