HTTP

HTTP stands for Hypertext Transfer Protocol. It's an application layer protocol. Application layer is the topmost layer in both the TCP/IP model and the OSI model. HTTP is a stateless protocol which means the server does not keep any data (state) between two requests. It functions as a request–response protocol in the client–server computing model. Popular HTTP clients include Mozilla Firefox, Google Chrome and HTTP servers include Apache and Nginx.

HTTP Session
An HTTP session consists of three phases:
 * 1) An HTTP client initiates a request by establishing a Transmission Control Protocol (TCP) connection to a particular port on a server (typically port 80, occasionally port 8080).
 * 2) The client sends its request and waits for the answer.
 * 3) The server processes the request, sending back its response, consisting of status line such as , response header and response body.

HTTP Request
A request message consists of the following:


 * 1) A request line consisting of the HTTP method, resource path (without domain name) and the HTTP protocol version (eg..
 * 2) Request header fields which are key-value pairs separated by new lines (eg.  ). See full list of HTTP header fields on Wikipedia.
 * 3) An empty line as a separator between header and body.
 * 4) An optional message body. In case of GET requests it can be empty while not so for POST requests.

The request line and other header fields must each end with  (that is, a carriage return character followed by a line feed character - ). The empty line must consist of only  and no other whitespace.

Request Methods
converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy.

deletes the specified resource.

requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect (hence called a safe method).

requests for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

returns the HTTP methods that the server supports for the specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource.

applies partial modifications to a resource.

requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI. The data POSTed might be, for example a block of data that is the result of submitting a web form to add to a database.

requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified; if the URI does not point to an existing resource, then the server can create the resource with that URI.

echoes the received request so that a client can see what (if any) changes or additions have been made by intermediate servers.

All general-purpose HTTP servers are required to implement at least the GET and HEAD methods, and all other methods are considered optional by the specification.

An Example GET Request
GET / HTTP/1.1 Host: www.example.com Accept-Language: en

An Example POST Request
POST / HTTP/1.1 Host: www.example.com Content-Length: 19 Content-Type: application/x-www-form-urlencoded foo=hello&bar=world

HTTP Response
A response message consists of the following:
 * 1) A status line which includes the status code and reason message (eg, , which indicates that the client's request succeeded).
 * 2) Response header fields (eg.  ).
 * 3) An empty line.
 * 4) An optional message body usually consisting of an HTML page.

The status line and other header fields must all end with . The empty line must consist of only  and no other whitespace. This strict requirement for  is relaxed somewhat within message bodies for consistent use of other system linebreaks such as  or  alone.

Status codes
1XX Informational Eg. 101 Switching Protocols

2XX Successful Eg. 200 OK

3XX Redirection Eg. 301 Moved Permanently

4XX Client Error Eg. 404 Not Found

5XX Server Error Eg. 502 Bad Gateway

See full list of status codes on Wikipedia.

An Example Response
HTTP/1.1 200 OK Date: Mon, 23 May 2005 22:38:34 GMT Content-Type: text/html; charset=UTF-8 Content-Length: 155 Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) ETag: "3f80f-1b6-3e1cb03b" Accept-Ranges: bytes Connection: close An Example Page Hello World, this is a very simple HTML document.