What are the principles of HTTP persistent connection and pipelining? What is head-of-line blocking?
1. Interviewer: Please tell me what are the main differences between HTTP1.1 and HTTP1.0?
1. Connection management: long connection and pipeline processing
HTTP/1.0: Short connection is used by default. A new TCP connection needs to be established for each request and closed immediately after the request is completed. If a long connection is required, Connection: Keep-Alive needs to be explicitly set, but the compatibility is poor (some proxies do not support it).
HTTP/1.1: Long connection (persistent connection) is enabled by default, allowing multiple requests/responses to be transmitted on a TCP connection, reducing handshake overhead. Pipelining technology is supported. The client can send multiple requests continuously (without waiting for a response), but the server needs to return responses in the order of requests to avoid head-of-line blocking.
2. Host header field supports virtual hosts
HTTP/1.0: Assuming that one IP corresponds to a unique server, there is no Host field in the request header, and virtual host technology cannot be supported (such as multiple domains sharing the same IP).
HTTP/1.1: It is mandatory to include the Host field in the request header to identify the target server domain name. If it is missing, the server returns 400 Bad Request, which solves the resource location problem in the virtual host scenario.
3. Enhanced cache mechanism
HTTP/1.0: Relies on Expires (absolute time) and Last-Modified (last modified time) to control cache, and there are clock synchronization issues. Cache verification is implemented through If-Modified-Since.
HTTP/1.1:
Introduced Cache-Control headers (such as max-age, no-cache) to support relative time cache strategies.
Added ETag (entity tag) mechanism to verify whether the resource has changed through If-None-Match.
Support Vary header to distinguish different cache versions based on request headers (such as Accept-Language).
4. Bandwidth optimization and chunked transmission
Range request: HTTP/1.1 supports the Range header, which allows requesting partial content of a resource (such as resuming a transfer from a breakpoint), and the response status code is 206 Partial Content.
Chunked Encoding: When dynamically generating content, it is transmitted in chunks using Transfer-Encoding: chunked, without the need to predict the full data size, which improves transmission efficiency.
100 Continue status code: Before the client sends a large request, it first asks the server whether it accepts it through the Expect: 100-continue header to avoid bandwidth waste.
5. Error status code and content negotiation
Error status code: HTTP/1.1 adds 24 new status codes, such as
100 Continue: (Continue request)
206 Partial Content: (Partial content)
409 Conflict: (Resource status conflict)
410 Gone: (Resource permanently deleted).
Content negotiation: Support multiple languages/compression formats through the Accept-* series of headers (such as Accept-Language, Accept-Encoding). The server returns the most suitable version and records the negotiation basis through the Vary header.
6. Other improvements
Request method expansion: HTTP/1.1 adds OPTIONS, PUT, DELETE, TRACE and other methods to support RESTful API design.
Message transmission optimization: Support the trailer header to pass metadata (such as checksum) after block transmission, improving the integrity of large file transmission.
Summary comparison table: