What happened after you enter a URL into the web browser? How does a complete HTTP request/response process look like?

Step 1: Parsing the URL to a IP address

First of all, the url typed on the web browser will transfer into a IP address. How does the url map to the related IP address? The details are as follow.

alt-text

  • The browser firstly looks up IP address for related domain name in its browser cache. If the IP adress is found, it would return to the client. Otherwise, next

  • If the browser cache doesn’t contain the desired IP adress record, it would look up in the OS cache (hosts file).

  • If not found, it send a request to local DNS(Domain Name System) server.

  • If not found, a recursive search would happen. 1) Local DNS Server sends a request to Root DNS Server, and then return gTLD Server address (e.g. .com, .org, .cn etc). 2) Local DNS Server sends a request to gTLD Server returned previously and return the address of the Name Serverfor related domain name. 3) Local DNS Server sends a requst to Name Server and return IP address for the domain name. Finally, the Local DNS Server return IP address to client.

alt-text

Step 2: Sending the request to web server

After obtaining IP address, browser would form a HTTP request, and then send it to web server. Here is what a HTTP request look like.
We can obtain a mock HTTP request/response process by using cURL.

1
curl -v www.google.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
* Rebuilt URL to: www.google.com/
* Trying 24.226.16.25...
* Connected to www.google.com (24.226.16.25) port 80 (#0)
> GET / HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.49.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Referrer-Policy: no-referrer
< Location: http://www.google.ca/?gfe_rd=cr&ei=5o0SWaqWMsSC8QfHmbDAAw
< Content-Length: 258
< Date: Wed, 10 May 2017 03:49:58 GMT
<
<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.ca/?gfe_rd=cr&amp;ei=5o0SWaqWMsSC8QfHmbDAAw">here</A>.
</BODY></HTML>
* Connection #0 to host www.google.com left intact

Lines start with “>” are HTTP request and with “<” are HTTP response (explain in next step).
A HTTP request consists of three parts. They are:

  1. Request Line (line 1)
  2. Request Header (following a few lines)
  3. A blank line (means the ending of a HTTP request)

Request Line
format: HTTP-Method + Request-URI + HTTP-Version
common HTTP method: GET, POST, DELETE, PUT, PATCH
Differences between ‘GET’ and ‘POST’?

Step 3: The server response

A HTTP response consists of 4 parts. They are:

  1. Status Line (line 1)
  2. Response Header (following a few lines)
  3. A blank line (uses to seperate the response header and the HTML contents)
  4. HTTP contents

Status Line
format: HTTP-Version + Status-Code + Reason-Phrase
Response Code:

  • 1XX: provisional response
  • 2XX: sucess (e.g. 200)
  • 3XX: redirection
  • 4XX: client error (e.g. 404)
  • 5XX: server error

Steip 4: Browser redering

alt-text

For this step, the web browser would render HTTP contents obtained from HTTP response and may send request to web server for object embedded in HTML.