mod_negotiation.c revision 58fd79b56eb624bf011772994e9761d3c2e228c1
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The Apache Software License, Version 1.1
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * reserved.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Redistribution and use in source and binary forms, with or without
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * modification, are permitted provided that the following conditions
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * are met:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 1. Redistributions of source code must retain the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 2. Redistributions in binary form must reproduce the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer in
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * the documentation and/or other materials provided with the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * distribution.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 3. The end-user documentation included with the redistribution,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if any, must include the following acknowledgment:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * "This product includes software developed by the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Apache Software Foundation (http://www.apache.org/)."
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Alternately, this acknowledgment may appear in the software itself,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if and wherever such third-party acknowledgments normally appear.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 4. The names "Apache" and "Apache Software Foundation" must
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * not be used to endorse or promote products derived from this
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * software without prior written permission. For written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission, please contact apache@apache.org.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 5. Products derived from this software may not be called "Apache",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * nor may "Apache" appear in their name, without prior written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission of the Apache Software Foundation.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SUCH DAMAGE.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This software consists of voluntary contributions made by many
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * individuals on behalf of the Apache Software Foundation. For more
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * information on the Apache Software Foundation, please see
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Portions of this software are based upon public domain software
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * originally written at the National Center for Supercomputing Applications,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * University of Illinois, Urbana-Champaign.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * mod_negotiation.c: keeps track of MIME types the client is willing to
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * accept, and contains code to handle type arbitration.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* Commands --- configuring document caching on a per (virtual?)
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames * server basis...
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbtypedef struct {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* forcelangpriority flags
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#define FLP_UNDEF 0 /* Same as FLP_DEFAULT, but base overrides */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#define FLP_NONE 1 /* Return 406, HTTP_NOT_ACCEPTABLE */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#define FLP_PREFER 2 /* Use language_priority rather than MC */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#define FLP_FALLBACK 4 /* Use language_priority rather than NA */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzmodule AP_MODULE_DECLARE_DATA negotiation_module;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void *create_neg_dir_config(apr_pool_t *p, char *dummy)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb neg_dir_config *new = (neg_dir_config *) apr_palloc(p, sizeof(neg_dir_config));
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void *merge_neg_dir_configs(apr_pool_t *p, void *basev, void *addv)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb neg_dir_config *new = (neg_dir_config *) apr_palloc(p, sizeof(neg_dir_config));
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* give priority to the config in the subdirectory */
c3e342e5b0b9fea6617ee16d2da02c3ef2108126dougm new->forcelangpriority = (add->forcelangpriority != FLP_UNDEF)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic const char *set_language_priority(cmd_parms *cmd, void *n_,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar const char *lang)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe const char **langp;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe n->language_priority = apr_array_make(cmd->pool, 4, sizeof(char *));
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe langp = (const char **) apr_array_push(n->language_priority);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic const char *set_force_priority(cmd_parms *cmd, void *n_, const char *w)
ea512a4af20e6b6e6931de4929d54d93f03a0139ianh return "Cannot combine ForceLanguagePriority options with None";
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return "Cannot combine ForceLanguagePriority options None and Prefer";
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return "Cannot combine ForceLanguagePriority options None and Fallback";
3e392a5afd51526de3cb15d57ee46d8cb160ae65gregames return apr_pstrcat(cmd->pool, "Invalid ForceLanguagePriority option ", w, NULL);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowestatic const char *cache_negotiated_docs(cmd_parms *cmd, void *dummy,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_set_module_config(cmd->server->module_config, &negotiation_module,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return (ap_get_module_config(s->module_config, &negotiation_module) != NULL);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe AP_INIT_FLAG("CacheNegotiatedDocs", cache_negotiated_docs, NULL, RSRC_CONF,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "Either 'on' or 'off' (default)"),
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe AP_INIT_ITERATE("LanguagePriority", set_language_priority, NULL, OR_FILEINFO,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "space-delimited list of MIME language abbreviations"),
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe AP_INIT_ITERATE("ForceLanguagePriority", set_force_priority, NULL, OR_FILEINFO,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "Force LanguagePriority elections, either None, or Fallback and/or Prefer"),
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Record of available info on a media type specified by the client
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * (we also use 'em for encodings and languages)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowetypedef struct accept_rec {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * Record of available info on a particular variant
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * Note that a few of these fields are updated by the actual negotiation
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * code. These are:
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * level_matched --- initialized to zero. Set to the value of level
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * if the client actually accepts this media type at that
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * level (and *not* if it got in on a wildcard). See level_cmp
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * mime_stars -- initialized to zero. Set to the number of stars
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * present in the best matching Accept header element.
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * definite -- initialized to 1. Set to 0 if there is a match which
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * makes the variant non-definite according to the rules
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * in rfc2296.
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowetypedef struct var_rec {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz request_rec *sub_req; /* May be NULL (is, for map files) */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz const char *file_name; /* Set to 'this' (for map file body content) */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar apr_array_header_t *content_languages; /* list of languages for this variant */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* The next five items give the quality values for the dimensions
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * of negotiation for this variant. They are obtained from the
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * appropriate header lines, except for source_quality, which
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * is obtained from the variant itself (the 'qs' parameter value
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * from the variant's mime-type). Apart from source_quality,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * these values are set when we find the quality for each variant
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * (see best_match()). source_quality is set from the 'qs' parameter
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * of the variant description or mime type: see set_mime_fields().
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz float lang_quality; /* quality of this variant's language */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar float source_quality; /* source quality for this variant */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* Now some special values */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz int lang_index; /* Index into LanguagePriority list */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe int is_pseudo_html; /* text/html, *or* the INCLUDES_MAGIC_TYPEs */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Above are all written-once properties of the variant. The
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * three fields below are changed during negotiation:
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* Something to carry around the state of negotiation (and to keep
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * all of this thread-safe)...
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowetypedef struct {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int accept_q; /* 1 if an Accept item has a q= param */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe float default_lang_quality; /* fiddle lang q for variants with no lang */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* the array pointers below are NULL if the corresponding accept
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * headers are not present
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_array_header_t *accept_encodings; /* accept_recs */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_array_header_t *accept_charsets; /* accept_recs */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_array_header_t *avail_vars; /* available variants */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int count_multiviews_variants; /* number of variants found on disk */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int is_transparent; /* 1 if this resource is trans. negotiable */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int dont_fiddle_headers; /* 1 if we may not fiddle with accept hdrs */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe int ua_supports_trans; /* 1 if ua supports trans negotiation */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int send_alternates; /* 1 if we want to send an Alternates header */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int may_choose; /* 1 if we may choose a variant for the client */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int use_rvsa; /* 1 if we must use RVSA/1.0 negotiation algo */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe/* A few functions to manipulate var_recs.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * Cleaning out the fields...
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp/* Initializing the relevant fields of a variant record from the
7a2edaa0193cbb0d79a65a8461a609a9402aea49brianp * accept_info read out of its content-type, one way or another.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowestatic void set_mime_fields(var_rec *var, accept_rec *mime_info)
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe var->is_pseudo_html = (!strcmp(var->mime_type, "text/html")
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* Create a variant list validator in r using info from vlistr. */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void set_vlist_validator(request_rec *r, request_rec *vlistr)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Calculating the variant list validator is similar to
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * calculating an etag for the source of the variant list
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * information, so we use ap_make_etag(). Note that this
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * validator can be 'weak' in extreme case.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* ap_set_etag will later take r->vlist_validator into account
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * when creating the etag header
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/*****************************************************************
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Parsing (lists of) media types and their parameters, as seen in
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * HTTPD header lines and elsewhere.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Get a single mime type entry --- one media type and parameters;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * enter the values we recognize into the argument accept_rec
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic const char *get_entry(apr_pool_t *p, accept_rec *result,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb const char *accept_line)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * Note that this handles what I gather is the "old format",
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * without any compatibility kludges --- if the token after the
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * MIME type begins with a semicolon, we know we're looking at parms,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * otherwise, we know we aren't. (So why all the pissing and moaning
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * in the CERN server code? I must be missing something).
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_str_tolower(result->name); /* You want case insensitive,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * you'll *get* case insensitive.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* KLUDGE!!! Default HTML to level 2.0 unless the browser
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * *explicitly* says something else.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (!strcmp(result->name, "text/html") && (result->level == 0.0)) {
11fb2f3611e6ff9a541e10b13e3108934f828141gregames else if (!strcmp(result->name, INCLUDES_MAGIC_TYPE)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe else if (!strcmp(result->name, INCLUDES_MAGIC_TYPE3)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Parameters ... */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Look for 'var = value' --- and make sure the var is in lcase. */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar for (cp = parm; (*cp && !apr_isspace(*cp) && *cp != '='); ++cp) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe continue; /* No '='; just ignore it. */
8aefbd756763807188d2e3ce336a8680e4893066wrowe (*end && *end != '\n' && *end != '\r' && *end != '\"');
8aefbd756763807188d2e3ce336a8680e4893066wrowe && (parm[1] == '\0' || (parm[1] == 's' && parm[2] == '\0'))) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe else if (parm[0] == 'l' && !strcmp(&parm[1], "evel")) {
23c6309e36a63b13b61c35999c978017521993d6wrowe/*****************************************************************
23c6309e36a63b13b61c35999c978017521993d6wrowe * Dealing with header lines ...
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Accept, Accept-Charset, Accept-Language and Accept-Encoding
8aefbd756763807188d2e3ce336a8680e4893066wrowe * are handled by do_header_line() - they all have the same
8aefbd756763807188d2e3ce336a8680e4893066wrowe * basic structure of a list of items of the format
cf6ef072483172309861d06e85b1aeff4573c060wrowe * name; q=N; charset=TEXT
cf6ef072483172309861d06e85b1aeff4573c060wrowe * where charset is only valid in Accept.
8aefbd756763807188d2e3ce336a8680e4893066wrowestatic apr_array_header_t *do_header_line(apr_pool_t *p, const char *accept_line)
8aefbd756763807188d2e3ce336a8680e4893066wrowe accept_recs = apr_array_make(p, 40, sizeof(accept_rec));
cf6ef072483172309861d06e85b1aeff4573c060wrowe accept_rec *new = (accept_rec *) apr_array_push(accept_recs);
cf6ef072483172309861d06e85b1aeff4573c060wrowe/* Given the text of the Content-Languages: line from the var map file,
cf6ef072483172309861d06e85b1aeff4573c060wrowe * return an array containing the languages of this variant
cf6ef072483172309861d06e85b1aeff4573c060wrowestatic apr_array_header_t *do_languages_line(apr_pool_t *p, const char **lang_line)
cf6ef072483172309861d06e85b1aeff4573c060wrowe apr_array_header_t *lang_recs = apr_array_make(p, 2, sizeof(char *));
23c6309e36a63b13b61c35999c978017521993d6wrowe/*****************************************************************
23c6309e36a63b13b61c35999c978017521993d6wrowe * Handling header lines from clients...
c2cf53a40a9814eb91db2cdf820f97d943f21628coarstatic negotiation_state *parse_accept_headers(request_rec *r)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe (negotiation_state *) apr_pcalloc(r->pool, sizeof(negotiation_state));
a9a4544168a37b43bd180b3703ccee995f27a80awrowe new->conf = (neg_dir_config *)ap_get_module_config(r->per_dir_config,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar new->dir_name = ap_make_dirstr_parent(r->pool, r->filename);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar new->accepts = do_header_line(r->pool, apr_table_get(hdrs, "Accept"));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* calculate new->accept_q value */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe do_header_line(r->pool, apr_table_get(hdrs, "Accept-Encoding"));
cf6ef072483172309861d06e85b1aeff4573c060wrowe do_header_line(r->pool, apr_table_get(hdrs, "Accept-Language"));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe do_header_line(r->pool, apr_table_get(hdrs, "Accept-Charset"));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* This is possibly overkill for some servers, heck, we have
8aefbd756763807188d2e3ce336a8680e4893066wrowe * only 33 index.html variants in docs/docroot (today).
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * Make this configurable?
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz new->avail_vars = apr_array_make(r->pool, 40, sizeof(var_rec));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic void parse_negotiate_header(request_rec *r, negotiation_state *neg)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe const char *negotiate = apr_table_get(r->headers_in, "Negotiate");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* First, default to no TCN, no Alternates, and the original Apache
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * negotiation algorithm with fiddles for broken browser configs.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * To save network bandwidth, we do not configure to send an
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Alternates header to the user agent by default. User
8aefbd756763807188d2e3ce336a8680e4893066wrowe * agents that want an Alternates header for agent-driven
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * negotiation will have to request it by sending an
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * appropriate Negotiate header.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* Lynx 2.7 and 2.8 send 'negotiate: trans' even though they
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * do not support transparent content negotiation, so for Lynx we
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * ignore the negotiate header when its contents are exactly "trans".
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * If future versions of Lynx ever need to say 'negotiate: trans',
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * they can send the equivalent 'negotiate: trans, trans' instead
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * to avoid triggering the workaround below.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe const char *ua = apr_table_get(r->headers_in, "User-Agent");
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe neg->may_choose = 0; /* An empty Negotiate would require 300 response */
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe while ((tok = ap_get_list_item(neg->pool, &negotiate)) != NULL) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* The user agent supports transparent negotiation */
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* Send-alternates could be configurable, but note
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * that it must be 1 if we have 'vlist' in the
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * negotiate header.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* we may use the RVSA/1.0 algorithm, configure for it */
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* we may use any variant selection algorithm, configure
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * to use the Apache algorithm
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* We disable header fiddles on the assumption that a
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * client sending Negotiate knows how to send correct
cf6ef072483172309861d06e85b1aeff4573c060wrowe * headers which don't need fiddling.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe "dont_fiddle_headers=%d use_rvsa=%d ua_supports_trans=%d "
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe "send_alternates=%d, may_choose=%d",
38bcc87d9a06e8ba81165421403f275eca4e313btrawick neg->ua_supports_trans, neg->send_alternates, neg->may_choose);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe/* Sometimes clients will give us no Accept info at all; this routine sets
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * up the standard default for that case, and also arranges for us to be
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * willing to run a CGI script if we find one. (In fact, we set up to
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * dramatically prefer CGI scripts in cases where that's appropriate,
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * e.g., POST or when URI includes query args or extra path info).
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowestatic void maybe_add_default_accepts(negotiation_state *neg,
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz neg->accepts = apr_array_make(neg->pool, 4, sizeof(accept_rec));
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz new_accept = (accept_rec *) apr_array_push(neg->accepts);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe new_accept = (accept_rec *) apr_array_push(neg->accepts);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz new_accept->quality = prefer_scripts ? 2.0f : 0.001f;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe/*****************************************************************
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Parsing type-map files, in Roy's meta/http format augmented with
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * #-comments.
4fca95918a9c0ae93593806544b425d0adc2fcc3wrowe/* Reading RFC822-style header lines, ignoring #-comments and
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * handling continuations.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic enum header_state get_header_line(char *buffer, int len, apr_file_t *map)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Get a noncommented line */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (apr_file_gets(buffer, MAX_STRING_LEN, map) != APR_SUCCESS) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* If blank, just return it --- this ends information on this variant */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* If non-blank, go looking for header lines, but note that we still
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * have to treat comments specially...
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* We need to shortcut the rest of this block following the Body:
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * tag - we will not look for continutation after this line.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (c == '#') {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Comment line */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (apr_file_getc(&c, map) != APR_EOF && c != '\n') {
8aefbd756763807188d2e3ce336a8680e4893066wrowe else if (apr_isspace(c)) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Leading whitespace. POSSIBLE continuation line
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * Also, possibly blank --- if so, we ungetc() the final newline
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * so that we will pick up the blank line the next time 'round.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (c == '\n') {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* Continuation */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (cp < buf_end - 2 && (apr_file_getc(&c, map)) != APR_EOF && c != '\n') {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* Line beginning with something other than whitespace */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic apr_off_t get_body(char *buffer, apr_size_t *len, const char *tag,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* We are at the first character following a body:tag\n entry
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Suck in the body, then backspace to the first char after the
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * closing tag entry. If we fail to read, find the tag or back
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * up then we have a hosed file, so give up already
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar return -1;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Skip all the trailing cruft after the end tag to the next line */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (apr_file_seek(map, APR_CUR, &pos) != APR_SUCCESS) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Give the caller back the actual body's file offset and length */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe/* Stripping out RFC822 comments */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Hmmm... is this correct? In Roy's latest draft, (comments) can nest! */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Nope, it isn't correct. Fails to handle backslash escape as well. */
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe/* Getting to a header body from the header */
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowestatic char *lcase_header_name_return_body(char *header, request_rec *r)
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "Syntax error in type map, no ':' in %s for header %s",
8aefbd756763807188d2e3ce336a8680e4893066wrowe "Syntax error in type map --- no header body: %s for %s",
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantzstatic int read_type_map(apr_file_t **map, negotiation_state *neg, request_rec *rr)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* We are not using multiviews */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if ((status = apr_file_open(map, rr->filename, APR_READ | APR_BUFFERED,
cf6ef072483172309861d06e85b1aeff4573c060wrowe hstate = get_header_line(buffer, MAX_STRING_LEN, *map);
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe char *body1 = lcase_header_name_return_body(buffer, neg->r);
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe const char *body;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe mime_info.file_name = ap_get_token(neg->pool, &body, 0);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe mime_info.content_languages = do_languages_line(neg->pool,
cf6ef072483172309861d06e85b1aeff4573c060wrowe mime_info.content_encoding = ap_get_token(neg->pool, &body, 0);
cadddb2c31d24d48f4017db4df0a29687432326cwrowe if ((mime_info.body = get_body(buffer, &len, tag, *map)) < 0) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Syntax error in type map, no end tag '%s'"
cf6ef072483172309861d06e85b1aeff4573c060wrowe "found in %s for Body: content.",
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe memcpy(new_var, (void *) &mime_info, sizeof(var_rec));
8aefbd756763807188d2e3ce336a8680e4893066wrowe/* Sort function used by read_types_multi. */
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* First key is the source quality, sort in descending order. */
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* XXX: note that we currently implement no method of setting the
cf6ef072483172309861d06e85b1aeff4573c060wrowe * source quality for multiviews variants, so we are always comparing
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe * 1.0 to 1.0 for now
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* Second key is the variant name */
cf6ef072483172309861d06e85b1aeff4573c060wrowe/*****************************************************************
cf6ef072483172309861d06e85b1aeff4573c060wrowe * Same as read_type_map, except we use a filtered directory listing
cf6ef072483172309861d06e85b1aeff4573c060wrowe * as the map...
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* XXX this should be more general, and quit using 'specials' */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if ((status = apr_dir_open(&dirp, neg->dir_name, neg->pool)) != APR_SUCCESS) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "cannot read directory for multi: %s", neg->dir_name);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Do we have a match? */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Don't negotiate directories and other unusual files
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * Really shouldn't see anything but DIR/LNK/REG here,
8aefbd756763807188d2e3ce336a8680e4893066wrowe * and we aught to discover if the LNK was interesting.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * Of course, this only helps platforms that capture the
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * the filetype in apr_dir_read(), which most can once
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * they are optimized with some magic [it's known to the
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * dirent, not associated to the inode, on most FS's.]
8aefbd756763807188d2e3ce336a8680e4893066wrowe if ((dirent.valid & APR_FINFO_TYPE) && (dirent.filetype == APR_DIR))
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Ok, something's here. Maybe nothing useful. Remember that
8aefbd756763807188d2e3ce336a8680e4893066wrowe * we tried, if we completely fail, so we can reject the request!
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* See if it's something which we have access to, and which
8aefbd756763807188d2e3ce336a8680e4893066wrowe * has a known type and encoding (as opposed to something
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * which we'll be slapping default_type on later).
8aefbd756763807188d2e3ce336a8680e4893066wrowe sub_req = ap_sub_req_lookup_dirent(&dirent, r, AP_SUBREQ_MERGE_ARGS, NULL);
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Double check, we still don't multi-resolve non-ordinary files
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe /* If it has a handler, we'll pretend it's a CGI script,
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * since that's a good indication of the sort of thing it
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * might be doing.
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * mod_mime will _always_ provide us the base name in the
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * ap-mime-exception-list, if it processed anything. If
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * this list is empty, give up immediately, there was
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * nothing interesting. For example, looking at the files
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * readme.txt and readme.foo, we will throw away .foo if
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * it's an insignificant file (e.g. did not identify a
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * language, charset, encoding, content type or handler,)
0540a0b469147b52e858587270dba31c2aaa9e09wrowe "ap-mime-exceptions-list");
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Each unregonized bit better match our base name, in sequence.
8aefbd756763807188d2e3ce336a8680e4893066wrowe * A test of index.html.foo will match index.foo or index.html.foo,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * but it will never transpose the segments and allow index.foo.html
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe * because that would introduce too much CPU consumption. Better that
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * we don't attempt a many-to-many match here.
948096a99010fccf648814fecf38f75c689172d7wrowe /* Something you don't know is, something you don't know...
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * ###: be warned, the _default_ content type is already
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * picked up here! If we failed the subrequest, or don't
948096a99010fccf648814fecf38f75c689172d7wrowe * know what we are serving, then continue.
948096a99010fccf648814fecf38f75c689172d7wrowe if (sub_req->status != HTTP_OK || (!sub_req->content_type)) {
a9a4544168a37b43bd180b3703ccee995f27a80awrowe /* If it's a map file, we use that instead of the map
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * we're building...
a9a4544168a37b43bd180b3703ccee995f27a80awrowe !strcmp(sub_req->content_type, MAP_FILE_MAGIC_TYPE)) ||
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Have reasonable variant --- gather notes. */
948096a99010fccf648814fecf38f75c689172d7wrowe mime_info.file_name = apr_pstrdup(neg->pool, dirent.name);
948096a99010fccf648814fecf38f75c689172d7wrowe mime_info.content_encoding = sub_req->content_encoding;
053497224246c4dbef9af594cacf5c00ed271e6cwrowe mime_info.content_languages = sub_req->content_languages;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz get_entry(neg->pool, &accept_info, sub_req->content_type);
053497224246c4dbef9af594cacf5c00ed271e6cwrowe memcpy(new_var, (void *) &mime_info, sizeof(var_rec));
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* We found some file names that matched. None could be served.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * Rather than fall out to autoindex or some other mapper, this
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * request must die.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Negotiation: discovered file(s) matching request: %s"
948096a99010fccf648814fecf38f75c689172d7wrowe " (None could be negotiated).",
948096a99010fccf648814fecf38f75c689172d7wrowe /* Sort the variants into a canonical order. The negotiation
948096a99010fccf648814fecf38f75c689172d7wrowe * result sometimes depends on the order of the variants. By
948096a99010fccf648814fecf38f75c689172d7wrowe * sorting the variants into a canonical order, rather than using
948096a99010fccf648814fecf38f75c689172d7wrowe * the order in which readdir() happens to return them, we ensure
948096a99010fccf648814fecf38f75c689172d7wrowe * that the negotiation result will be consistent over filesystem
948096a99010fccf648814fecf38f75c689172d7wrowe * backup/restores and over all mirror sites.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb qsort((void *) neg->avail_vars->elts, neg->avail_vars->nelts,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar sizeof(var_rec), (int (*)(const void *, const void *)) variantsortf);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe/*****************************************************************
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * And now for the code you've been waiting for... actually
696218c49632c863d18b25fa52ab63617088cb38wrowe * finding a match to the client's requirements.
948096a99010fccf648814fecf38f75c689172d7wrowe/* Matching MIME types ... the star/star and foo/star commenting conventions
948096a99010fccf648814fecf38f75c689172d7wrowe * are implemented here. (You know what I mean by star/star, but just
948096a99010fccf648814fecf38f75c689172d7wrowe * try mentioning those three characters in a C comment). Using strcmp()
948096a99010fccf648814fecf38f75c689172d7wrowe * is legit, because everything has already been smashed to lowercase.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * Note also that if we get an exact match on the media type, we update
948096a99010fccf648814fecf38f75c689172d7wrowe * level_matched for use in level_cmp below...
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * We also give a value for mime_stars, which is used later. It should
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * be 1 for star/star, 2 for type/star and 3 for type/subtype.
a9a4544168a37b43bd180b3703ccee995f27a80awrowestatic int mime_match(accept_rec *accept_r, var_rec *avail)
053497224246c4dbef9af594cacf5c00ed271e6cwrowe if (accept_type[0] == '*') { /* Anything matches star/star */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe/* This code implements a piece of the tie-breaking algorithm between
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * variants of equal quality. This piece is the treatment of variants
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * of the same base media type, but different levels. What we want to
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * return is the variant at the highest level that the client explicitly
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * claimed to accept.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * If all the variants available are at a higher level than that, or if
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * the client didn't say anything specific about this media type at all
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * and these variants just got in on a wildcard, we prefer the lowest
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * level, on grounds that that's the one that the client is least likely
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * to choke on.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * (This is all motivated by treatment of levels in HTML --- we only
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * want to give level 3 to browsers that explicitly ask for it; browsers
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * that don't, including HTTP/0.9 browsers that only get the implicit
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * "Accept: * / *" [space added to avoid confusing cpp --- no, that
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * syntax doesn't really work] should get HTML2 if available).
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * (Note that this code only comes into play when we are choosing among
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * variants of equal quality, where the draft standard gives us a fair
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * bit of leeway about what to do. It ain't specified by the standard;
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * rather, it is a choice made by this server about what to do in cases
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * where the standard does not specify a unique course of action).
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Levels are only comparable between matching media types */
a9a4544168a37b43bd180b3703ccee995f27a80awrowe if (!var1->is_pseudo_html && strcmp(var1->mime_type, var2->mime_type)) {
a9a4544168a37b43bd180b3703ccee995f27a80awrowe /* The result of the above if statements is that, if we get to
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * here, both variants have the same mime_type or both are
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * pseudo-html.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Take highest level that matched, if either did match. */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Neither matched. Take lowest level, if there's a difference. */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar/* Finding languages. The main entry point is set_language_quality()
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * which is called for each variant. It sets two elements in the
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * variant record:
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * language_quality - the 'q' value of the 'best' matching language
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * from Accept-Language: header (HTTP/1.1)
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * lang_index - Non-negotiated language priority, using
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * position of language on the Accept-Language:
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * header, if present, else LanguagePriority
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * directive order.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * When we do the variant checking for best variant, we use language
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * quality first, and if a tie, language_index next (this only applies
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * when _not_ using the RVSA/1.0 algorithm). If using the RVSA/1.0
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * algorithm, lang_index is never used.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * set_language_quality() calls find_lang_index() and find_default_index()
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * to set lang_index.
aa047239dedf0d26e8efecfade32e7337f35df19wrowestatic int find_lang_index(apr_array_header_t *accept_langs, char *lang)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb const char **alang;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar/* set_default_lang_quality() sets the quality we apply to variants
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * which have no language assigned to them. If none of the variants
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe * have a language, we are not negotiating on language, so all are
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * acceptable, and we set the default q value to 1.0. However if
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * some of the variants have languages, we set this default to 0.0001.
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * The value of this default will be applied to all variants with
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * no explicit language -- which will have the effect of making them
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * acceptable, but only if no variants with an explicit language
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe * are acceptable. The default q value set here is assigned to variants
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * with no language type in set_language_quality().
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * Note that if using the RVSA/1.0 algorithm, we don't use this
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowestatic void set_default_lang_quality(negotiation_state *neg)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb var_rec *avail_recs = (var_rec *) neg->avail_vars->elts;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* Set the language_quality value in the variant record. Also
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe * assigns lang_index for ForceLanguagePriority.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * To find the language_quality value, we look for the 'q' value
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * of the 'best' matching language on the Accept-Language
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe * header. The 'best' match is the language on Accept-Language
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * header which matches the language of this variant either fully,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * or as far as the prefix marker (-). If two or more languages
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe * match, use the longest string from the Accept-Language header
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe * (see HTTP/1.1 [14.4])
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * When a variant has multiple languages, we find the 'best'
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * match for each variant language tag as above, then select the
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * one with the highest q value. Because both the accept-header
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * and variant can have multiple languages, we now have a hairy
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * loop-within-a-loop here.
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * If the variant has no language and we have no Accept-Language
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * items, leave the quality at 1.0 and return.
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * If the variant has no language, we use the default as set by
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * set_default_lang_quality() (1.0 if we are not negotiating on
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * language, 0.001 if we are).
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * Following the setting of the language quality, we drop through to
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * set the old 'lang_index'. This is set based on either the order
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * of the languages on the Accept-Language header, or the
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * order on the LanguagePriority directive. This is only used
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * in the negotiation if the language qualities tie.
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowestatic void set_language_quality(negotiation_state *neg, var_rec *variant)
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe if (!variant->content_languages || !variant->content_languages->nelts) {
3ed53b42bfd638a63919b6cf2c2009a5a148f066brianp /* This variant has no content-language, so use the default
3ed53b42bfd638a63919b6cf2c2009a5a148f066brianp * quality factor for variants with no content-language
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * (previously set by set_default_lang_quality()).
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * Leave the factor alone (it remains at 1.0) when we may not fiddle
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * with the headers.
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe return; /* no accept-language header */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Variant has one (or more) languages. Look for the best
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * match. We do this by going through each language on the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * variant description looking for a match on the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Accept-Language header. The best match is the longest
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * matching language on the header. The final result is the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * best q value from all the languages on the variant
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * description.
68b29bcadd6c46aecdc9fe14c93555a2238ad2aagregames /* no accept-language header makes the variant indefinite */
68b29bcadd6c46aecdc9fe14c93555a2238ad2aagregames else { /* There is an accept-language with 0 or more items */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb accept_rec *accs = (accept_rec *) neg->accept_langs->elts;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *lang, *p;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb for (j = 0; j < variant->content_languages->nelts; ++j) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* lang is the variant's language-tag, which is the one
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * we are allowed to use the prefix of in HTTP/1.1
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar lang = ((char **) (variant->content_languages->elts))[j];
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* now find the best (i.e. longest) matching
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * Accept-Language header language. We put the best match
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * for this tag in bestthistag. We cannot update the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * overall best (based on q value) because the best match
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * for this tag is the longest language item on the accept
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * header, not necessarily the highest q.
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe /* Find language. We match if either the variant
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe * language tag exactly matches the language range
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe * from the accept header, or a prefix of the variant
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb * language tag up to a '-' character matches the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * whole of the language range in the Accept-Language
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * header. Note that HTTP/1.x allows any number of
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * '-' characters in a tag or range, currently only
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * tags with zero or one '-' characters are defined
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * for general use (see rfc1766).
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * We only use language range in the Accept-Language
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * header the best match for the variant language tag
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if it is longer than the previous best match.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* The next bit is a fiddle. Some browsers might
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * be configured to send more specific language
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ranges than desirable. For example, an
efa1a34b0a7785fc72863eff175b0cfc1ecb0e38wrowe * Accept-Language of en-US should never match
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * variants with languages en or en-GB. But US
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe * English speakers might pick en-US as their
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * language choice. So this fiddle checks if the
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * language range has a prefix, and if so, it
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * matches variants which match that prefix with a
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * priority of 0.001. So a request for en-US would
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * match variants of types en and en-GB, but at
6c24fd6cfe148639988d5b335185ffb215662801wrowe * much lower priority than matches of en-US
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * directly, or of any other language listed on
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe * the Accept-Language header. Note that this
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * fiddle does not handle multi-level prefixes.
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames /* Finished looking at Accept-Language headers, the best
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames * (longest) match is in bestthistag, or NULL if no match
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe (bestthistag && bestthistag->quality > best->quality)) {
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe /* See if the tag matches on a * in the Accept-Language
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe * header. If so, record this fact for later use
117026201e6d8fe7d82416b8a7324830f5a87292wrowe /* If one of the language tags of the variant matched on *, we
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * need to see if its q is better than that of any non-* match
e7505ba54ac56ae30e4e250f912f3dbaf92ca45fwrowe * on any other tag of the variant. If so the * match takes
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * precedence and the overall match is not definite.
efa1a34b0a7785fc72863eff175b0cfc1ecb0e38wrowe variant->lang_quality = best ? best->quality : fiddle_q;
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe /* Handle the ForceDefaultLanguage overrides, based on the best match
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe * to LanguagePriority order. The best match is the lowest index of
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe * any LanguagePriority match.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar for (j = 0; j < variant->content_languages->nelts; ++j)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* lang is the variant's language-tag, which is the one
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * we are allowed to use the prefix of in HTTP/1.1
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar char *lang = ((char **) (variant->content_languages->elts))[j];
117026201e6d8fe7d82416b8a7324830f5a87292wrowe /* If we wish to fallback or
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * we use our own LanguagePriority index.
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe idx = find_lang_index(neg->conf->language_priority, lang);
cadddb2c31d24d48f4017db4df0a29687432326cwrowe if ((idx >= 0) && ((bestidx == -1) || (idx < bestidx))) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar if (bestidx >= 0) {
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe/* Determining the content length --- if the map didn't tell us,
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe * we have to do a stat() and remember for next time.
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowestatic apr_off_t find_content_length(negotiation_state *neg, var_rec *variant)
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe if (variant->sub_req && (variant->sub_req->finfo.valid & APR_FINFO_SIZE)) {
117026201e6d8fe7d82416b8a7324830f5a87292wrowe char *fullname = ap_make_full_path(neg->pool, neg->dir_name,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* For a given variant, find the best matching Accept: header
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe * and assign the Accept: header's quality value to the
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe * mime_type_quality field of the variant, for later use in
ecc4a080f07af3fbc1b91bbd00997ec1d592c6f9wrowe * determining the best matching variant.
2d2dadb81bf34e3bc9321eabcd971a738431b364wrowestatic void set_accept_quality(negotiation_state *neg, var_rec *variant)
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe float q = 0.0f;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* if no Accept: header, leave quality alone (will
ecc4a080f07af3fbc1b91bbd00997ec1d592c6f9wrowe * remain at the default value of 1)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * XXX: This if is currently never true because of the effect of
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * maybe_add_default_accepts().
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * Go through each of the ranges on the Accept: header,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * looking for the 'best' match with this variant's
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * content-type. We use the best match's quality
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * value (from the Accept: header) for this variant's
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * mime_type_quality field.
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * The best match is determined like this:
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * type/type is better than type/ * is better than * / *
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * if match is type/type, use the level mime param if available
0540a0b469147b52e858587270dba31c2aaa9e09wrowe continue; /* didn't match the content type at all */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* did match - see if there were less or more stars than
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * in previous match
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb continue; /* more stars => not as good a match */
5b3abd2fecc712f08ad728114aa77137b9f67716wrowe /* If we are allowed to mess with the q-values
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * and have no explicit q= parameters in the accept header,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * make wildcards very low, so we have a low chance
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * of ending up with them if there's something better.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb q = 0.01f;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe else if (!neg->dont_fiddle_headers && !neg->accept_q &&
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb q = 0.02f;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* For a given variant, find the 'q' value of the charset given
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * on the Accept-Charset line. If no charsets are listed,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * assume value of '1'.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void set_charset_quality(negotiation_state *neg, var_rec *variant)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* if no Accept-Charset: header, leave quality alone (will
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * remain at the default value of 1)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb accept_recs = (accept_rec *) neg->accept_charsets->elts;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Charset of variant not known */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* if not a text / * type, leave quality alone */
if (star) {
if (star) {
enum algorithm_results {
#ifdef NEG_DEBUG
if (q > bestq) {
*p_bestq = q;
if (q == bestq) {
*p_bestq = q;
int levcmp;
#ifdef NEG_DEBUG
*p_bestq = q;
*p_bestq = q;
*p_bestq = q;
*p_bestq = q;
*p_bestq = q;
*p_bestq = q;
*p_bestq = q;
return algorithm_result;
* example if we have 3 variants x.html, x.ps.en and x.ps.nl, and if
int alg_result)
char *lang;
char *qstr;
char *lenstr;
int vary_by_type = 0;
int vary_by_language = 0;
int vary_by_charset = 0;
int vary_by_encoding = 0;
if (first_variant) {
if (!vary_by_type &&
if (!vary_by_charset &&
if (!vary_by_language &&
if (!vary_by_encoding &&
first_variant = 0;
if (lang) {
const char *sub_vary;
int status;
return status;
return HTTP_VARIANT_ALSO_VARIES;
return HTTP_VARIANT_ALSO_VARIES;
int res;
return HTTP_MULTIPLE_CHOICES;
if (!*bestp) {
return HTTP_NOT_ACCEPTABLE;
* Manual setting of cache-control/expires always overrides this
return OK;
int res;
char *udir;
return DECLINED;
return res;
apr_bucket *e;
return res;
return HTTP_METHOD_NOT_ALLOWED;
return res;
c->bucket_alloc);
return OK;
int res;
return DECLINED;
return res;
return DECLINED;
if (res != 0)
goto return_from_multi;
goto return_from_multi;
r->mtime = 0;
return OK;
return DECLINED;
return DECLINED;
return OK;
if (x_enc) {
return OK;
return DECLINED;