Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1103383 of A typical HTTP session

  • Revision slug: Web/HTTP/Session
  • Revision title: A typical HTTP session
  • Revision id: 1103383
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment
Tags: 

Revision Content

{{HTTPSidebar}}

In client-server protocols, like HTTP, sessions consists of three phases:

  1. The client establishes a TCP connection (or the appropriate connection if the transport layer is not TCP).
  2. The client sends its request and then waits for the answer.
  3. The server processes the request and sends back its answer, containing a status code and the appropriate data.

Starting with HTTP/1.1, the connection is no longer closed after the third phase, as the client is allowed to issue another request at this point: the second and third phases can therefore be done several times.

Establishing a connection

In client-server protocols, the client establishes the connection. Opening a connection in HTTP means establishing a connection in the underlying transport layer, usually TCP.

With TCP, the default port for an HTTP server on a computer is port 80, though others are often used, like 8000 or 8080. The URL of a page to fetch contains both the domain name and the port number, though the latter can be omitted if it is 80. See Identifying resources on the Web for more details.

Note: The client-server model does not allow the server to send data to the client without an explicit request for it. To work around this problem, web developers use several techniques: pinging the server periodically via the {{domxref("XMLHTTPRequest")}} or {{domxref("Fetch")}} APIs, using the HTML WebSockets API, or similar protocols.

Sending a client request

Once the connection is established, the user-agent can send a request (a user-agent is typically a web browser, but can be anything else, a crawler, for example). A client request consists of text directives, separated by CRLF (carriage return, followed by line feed), divided in three blocks:

  1. The first line contains a request method followed by its parameters:
    • the path of the document, i.e., an absolute URL without the protocol and the domain name
    • the HTTP protocol version used
  2. The subsequent lines each represent a specific HTTP header, giving the server some information about what kind of data is appropriate (e.g., what language, what MIME types) or some data altering its behavior (e.g., not sending an answer if it is already cached). These HTTP headers form a block that ends with an empty line.
  3. The final block is the optional data block, which contains further data and is mainly used by the POST method.

Example requests

Fetching the root page of developer.mozilla.org, i.e. https://developer.mozilla.org/, and telling the server that the user-agent would prefer the page in French, if possible:

GET / HTTP/1.1
Host: developer.mozilla.org
Accept-Language: fr
 

Note the final empty line, separating the data block from the headers block. As there is no Content-Length: HTTP header, the data block is empty and the server can process the request as soon as it receives the empty line marking the end of the headers.

Sending the result of a form:

POST /contact_form.php HTTP/1.1
Host: developer.mozilla.org
Content-Length: 64
Content-Type: application/x-www-form-urlencoded

name=Joe%20User&request=Send%20me%20one%20of%20your%20catalogue

Request methods

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. Although they can also be nouns, these requests methods are sometimes referred as HTTP verbs. The most common requests are GET and POST:

  • The {{HTTPMethod("GET")}} method requests a representation of the specified resource. Requests using GET should only retrieve data.
  • The {{HTTPMethod("POST")}} method sends data to a server so that it changed its state. This is a method often used in HTML Forms.

Structure of a server response

After the connected agent has sent its request, the web server handles it, and finally sends a response back. Similarly to a client request, a server response is formed of text directives, separated by CRLF, though divided in three different blocks:

  1. The first line, the status line, consists of an acknowledgment of the HTTP version used followed by a status request (and its meaning in human-readable text).
  2. The subsequent lines each represent a specific HTTP header giving the client some information about the data sent (e.g., type, data size, compression algorithm used, hints about caching). Similarly to the block of HTTP headers for a client request, these HTTP headers form a block that ends with an empty line.
  3. The final block is the data block, which contains the data (if any).

Example responses

Successful reception of a web page:

HTTP/1.1 200 OK
Date: Sat, 09 Oct 2010 14:28:02 GMT
Server: Apache
Last-Modified: Tue, 01 Dec 2009 20:18:22 GMT
ETag: "51142bc1-7449-479b075b2891b"
Accept-Ranges: bytes
Content-Length: 29769
Content-Type: text/html

<!DOCTYPE html... (here comes the 29769 bytes of the requested web page)

Notification that the requested resource has been permanently moved

HTTP/1.1 301 Moved Permanently
Server: Apache/2.2.3 (Red Hat)
Content-Type: text/html; charset=iso-8859-1
Date: Sat, 09 Oct 2010 14:30:24 GMT
Location: https://developer.mozilla.org/ (this is the new link to the resource; it is expected that the user-agent will fetch it)
Keep-Alive: timeout=15, max=98
Accept-Ranges: bytes
Via: Moz-Cache-zlb05
Connection: Keep-Alive
X-Cache-Info: caching
X-Cache-Info: caching
Content-Length: 325 (the content contains a default page to display if the user-agent is not able to follow the link)

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://developer.mozilla.org/">here</a>.</p>
<hr>
<address>Apache/2.2.3 (Red Hat) Server at developer.mozilla.org Port 80</address>
</body></html>
 

Notification that the requested resource doesn't exist

HTTP/1.1 404 Not Found
Date: Sat, 09 Oct 2010 14:33:02 GMT
Server: Apache
Last-Modified: Tue, 01 May 2007 14:24:39 GMT
ETag: "499fd34e-29ec-42f695ca96761;48fe7523cfcc1"
Accept-Ranges: bytes
Content-Length: 10732
Content-Type: text/html

<!DOCTYPE html... (contains a site-customized page helping the user to find the missing resource)

Response status codes

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: informational responses, successful responses, redirects, client errors, and servers errors.

  • {{HTTPStatus(200)}}: OK. The request has succeeded.
  • {{HTTPStatus(301)}}: Moved Permanently. This response code means that URI of requested resource has been changed.
  • {{HTTPStatus(404)}}: Not Found. The server cannot find requested resource.

See also

Revision Source

<div>{{HTTPSidebar}}</div>

<p>In client-server protocols, like HTTP, sessions consists of three phases:</p>

<ol>
 <li>The client establishes a TCP connection (or the appropriate connection if the transport layer is not TCP).</li>
 <li>The client sends its request and then waits for the answer.</li>
 <li>The server processes the request and sends back its answer, containing a status code and the appropriate data.</li>
</ol>

<p>Starting with HTTP/1.1, the connection is no longer closed after the third phase, as the client is allowed to issue another request at this point: the second and third phases can therefore be done several times.</p>

<h2 id="Establishing_a_connection">Establishing a connection</h2>

<p>In client-server protocols, the client establishes the connection. Opening a connection in&nbsp;HTTP means establishing a connection in the underlying transport layer, usually TCP.</p>

<p>With TCP, the default port for an HTTP server on a computer is port 80, though others are often used, like 8000 or 8080. The URL of a page to fetch contains both the domain name and the port number, though the latter can be omitted if it is 80. See <a href="/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web">Identifying resources on the Web</a> for more details.</p>

<div class="note"><strong>Note:</strong> The client-server model does not allow the server to send data to the client without an explicit request for it. To work around this problem, web developers use several techniques: pinging the server periodically via the {{domxref("XMLHTTPRequest")}} or {{domxref("Fetch")}} APIs, using the HTML <a href="/en/WebSockets" title="en/WebSockets">WebSockets API</a>, or similar protocols.</div>

<h2 id="Sending_a_client_request">Sending a client request</h2>

<p>Once the connection is established, the user-agent can send a request (a user-agent is typically a web browser, but can be anything else, a crawler, for example). A client request consists of text directives, separated by CRLF (carriage return, followed by line feed), divided in three blocks:</p>

<ol>
 <li>The first line contains a request method followed by its parameters:
  <ul>
   <li>the path of the document, i.e., an absolute URL without the protocol and the domain name</li>
   <li>the HTTP protocol version used</li>
  </ul>
 </li>
 <li>The subsequent lines each represent a specific HTTP&nbsp;header, giving the server some information about what kind of data is appropriate (e.g., what language, what MIME types) or some data altering its behavior (e.g., not sending an answer if it is already cached). These HTTP headers form a block that ends with an empty line.</li>
 <li>The final block is the optional data block, which contains further data and is mainly used by the POST method.</li>
</ol>

<h3 id="Example_requests">Example requests</h3>

<p>Fetching the root page of developer.mozilla.org, i.e. <a class="linkification-ext external" href="/" title="Linkification: https://developer.mozilla.org/">https://developer.mozilla.org/</a>, and telling the server that the user-agent would prefer the page in French, if possible:</p>

<pre>
GET / HTTP/1.1
Host: developer.mozilla.org
Accept-Language: fr
&nbsp;
</pre>

<p>Note the final empty line, separating the data block from the headers block. As there is no <code>Content-Length:</code> HTTP&nbsp;header, the data block is empty and the server can process the request as soon as it receives the empty line marking the end of the headers.</p>

<p>Sending the result of a form:</p>

<pre>
POST /contact_form.php HTTP/1.1
Host: developer.mozilla.org
Content-Length: 64
Content-Type: application/x-www-form-urlencoded

name=Joe%20User&amp;request=Send%20me%20one%20of%20your%20catalogue
</pre>

<h3 id="Request_methods">Request methods</h3>

<p>HTTP defines a set of <a href="/en-US/docs/Web/HTTP/Methods">request methods</a> to indicate the desired action to be performed for a given resource. Although they can also be nouns, these requests methods are sometimes referred as HTTP verbs. The most common requests are <code>GET</code> and <code>POST</code>:</p>

<ul>
 <li>The {{HTTPMethod("GET")}} method requests a representation of the specified resource. Requests using <code>GET</code> should only retrieve data.</li>
 <li>The {{HTTPMethod("POST")}} method sends data to a server so that it changed its state. This is a method often used in <a href="/en-US/docs/Web/Guide/HTML/Forms">HTML Forms</a>.</li>
</ul>

<h2 id="Structure_of_a_server_response">Structure of a server response</h2>

<p>After the connected agent has sent its request, the web server handles it, and finally sends a response back. Similarly to a client request, a server response is formed of text directives, separated by CRLF, though divided in three different blocks:</p>

<ol>
 <li>The first line, the <em>status line</em>, consists of an acknowledgment of the HTTP&nbsp;version used followed by a status request (and its meaning in human-readable text).</li>
 <li>The subsequent lines each represent a specific HTTP&nbsp;header giving the client some information about the data sent (e.g., type, data size, compression algorithm used, hints about caching). Similarly to the block of HTTP&nbsp;headers for a client request, these HTTP headers form a block that ends with an empty line.</li>
 <li>The final block is the data block, which contains the data (if any).</li>
</ol>

<h3 id="Example_responses">Example responses</h3>

<p>Successful reception of a web page:</p>

<pre>
HTTP/1.1 200 OK
Date: Sat, 09 Oct 2010 14:28:02 GMT
Server: Apache
Last-Modified: Tue, 01 Dec 2009 20:18:22 GMT
ETag: "51142bc1-7449-479b075b2891b"
Accept-Ranges: bytes
Content-Length: 29769
Content-Type: text/html

&lt;!DOCTYPE html... <em><strong>(here comes the 29769 bytes of the requested web page)</strong></em>

</pre>

<p>Notification that the requested resource has been permanently moved</p>

<pre>
HTTP/1.1 301 Moved Permanently
Server: Apache/2.2.3 (Red Hat)
Content-Type: text/html; charset=iso-8859-1
Date: Sat, 09 Oct 2010 14:30:24 GMT
Location: <a class="linkification-ext" href="../../../../" title="Linkification: https://developer.mozilla.org/">https://developer.mozilla.org/</a> <strong><em>(this is the</em><em> new link to the resource; it is expected that the user-agent will fetch it)</em></strong>
Keep-Alive: timeout=15, max=98
Accept-Ranges: bytes
Via: Moz-Cache-zlb05
Connection: Keep-Alive
X-Cache-Info: caching
X-Cache-Info: caching
Content-Length: 325 <em>(<strong>the content contains a default page to display if the user-agent is not able to follow the link)</strong></em>

&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
&lt;html&gt;&lt;head&gt;
&lt;title&gt;301 Moved Permanently&lt;/title&gt;
&lt;/head&gt;&lt;body&gt;
&lt;h1&gt;Moved Permanently&lt;/h1&gt;
&lt;p&gt;The document has moved &lt;a href="<a class="linkification-ext" href="../../../../" title="Linkification: https://developer.mozilla.org/">https://developer.mozilla.org/</a>"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;address&gt;Apache/2.2.3 (Red Hat) Server at developer.mozilla.org Port 80&lt;/address&gt;
&lt;/body&gt;&lt;/html&gt;
 
</pre>

<p>Notification that the requested resource doesn't exist</p>

<pre>
HTTP/1.1 404 Not Found
Date: Sat, 09 Oct 2010 14:33:02 GMT
Server: Apache
Last-Modified: Tue, 01 May 2007 14:24:39 GMT
ETag: "499fd34e-29ec-42f695ca96761;48fe7523cfcc1"
Accept-Ranges: bytes
Content-Length: 10732
Content-Type: text/html

&lt;!DOCTYPE html... <strong><em>(contains a site-customized page helping the user to find the missing resource)</em></strong>

</pre>

<h3 id="Response_status_codes">Response status codes</h3>

<p><a href="/en-US/docs/Web/HTTP/Status">HTTP response status codes</a> indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: informational responses, successful responses, redirects, client errors, and servers errors.</p>

<ul>
 <li>{{HTTPStatus(200)}}: OK. The request has succeeded.</li>
 <li>{{HTTPStatus(301)}}: Moved Permanently. This response code means that URI of requested resource has been changed.</li>
 <li>{{HTTPStatus(404)}}: Not Found. The server cannot find requested resource.</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web">Identifying resources on the Web</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Headers">HTTP headers</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Methods">HTTP request methods</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Status">HTTP response status codes </a></li>
</ul>
Revert to this revision