Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

HTTP Messages

joshthecoder edited this page May 25, 2011 · 5 revisions

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

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.

RFC 2616 Section 4.1

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.

Start line

Both request and response messages begin with a start line which is terminated by a CRLF.

RFC 2616 Section 5.1

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>

RFC 2616 Section 6.1

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>

Headers

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:

Each header field consists of a name followed by a colon (":") and the field value.

Field-Name ":" Field-Value

Field names are case-insensitive.

Clone this wiki locally