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.
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:
- 1xx : Informational
- 2xx : Success
- 3xx : Redirection
- 4xx : Client Error
- 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.
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 HTTPAccess-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.
Top comments (2)
Nice write up
Glad to know you find it helpful. stay tuned.