HTTP?

Client-server/Request-Response protocol. Default port 80, other can be used. Uses TCP, does not use UDP. Google’s QUIC uses HTTP over UDP
HTTP Clients: Web-browser, Any process can create socket and get information from server.

HTTP Payload + Headers

HTTP Request

HTTP Packet Flow

Sender
- Browser create HTTP request which goes to TLS Layer which sets TLS header encrypts the HTTP request it passes through the TCP layer
- TCP header, IP header are added
- NIC(Network interface Card) runs the Data Link layer which add the DL header and trailer and converts whole packet into 0101010111
- Modulation Chip converts these 010101 to electrical signals
- Routing table points to which next Hop Packet should be forwarded
- via ARP dst MAC is recieved and filled the DL header
Reciever
- Packet arrives on the destination network NIC, NIC removes the DL header
- Copies the IP-TCP-Encrypted-Payload via DMA(direct memory access) RAM
- A interrupt is generated which notify the kernel. Kernel removes IP, TCP Header
- TLS header is removed, packet is decrypted inside User space and recv() returns the Bytes

Layers adding headers

How HTTP parses Header, HTTP Body, End of HTTP Request

Every HTTP Header line is seperated by \r\n
HTTP headers and body is separated by exactly \r\n\r\n (two carriage return and line feed sequences)
Example of 2 back to back HTTP Requests, POST & GET

POST /upload HTTP/1.1\r\n
Host: example.com\r\n
Content-Length: 12\r\n
\r\n
[FILE DATA!]GET /status HTTP/1.1\r\n
Host: example.com\r\n
\r\n

Parse POST

HTTP parser reads the incoming stream line-by-line (looking for \r\n).
It stops when it hits \r\n\r\n. Now, It looks inside the headers & finds Content-Length: 12.
Since it knows content-length=12, it blindly reads exactly 12 bytes from the stream immediately following the \r\n\r\n. It captures [FILE DATA!] as the file payload.

Parse GET

Because the parser read exactly 12 bytes, the stream pointer is now positioned precisely at the start of the next request: GET /status…​.
The parser loops back to step 1, reading line-by-line until it hits the next \r\n\r\n.
recv() returns whatever bytes are currently available. It might return 100 bytes or 4 KB, There is no guarantee that a read data is complete HTTP or not.
For example, read 1 might contain GET /api HTTP/1.1\r\nHo, and read 2 might contain st: example.com\r\n\r\n.
Parser maintains state across reads, recognizes that the first read contains an incomplete header, and continues parsing when the next bytes arrive.

Features of HTTP

1. CONNECTIONLESS: HTTP-client(browser) initiates an HTTP request and after a request is made, the client disconnects from the server and waits for a response. server processes the request and re-establishes the connection with the client to send a response back.
2. MEDIA INDEPENDENT: Any type of data can be sent by HTTP as long as both the client and the server know how to handle the data content.
3. STATELESS: No session information is stored either by client or server.
4. Persistant & Parallel Connection: 2 access requests can be sent back-2-back without waiting for response to arrive.

      Client                         Server
      <-----TCP-3-way-handshake---->
      --------GET Page-1----------->
      --------GET Page-2----------->
      <-------Page-1---------------
      <-------Page-2---------------      
    

Advatanges, Disadvatanges/pitfalls of HTTP

Advatanges

1. Flexible. Whenever there are additional capabilities needed by an application, HTTP has the capability to extend. These can include Flash players and Acrobat reader.
2. Less connections. With HTTP2 and HTTP3 multiple requests can be sent in 1 packet.
3. Network BW: Less dues to less connections as HTTP2, HTTP3 sends multiple requests in 1 connection.
4. Error reporting: Reports errors without closing the TCP connection.

DisAdvatanges

1. Can only be used for point to point connection.
2. Does not have push capabilities as in [Long Pooling or server sent Events](/Networking/OSI-Layers/Layer5/WebServer_to_WebClient_Connection_Methods).
3. HTTPv2 does not offer reliable exchange (without retry logic).

HTTP Methods

Method / Operations Methods for requesting web page or content.
  Safe methods? Those which donot change state of server. Eg: GET, HEAD
  Read only methods? GET, OPTIONS, HEAD
  Idempotent methods? A operation that will produce the same results if executed once or multiple times. Eg: GET, HEAD, PUT, DELETE, TRACE, OPTIONS
  CRUD Operations: Protocol should support minimum these operations/methods.
Method Description
Create (POST) send data to a server to create or update a resource, data will be in request body.

POST /api/users HTTP/1.1      << POST REST_ENDPOINT
Host: example.com
Content-Type: application/json
Content-Length: 45

{
  "name": "John Doe",
  "job": "Software Engineer"
}

             
Read (GET) Reading data(page,object etc) from web server

curl http://example.com
OR 
curl -X GET http://example.com
            
Update (PUT) Update existing data on web server.
Delete (DELETE) Delete content from server
HEAD Requests only the headers of a resource, not the body
Server responds with headers (including metadata, content-type, length, status codes, etc.), but no content.
Why its helpful? check what a GET request would return, without actually downloading the resource (useful for checking file size, last-modified, existence, etc.)

curl -X HEAD http://example.com