If you request html webpage that has some text and an image, I believe http uses 2 responses for that of type text/html and image/html. Are both the http responses put in the same TCP response or not?
I would think that would be the most efficient way to do it unless the server expects another request for the image. Which brings me to my next question, does the server expect multiple requests for each http response or can it just send all the http responses that are needed for the html page?
ie.
TCP (GET /)
TCP (HTTP response 1, response 2, response 3)
Also can the tcp response have a fraction of one http response? For example if it is larger than the max packet size.
Typically, images have their own URLs. In HTTP/1.x, only the client can initiate a request. Even though the server might know that the client would need a resource, it has no way to send it to the client and must wait to receive a request for the resource from the client. Thus, images are fetched through a separate HTTP request, and thus get a separate HTTP response.
TCP Connections per HTTP request:
With regard to a TCP connection, a single connection can be used for multiple HTTP requests, for example in case several images are being fetched from the same server. However, this still requires a FIFO queue of requests and responses. Thus, browsers prefer separate TCP connections per HTTP request.
TCP Packets per HTTP request:
A single HTTP request/response can span across multiple TCP packets, if they don't fit in a single packet. Think of a 1 GB file upload or download request - its a single request/response, that would span over multiple packets.
When the server and client support "persistent connection" (Connection: keep-alive
), then multiple HTTP requests can be pipelined in a single TCP packet, if they can fit in.
Try this:
(echo "HEAD /index.html HTTP/1.1\nHost: www.google.co.in\nConnection: keep-alive\n\nHEAD /index.html HTTP/1.1\nHost: www.google.co.in\n\n"; sleep 1) | telnet www.google.co.in 80
The two pipelined HEAD requests evidently fit in a single packet.
In general, I wouldn't worry if HTTP request(s) are payloaded in single/multiple TCP packet(s), and leave that to the TCP protocol. Same holds for HTTP response.
While you are on this subject, I would recommend some reading on HTTP/2 as well :)