What you should know about HTTP


Everyone seems to know that the web runs on HTTP, but what is HTTP all about?

HTTP stands for HyperText Transfer Protocol; an application-layer protocol that the web runs on. Implemented as a client-server model; HTTP defines how clients should make requests to servers and how servers should respond. It is a stateless protocol because servers do not maintain information about clients; runs on TCP and is probably the most widely known of the application-layer protocols.

Sample HTTP Client request

GET /index.html HTTP/1.1

Host: openfirstsolutions.com
Connection: close
User-agent: Mozilla/4.0
Accept-Language: en

The first line is the request line and the remaining lines are header lines.

The request line has three fields; the method – GET (some other methods include POST, PUT, DELETE and HEAD), the URL specifying the path to the desired file and the HTTP version being used by the client.

In the example, I made a GET request for the object at /index.html on the server using the HTTP version 1.1.

Headers

Host: openfirstsolutions.com

This identifies the host on which the object resides; although this might seem redundant, it is quite useful for caching.

Connection: close

This instructs the server to close the connection after sending back a response.

User-agent: Mozilla/4.0

This identifies the type of browser making the request.

Accept-Language: fr

Identifies the language preference of the client; do you now know why your browser always returns search results in Arabic or French. :)

Sample HTTP Server Response

HTTP/1.1 200 OK

Date: Sat, 11 Feb 2012 14:46:43 GMT
Server: Apache/2.2.16 (Ubuntu)
Last-Modified: Sun, 11 Sep 2011 18:40:40 GMT
ETag: “42750-b1-4acaebf4ecb35”
Content-Length: 177
Connection: close
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>

The web server software is running but no content has been added, yet

</body></html>

The sample response message has three sections: a status message, 7 header lines and then the entity body.

The status line states the HTTP version, the status code and the status message. Generally, HTTP response codes beginning with 2 indicate success, 3 indicate redirection (clients should automatically handle this), while those with 4 and 5 indicate errors.

The entity body contains the requested object.

Response Headers

Date: Sat, 11 Feb 2012 14:46:43 GMT

Indicates the time and date the server created the response.

Server: Apache/2.2.16 (Ubuntu)

Shows the server name and version (Apache/2.2.16) and the underlying host platform(Ubuntu).

Last-modified: Sun, 11 Sep 2011 18:40:40 GMT

The date and time the object was last modified; this is particularly useful to cache servers.

Content-Type:

Type of response; here it is an html file, it could also be application/json or application/xml.

E-Tag: “42750-b1-4acaebf4ecb35”

Entity Tag; This is used in caching; if the client wants to retrieve the same file again from the server, it uses the E-Tag as a check to verify if the file has changed on the server.

Content-length: 177

The number of bytes being sent in the response.

Connection: close

The TCP connection between the client and user is closed immediately after the transfer.

Enough talk, lets send an HTTP request to a server!

telnet url_of_some_web_server 80

Type the following exactly; you can add more headers from the Client request section above if you want to so wish:

GET /path-to-some-object-on-your-server  HTTP/1.1

Host:  url_of_some_web_server

Press Enter twice.

Do you see a response?

Do some more exploration and have fun with HTTP like I did.

4 thoughts on “What you should know about HTTP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.