Skip to content
Sanne Albreghs edited this page Aug 26, 2022 · 1 revision

HTTP

HTTP overview

Hypertext transfer protocol (HTTP) is a protocol for fetching resources, such as HTML documents. HTTP is a client-server protocol, which communicates in a request-response messaging pattern. The messages send by the client, usually a web browser, are called requests and the messages sent by the server as an answer are called responses.

The Client

The browser is always the one initiating the request. To display a web page, the browser sends a request to fetch the HTML document that represents this page.

The Webserver

On the opposite side of the communication channel is the server. The server serves the document as requested by the client.

HTTP flow

When a client wants to communicatie with a server, the following steps are performed:

Open a TCP connection

The TCP connection is used to send a requst and receive an answer.

Send a HTTP request

A HTTP request is a message sent by the client to initiate an action from the server.

GET / HTTP/1.1
Host: intra.42.fr
Accept-Language: nl

Read the response sent by the webserver

The response provides the client with the resources it requested, this can be a html document or an error page if the request could not be performed.

HTTP/1.1 200 OK
Date: Sat, 2 Feb 2022 00:00:01 GMT
Server: Nginx
Content-Length: 29384
Content-Type: text/html

<!DOCTYPE html.... (here follow the 29384 bytes specified in the Content length)

Close or reuse the TCP connection for further requests.

When the TCP pipelining is activated, serveral requests can be went without waiting for reponses to arrive first.

HTTP messages

HTTP messages are how data is exchanges between a server and a client, these are human-readable. There are two types or HTTP messages, requests and responses, each with their own format.

Requests

A request consists of the following elements: * A HTTP method, which defines the operation the clients want to perform. * The path of the resource to fetch, which is the URL of the resource stripped away from elements that are obvious from the context. * The version of HTTP protocol. * Optional headers with additional information. * A body which contain the resource sent, this is for methods like POST.

Responses

Responses consist of the following elements: * The version of HTTP. * A status code, indicating if the request was successful or not and why. * A status message which is a short description of the status code. * HTTP headers, like for the requests. * A body containing the fetched resource (HTML page for example).