-
Notifications
You must be signed in to change notification settings - Fork 0
HTTP Messages
These notes cover HTTP messages and their structure. This will be useful for understanding how to parse and compose these messages. While we already re-use the existing HTTP parser from the Node.js folks, it can't hurt to understand it better!
Messages form the basic unit of communication between client and server. There are two types of messages: requests and responses. Requests are issued from the client to the server, while responses are issued from server to the requesting client. Messages are composed of a starting line, zero or more headers, an empty line, and optionally a message-body.
In the interest of robustness, servers SHOULD ignore any empty line(s) received where a Request-Line is expected. In other words, if the server is reading the protocol stream at the beginning of a message and receives a CRLF first, it should ignore the CRLF.
... an HTTP/1.1 client MUST NOT preface or follow a request with an extra CRLF.
Both request and response messages begin with a start line which is terminated by a CRLF.
The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.
Method <SP> Request-URI <SP> HTTP-Version <CRLF>
The first line of a Response message is the Status-Line, consisting of the protocol version followed by a numeric status code and its associated textual phrase, with each element separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.
HTTP-Version <SP> Status-Code <SP> Reason-Phrase <CRLF>
A message may optionally include a block of headers following its start line. The last header line is followed by an empty line (CRLF). If no headers are provided the empty line must still be included.
There are four categories of headers:
- General (RFC 2616 Section 4.5)
- Request (RFC 2616 Section 5.3)
- Response (RFC 2616 Section 6.2)
- Entity (RFC 2616 Section 7.1)
Each header field consists of a name followed by a colon (":") and the field value.
Field-Name ":" Field-Value
Field names are case-insensitive.