Sunday, October 16, 2011

HTTP responses

When the web server receives the request it processes it and sends back HTTP response. The HTTP response doesn't differ very much from the request. It contains the data requested from the browser and also additional info about the message itself. HTTP response is structured like the response with 4 parts:

1. Status line - This tells the status of the request, whether was bad or good or an error happened.
2. List of HTTP headers - Headers give additional info about the message, like the type and the length of the message.
3. Empty line - This is an empty line between the status line and the headers.
4. Message body(optional) - This part contains the returned web page or maybe other web resource like image or stream.

Here is an example taken from http://web-sniffer.net/

Status: HTTP/1.1 302 Found  
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=40bebf53f84a37a4:FF=0:TM=1318795218:LM=1318795218:S=Uj37tfkySeEueQGz; expires=Tue, 15-Oct-2013 20:00:18 GMT; path=/; domain=.google.com
Date: Sun, 16 Oct 2011 20:00:18 GMT
Server: gws
Content-Length: 218
X-XSS-Protection: 1; mode=block
Connection: close

<HTML> 
 <HEAD> 
   <meta http-equiv="content-type" content="text/html;charset=utf-8">
   <TITLE>302 Moved</TITLE> 
 </HEAD>
 <BODY>
   <H1>302 Moved</H1>
      The document has moved
      <A HREF="http://www.google.de/">here</A>.
 </BODY> 
</HTML>
 
So the status line contains the given status of the status of the message. There are several most 
important statuses that can be acquired by the browser:

200 - OK - Browser request was successful, follows the returned content.
301 - Moved Permanently - Requested resource is at different location, new URL will be returned in the 
Location header. Browser should use new URL 
302 - Found - requested resource is temporarily at new URL, new URL will be returned in the Location 
header. Browser should still use the same URL. 
400 - Bad Request - Request sent by the browser was invalid. (example: wrong syntax)
403 - Forbidden - Browser accesses web resource that has no permission to. For example browser
 tries to access file that is password protected.
404 - Not Found - Requested resource cannot be found on the server.
500 - Internal Server Error - Problem occurred while processing the request

Now about the response headers. There are several most important response headers that a web 
server can return:

Date - Date and time of the response 
Content-Length - Length of the returned contents in bytes.
Content-Type - MIME type of the contents that follow.
Location - Alternative URL of the requested content, usually used with 301 and 302 status codes.
Server - Info about the web server, like type, name and version
Set-Cookie - Request that an cookie be set on the requesting browser.

One web page can have several other resourses connected to it like, javascript files, css files, 
php file etc. So when one page is requested, usually several HTTP requests/responses will be 
done in order to display the whole web page. 

No comments:

Post a Comment