What is HTTP?
Published: 2022-07-30 20:49:00

We all use HTTP when we surf on the web.
And without HTTP it's generally not possible to search the web.

Even if we try not to write "http" before our links, browser sets it to http by default.

Let's see what is HTTP?

## What is HTTP?
We all know it is a transfer protocol used to send data over the web.

**According to web** - HTTP is an application level protocol used for fetching resources such as HTML document.

Application level means that it works on the top layer of the OSI model. 

**We won't go deep in the OSI model but for an overview:**

An OSI model is a conceptual model which provides a standard of communication between computers.

And the application layer is where applications can access network services and can interacts with the users. 

HTTP is the foundation of data exchange on web and is also known as a **client-server protocol** as it allows the browser to communicate with the server.

(here the client is the browser and hence the name client-server protocol).

Apart from being a simple data transfer protocol, it can be used as a generic protocol for user agents (such as browsers), proxies/gateways including those supported by protocols like SMTP and FTP and hence it can access resources from different apps.

## History of HTTP

- The first version of HTTP formed was `HTTP/0.9` that was used to transfer raw data.
- `HTTP/1.0` was introduced later that allowed us to transfer MIME-like messages such as media files.
- `HTTP/1.0` was doing great but there was a problem, it was not reliable, so to overcome that `HTTP/1.1` was introduced that works on a **TCP** connection to ensure reliable implementations.

BUT HTTP doesn't just depends on TCP. It can even send data over a TLS/SSL protocol or any other reliable transport protocol.

**Just to understand** - A Transport Layer Security (TLS) also known as Secure Socket Layer (SSL) is a protocol to communicate securely ensuring privacy through cryptographic protocols.

## HTTP versioning
HTTP uses structure as &lt;major&gt;.&lt;minor&gt; to indicate versions.

&lt;minor&gt; is incremented when a version change do not affect the communication behavior and is an addition of message components.

&lt;major&gt; is incremented when format of a message within the protocol is changed.

## Working of HTTP
So now we know what is http? And it's history. Let's see how it works.

As we know it is a client-server protocol, it's follows a request-response method.

### Request
A client sends a message to server (request) in the form of a request method, URI, and protocol version followed by a MIME-like message containing request modifiers, client information, and content body.

- Request method: GET, POST, PUT, DELETE
- Protocol version: such as `HTTP/1.0` `HTTP/1.1`
- Client information: your browser identity
- Content body: what you want from server

The request sender can be a proxy or browser know as a user agent or a robot crawling the web to populate and maintain the search engine index.

#### Proxy
A Proxy is a computer or program used when navigating through the network. It lies between the client and the server (these can be modems and routers as well).

It can access the web and can intercepts the request and respond back and can manipulate the request received before sending it further.

Most of the proxy machines lies in the last four layers of the OSI model.

### Response
When the request is received by the server, it responds back with a status code, status message, MIME-like message containing server information, metainformation, and entity body content

- Status codes: A code to if its a success or fail
- Status message: human readable form of the success/fail message
- Body content: expected result

Although most communications are initiated by the clients to the origin server but nowadays servers can initiate a communication too.


## Components of HTTP-based Systems
### Client (The User Agent)
A user agent is anything that works on behalf of the user, generally this role is performed by the browsers. Browsers sends the request to the server and receives an HTML page and then requests for other files as specified in the HTML page.

### Proxies
As we have covered proxies above, it's the same old same old.

Proxies can manipulate a request coming from the client and can translate it to different HTTP versions and to the server understandable form.

### Servers
A server is a computer that serves the documents to the client.
It is called as a computer but actually it is collection of computers sharing the load or communicating with other computers.

## HTTP Flow:
- Open a TCP connection: A connection is established between client and server. Client sends a message to the server and server responds back with acknowledgement, this is how it is made sure that the connection is reliable and the packets are not being sent to nothing.

- Send HTTP message: A request is formed and sent to the server. 
Example: `GET / HTTP/1.1` 

- Server responds back: Server sends a response back to the cliend
Example: `HTTP/1.1 200 OK`

- Connection is closed or reused.

## Connection and HTTP
A connection can be established between two devices in any form.
It's not necessary whether the connection is secured or not.

A connection is formed at the transport layer with or without reliability.

But for an HTTP connection, it must be reliable, protocols like TCP are used here.

For a simple connection between a client and a server it's not much of a story.

But when proxies, gateways, and tunnels come in, it gets complicated and request is rewritten a few time.

A proxy is a forwarding agent rewriting all or part of message and send it to the server identified by the URI.

A gateway is a receiving agent, lies above server layer and can translate request to server protocol.

A Tunnel acts as a relay point between two connections without changing the message, used to pass messages through firewall generally

Request ------> UA --------> P --------> V ------> G --------> V --------> S

UA - user-agent
P - proxy
V - single connection
G - gateway
S - server

Proxy and gateways must be careful while forwarding the messages in different version of protocol, as they must not send a request with higher version of protocol than the actual version.
If that happens then they must downgrade it or respond with error or use a tunnel.

That's all about HTTP. Thank you for reading.
Feedbacks are appreciated.