]> granicus.if.org Git - apache/blob - docs/manual/content-negotiation.html
Fixed mismatched list tag.
[apache] / docs / manual / content-negotiation.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <HTML>
3 <HEAD>
4 <TITLE>Apache Content Negotiation</TITLE>
5 </HEAD>
6
7 <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
8 <BODY
9  BGCOLOR="#FFFFFF"
10  TEXT="#000000"
11  LINK="#0000FF"
12  VLINK="#000080"
13  ALINK="#FF0000"
14 >
15 <!--#include virtual="header.html" -->
16 <H1 ALIGN="CENTER">Content Negotiation</H1>
17
18 <P>
19 Apache's support for content negotiation has been updated to meet the
20 HTTP/1.1 specification. It can choose the best representation of a
21 resource based on the browser-supplied preferences for media type,
22 languages, character set and encoding.  It is also implements a
23 couple of features to give more intelligent handling of requests from
24 browsers which send incomplete negotiation information.  <P>
25
26 Content negotiation is provided by the
27 <A HREF="mod/mod_negotiation.html">mod_negotiation</A> module,
28 which is compiled in by default.
29
30 <HR>
31
32 <H2>About Content Negotiation</H2>
33
34 <P>
35 A resource may be available in several different representations. For
36 example, it might be available in different languages or different
37 media types, or a combination.  One way of selecting the most
38 appropriate choice is to give the user an index page, and let them
39 select. However it is often possible for the server to choose
40 automatically. This works because browsers can send as part of each
41 request information about what representations they prefer. For
42 example, a browser could indicate that it would like to see
43 information in French, if possible, else English will do. Browsers
44 indicate their preferences by headers in the request. To request only
45 French representations, the browser would send
46
47 <PRE>
48   Accept-Language: fr
49 </PRE>
50
51 <P>
52 Note that this preference will only be applied when there is a choice
53 of representations and they vary by language.
54 <P>
55
56 As an example of a more complex request, this browser has been
57 configured to accept French and English, but prefer French, and to
58 accept various media types, preferring HTML over plain text or other
59 text types, and preferring GIF or JPEG over other media types, but also
60 allowing any other media type as a last resort:
61
62 <PRE>
63   Accept-Language: fr; q=1.0, en; q=0.5
64   Accept: text/html; q=1.0, text/*; q=0.8, image/gif; q=0.6,
65         image/jpeg; q=0.6, image/*; q=0.5, */*; q=0.1
66 </PRE>
67
68 Apache 1.2 supports 'server driven' content negotiation, as defined in
69 the HTTP/1.1 specification. It fully supports the Accept,
70 Accept-Language, Accept-Charset and Accept-Encoding request headers.
71 Apache 1.3.4 also supports 'transparent' content negotiation, which is
72 an experimental negotiation protocol defined in RFC 2295 and RFC 2296.
73 It does not offer support for 'feature negotiation' as defined in
74 these RFCs.  
75 <P>
76
77 A <STRONG>resource</STRONG> is a conceptual entity identified by a URI
78 (RFC 2396).  An HTTP server like Apache provides access to
79 <STRONG>representations</STRONG> of the resource(s) within its namespace,
80 with each representation in the form of a sequence of bytes with a
81 defined media type, character set, encoding, etc.  Each resource may be
82 associated with zero, one, or more than one representation
83 at any given time.  If multiple representations are available,
84 the resource is referred to as <STRONG>negotiable</STRONG> and each of its
85 representations is termed a <STRONG>variant</STRONG>. The ways in which the
86 variants for a negotiable resource vary are called the
87 <STRONG>dimensions</STRONG> of negotiation.
88
89 <H2>Negotiation in Apache</H2>
90
91 <P>
92 In order to negotiate a resource, the server needs to be given
93 information about each of the variants. This is done in one of two
94 ways:
95
96 <UL>
97   <LI> Using a type map (<EM>i.e.</EM>, a <CODE>*.var</CODE> file) which
98        names the files containing the variants explicitly, or
99   <LI> Using a 'MultiViews' search, where the server does an implicit
100        filename pattern match and chooses from among the results.
101 </UL>
102
103 <H3>Using a type-map file</H3>
104
105 <P>
106 A type map is a document which is associated with the handler
107 named <CODE>type-map</CODE> (or, for backwards-compatibility with
108 older Apache configurations, the mime type
109 <CODE>application/x-type-map</CODE>).  Note that to use this feature,
110 you must have a handler set in the configuration that defines a
111 file suffix as <CODE>type-map</CODE>; this is best done with a
112
113 <PRE>
114   AddHandler type-map .var
115 </PRE>
116
117 in the server configuration file.  See the comments in the sample config
118 file for more details. <P>
119
120 Type map files have an entry for each available variant; these entries
121 consist of contiguous HTTP-format header lines.  Entries for
122 different variants are separated by blank lines.  Blank lines are
123 illegal within an entry.  It is conventional to begin a map file with
124 an entry for the combined entity as a whole (although this
125 is not required, and if present will be ignored). An example
126 map file is:
127
128 <PRE>
129   URI: foo
130
131   URI: foo.en.html
132   Content-type: text/html
133   Content-language: en
134
135   URI: foo.fr.de.html
136   Content-type: text/html;charset=iso-8859-2
137   Content-language: fr, de
138 </PRE>
139
140 If the variants have different source qualities, that may be indicated
141 by the "qs" parameter to the media type, as in this picture (available
142 as jpeg, gif, or ASCII-art):
143
144 <PRE>
145   URI: foo
146
147   URI: foo.jpeg
148   Content-type: image/jpeg; qs=0.8
149
150   URI: foo.gif
151   Content-type: image/gif; qs=0.5
152
153   URI: foo.txt
154   Content-type: text/plain; qs=0.01
155 </PRE>
156 <P>
157
158 qs values can vary in the range 0.000 to 1.000. Note that any variant with
159 a qs value of 0.000 will never be chosen. Variants with no 'qs'
160 parameter value are given a qs factor of 1.0. The qs parameter indicates
161 the relative 'quality' of this variant compared to the other available
162 variants, independent of the client's capabilities.  For example, a jpeg
163 file is usually of higher source quality than an ascii file if it is
164 attempting to represent a photograph.  However, if the resource being
165 represented is an original ascii art, then an ascii representation would
166 have a higher source quality than a jpeg representation.  A qs value
167 is therefore specific to a given variant depending on the nature of
168 the resource it represents.
169
170 <P>
171 The full list of headers recognized is:
172
173 <DL>
174   <DT> <CODE>URI:</CODE>
175   <DD> uri of the file containing the variant (of the given media
176        type, encoded with the given content encoding).  These are
177        interpreted as URLs relative to the map file; they must be on
178        the same server (!), and they must refer to files to which the
179        client would be granted access if they were to be requested
180        directly.
181   <DT> <CODE>Content-Type:</CODE>
182   <DD> media type --- charset, level and "qs" parameters may be given.  These
183        are often referred to as MIME types; typical media types are
184        <CODE>image/gif</CODE>, <CODE>text/plain</CODE>, or
185        <CODE>text/html;&nbsp;level=3</CODE>.
186   <DT> <CODE>Content-Language:</CODE>
187   <DD> The languages of the variant, specified as an Internet standard
188        language tag from RFC 1766 (<EM>e.g.</EM>, <CODE>en</CODE> for English,
189        <CODE>kr</CODE> for Korean, <EM>etc.</EM>).
190   <DT> <CODE>Content-Encoding:</CODE>
191   <DD> If the file is compressed, or otherwise encoded, rather than
192        containing the actual raw data, this says how that was done.
193        Apache only recognizes encodings that are defined by an
194        <A HREF="mod/mod_mime.html#addencoding">AddEncoding</A> directive.
195        This normally includes the encodings <CODE>x-compress</CODE>
196        for compress'd files, and <CODE>x-gzip</CODE> for gzip'd files.
197        The <CODE>x-</CODE> prefix is ignored for encoding comparisons.
198   <DT> <CODE>Content-Length:</CODE>
199   <DD> The size of the file.  Specifying content
200        lengths in the type-map allows the server to compare file sizes
201        without checking the actual files.
202   <DT> <CODE>Description:</CODE>
203   <DD> A human-readable textual description of the variant.  If Apache cannot
204        find any appropriate variant to return, it will return an error 
205        response which lists all available variants instead.  Such a variant
206        list will include the human-readable variant descriptions.
207 </DL>
208
209 <H3>Multiviews</H3>
210
211 <P>
212 <CODE>MultiViews</CODE> is a per-directory option, meaning it can be set with
213 an <CODE>Options</CODE> directive within a <CODE>&lt;Directory&gt;</CODE>,
214 <CODE>&lt;Location&gt;</CODE> or <CODE>&lt;Files&gt;</CODE>
215 section in <CODE>access.conf</CODE>, or (if <CODE>AllowOverride</CODE>
216 is properly set) in <CODE>.htaccess</CODE> files.  Note that
217 <CODE>Options All</CODE> does not set <CODE>MultiViews</CODE>; you
218 have to ask for it by name.
219
220 <P>
221 The effect of <CODE>MultiViews</CODE> is as follows: if the server
222 receives a request for <CODE>/some/dir/foo</CODE>, if
223 <CODE>/some/dir</CODE> has <CODE>MultiViews</CODE> enabled, and
224 <CODE>/some/dir/foo</CODE> does <EM>not</EM> exist, then the server reads the
225 directory looking for files named foo.*, and effectively fakes up a
226 type map which names all those files, assigning them the same media
227 types and content-encodings it would have if the client had asked for
228 one of them by name.  It then chooses the best match to the client's
229 requirements.
230
231 <P>
232 <CODE>MultiViews</CODE> may also apply to searches for the file named by the
233 <CODE>DirectoryIndex</CODE> directive, if the server is trying to
234 index a directory. If the configuration files specify
235
236 <PRE>
237   DirectoryIndex index
238 </PRE>
239
240 then the server will arbitrate between <CODE>index.html</CODE>
241 and <CODE>index.html3</CODE> if both are present.  If neither are
242 present, and <CODE>index.cgi</CODE> is there, the server will run it.
243
244 <P>
245 If one of the files found when reading the directive is a CGI script,
246 it's not obvious what should happen.  The code gives that case
247 special treatment --- if the request was a POST, or a GET with
248 QUERY_ARGS or PATH_INFO, the script is given an extremely high quality
249 rating, and generally invoked; otherwise it is given an extremely low
250 quality rating, which generally causes one of the other views (if any)
251 to be retrieved.
252
253 <H2>The Negotiation Methods</H2>
254
255 After Apache has obtained a list of the variants for a given resource,
256 either from a type-map file or from the filenames in the directory, it
257 invokes one of two methods to decide on the 'best' variant to
258 return, if any.  It is not necessary to know any of the details of how
259 negotiation actually takes place in order to use Apache's content
260 negotiation features. However the rest of this document explains the
261 methods used for those interested.
262 <P>
263
264 There are two negotiation methods:
265
266 <OL> 
267
268 <LI><STRONG>Server driven negotiation with the Apache
269 algorithm</STRONG> is used in the normal case.  The Apache algorithm is
270 explained in more detail below.  When this algorithm is used, Apache
271 can sometimes 'fiddle' the quality factor of a particular dimension to
272 achieve a better result. The ways Apache can fiddle quality factors is
273 explained in more detail below.
274
275 <LI><STRONG>Transparent content negotiation</STRONG> is used when the
276 browser specifically requests this through the mechanism defined in RFC
277 2295.  This negotiation method gives the browser full control over
278 deciding on the 'best' variant, the result is therefore dependent on
279 the specific algorithms used by the browser.  As part of the
280 transparent negotiation process, the browser can ask Apache to run the
281 'remote variant selection algorithm' defined in RFC 2296.
282
283 </OL>
284
285
286 <H3>Dimensions of Negotiation</H3>
287
288 <TABLE>
289 <TR valign="top">
290 <TH>Dimension
291 <TH>Notes
292 <TR valign="top">
293 <TD>Media Type
294 <TD>Browser indicates preferences with the Accept header field. Each item
295 can have an associated quality factor. Variant description can also
296 have a quality factor (the "qs" parameter).
297 <TR valign="top">
298 <TD>Language
299 <TD>Browser indicates preferences with the Accept-Language header field.
300 Each item can have a quality factor. Variants can be associated with none, one
301 or more than one language.
302 <TR valign="top">
303 <TD>Encoding
304 <TD>Browser indicates preference with the Accept-Encoding header field.
305 Each item can have a quality factor.
306 <TR valign="top">
307 <TD>Charset
308 <TD>Browser indicates preference with the Accept-Charset header field.
309 Each item can have a quality factor.
310 Variants can indicate a charset as a parameter of the media type.
311 </TABLE>
312
313 <H3>Apache Negotiation Algorithm</H3>
314
315 <P>
316 Apache can use the following algorithm to select the 'best' variant
317 (if any) to return to the browser.  This algorithm is not
318 further configurable. It operates as follows:
319
320 <OL>
321 <LI>First, for each dimension of the negotiation, check the appropriate
322 <EM>Accept*</EM> header field and assign a quality to each
323 variant. If the <EM>Accept*</EM> header for any dimension implies that this
324 variant is not acceptable, eliminate it. If no variants remain, go
325 to step 4.
326
327 <LI>Select the 'best' variant by a process of elimination. Each of the
328 following tests is applied in order. Any variants not selected at each
329 test are eliminated. After each test, if only one variant remains,
330 select it as the best match and proceed to step 3. If more than one
331 variant remains, move on to the next test.
332
333 <OL>
334 <LI>Multiply the quality factor from the Accept header with the
335   quality-of-source factor for this variant's media type, and select
336   the variants with the highest value.
337
338 <LI>Select the variants with the highest language quality factor.
339
340 <LI>Select the variants with the best language match, using either the
341   order of languages in the Accept-Language header (if present), or else 
342   the order of languages in the <CODE>LanguagePriority</CODE> 
343   directive (if present).
344
345 <LI>Select the variants with the highest 'level' media parameter
346   (used to give the version of text/html media types).
347
348 <LI>Select variants with the best charset media parameters,
349   as given on the Accept-Charset header line. Charset ISO-8859-1
350   is acceptable unless explicitly excluded. Variants with a
351   <CODE>text/*</CODE> media type but not explicitly associated
352   with a particular charset are assumed to be in ISO-8859-1.
353
354 <LI>Select those variants which have associated
355     charset media parameters that are <EM>not</EM> ISO-8859-1.
356     If there are no such variants, select all variants instead.
357
358 <LI>Select the variants with the best encoding. If there are
359   variants with an encoding that is acceptable to the user-agent,
360   select only these variants. Otherwise if there is a mix of encoded
361   and non-encoded variants, select only the unencoded variants.
362   If either all variants are encoded or all variants are not encoded, 
363   select all variants.
364
365 <LI>Select the variants with the smallest content length.
366
367 <LI>Select the first variant of those remaining. This will be either the
368   first listed in the type-map file, or when variants are read from
369   the directory, the one whose file name comes first when sorted using
370   ASCII code order.
371
372 </OL>
373
374 <LI>The algorithm has now selected one 'best' variant, so return
375   it as the response. The HTTP response header Vary is set to indicate the
376   dimensions of negotiation (browsers and caches can use this
377   information when caching the resource). End.
378
379 <LI>To get here means no variant was selected (because none are acceptable
380   to the browser). Return a 406 status (meaning "No acceptable representation")
381   with a response body consisting of an HTML document listing the
382   available variants. Also set the HTTP Vary header to indicate the
383   dimensions of variance.
384
385 </OL>
386
387 <H2><A NAME="better">Fiddling with Quality Values</A></H2>
388
389 <P> 
390 Apache sometimes changes the quality values from what would be
391 expected by a strict interpretation of the Apache negotiation
392 algorithm above. This is to get a better result from the algorithm for
393 browsers which do not send full or accurate information. Some of the
394 most popular browsers send Accept header information which would
395 otherwise result in the selection of the wrong variant in many
396 cases. If a browser sends full and correct information these fiddles
397 will not be applied.
398 <P>
399
400 <H3>Media Types and Wildcards</H3>
401
402 <P>
403 The Accept: request header indicates preferences for media types. It
404 can also include 'wildcard' media types, such as "image/*" or "*/*"
405 where the * matches any string. So a request including:
406 <PRE>
407   Accept: image/*, */*
408 </PRE>
409
410 would indicate that any type starting "image/" is acceptable,
411 as is any other type (so the first "image/*" is redundant). Some
412 browsers routinely send wildcards in addition to explicit types they
413 can handle. For example:
414 <PRE>
415   Accept: text/html, text/plain, image/gif, image/jpeg, */*
416 </PRE>
417
418 The intention of this is to indicate that the explicitly
419 listed types are preferred, but if a different representation is
420 available, that is ok too. However under the basic algorithm, as given
421 above, the */* wildcard has exactly equal preference to all the other
422 types, so they are not being preferred. The browser should really have
423 sent a request with a lower quality (preference) value for *.*, such
424 as:
425 <PRE>
426   Accept: text/html, text/plain, image/gif, image/jpeg, */*; q=0.01
427 </PRE>
428
429 The explicit types have no quality factor, so they default to a
430 preference of 1.0 (the highest). The wildcard */* is given
431 a low preference of 0.01, so other types will only be returned if
432 no variant matches an explicitly listed type.
433 <P>
434
435 If the Accept: header contains <EM>no</EM> q factors at all, Apache sets
436 the q value of "*/*", if present, to 0.01 to emulate the desired
437 behavior. It also sets the q value of wildcards of the format
438 "type/*" to 0.02 (so these are preferred over matches against
439 "*/*". If any media type on the Accept: header contains a q factor,
440 these special values are <EM>not</EM> applied, so requests from browsers
441 which send the correct information to start with work as expected.
442
443 <H3>Variants with no Language</H3>
444
445 <P>
446 If some of the variants for a particular resource have a language
447 attribute, and some do not, those variants with no language
448 are given a very low language quality factor of 0.001.<P>
449
450 The reason for setting this language quality factor for
451 variant with no language to a very low value is to allow
452 for a default variant which can be supplied if none of the
453 other variants match the browser's language preferences.
454
455 For example, consider the situation with three variants:
456
457 <UL>
458 <LI>foo.en.html, language en
459 <LI>foo.fr.html, language en
460 <LI>foo.html, no language
461 </UL>
462
463 <P>
464 The meaning of a variant with no language is that it is
465 always acceptable to the browser. If the request Accept-Language
466 header includes either en or fr (or both) one of foo.en.html
467 or foo.fr.html will be returned. If the browser does not list
468 either en or fr as acceptable, foo.html will be returned instead.
469
470 <H2>Extensions to Transparent Content Negotiation</H2>
471
472 Apache extends the transparent content negotiation protocol (RFC 2295)
473 as follows.  A new <CODE> {encoding ..}</CODE> element is used in
474 variant lists to label variants which are available with a specific
475 content-encoding only. The implementation of the
476 RVSA/1.0 algorithm (RFC 2296) is extended to recognize encoded
477 variants in the list, and to use them as candidate variants whenever
478 their encodings are acceptable according to the Accept-Encoding
479 request header.  The RVSA/1.0 implementation does not round computed
480 quality factors to 5 decimal places before choosing the best variant.
481
482 <H2>Note on hyperlinks and naming conventions</H2>
483
484 <P>
485 If you are using language negotiation you can choose between
486 different naming conventions, because files can have more than one
487 extension, and the order of the extensions is normally irrelevant
488 (see <A HREF="mod/mod_mime.html">mod_mime</A> documentation for details).
489 <P>
490 A typical file has a MIME-type extension (<EM>e.g.</EM>, <SAMP>html</SAMP>),
491 maybe an encoding extension (<EM>e.g.</EM>, <SAMP>gz</SAMP>), and of course a
492 language extension (<EM>e.g.</EM>, <SAMP>en</SAMP>) when we have different
493 language variants of this file.
494
495 <P>
496 Examples:
497 <UL>
498 <LI>foo.en.html
499 <LI>foo.html.en
500 <LI>foo.en.html.gz
501 </UL>
502
503 <P>
504 Here some more examples of filenames together with valid and invalid
505 hyperlinks:
506 </P>
507
508 <TABLE BORDER=1 CELLPADDING=8 CELLSPACING=0>
509 <TR>
510  <TH>Filename</TH>
511  <TH>Valid hyperlink</TH>
512  <TH>Invalid hyperlink</TH>
513 </TR>
514 <TR>
515  <TD><EM>foo.html.en</EM></TD>
516  <TD>foo<BR>
517      foo.html</TD>
518  <TD>-</TD>
519 </TR>
520 <TR>
521  <TD><EM>foo.en.html</EM></TD>
522  <TD>foo</TD>
523  <TD>foo.html</TD>
524 </TR>
525 <TR>
526  <TD><EM>foo.html.en.gz</EM></TD>
527  <TD>foo<BR>
528      foo.html</TD>
529  <TD>foo.gz<BR>
530      foo.html.gz</TD>
531 </TR>
532 <TR>
533  <TD><EM>foo.en.html.gz</EM></TD>
534  <TD>foo</TD>
535  <TD>foo.html<BR>
536      foo.html.gz<BR>
537      foo.gz</TD>
538 </TR>
539 <TR>
540  <TD><EM>foo.gz.html.en</EM></TD>
541  <TD>foo<BR>
542      foo.gz<BR>
543      foo.gz.html</TD>
544  <TD>foo.html</TD>
545 </TR>
546 <TR>
547  <TD><EM>foo.html.gz.en</EM></TD>
548  <TD>foo<BR>
549      foo.html<BR>
550      foo.html.gz</TD>
551  <TD>foo.gz</TD>
552 </TR>
553 </TABLE>
554
555 <P>
556 Looking at the table above you will notice that it is always possible to
557 use the name without any extensions  in an hyperlink (<EM>e.g.</EM>, <SAMP>foo</SAMP>).
558 The advantage is that you can hide the actual type of a
559 document rsp. file and can change it later, <EM>e.g.</EM>, from <SAMP>html</SAMP>
560 to <SAMP>shtml</SAMP> or <SAMP>cgi</SAMP> without changing any
561 hyperlink references.
562
563 <P>
564 If you want to continue to use a MIME-type in your hyperlinks (<EM>e.g.</EM>
565 <SAMP>foo.html</SAMP>) the language extension (including an encoding extension
566 if there is one) must be on the right hand side of the MIME-type extension
567 (<EM>e.g.</EM>, <SAMP>foo.html.en</SAMP>).
568
569
570 <H2>Note on Caching</H2>
571
572 <P>
573 When a cache stores a representation, it associates it with the request URL.
574 The next time that URL is requested, the cache can use the stored
575 representation. But, if the resource is negotiable at the server,
576 this might result in only the first requested variant being cached and
577 subsequent cache hits might return the wrong response. To prevent this,
578 Apache normally marks all responses that are returned after content negotiation
579 as non-cacheable by HTTP/1.0 clients. Apache also supports the HTTP/1.1
580 protocol features to allow caching of negotiated responses. <P>
581
582 For requests which come from a HTTP/1.0 compliant client (either a
583 browser or a cache), the directive <TT>CacheNegotiatedDocs</TT> can be
584 used to allow caching of responses which were subject to negotiation.
585 This directive can be given in the server config or virtual host, and
586 takes no arguments. It has no effect on requests from HTTP/1.1 clients.
587
588 <!--#include virtual="footer.html" -->
589 </BODY>
590 </HTML>