Content negotiation (ContentNegotiation)

Source: Internet
Author: User
For details about ContentNegotiation-Linux Enterprise Application-Linux server application, see the following. Content negotiation

Apache supports content negotiation defined in the HTTP/1.1 standard. It can select the most suitable media type, language, Character Set, and encoding mode for a resource based on the parameters provided by the browser. It also provides some smart processing capabilities for browsers to send incomplete content negotiation information.

Content negotiation is supported by the mod_negotiation Module and compiled to the server by default.
Top
Content Negotiation)

A resource may have multiple forms of representation, for example, it may have versions of different languages or media types, or even their combinations. The most common choice is to provide an index page for selection. However, because the browser can provide the representation of its preference in the request header information, it is possible to make the server automatically select. For example, a browser can indicate that you want to see information in French. If you only need to request the expression in French, the browser can send:

Accept-Language: fr

Note: This preference information is valid only when there are multiple optional language representations.

The following is a more complex request. The browser indicates that it can accept French and English, but preferably French. It can accept various media types, preferably HTML, but only files or other text types; it is best to use GIF or JPEG, but other media types can also be used, and other media types can be used as the final representation:

Accept-Language: fr; q = 1.0, en; q = 0.5
Accept: text/html; q = 1.0, text/*; q = 0.8, image/gif; q = 0.6, image/jpeg; q = 0.6, image /*; q = 0.5, */*; q = 0.1

Apache supports content negotiation for "server-driven" defined in HTTP/1.1 specifications. It can fully support Accept, Accept-Language, Accept-Charset, and Accept-Encoding request headers, these are the experimental negotiation protocols defined in RFC2295 and RFC2296, but the "function negotiation" defined in these RFC is not supported ".

Resource is a conceptual entity defined in URI (RFC2396. An HTTP server, such as Apache, provides access to resources in its namespace in a representation (representation, the representations consist of the defined media types, character sets, and encoded byte streams. A resource can be absent at any specific time, or has one or more representations. If multiple representations exist, the resource is called a negotiate (negotiable), and its manifestations are called variant ). The difference between different variants of a negotiable resource is called dimension ).
Top
Content negotiation in Apache

You can use either of the following two methods to provide the server with information about each variant for Resource Negotiation:

* Use a type table (that is, a *. var file) to specify the file names of each variant.
* Use "MultiViews" to search, that is, the server executes an implicit file name pattern match and selects it in the result.

Use Type table files

A type table is a document associated with the type-map processor (or compatible with the MIME type: application/x-type-map configured in earlier Apache versions ). To use this function, you must create a processor in the configuration to define a file suffix as type-map. The best way is to set it in the configuration file as follows:

AddHandler type-map. var

The Type table file should have the same name as the described resource and have a block (entry) for each valid variant. Each block consists of several consecutive HTTP header rows, blocks of different variants are separated by empty rows, and empty rows are not allowed in the blocks. Traditionally, Type tables start with a composite block that describes the overall nature (this is not necessary and will be ignored if any ). The following is a type table file that describes the resource foo named foo. var:

URI: foo

URI: foo.en.html
Content-type: text/html
Content-language: en

URI: foo.fr.de.html
Content-type: text/html; charset = iso-8859-2
Content-language: fr, de

Note: Even if you set MultiViews to On, the type table still takes precedence over the file suffix. If different variants have different resource qualities, you can use the "qs" parameter for media types to indicate these differences. The following example demonstrates three valid variants: jpeg, gif, and ASCII-art:

URI: foo

URI: foo.jpeg
Content-type: image/jpeg; qs = 0.8

URI: foo.gif
Content-type: image/gif; qs = 0.5

URI: foo.txt
Content-type: text/plain; qs = 0.01

The value range of qs is 0.000 to 1.000. variants with a value of 0.000 will never be selected. If no value is specified, their qs value is 1.0. The qs value indicates the "quality" of a variant relative to other variants. For example, when presenting a photo, jpeg is generally of a higher quality than the Character Chart; however, if you want to present an ASCII-art file, the character structure will be of higher quality than jpeg files. Therefore, the value of qs depends on the resource represented by the variant.

The mod_negotation table contains a complete list of HTTP headers.
Multiviews

MultiViews is an option for each directory, that is, it can be in the httpd. conf or. htaccess (if AllowOverride is set correctly) file. , , In the configuration section, use the Options command to specify. Note that Options All does not set MultiViews. You must specify them explicitly.

The effect of MultiViews is: if the server receives a request to/some/dir/foo, but/some/dir/foo does not exist, if/some/dir enables MultiViews, the server will find all foo. * file, and effectively forge a description of these foo. * The file type table is allocated to them with the same media type and content encoding, and the most suitable matching is selected and returned to the customer.

MultiViews can also search for the file name of a directory in the DirectoryIndex command on the server. If:

DirectoryIndex index

While index.html and index.html 3 coexist, the server will make a trade-off; if none exist, but index. cgi exists, the server will execute it.

If no file in a directory has a suffix that can be identified by mod_mime to indicate its character set, content type, language, and encoding, the result will depend on the settings of the MultiViewsMatch command, this command determines the processor, filter, and other suffix types to be used in the MultiViews negotiation.
Top
Negotiation Method

After Apache obtains a list of resource variants from a type table or a directory file name, it will use either of the two methods to select the possible "best" variant and return it to the customer. The content negotiation feature of Apache does not need to be detailed. The following documents describe these methods in detail for interested parties.

There are two ways to negotiate:

1. Server-driven negotiation using the Apache algorithm is usually the default method. Using this algorithm (which is described in detail below), Apache sometimes "disrupts" the quality factor of a specific dimension for better results. The method will be described in detail later.
2. Transparent content negotiation is only used when the browser explicitly sends requests using the mechanism defined in RFC2295. This method gives the browser full control over the "best" variant selection, so the effect also depends on the algorithm used by the browser. As part of the transparent negotiation process, the browser can require Apache to execute the "remote variant selection algorithm" defined in RFC2296 ".

Negotiated Dimension)
Variable description
The media browser specifies the preference in the Accept header. each item is associated with the quality factor. The variant description can also contain the quality factor (parameter "qs ").
The Language browser specifies the preference in the Accept-Language header. each item is associated with the quality factor. variants can be associated with zero, one, or more languages.
The Encoding browser specifies the preference in the Accept-Encoding header, and each item is associated with the quality factor.
The character set browser specifies the preference in the Accept-Charset header. each item is associated with the quality factor. A variant can specify a character set as a parameter of the media type.
Apache negotiation Algorithm

Apache uses the following algorithm to return a possible "best" variant to the browser. This algorithm cannot be configured any more. The process is as follows:

1. First, check the Accept * Header for each negotiated variable, and specify a quality for each variant. If the Accept * Header of a variable does not Accept this variant, it is removed. If no variants exist, go to step 4.
2. perform the following tests in sequence and use the elimination method to select the "best" variant. Variants that cannot pass the test will be removed. After each test is complete, if there is only one variant left, it will be used as the best match and go to step 3. If there is more than one variant, continue the next test.
1. Multiply the quality factor of the Accept header by the restored quality factor of the variant media type and select the highest product.
2. Select the variant with the highest language quality factor.
3. Use the Language sequence in the Accept-Language Header (if any), or use the Language sequence in the LanguagePriority command (if any) to select the most matching Language.
4. Select the variant of the highest "level" Media parameter (used to determine the media type of text/html ).
5. Select the variant of the optimal character set media parameter specified in the Accept-Charset header. If not explicitly specified, the ISO-8859-1 character set is used. A variant with a text/* media type that is not explicitly associated with a specific character set will use a ISO-8859-1.
6. Select a character set associated with it not a variant of the ISO-8859-1, if there is no such variant, select all variants.
7. Select the best encoding variant. If the user agent can accept the encoding variant, select it; otherwise, if there is a mixed encoding or unencoded variant, select the unencoded variant. If all variants are encoded, or all variants are not encoded, select all variants.
8. Select the variant with the smallest content length.
9. Select the first one of the remaining variants, the first one in the type table file, or the first file in the ASCII encoding sequence when the variant is read from the directory.
3. At this time, the algorithm has selected an "best" variant and returned it as a response. The Vary of the HTTP response header indicates the negotiated variable (the browser and cache can use this information to cache the resource ).
4. if no variant is selected (because none can be accepted by the browser), a status value of 406 is returned and contains a list of valid variants in HTML format. Similarly, in the Vary of the HTTP header, specify the variable of the variant.

Top
Disrupt Quality Value

Apache sometimes changes the quality value that should be strictly parsed according to the Apache negotiation algorithm, so as to get better results when the browser does not send complete and accurate information. In many cases, some frequently-used browsers send Accept header information that leads to incorrect variant selection. If a browser sends complete and correct information, the operation will not be disrupted.
Media type and wildcard

Accept: The request header specifies the media type preferences. It can also contain "wildcard" media types, such as "image/*" and "*/*" that matches any string "*/*". Therefore, if a request contains:

Accept: image /*,*/*

Indicates that any type starting with "image/" is acceptable, and any other type (the preceding "image/*" is redundant ). Some browsers will routinely Add a wildcard type after explicitly specifying the allowed type, for example:

Accept: text/html, text/plain, image/gif, image/jpeg ,*/*

The purpose is to indicate that preferences are clearly listed, and other performance is acceptable. This method is not optional, but "*/*" can be used with all other types, so it is not recommended to use this method, but should be "*. * "assign a lower quality (preferred) value of 0.01, for example:

Accept: text/html, text/plain, image/gif, image/jpeg, */*; q = 0.01

The specified type does not have a quality value, so its quality value is the default maximum value of 1.0, while "*/*" is a low value of 0.01. Therefore, other types are returned only when there is no matching variant of the specified type.

If the Accept: Header does not specify any q factor, Apache sets the q value of "*/*" to 0.01 to simulate the above recommended behavior, the Q value of "type/*" is set to 0.02 to give priority "*/*". If the q factor is specified for any media type in the Accept header, these special values are not used to make the browser that sends the correct information work properly.
Language negotiation exceptions

Some exception rules are added to the negotiation Algorithm in Apache 2.0 to allow clever compromise when the language negotiation fails.

Generally, when a client requests a unique page that cannot match the browser's Accept-language, the server returns a "No Acceptable Variant" or "Multiple Choices" response. However, it is possible to ignore the Accept-language in these cases by configuring Apache, and return a text that does not match the customer's request very well to avoid the emergence of these error messages. The ForceLanguagePriority command can block these two types of error messages and take over the server ruling mechanism controlled by the LanguagePriority command.

The server will also try to use the language subset to match when the matching fails. For example, if a customer requests a page in English in the English language en-GB, the server only supports the simple en standard HTTP/1.1. (Note: specifying en-GB instead of en in Accept-Language is almost always a mistake, because it seems that the reader understands English but does not understand English. Unfortunately, the default configuration of many popular clients is like this ). If No matching language is available, the server will ignore the setting of its language subset, return the "No Acceptable Variants" error, or press the required agepriority command to compromise. Apache implicitly attaches a parent language with a low quality value to the list of acceptable languages of the customer. However, if the customer requests "en-GB; q = 0.9, fr; q = 0.8 "then the text of" fr "will be returned, which is required to follow the HTTP/1.1 standard so that the correctly configured browser can work properly.

To support advanced technologies (such as cookies or special URL paths) used to determine users' preferred languages, the mod_negotiation Module starts to support prefer-language environment variables from version 2.0.47. If a language tag exists, the mod_negotiation Module tries to select a matching variant. If such a variant does not exist, the preceding common negotiation process will be used.
Example

SetEnvIf Cookie "language = (. +)" prefer-language = $1
Top
Extension of transparent content negotiation

Apache uses a new {encoding...} element in the variant list to mark the variant, thus extending the transparent content negotiation protocol (RFC2295 ). The RVSA/1.0 algorithm (RFC2296) is implemented to identify the encoded variants in the list as candidate variants that can be accepted by the Accept-Encoding request header. Before selecting the optimal variant, the implementation of RVSA/1.0 will not perform a rounding operation on the quality factor.
Top
Hyperlink and name Conversion

If you use language negotiation, because the file can have more than one suffix, you can select different name conversion. The suffix order is usually irrelevant (see the mod_mime document ).

A typical file with a MIME-type suffix (such as html). Its suffix can be an encoding suffix (such as gz) or a language variant suffix (such as en)

For example:

* Foo.en.html
* Foo.html. en
* Foo.en.html.gz

Example of file names and valid and invalid hyperlinks:
Invalid name of a file
Foo.html. enfoo
Foo.html-
Foo.en.htmlfoofoo.html
Foo.html.en.gz foo
Foo.htmlfoo.gz
Foo.html.gz
Foo.en.html.gzfoofoo.html
Foo.html.gz
Foo.gz
Foo.gz.html. enfoo
Foo.gz
Foo.gz.htmlfoo.html
Foo.html.gz. enfoo
Foo.html
Foo.html.gzfoo.gz

It can be seen that the use of hyperchains without any suffixes (such as foo) in the above table is always feasible, and its advantage is that rsp can be hidden. the actual file type, but can be modified in the future. For example, you do not need to modify the hyperchain itself, but change html to shtml or cgi.

Token. en ).
Top
Buffer description

If there is a representation associated with a specific URL in the cache, it can be used in the cache next time the URL is requested. However, if the resource is negotiable on the server side, only the variant of the first request may be correct, and the result retrieved due to the cache hit is incorrect. To avoid this situation, Apache usually marks the response returned after content negotiation as not buffered by the HTTP/1.1 client. Apache also supports HTTP/1.1 to buffer negotiated requests.

For requests from HTTP/1.0 clients (browsers or caches), The CacheNegotiatedDocs command allows the cache to obey the negotiated requests. This command should appear in the configuration of the master server or virtual host, with no parameters and no impact on requests from the HTTP/1.1 client.

For clients that comply with HTTP/1.1 specifications, Apache sends a Vary response header to specify the negotiation variable for the response. The cache can use this information to determine whether a subsequent request can provide services from a local copy. To encourage the cache to use local replicas instead of negotiated yuan, set the force-no-vary environment variable.
Top
More information

For more information about content Negotiation, see Alan J. Flavell's Language Negotiation Notes. However, note that this document may not be upgraded to include changes in Apache2.0.

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.