The Basics: Everything About Hypertext Transfer Protocol
Tags: http, webdev, networking, computerscience
Published: 2023-12-23 04:40:23

> A **protocol** is a system of rules that define how data is exchanged within or between computers.

**HTTP**, designed in early 1900s as the fundamental of _world wide web_. As the name suggests, it initially built to transfer hypertext links to load webpages, but due to its extensibility, we use it to transfer images, videos or to post content to servers and so on. 

It is an **application layer** protocol that is sent over **TCP** [ _mostly sent over TLS/SSL-encrypted TCP connection to protect privacy and integrity_ ]. It uses **port 80** and carry the data in the form of _MIME-like format_.

HTTP is **stateless** but **not session less**. Every successful request-response pair is unique and there is no link in between them. However with HTTP cookies, session can be created to share same context or same state.



## HTTP Transactions

### 1. Opening Connection:
Before communicating over the network, a computer (which is mainly client) need to open a TCP connection. The client may use any existing connection or open several TCP connections to the server.

### 2. Send HTTP Request Message:
There are two types of HTTP messages, _request message_ is one of those.

In early version of HTTP (HTTP/1.1 or earlier), request message was human readable. In HTTP/2 and HTTP/3, these messages embedded into binary structure but underlying principle remains same.

A typical HTTP request contains:

- The **version** of the HTTP protocol.

- A HTTP **method** _( GET, POST, PUT, DELETE, HEAD, CONNECT, OPTIONS, TRACE, PATCH )_, to indicate the desired action to be performed  from the queried server.

- An **URL**, path of the resource to fetch.

- Optional HTTP **request headers**. These headers are useful to supply authentication credentials, to control caching, or to get information about the user agent or referrer, etc.

- Optional **body** for some methods like _POST_ or _PUT_.


![http request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezsx75pi8w175b8nli8t.jpg)

### 3. Receive HTTP Response Message:
This is another type of HTTP message, which client receive from a server in answer to the request.

A typical HTTP response contains:

- The **version** of the HTTP protocol.
- A HTTP **status code**. These are 3-digits code _( in between 100 - 599 )_ that tells the state of the response.
Status codes are grouped in 5 blocks:
  1. **1xx** : Informational
  2. **2xx** : Success
  3. **3xx** : Redirection
  4. **4xx** : Client Error
  5. **5xx** : Server Error

  The “xx” refers to different numbers between 00 and 99.

- A **status message**.

- **HTTP headers**, that consists information like language, set cookie to browser, format of the data, etc.

- optional **body** containing the fetched resource.


![http response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hyxatjzkqac7yr7b5qh.jpg)

### 4. Closing or Reusing The Connection 
_The original model of HTTP, and the default one in HTTP/1.0, is **short-lived connections**. Each HTTP request is completed on its own connection; this means a TCP handshake happens before each HTTP request, and these are serialized._
And this is **time consuming**.

To address this issue, _the concept of a **persistent connection** has been designed, even prior to HTTP/1.1. Alternatively this may be called a **keep-alive connection**._ 
This connection will not stay open forever: idle connections are closed after some time (a server may use the **_Keep-Alive header_** to specify a minimum time the connection should be kept open).

Also there is **HTTP Pipeline**, where several requests can be sent without waiting for the first response to be fully received. But this is _complex to implement_ and have issues with _proxies_. That's why it is not activated by default in modern browsers.

_However, pipelining has been superseded by a better algorithm, multiplexing, that is used by HTTP/2._


## Conclusion
So In modern days the use of HTTP is everywhere. originally it was only used to get HTML document, but nowadays you can use it for:

- Getting static files from remote server.
- JavaScript **fetch API**, which is based on HTTP, used for web services
- **Caching** can be controlled by HTTP. Server can instruct proxies and client about what to cache and for how long.
- To prevent snooping, HTTP helps to relax **origin constraint**. HTTP headers help to setup that which origin can access the resource. The `CORS` error you're getting most often, is the result of HTTP `Access-Control-Allow-Origin` response header.
- Application programs use HTTP to request files and updates from remote servers.
- Media players also use HTTP.

---
#### Thanks for reading
Feel free to share your opinion in the comments! See you next time.