Like most network protocols, HTTP uses the client-server model: An HTTP client opens a connection and sends a request message to an HTTP server; the server then returns a response message, usually containing the resource that was requested. After delivering the response, the server closes the connection.
The format of the request and response messages are similar and will have following structure:
- An initial line CRLF
- Zero or more header lines CRLF
- A blank line ie. a CRLF
- An optional message body like file, query data or query output.
Initial lines and headers should end in CRLF. Though you should gracefully handle lines ending in just LF. More exactly, CR and LF here mean ASCII values 13 and 10.
Initial Line : Request
The initial line is different for the request than for the response. A request line has three parts, separated by spaces:
Here is an exampple of initial line for Request Message.
GET /path/to/file/index.html HTTP/1.0
GET is the most common HTTP method. Other methods could be POST, HEAD etc.
The path is the part of the URL after the host name. This path is also called the request Uniform Resource Identifier (URI). A URI is like a URL, but more general.
The HTTP version always takes the form "HTTP/x.x", uppercase.
Initial Line : Response
The initial response line, called the status line, also has three parts separated by spaces:
The version of HTTP being used.
A response status code that gives the result of the request.
An English reason phrase describing the status code.
Here is an exampple of initial line for Response Message.
HTTP/1.0 200 OK
HTTP/1.0 404 Not Found
Header lines provide information about the request or response, or about the object sent in the message body.
The header lines are in the usual text header format, which is: one line per header, of the form "Header-Name: value", ending with CRLF. It's the same format used for email and news postings, defined in RFC 822.
A header line should end in CRLF, but you should handle LF correctly.
The header name is not case-sensitive.
Any number of spaces or tabs may be between the ":" and the value.
Header lines beginning with space or tab are actually part of the previous header line, folded into multiple lines for easy reading.
Here is an exampple of ione header line
Last-Modified: Fri, 31 Dec 1999 23:59:59 GMT
The Message Body
An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
If an HTTP message includes a body, there are usually header lines in the message that describe the body. In particular: