Sunday, October 16, 2011

HTTP requests

HTTP stands for Hyper Text Transfer Protocol. It is a part of the TCP/IP network stack i.e part of set of several networking protocols like, TCP, IP, SMTP, FTP, ICMP, IGMP and others. It used as a main protocol for exchange of hypermedia contents like web pages, video, audio, images and also file sharing. So every computer system that sends request with HTTP to the server acts like a HTTP client and the system that returns response is called HTTP server. Usually the browser (Firefox, IE, Crome) is the HTTP client and as a server it is Apache HTTP server (also Glassfish for Java, IIS for .NET and php).

Every HTTP request consists of 4 parts:

1. Request line - this part of the message tells the web server which resource (URL) the browser has requested, i.e which web page or video/audio should the browser display.
2. List of HTTP headers - HTTP headers are additional info for the resource and how the browser should display the resource. In headers there is an info for cookies, character set of the page or to which page to redirect the user.
3. Empty line - An empty line between the request line and the headers.
4. Message body (optional) - This part of the request message may contain additional data for example data from given form sent via the POST method.

Note: Every line in the HTTP request message should end with carriage return character followed by line feed character, i.e must end with "enter" which ends the line and starts new line.

Here is an example of a given HTTP request message taken from http://web-sniffer.net/  . You can try it yourself

Connect to xxx.xxx.xxx.xxx on port 80 ... ok


GET / HTTP/1.1[CRLF]
Host: www.google.com[CRLF]
Connection: close[CRLF]
User-Agent: Web-sniffer/1.0.37 (+http://web-sniffer.net/)[CRLF]
Accept-Encoding: gzip[CRLF]
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
Cache-Control: no-cache[CRLF]
Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
Referer: http://web-sniffer.net/[CRLF]
[CRLF]
 
 The request line consists of three parts: 
1. Request method - In this case it is GET, but it can be POST, HEAD, PUT, DELETE, TRACE, 
CONNECT etc. Visit wikipedia page for more info at HTTP.
2. The url to the resource - in this case it is forward slash("/"). We don't know the index page for 
the www.google.com so the server resolves on its own. Usually it would be something like /index.php.
3. The version of the HTTP protocol - In this case it is 1.1 which is default for most modern browsers.  
 
Follows a list of the most common HTTP headers that can be sent via HTTP request

-------------------------------------------------------------------------------------
Accept - List of MIME types that a browser will accept with the returned content.
Example:  Accept: text/html, application.xml, ...
-------------------------------------------------------------------------------------
Accept-Charset - a list of charsets that the browser will accept with the returned
content. Example: Accept-Charset: ISO-8859-1, utf-8
-------------------------------------------------------------------------------------
Accept-Encoding - a list of compression methods that the browser will accept from
the response. Example: Accept-Encoding: gzip,deflate
-------------------------------------------------------------------------------------
Accept-Language - a list of languages that the browser will accept from the recieved
content. Example: Accept-Language: en-gb, pt-br
-------------------------------------------------------------------------------------
Cookie - HTTP cookie sent by the sending server. Example: Cookie: name = John, 
surname: Doe.
--------------------------------------------------------------------------------------
Host - The Host header is the only mandatory header. It is mandatory because most
modern web servers can support several websites on the same machine, Host header
is needed to resolve from which web site the web server should send response to the
browser. Example: Host: www.google.com
-------------------------------------------------------------------------------------
Referer - Referer is the referers URL. Web servers constantly log from which web
page, given new page has been visited from. So the referers url is the url of the page
that the new page has been accessed from. Example: Referer: www.web-sniffer.net
------------------------------------------------------------------------------------
User-Agent - It holds info about the browser such as the type and current version.
Example: User-Agent: Mozilla/5.0 ...Gecko/xxxxxxx  Firefox/3.0.5 
------------------------------------------------------------------------------------

So this was mainly for the HTTP requests. In the next post I will explain the HTTP responses the same
way as requests.

No comments:

Post a Comment