What is HTTP?

  • HTTP stands for Hyper Text Transfer Protocol
  • It is TCP/IP based communication protocol which is used to deliver resources on the World Wide Web. Resources could be HTML files, image files, query results etc.
  • Three important characteristics of HTTP are:
  • HTTP is connectionless
    • After a request is made, the client disconnects from the server and waits for a response. The server must re-establish the connection after it processes the request.
  • HTTP is stateless
    • This is a direct result of HTTP's being connectionless
    • The server and client are aware of each other only during a request. Afterwards, they forget each the other.
    • In other words it means that each time a client retrieves a Web page, the client opens a separate connection to the Web server and the server automatically does not keep any record of previous client request.
  • HTTP is media independent
    • Any type of data can be sent by HTTP as long as both the client and server know how to handle the data content



HTTP Message Structure

  • HTTP uses the client-server OR request-response mechanism
  • Client opens a connection and sends a request message to an HTTP server; the server then returns a response message and closes the connection after delivering response.
  • The format of the request and response messages are similar and will have following structure:

  • An initial line  
  • Zero or more header lines
  • A blank line
  • An optional message body like file, query data or query output
Initial Line: (for Request)
  •  The initial line for REQUEST is different than for RESPONSE. A request line has three parts, separated by spaces:
  • An HTTP Method Name
  • The local path of the requested resource
  • The version of HTTP being used
  • Example of initial line for the HTTP request is:
GET /path/to/file/index.html HTTP/1.0

Initial Line: (for Response)

  • The initial line for response, called the status line, also has three parts separated by spaces:
  • The version of HTTP being used.
  • A response status code
  •  An English reason phrase describing the status code.
  • Example of initial line for the HTTP response is:
HTTP/1.0 200 OK
OR
HTTP/1.0 404 Not Found
Header Lines
  • Header lines provide information about the request or response, or about the object sent in the message body.
  •  Example of header lines are:
User-agent: Mozilla/3.0Gold
OR
Last-Modified: Fri, 31 Dec 1999 23:59:59 GMT

Message Body
  • An HTTP message may have a body of data sent after the header lines
  • This contains information requested.

HTTP Message Example
  • HTTP request message looks like:
GET /path/file.html HTTP/1.0
From: someuser@tutorialspoint.com
User-Agent: HTTPTool/1.0
[blank line here]
·         HTTP response message looks like:
HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354

<html>
<body>
<h1>Happy New Millennium!</h1>

(more file contents)
  .
  .
  .
</body>
</html>

HTTP Methods
       Some common methods of HTTP are

·         GET Method
o   GET means that the form data is to be encoded (by a browser) into a URL and this is the default HTTP method
o   GET is less secure compared to POST, because data sent is part of the URL and Easier to hack
o   URL length is restricted in GET method
o   Can be bookmarked and cached
o   GET requests are re-executed, on back button or re-submit behavior
o   GET requests are idempotent (unchanged). It can be executed more than once without any side effects.
o   Example:
http://www.test.com/hello?key1=value1&key2=value2

·         POST Method
o   POST means that the form data is to appear within the message body of the HTTP request
o   POST is secure as compared to GET and difficult to hack
o   No restriction on length
o   Cannot be bookmarked and cached
o   The browser usually alerts the user that data will need to be re-submitted, on back button or re-submit behavior
o   POST requests are non-idempotent. So you have to be very careful while using POST functionality.

·         HEAD Method
o   A HEAD request is just like a GET request, except it asks the server to return the response headers only, and not the actual resource (i.e. no message body). 
o    This is useful to check characteristics of a resource without actually downloading it, thus saving bandwidth.
o   Use HEAD when you don't actually need a file's contents.
 

No comments:

Post a Comment