Last modified on 2007-12-12 15:27:09
Introduction
The Hypertext Transfer Protocol (HTTP) protocol is a protocol that works over an IP connection between a server and a client. The client sends a request to the server and the server will return a response. The protocol is stateless meaning that every request is independent of any other request and no state information is preserved. So, if persistent state is required the server and the client themselves will have to devise a scheme on top of HTTP. This is often implemented by using a session cookie.
Text based protocol
HTTP is a text based protocol. This means that HTTP commands are formulated in human readable text that follow a certain syntax. The syntax for HTTP is straightforward. Text lines are separated by CRLFs. A request and a response message both follow the same syntax. Every message consists of two parts: the HTTP header and the message body.
Message header
The message header is formed by a number of text lines, closed by a double CRLF (a blank line). Every request starts with a single line, the request line, indicating which version of HTTP protocol is being used, which HTTP method should be performed and which URI is requested.
GET http://www.chartasoftware.com/ HTTP/1.1
Every response starts with a single line, the status line, which indicates the HTTP version, a response code together with a reason phrase.
HTTP/1.1 200 OK
The current most prevalent version of HTTP is version 1.1. HTTP supports the following methods:
- HEAD
- GET
- POST
- PUT
- DELETE
- TRACE
- OPTIONS
- CONNECT
After the request line a number of header fields can be supplied. Each header field is of the following form:
Field-name: Field-Value CRLF
Header fields are used to supply extra information on the specific request or response. For instance, the Content-Length field indicates the length in bytes of the message body.
An example of an HTTP request:
GET http://wwww.chartasoftware.com/ HTTP/1.1
Accept: text/plain, text/html
If-Modified-Since: Wed, 12 Dec 2007 14:04:01 GMT
Message body
The message body follows the message header. The message body is optional. Both a request message and a response message may include a body. When a message body is supplied the Content-Length header field should specify the length of the body. This way the recipient of the message knows how much data it expects to receive. The message body can be encoded in various ways. The particular encoding of the attached message body should be specified in the Content-Type header field. For instance, the following might be a response to a CSS file request request:
HTTP/1.1 200 OK
Content-Type: text/css
Content-Length: 23
Location: /Contents.css
Last-Modified: Tue, 11 Dec 2007 12:34:05 GMT
h1 {
color: blue;
}
Applications
HTTP can be used for a number of applications. The most of obvious application is serving a web site by providing HTML content. Examples of applications are:
- HTML based web sites, servers sends HTML and a browser renders HTML
- XML based web services, both server and client send XML messages
- Custom service to circumvent limitations imposed by firewalls and proxies