HTTP content negotiation

Source: Internet
Author: User
I. PrefaceWe often see the request header starting with accept during the daily packet capture process. For example, accept-language has a Q value, and someone must be wondering why this Q value should be defined in the HTTP specification; there is also a header named vary in the Response Header. What is the significance of this header? : Ii. content negotiationTo clarify these two problems, we need to introduce the content negotiation concept of the HTTP protocol: a resource, the server has multiple versions, and the client informs the server of its preference, the server selects an appropriate version based on its preferences to respond to client requests. Content negotiation technology generally has three implementation solutions: (1) the client drives the client to initiate a request, the server sends a list of options, and the client selects the option to send the second request. Advantage: relatively easy to implement disadvantages: Increased latency. At least two requests must be sent. The first request gets the resource list and the second request gets the selected copy. (2) The Server Driver server checks the client's request header set and determines the version of the page to be provided. Advantage: It is faster than client-driven negotiation. HTTP provides a Q mechanism to allow server similarity matching, and a vary header for the server to tell downstream devices (such as the proxy server) how to evaluate the request. Disadvantage: the header set does not match. The server needs to make guesses. (3) transparent negotiation. An intermediate device (usually a cache proxy) represents the client for negotiation. Advantages: free from the negotiation overhead of the Web server, it is faster than client-driven negotiation. Disadvantages: HTTP does not provide relevant specifications, among which server-driven solutions are widely used. Iii. General content negotiation HeadersThe client sends the accept header set to send user preference information. The server sends the object header set to match the client's accept header set: accept content-typeaccept-language content-languageaccept-encoding content-Encoding Iv. Q quality value application scenariosAssume that the accept-language of the client is in Spanish, but the server only has English and French versions. This client wants to return English first when there is no Spanish language. This means that we need an HTTP mechanism to describe preferences in more detail. This mechanism is the quality value (Q value ). Example: Accept-language: en; q = 0.5, fr; q = 0.0, NL; q = 1.0, TR; q = 0.0 indicates the header: users are most willing to accept Dutch (NL) and English (En), that is, they are not willing to accept French (FR) or Turkish (TR) The Q value ranges from 0.0 ~ 1.0 (highest priority of 1.0)  V. Vary's First Application Scenario The server's decision is not based on the accept header set (the conventional content negotiation header set), but such as accept-EncodingAssume that the entire request process is as follows: client> Proxy Server (with cache function)> Web server. The first client that supports gzip compression sends a request to the intermediate proxy server. The proxy server forwards the request and pulls the content from the web server, after the content is obtained, the Proxy Server caches the content (because the request header has accept-encoding: gzip, the content will be compressed ). The second client that does not support gzip compression also sends the same request to the intermediate proxy server. The proxy server finds that the request has been cached, and then responds the compressed content to the client. Tragedy, because the client does not support gzip compression, it cannot be decompressed. Vi. Working Principles of the vary HeaderThe HTTP vary Response Header lists all client request headers. The cache server can use these headers to select documents or produce customized content. For example, if the response content to the client depends on accept-encoding, the vary header must contain accept-encoding. When a new request arrives, the cache server will negotiate the first set based on the content to find the best match. However, before providing the document to the client, it must check whether the Web server has a vary header in the cached response. If yes, the header values in the new request must be the same as those in the old cached request. Because the Web server may change the response according to the header of the client request, in order to achieve transparent negotiation, The cache server must store the client request header and the corresponding Server Response Header for each cached variant.. To put it simply, the Web Server adds the Response Header vary: Accept-encoding to inform the proxy server to cache different versions based on the client's request header accept-encoding, so that the next time the client requests the same resource, select the corresponding Cache version response based on accept-encoding. In fact, we can also disable the cache function of the intermediate entity to solve this problem: the Web server sets the response header: cache-control: Private. Generally, the solution for adding the vary header is more common, because we still want to make full use of the cache function of the intermediate entity. VII. ReferencesBook: HTTP authoritative guide qu's blog: https://www.imququ.com/post/vary-header-in-http.html

HTTP content negotiation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.