mod_negotiation.c revision 68f0e394246eba4bee73068632a54c9e29b003d3
97a9a944b5887e91042b019776c41d5dd74557aferikabele/* ====================================================================
97a9a944b5887e91042b019776c41d5dd74557aferikabele * The Apache Software License, Version 1.1
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd *
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * reserved.
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive *
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * Redistribution and use in source and binary forms, with or without
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * modification, are permitted provided that the following conditions
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * are met:
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc *
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * 1. Redistributions of source code must retain the above copyright
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * notice, this list of conditions and the following disclaimer.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen *
2e545ce2450a9953665f701bb05350f0d3f26275nd * 2. Redistributions in binary form must reproduce the above copyright
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * notice, this list of conditions and the following disclaimer in
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * the documentation and/or other materials provided with the
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * distribution.
fe64b2ba25510d8c9dba5560a2d537763566cf40nd *
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * 3. The end-user documentation included with the redistribution,
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen * if any, must include the following acknowledgment:
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * "This product includes software developed by the
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * Apache Software Foundation (http://www.apache.org/)."
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * Alternately, this acknowledgment may appear in the software itself,
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * if and wherever such third-party acknowledgments normally appear.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd *
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * 4. The names "Apache" and "Apache Software Foundation" must
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * not be used to endorse or promote products derived from this
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * software without prior written permission. For written
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * permission, please contact apache@apache.org.
3e6b8c7840a46cdccd46b7a4b1902c2bc82f6cc2gryzor *
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd * 5. Products derived from this software may not be called "Apache",
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * nor may "Apache" appear in their name, without prior written
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * permission of the Apache Software Foundation.
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin *
f3f945b6f394da4d080082fae12da22b4989914and * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
cf663b70eeea0b51582be629e148a697aa2a116eminfrin * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
cf663b70eeea0b51582be629e148a697aa2a116eminfrin * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
8e0c9984e1432c934dacca53efc92cde30d0fe53rbowen * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
cf663b70eeea0b51582be629e148a697aa2a116eminfrin * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cf663b70eeea0b51582be629e148a697aa2a116eminfrin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1acae7fb634ac2b01682507175e072b168995e0dcolm * SUCH DAMAGE.
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * ====================================================================
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin *
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * This software consists of voluntary contributions made by many
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * individuals on behalf of the Apache Software Foundation. For more
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * information on the Apache Software Foundation, please see
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * <http://www.apache.org/>.
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin *
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * Portions of this software are based upon public domain software
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * originally written at the National Center for Supercomputing Applications,
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * University of Illinois, Urbana-Champaign.
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin/*
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * mod_negotiation.c: keeps track of MIME types the client is willing to
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * accept, and contains code to handle type arbitration.
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin *
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * rst
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "apr.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "apr_strings.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "apr_file_io.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "apr_lib.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#define APR_WANT_STRFUNC
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "apr_want.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "ap_config.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "httpd.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "http_config.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "http_request.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "http_protocol.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "http_core.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "http_log.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#include "util_script.h"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#define MAP_FILE_MAGIC_TYPE "application/x-type-map"
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin/* Commands --- configuring document caching on a per (virtual?)
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin * server basis...
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrintypedef struct {
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin int forcelangpriority;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin apr_array_header_t *language_priority;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin} neg_dir_config;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#define FLP_UNDEF 0 /* Same as FLP_NONE, but base overrides */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#define FLP_NONE 1 /* Return 406, HTTP_NOT_ACCEPTABLE */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#define FLP_PREFIX 2 /* Try xx(-.*) from language_priority */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin#define FLP_ANY 4 /* Try anything in language_priority */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin /* If both, tries FLP_PREFIX then FLP_ANY */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrinmodule AP_MODULE_DECLARE_DATA negotiation_module;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrinstatic void *create_neg_dir_config(apr_pool_t *p, char *dummy)
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin{
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin neg_dir_config *new = (neg_dir_config *) apr_palloc(p, sizeof(neg_dir_config));
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin new->forcelangpriority = FLP_UNDEF;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin new->language_priority = apr_array_make(p, 4, sizeof(char *));
5df82feb5bbe57f91f774801e1118edb5e2c87d7jailletc return new;
06ba4a61654b3763ad65f52283832ebf058fdf1cslive}
06ba4a61654b3763ad65f52283832ebf058fdf1cslive
78f97ce162b66a0dbfd7af4dcd9984f162569b04minfrinstatic void *merge_neg_dir_configs(apr_pool_t *p, void *basev, void *addv)
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin{
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin neg_dir_config *base = (neg_dir_config *) basev;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin neg_dir_config *add = (neg_dir_config *) addv;
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin neg_dir_config *new = (neg_dir_config *) apr_palloc(p, sizeof(neg_dir_config));
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin /* give priority to the config in the subdirectory */
4624ec7c743a08f22e90521b97d612c9499ae7efminfrin new->forcelangpriority = add->forcelangpriority ? add->forcelangpriority
0734f81384d5f2528bb119f89d135043a280d5b2minfrin : base->forcelangpriority;
0734f81384d5f2528bb119f89d135043a280d5b2minfrin new->language_priority = apr_array_append(p, add->language_priority,
0734f81384d5f2528bb119f89d135043a280d5b2minfrin base->language_priority);
0734f81384d5f2528bb119f89d135043a280d5b2minfrin return new;
0734f81384d5f2528bb119f89d135043a280d5b2minfrin}
0734f81384d5f2528bb119f89d135043a280d5b2minfrin
06ba4a61654b3763ad65f52283832ebf058fdf1cslivestatic const char *set_language_priority(cmd_parms *cmd, void *n,
2aff288113d772cedca6add888eb643afffe9fb1nd const char *lang)
b536777217c919cacb1a6f18eaeff001f3b3f13crbowen{
b536777217c919cacb1a6f18eaeff001f3b3f13crbowen apr_array_header_t *arr = ((neg_dir_config *) n)->language_priority;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd const char **langp = (const char **) apr_array_push(arr);
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri *langp = lang;
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri return NULL;
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri}
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeristatic const char *set_force_priority(cmd_parms *cmd, void *n_, const char *w)
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri{
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri neg_dir_config *n = n_;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd if (!strcasecmp(w, "None")) {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen n->forcelangpriority = FLP_NONE;
117c1f888a14e73cdd821dc6c23eb0411144a41cnd }
117c1f888a14e73cdd821dc6c23eb0411144a41cnd else if (!strcasecmp(w, "Prefix")) {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen n->forcelangpriority = FLP_PREFIX;
117c1f888a14e73cdd821dc6c23eb0411144a41cnd }
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak else if (!strcasecmp(w, "Any")) {
117c1f888a14e73cdd821dc6c23eb0411144a41cnd n->forcelangpriority = FLP_ANY;
90c99eca06310d91254dd3525e1f5e9fa612af71jim }
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd else if (!strcasecmp(w, "Full")) {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin n->forcelangpriority = FLP_PREFIX | FLP_ANY;
117c1f888a14e73cdd821dc6c23eb0411144a41cnd }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin else {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin return apr_pstrcat(cmd->pool, "Illegal ForceLanguagePriority option ", w, NULL);
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
117c1f888a14e73cdd821dc6c23eb0411144a41cnd
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem return NULL;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin}
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowenstatic const char *cache_negotiated_docs(cmd_parms *cmd, void *dummy,
cf2c758d9a7837808846a4bbf5611f268954c79dnd int arg)
cf2c758d9a7837808846a4bbf5611f268954c79dnd{
fe64b2ba25510d8c9dba5560a2d537763566cf40nd ap_set_module_config(cmd->server->module_config, &negotiation_module,
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeri (arg ? "Cache" : NULL));
b536777217c919cacb1a6f18eaeff001f3b3f13crbowen return NULL;
b536777217c919cacb1a6f18eaeff001f3b3f13crbowen}
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
fe64b2ba25510d8c9dba5560a2d537763566cf40ndstatic int do_cache_negotiated_docs(server_rec *s)
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar{
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar return (ap_get_module_config(s->module_config, &negotiation_module) != NULL);
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar}
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarstatic const command_rec negotiation_cmds[] =
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar{
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar AP_INIT_FLAG("CacheNegotiatedDocs", cache_negotiated_docs, NULL, RSRC_CONF,
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar "Either 'on' or 'off' (default)"),
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar AP_INIT_ITERATE("LanguagePriority", set_language_priority, NULL, OR_FILEINFO,
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar "space-delimited list of MIME language abbreviations"),
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar AP_INIT_TAKE1("ForceLanguagePriority", set_force_priority, NULL, OR_FILEINFO,
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar "One of 'none', 'prefix', 'any', or 'full'"),
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar {NULL}
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar};
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/*
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * Record of available info on a media type specified by the client
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * (we also use 'em for encodings and languages)
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coartypedef struct accept_rec {
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar char *name; /* MUST be lowercase */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float quality;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float level;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar char *charset; /* for content-type only */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar} accept_rec;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/*
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * Record of available info on a particular variant
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar *
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * Note that a few of these fields are updated by the actual negotiation
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * code. These are:
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar *
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * level_matched --- initialized to zero. Set to the value of level
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * if the client actually accepts this media type at that
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * level (and *not* if it got in on a wildcard). See level_cmp
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * below.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * mime_stars -- initialized to zero. Set to the number of stars
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * present in the best matching Accept header element.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * 1 for star/star, 2 for type/star and 3 for
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * type/subtype.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar *
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * definite -- initialized to 1. Set to 0 if there is a match which
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * makes the variant non-definite according to the rules
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * in rfc2296.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coartypedef struct var_rec {
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar request_rec *sub_req; /* May be NULL (is, for map files) */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar const char *mime_type; /* MUST be lowercase */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar const char *file_name; /* Set to 'this' (for map file body content) */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_off_t body; /* Only for map file body content */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar const char *content_encoding;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_array_header_t *content_languages; /* list of languages for this variant */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar const char *content_charset;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar const char *description;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar /* The next five items give the quality values for the dimensions
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * of negotiation for this variant. They are obtained from the
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * appropriate header lines, except for source_quality, which
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * is obtained from the variant itself (the 'qs' parameter value
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * from the variant's mime-type). Apart from source_quality,
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * these values are set when we find the quality for each variant
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * (see best_match()). source_quality is set from the 'qs' parameter
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * of the variant description or mime type: see set_mime_fields().
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float lang_quality; /* quality of this variant's language */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float encoding_quality; /* ditto encoding */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float charset_quality; /* ditto charset */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float mime_type_quality; /* ditto media type */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float source_quality; /* source quality for this variant */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar /* Now some special values */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float level; /* Auxiliary to content-type... */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_off_t bytes; /* content length, if known */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int lang_index; /* pre HTTP/1.1 language priority stuff */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int is_pseudo_html; /* text/html, *or* the INCLUDES_MAGIC_TYPEs */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar /* Above are all written-once properties of the variant. The
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * three fields below are changed during negotiation:
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float level_matched;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int mime_stars;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int definite;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar} var_rec;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/* Something to carry around the state of negotiation (and to keep
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * all of this thread-safe)...
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coartypedef struct {
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_pool_t *pool;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar request_rec *r;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar char *dir_name;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int accept_q; /* 1 if an Accept item has a q= param */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar float default_lang_quality; /* fiddle lang q for variants with no lang */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar /* the array pointers below are NULL if the corresponding accept
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * headers are not present
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_array_header_t *accepts; /* accept_recs */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_array_header_t *accept_encodings; /* accept_recs */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_array_header_t *accept_charsets; /* accept_recs */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_array_header_t *accept_langs; /* accept_recs */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar apr_array_header_t *avail_vars; /* available variants */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int count_multiviews_variants; /* number of variants found on disk */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int is_transparent; /* 1 if this resource is trans. negotiable */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int dont_fiddle_headers; /* 1 if we may not fiddle with accept hdrs */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int ua_supports_trans; /* 1 if ua supports trans negotiation */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int send_alternates; /* 1 if we want to send an Alternates header */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int may_choose; /* 1 if we may choose a variant for the client */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar int use_rvsa; /* 1 if we must use RVSA/1.0 negotiation algo */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar} negotiation_state;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/* A few functions to manipulate var_recs.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * Cleaning out the fields...
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarstatic void clean_var_rec(var_rec *mime_info)
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar{
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->sub_req = NULL;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->mime_type = "";
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->file_name = "";
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->body = 0;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->content_encoding = NULL;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->content_languages = NULL;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->content_charset = "";
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->description = "";
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->is_pseudo_html = 0;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->level = 0.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->level_matched = 0.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->bytes = -1;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->lang_index = -1;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->mime_stars = 0;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->definite = 1;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->charset_quality = 1.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->encoding_quality = 1.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->lang_quality = 1.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->mime_type_quality = 1.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar mime_info->source_quality = 0.0f;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar}
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/* Initializing the relevant fields of a variant record from the
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * accept_info read out of its content-type, one way or another.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarstatic void set_mime_fields(var_rec *var, accept_rec *mime_info)
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar{
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar var->mime_type = mime_info->name;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar var->source_quality = mime_info->quality;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar var->level = mime_info->level;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar var->content_charset = mime_info->charset;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar var->is_pseudo_html = (!strcmp(var->mime_type, "text/html")
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar || !strcmp(var->mime_type, INCLUDES_MAGIC_TYPE)
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar || !strcmp(var->mime_type, INCLUDES_MAGIC_TYPE3));
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar}
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/* Create a variant list validator in r using info from vlistr. */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarstatic void set_vlist_validator(request_rec *r, request_rec *vlistr)
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar{
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar /* Calculating the variant list validator is similar to
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * calculating an etag for the source of the variant list
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * information, so we use ap_make_etag(). Note that this
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * validator can be 'weak' in extreme case.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
aaed4ebc116f5862aff2610a809a9a9e9a28f50ecoar ap_update_mtime(vlistr, vlistr->finfo.mtime);
aaed4ebc116f5862aff2610a809a9a9e9a28f50ecoar r->vlist_validator = ap_make_etag(vlistr, 0);
aaed4ebc116f5862aff2610a809a9a9e9a28f50ecoar
aaed4ebc116f5862aff2610a809a9a9e9a28f50ecoar /* ap_set_etag will later take r->vlist_validator into account
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar * when creating the etag header
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar}
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar/*****************************************************************
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar *
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * Parsing (lists of) media types and their parameters, as seen in
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * HTTPD header lines and elsewhere.
fe64b2ba25510d8c9dba5560a2d537763566cf40nd */
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd/*
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin * Get a single mime type entry --- one media type and parameters;
f3f945b6f394da4d080082fae12da22b4989914and * enter the values we recognize into the argument accept_rec
fe64b2ba25510d8c9dba5560a2d537763566cf40nd */
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
9e16224e92151e6c142e63ec8e3d06591c61352dndstatic const char *get_entry(apr_pool_t *p, accept_rec *result,
9e16224e92151e6c142e63ec8e3d06591c61352dnd const char *accept_line)
9e16224e92151e6c142e63ec8e3d06591c61352dnd{
9e16224e92151e6c142e63ec8e3d06591c61352dnd result->quality = 1.0f;
2aff288113d772cedca6add888eb643afffe9fb1nd result->level = 0.0f;
20f499565e77defe9dab24dd85c02f38a1175855nd result->charset = "";
17ade6df5ec233536985eb1c130a906c725dd614humbedooh
fe64b2ba25510d8c9dba5560a2d537763566cf40nd /*
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * Note that this handles what I gather is the "old format",
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen *
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * Accept: text/html text/plain moo/zot
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen *
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * without any compatibility kludges --- if the token after the
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * MIME type begins with a semicolon, we know we're looking at parms,
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * otherwise, we know we aren't. (So why all the pissing and moaning
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * in the CERN server code? I must be missing something).
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen */
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen result->name = ap_get_token(p, &accept_line, 0);
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen ap_str_tolower(result->name); /* You want case insensitive,
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * you'll *get* case insensitive.
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen */
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf /* KLUDGE!!! Default HTML to level 2.0 unless the browser
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * *explicitly* says something else.
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen */
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen if (!strcmp(result->name, "text/html") && (result->level == 0.0)) {
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf result->level = 2.0f;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen }
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd else if (!strcmp(result->name, INCLUDES_MAGIC_TYPE)) {
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd result->level = 2.0f;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen }
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen else if (!strcmp(result->name, INCLUDES_MAGIC_TYPE3)) {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen result->level = 3.0f;
4aa603e6448b99f9371397d439795c91a93637eand }
4aa603e6448b99f9371397d439795c91a93637eand
17ade6df5ec233536985eb1c130a906c725dd614humbedooh while (*accept_line == ';') {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen /* Parameters ... */
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen char *parm;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen char *cp;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen char *end;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
fe64b2ba25510d8c9dba5560a2d537763566cf40nd ++accept_line;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd parm = ap_get_token(p, &accept_line, 1);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd /* Look for 'var = value' --- and make sure the var is in lcase. */
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
9ec258b293b038250ec81887da01318ccc350690minfrin for (cp = parm; (*cp && !apr_isspace(*cp) && *cp != '='); ++cp) {
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin *cp = apr_tolower(*cp);
f3f945b6f394da4d080082fae12da22b4989914and }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd if (!*cp) {
06ba4a61654b3763ad65f52283832ebf058fdf1cslive continue; /* No '='; just ignore it. */
2aff288113d772cedca6add888eb643afffe9fb1nd }
2aff288113d772cedca6add888eb643afffe9fb1nd
06ba4a61654b3763ad65f52283832ebf058fdf1cslive *cp++ = '\0'; /* Delimit var */
20f499565e77defe9dab24dd85c02f38a1175855nd while (*cp && (apr_isspace(*cp) || *cp == '=')) {
e487d6c09669296f94a5190cc34586a98e624a00nd ++cp;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
9ec258b293b038250ec81887da01318ccc350690minfrin
9ec258b293b038250ec81887da01318ccc350690minfrin if (*cp == '"') {
9ec258b293b038250ec81887da01318ccc350690minfrin ++cp;
9ec258b293b038250ec81887da01318ccc350690minfrin for (end = cp;
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar (*end && *end != '\n' && *end != '\r' && *end != '\"');
17ade6df5ec233536985eb1c130a906c725dd614humbedooh end++);
4aa603e6448b99f9371397d439795c91a93637eand }
e487d6c09669296f94a5190cc34586a98e624a00nd else {
9ec258b293b038250ec81887da01318ccc350690minfrin for (end = cp; (*end && !apr_isspace(*end)); end++);
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf }
dc4794add358378bce888ab64e20006c263f2651covener if (*end) {
b4f25203520c168e2df9f7d83024db09e68efe7ecovener *end = '\0'; /* strip ending quote or return */
dc4794add358378bce888ab64e20006c263f2651covener }
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener ap_str_tolower(cp);
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener if (parm[0] == 'q'
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener && (parm[1] == '\0' || (parm[1] == 's' && parm[2] == '\0'))) {
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener result->quality = (float)atof(cp);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd else if (parm[0] == 'l' && !strcmp(&parm[1], "evel")) {
fe64b2ba25510d8c9dba5560a2d537763566cf40nd result->level = (float)atof(cp);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd else if (!strcmp(parm, "charset")) {
fe64b2ba25510d8c9dba5560a2d537763566cf40nd result->charset = cp;
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin }
7859c11126b92a9ff7ca9ebf9ec0c7dee8ddb70fcovener }
f3f945b6f394da4d080082fae12da22b4989914and
fe64b2ba25510d8c9dba5560a2d537763566cf40nd if (*accept_line == ',') {
253547fb9cc7986e84ff68aef076f664fc4169dctakashi ++accept_line;
253547fb9cc7986e84ff68aef076f664fc4169dctakashi }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
06ba4a61654b3763ad65f52283832ebf058fdf1cslive return accept_line;
2aff288113d772cedca6add888eb643afffe9fb1nd}
9e16224e92151e6c142e63ec8e3d06591c61352dnd
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin/*****************************************************************
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin *
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin * Dealing with header lines ...
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin *
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin * Accept, Accept-Charset, Accept-Language and Accept-Encoding
2aff288113d772cedca6add888eb643afffe9fb1nd * are handled by do_header_line() - they all have the same
9e16224e92151e6c142e63ec8e3d06591c61352dnd * basic structure of a list of items of the format
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * name; q=N; charset=TEXT
0734f81384d5f2528bb119f89d135043a280d5b2minfrin *
0734f81384d5f2528bb119f89d135043a280d5b2minfrin * where charset is only valid in Accept.
0734f81384d5f2528bb119f89d135043a280d5b2minfrin */
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dndstatic apr_array_header_t *do_header_line(apr_pool_t *p, const char *accept_line)
9e16224e92151e6c142e63ec8e3d06591c61352dnd{
9e16224e92151e6c142e63ec8e3d06591c61352dnd apr_array_header_t *accept_recs;
9e16224e92151e6c142e63ec8e3d06591c61352dnd
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin if (!accept_line) {
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin return NULL;
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin }
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin
06ba4a61654b3763ad65f52283832ebf058fdf1cslive accept_recs = apr_array_make(p, 40, sizeof(accept_rec));
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coar
595ee65a4fefd1f5df0c6969a5ea88370751f9e4covener while (*accept_line) {
4fa837f85f8e371c2f495ac56fe5180fcece130eminfrin accept_rec *new = (accept_rec *) apr_array_push(accept_recs);
4aa603e6448b99f9371397d439795c91a93637eand accept_line = get_entry(p, new, accept_line);
1126fe9569c24b1ef2acb98df6b84d4d7d934c82minfrin }
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
17ade6df5ec233536985eb1c130a906c725dd614humbedooh return accept_recs;
17ade6df5ec233536985eb1c130a906c725dd614humbedooh}
17ade6df5ec233536985eb1c130a906c725dd614humbedooh
1126fe9569c24b1ef2acb98df6b84d4d7d934c82minfrin/* Given the text of the Content-Languages: line from the var map file,
1126fe9569c24b1ef2acb98df6b84d4d7d934c82minfrin * return an array containing the languages of this variant
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar */
17ade6df5ec233536985eb1c130a906c725dd614humbedooh
17ade6df5ec233536985eb1c130a906c725dd614humbedoohstatic apr_array_header_t *do_languages_line(apr_pool_t *p, const char **lang_line)
17ade6df5ec233536985eb1c130a906c725dd614humbedooh{
595ee65a4fefd1f5df0c6969a5ea88370751f9e4covener apr_array_header_t *lang_recs = apr_array_make(p, 2, sizeof(char *));
17ade6df5ec233536985eb1c130a906c725dd614humbedooh
17ade6df5ec233536985eb1c130a906c725dd614humbedooh if (!lang_line) {
1126fe9569c24b1ef2acb98df6b84d4d7d934c82minfrin return lang_recs;
17ade6df5ec233536985eb1c130a906c725dd614humbedooh }
17ade6df5ec233536985eb1c130a906c725dd614humbedooh
595ee65a4fefd1f5df0c6969a5ea88370751f9e4covener while (**lang_line) {
4aa603e6448b99f9371397d439795c91a93637eand char **new = (char **) apr_array_push(lang_recs);
17ade6df5ec233536985eb1c130a906c725dd614humbedooh *new = ap_get_token(p, lang_line, 0);
89ebbccde2b87b469b9d6fdb606eeb943c12a824colm ap_str_tolower(*new);
9df39bf0053c6eaaa8d6d5afd87f26d9378bb20dminfrin if (**lang_line == ',' || **lang_line == ';') {
9df39bf0053c6eaaa8d6d5afd87f26d9378bb20dminfrin ++(*lang_line);
9df39bf0053c6eaaa8d6d5afd87f26d9378bb20dminfrin }
9df39bf0053c6eaaa8d6d5afd87f26d9378bb20dminfrin }
4aa603e6448b99f9371397d439795c91a93637eand
17ade6df5ec233536985eb1c130a906c725dd614humbedooh return lang_recs;
17ade6df5ec233536985eb1c130a906c725dd614humbedooh}
4aa603e6448b99f9371397d439795c91a93637eand
17ade6df5ec233536985eb1c130a906c725dd614humbedooh/*****************************************************************
9df39bf0053c6eaaa8d6d5afd87f26d9378bb20dminfrin *
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf * Handling header lines from clients...
dc4794add358378bce888ab64e20006c263f2651covener */
b4f25203520c168e2df9f7d83024db09e68efe7ecovener
89ebbccde2b87b469b9d6fdb606eeb943c12a824colmstatic negotiation_state *parse_accept_headers(request_rec *r)
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener{
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener negotiation_state *new =
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener (negotiation_state *) apr_pcalloc(r->pool, sizeof(negotiation_state));
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener accept_rec *elts;
8d36abdf61277cb8ffcecc763c8a86c28f11f8e9covener apr_table_t *hdrs = r->headers_in;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen int i;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->pool = r->pool;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->r = r;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->dir_name = ap_make_dirstr_parent(r->pool, r->filename);
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->accepts = do_header_line(r->pool, apr_table_get(hdrs, "Accept"));
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen /* calculate new->accept_q value */
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen if (new->accepts) {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen elts = (accept_rec *) new->accepts->elts;
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen for (i = 0; i < new->accepts->nelts; ++i) {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen if (elts[i].quality < 1.0) {
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->accept_q = 1;
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd }
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd }
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen }
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->accept_encodings =
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen do_header_line(r->pool, apr_table_get(hdrs, "Accept-Encoding"));
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->accept_langs =
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen do_header_line(r->pool, apr_table_get(hdrs, "Accept-Language"));
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen new->accept_charsets =
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen do_header_line(r->pool, apr_table_get(hdrs, "Accept-Charset"));
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen /* This is possibly overkill for some servers, heck, we have
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * only 33 index.html variants in docs/docroot (today).
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen * Make this configurable?
4aa603e6448b99f9371397d439795c91a93637eand */
4aa603e6448b99f9371397d439795c91a93637eand new->avail_vars = apr_array_make(r->pool, 40, sizeof(var_rec));
17ade6df5ec233536985eb1c130a906c725dd614humbedooh
17ade6df5ec233536985eb1c130a906c725dd614humbedooh return new;
20f499565e77defe9dab24dd85c02f38a1175855nd}
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowen
a547340d7d0f0e79c9ba921c7dec7b18d0c800ffrbowenstatic void parse_negotiate_header(request_rec *r, negotiation_state *neg)
fe64b2ba25510d8c9dba5560a2d537763566cf40nd{
fe64b2ba25510d8c9dba5560a2d537763566cf40nd const char *negotiate = apr_table_get(r->headers_in, "Negotiate");
fe64b2ba25510d8c9dba5560a2d537763566cf40nd char *tok;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
cf2c758d9a7837808846a4bbf5611f268954c79dnd /* First, default to no TCN, no Alternates, and the original Apache
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * negotiation algorithm with fiddles for broken browser configs.
fe64b2ba25510d8c9dba5560a2d537763566cf40nd *
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * To save network bandwidth, we do not configure to send an
f3f945b6f394da4d080082fae12da22b4989914and * Alternates header to the user agent by default. User
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * agents that want an Alternates header for agent-driven
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * negotiation will have to request it by sending an
cf2c758d9a7837808846a4bbf5611f268954c79dnd * appropriate Negotiate header.
cf2c758d9a7837808846a4bbf5611f268954c79dnd */
cf2c758d9a7837808846a4bbf5611f268954c79dnd neg->ua_supports_trans = 0;
589775a2f132eef1881f348ecdb969fb709d7ddbpctony neg->send_alternates = 0;
589775a2f132eef1881f348ecdb969fb709d7ddbpctony neg->may_choose = 1;
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coar neg->use_rvsa = 0;
06ba4a61654b3763ad65f52283832ebf058fdf1cslive neg->dont_fiddle_headers = 0;
20f499565e77defe9dab24dd85c02f38a1175855nd
17ade6df5ec233536985eb1c130a906c725dd614humbedooh if (!negotiate)
fe64b2ba25510d8c9dba5560a2d537763566cf40nd return;
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd if (strcmp(negotiate, "trans") == 0) {
cf2c758d9a7837808846a4bbf5611f268954c79dnd /* Lynx 2.7 and 2.8 send 'negotiate: trans' even though they
cf2c758d9a7837808846a4bbf5611f268954c79dnd * do not support transparent content negotiation, so for Lynx we
cf2c758d9a7837808846a4bbf5611f268954c79dnd * ignore the negotiate header when its contents are exactly "trans".
cf2c758d9a7837808846a4bbf5611f268954c79dnd * If future versions of Lynx ever need to say 'negotiate: trans',
cf2c758d9a7837808846a4bbf5611f268954c79dnd * they can send the equivalent 'negotiate: trans, trans' instead
cf2c758d9a7837808846a4bbf5611f268954c79dnd * to avoid triggering the workaround below.
cf2c758d9a7837808846a4bbf5611f268954c79dnd */
cf2c758d9a7837808846a4bbf5611f268954c79dnd const char *ua = apr_table_get(r->headers_in, "User-Agent");
cf2c758d9a7837808846a4bbf5611f268954c79dnd
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak if (ua && (strncmp(ua, "Lynx", 4) == 0))
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak return;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak }
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->may_choose = 0; /* An empty Negotiate would require 300 response */
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak while ((tok = ap_get_list_item(neg->pool, &negotiate)) != NULL) {
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak if (strcmp(tok, "trans") == 0 ||
f3f945b6f394da4d080082fae12da22b4989914and strcmp(tok, "vlist") == 0 ||
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak strcmp(tok, "guess-small") == 0 ||
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak apr_isdigit(tok[0]) ||
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak strcmp(tok, "*") == 0) {
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak /* The user agent supports transparent negotiation */
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->ua_supports_trans = 1;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak /* Send-alternates could be configurable, but note
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak * that it must be 1 if we have 'vlist' in the
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak * negotiate header.
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak */
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->send_alternates = 1;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak if (strcmp(tok, "1.0") == 0) {
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak /* we may use the RVSA/1.0 algorithm, configure for it */
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->may_choose = 1;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->use_rvsa = 1;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->dont_fiddle_headers = 1;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak }
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak else if (tok[0] == '*') {
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak /* we may use any variant selection algorithm, configure
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak * to use the Apache algorithm
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak */
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->may_choose = 1;
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak /* We disable header fiddles on the assumption that a
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak * client sending Negotiate knows how to send correct
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak * headers which don't need fiddling.
20f499565e77defe9dab24dd85c02f38a1175855nd */
e487d6c09669296f94a5190cc34586a98e624a00nd neg->dont_fiddle_headers = 1;
17ade6df5ec233536985eb1c130a906c725dd614humbedooh }
20f499565e77defe9dab24dd85c02f38a1175855nd }
e487d6c09669296f94a5190cc34586a98e624a00nd }
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak#ifdef NEG_DEBUG
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak "dont_fiddle_headers=%d use_rvsa=%d ua_supports_trans=%d "
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak "send_alternates=%d, may_choose=%d",
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->dont_fiddle_headers, neg->use_rvsa,
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak neg->ua_supports_trans, neg->send_alternates, neg->may_choose);
f21bea4c0f58e17aa1d9a0fac2c219852f89944amaczniak#endif
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd}
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd/* Sometimes clients will give us no Accept info at all; this routine sets
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * up the standard default for that case, and also arranges for us to be
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * willing to run a CGI script if we find one. (In fact, we set up to
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * dramatically prefer CGI scripts in cases where that's appropriate,
fe64b2ba25510d8c9dba5560a2d537763566cf40nd * e.g., POST or when URI includes query args or extra path info).
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin */
f3f945b6f394da4d080082fae12da22b4989914andstatic void maybe_add_default_accepts(negotiation_state *neg,
fe64b2ba25510d8c9dba5560a2d537763566cf40nd int prefer_scripts)
fe64b2ba25510d8c9dba5560a2d537763566cf40nd{
9e16224e92151e6c142e63ec8e3d06591c61352dnd accept_rec *new_accept;
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dnd if (!neg->accepts) {
9e16224e92151e6c142e63ec8e3d06591c61352dnd neg->accepts = apr_array_make(neg->pool, 4, sizeof(accept_rec));
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dnd new_accept = (accept_rec *) apr_array_push(neg->accepts);
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dnd new_accept->name = "*/*";
9e16224e92151e6c142e63ec8e3d06591c61352dnd new_accept->quality = 1.0f;
9e16224e92151e6c142e63ec8e3d06591c61352dnd new_accept->level = 0.0f;
06ba4a61654b3763ad65f52283832ebf058fdf1cslive }
20f499565e77defe9dab24dd85c02f38a1175855nd
17ade6df5ec233536985eb1c130a906c725dd614humbedooh new_accept = (accept_rec *) apr_array_push(neg->accepts);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
90c99eca06310d91254dd3525e1f5e9fa612af71jim new_accept->name = CGI_MAGIC_TYPE;
90c99eca06310d91254dd3525e1f5e9fa612af71jim if (neg->use_rvsa) {
90c99eca06310d91254dd3525e1f5e9fa612af71jim new_accept->quality = 0;
90c99eca06310d91254dd3525e1f5e9fa612af71jim }
90c99eca06310d91254dd3525e1f5e9fa612af71jim else {
90c99eca06310d91254dd3525e1f5e9fa612af71jim new_accept->quality = prefer_scripts ? 2.0f : 0.001f;
90c99eca06310d91254dd3525e1f5e9fa612af71jim }
90c99eca06310d91254dd3525e1f5e9fa612af71jim new_accept->level = 0.0f;
90c99eca06310d91254dd3525e1f5e9fa612af71jim}
90c99eca06310d91254dd3525e1f5e9fa612af71jim
90c99eca06310d91254dd3525e1f5e9fa612af71jim/*****************************************************************
90c99eca06310d91254dd3525e1f5e9fa612af71jim *
90c99eca06310d91254dd3525e1f5e9fa612af71jim * Parsing type-map files, in Roy's meta/http format augmented with
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf * #-comments.
90c99eca06310d91254dd3525e1f5e9fa612af71jim */
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
90c99eca06310d91254dd3525e1f5e9fa612af71jim/* Reading RFC822-style header lines, ignoring #-comments and
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf * handling continuations.
90c99eca06310d91254dd3525e1f5e9fa612af71jim */
90c99eca06310d91254dd3525e1f5e9fa612af71jim
20f499565e77defe9dab24dd85c02f38a1175855ndenum header_state {
17ade6df5ec233536985eb1c130a906c725dd614humbedooh header_eof, header_seen, header_sep
90c99eca06310d91254dd3525e1f5e9fa612af71jim};
90c99eca06310d91254dd3525e1f5e9fa612af71jim
282b62d8e9a4edbc2da22ba2d876ec94afc48084ndstatic enum header_state get_header_line(char *buffer, int len, apr_file_t *map)
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd{
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd char *buf_end = buffer + len;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd char *cp;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd char c;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd /* Get a noncommented line */
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd do {
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd if (apr_file_gets(buffer, MAX_STRING_LEN, map) != APR_SUCCESS) {
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd return header_eof;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd }
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd } while (buffer[0] == '#');
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd /* If blank, just return it --- this ends information on this variant */
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd for (cp = buffer; (*cp && apr_isspace(*cp)); ++cp) {
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd continue;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd }
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd if (*cp == '\0') {
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd return header_sep;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd }
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd /* If non-blank, go looking for header lines, but note that we still
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd * have to treat comments specially...
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd */
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd
20f499565e77defe9dab24dd85c02f38a1175855nd cp += strlen(cp);
e487d6c09669296f94a5190cc34586a98e624a00nd
17ade6df5ec233536985eb1c130a906c725dd614humbedooh /* We need to shortcut the rest of this block following the Body:
20f499565e77defe9dab24dd85c02f38a1175855nd * tag - we will not look for continutation after this line.
e487d6c09669296f94a5190cc34586a98e624a00nd */
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd if (!strncasecmp(buffer, "Body:", 5))
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd return header_seen;
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin while (apr_file_getc(&c, map) != APR_EOF) {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin if (c == '#') {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin /* Comment line */
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin while (apr_file_getc(&c, map) != APR_EOF && c != '\n') {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin continue;
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin }
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin }
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin else if (apr_isspace(c)) {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin /* Leading whitespace. POSSIBLE continuation line
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin * Also, possibly blank --- if so, we ungetc() the final newline
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin * so that we will pick up the blank line the next time 'round.
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin */
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin while (c != '\n' && apr_isspace(c)) {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin if(apr_file_getc(&c, map) != APR_SUCCESS)
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin break;
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin }
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin apr_file_ungetc(c, map);
4aa603e6448b99f9371397d439795c91a93637eand
4aa603e6448b99f9371397d439795c91a93637eand if (c == '\n') {
17ade6df5ec233536985eb1c130a906c725dd614humbedooh return header_seen; /* Blank line */
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin }
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin /* Continuation */
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin while (cp < buf_end - 2 && (apr_file_getc(&c, map)) != APR_EOF && c != '\n') {
3dd3d010c0efae7d1d6926f0b1de2da17eec796eminfrin *cp++ = c;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd *cp++ = '\n';
fe64b2ba25510d8c9dba5560a2d537763566cf40nd *cp = '\0';
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd else {
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd /* Line beginning with something other than whitespace */
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin
f3f945b6f394da4d080082fae12da22b4989914and apr_file_ungetc(c, map);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd return header_seen;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
9e16224e92151e6c142e63ec8e3d06591c61352dnd }
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dnd return header_seen;
9e16224e92151e6c142e63ec8e3d06591c61352dnd}
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dndstatic apr_off_t get_body(char *buffer, apr_size_t *len, const char *tag,
9e16224e92151e6c142e63ec8e3d06591c61352dnd apr_file_t *map)
9e16224e92151e6c142e63ec8e3d06591c61352dnd{
9e16224e92151e6c142e63ec8e3d06591c61352dnd char *endbody;
9e16224e92151e6c142e63ec8e3d06591c61352dnd int bodylen;
9e16224e92151e6c142e63ec8e3d06591c61352dnd apr_off_t pos;
9e16224e92151e6c142e63ec8e3d06591c61352dnd
9e16224e92151e6c142e63ec8e3d06591c61352dnd /* We are at the first character following a body:tag\n entry
9e16224e92151e6c142e63ec8e3d06591c61352dnd * Suck in the body, then backspace to the first char after the
9e16224e92151e6c142e63ec8e3d06591c61352dnd * closing tag entry. If we fail to read, find the tag or back
9e16224e92151e6c142e63ec8e3d06591c61352dnd * up then we have a hosed file, so give up already
9e16224e92151e6c142e63ec8e3d06591c61352dnd */
9e16224e92151e6c142e63ec8e3d06591c61352dnd if (apr_file_read(map, buffer, len) != APR_SUCCESS) {
06ba4a61654b3763ad65f52283832ebf058fdf1cslive return -1;
20f499565e77defe9dab24dd85c02f38a1175855nd }
17ade6df5ec233536985eb1c130a906c725dd614humbedooh endbody = strstr(buffer, tag);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd if (!endbody) {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin return -1;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin bodylen = endbody - buffer;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin endbody += strlen(tag);
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin /* Skip all the trailing cruft after the end tag to the next line */
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin while (*endbody) {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin if (*endbody == '\n') {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin ++endbody;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin break;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin ++endbody;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf pos = -(apr_off_t)(*len - (endbody - buffer));
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin if (apr_file_seek(map, APR_CUR, &pos) != APR_SUCCESS) {
17f88acd0b3fba7eddb6fd974927edf8f5dbe41dsf return -1;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
4aa603e6448b99f9371397d439795c91a93637eand
4aa603e6448b99f9371397d439795c91a93637eand /* Give the caller back the actual body's offset and length */
17ade6df5ec233536985eb1c130a906c725dd614humbedooh *len = bodylen;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin return pos - (endbody - buffer);
17f88acd0b3fba7eddb6fd974927edf8f5dbe41dsf}
17f88acd0b3fba7eddb6fd974927edf8f5dbe41dsf
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin/* Stripping out RFC822 comments */
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrinstatic void strip_paren_comments(char *hdr)
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin{
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin /* Hmmm... is this correct? In Roy's latest draft, (comments) can nest! */
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin /* Nope, it isn't correct. Fails to handle backslash escape as well. */
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin while (*hdr) {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin if (*hdr == '"') {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin hdr = strchr(hdr, '"');
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin if (hdr == NULL) {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin return;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf ++hdr;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin else if (*hdr == '(') {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin while (*hdr && *hdr != ')') {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin *hdr++ = ' ';
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin if (*hdr) {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin *hdr++ = ' ';
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin else {
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin ++hdr;
17f88acd0b3fba7eddb6fd974927edf8f5dbe41dsf }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin }
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin}
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin/* Getting to a header body from the header */
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
17f88acd0b3fba7eddb6fd974927edf8f5dbe41dsfstatic char *lcase_header_name_return_body(char *header, request_rec *r)
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coar{
17f88acd0b3fba7eddb6fd974927edf8f5dbe41dsf char *cp = header;
c1fcddc76fd5db5ac4ccd7c6ae839d53d128f354minfrin
fe64b2ba25510d8c9dba5560a2d537763566cf40nd for ( ; *cp && *cp != ':' ; ++cp) {
fe64b2ba25510d8c9dba5560a2d537763566cf40nd *cp = apr_tolower(*cp);
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd if (!*cp) {
fe64b2ba25510d8c9dba5560a2d537763566cf40nd ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
fe64b2ba25510d8c9dba5560a2d537763566cf40nd "Syntax error in type map, no ':' in %s for header %s",
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin r->filename, header);
f3f945b6f394da4d080082fae12da22b4989914and return NULL;
fe64b2ba25510d8c9dba5560a2d537763566cf40nd }
fe64b2ba25510d8c9dba5560a2d537763566cf40nd
9e16224e92151e6c142e63ec8e3d06591c61352dnd do {
9e16224e92151e6c142e63ec8e3d06591c61352dnd ++cp;
9e16224e92151e6c142e63ec8e3d06591c61352dnd } while (*cp && apr_isspace(*cp));
9e16224e92151e6c142e63ec8e3d06591c61352dnd
2aff288113d772cedca6add888eb643afffe9fb1nd if (!*cp) {
20f499565e77defe9dab24dd85c02f38a1175855nd ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
17ade6df5ec233536985eb1c130a906c725dd614humbedooh "Syntax error in type map --- no header body: %s for %s",
fe64b2ba25510d8c9dba5560a2d537763566cf40nd r->filename, header);
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem return NULL;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem }
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem return cp;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem}
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluemstatic int read_type_map(apr_file_t **map, negotiation_state *neg, request_rec *rr)
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin{
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem request_rec *r = neg->r;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem apr_file_t *map_ = NULL;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem apr_status_t status;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem char buffer[MAX_STRING_LEN];
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem enum header_state hstate;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem struct var_rec mime_info;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem int has_content;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem
20f499565e77defe9dab24dd85c02f38a1175855nd if (!map)
17ade6df5ec233536985eb1c130a906c725dd614humbedooh map = &map_;
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem
cf663b70eeea0b51582be629e148a697aa2a116eminfrin /* We are not using multiviews */
cf663b70eeea0b51582be629e148a697aa2a116eminfrin neg->count_multiviews_variants = 0;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
cf663b70eeea0b51582be629e148a697aa2a116eminfrin if ((status = apr_file_open(map, rr->filename, APR_READ | APR_BUFFERED,
cf663b70eeea0b51582be629e148a697aa2a116eminfrin APR_OS_DEFAULT, neg->pool)) != APR_SUCCESS) {
cf663b70eeea0b51582be629e148a697aa2a116eminfrin ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
cf663b70eeea0b51582be629e148a697aa2a116eminfrin "cannot access type map file: %s", rr->filename);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin return HTTP_FORBIDDEN;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin }
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
0e7b9978df6fac23db4330b09e890cf6de6ff7b6covener clean_var_rec(&mime_info);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin has_content = 0;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
cf663b70eeea0b51582be629e148a697aa2a116eminfrin do {
cf663b70eeea0b51582be629e148a697aa2a116eminfrin hstate = get_header_line(buffer, MAX_STRING_LEN, *map);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
cf663b70eeea0b51582be629e148a697aa2a116eminfrin if (hstate == header_seen) {
cf663b70eeea0b51582be629e148a697aa2a116eminfrin char *body1 = lcase_header_name_return_body(buffer, neg->r);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin const char *body;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
cf663b70eeea0b51582be629e148a697aa2a116eminfrin if (body1 == NULL) {
cf663b70eeea0b51582be629e148a697aa2a116eminfrin return HTTP_INTERNAL_SERVER_ERROR;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin }
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
51e213aed94e1ca678a7551c8a215b8caed301e7jim strip_paren_comments(body1);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin body = body1;
4aa603e6448b99f9371397d439795c91a93637eand
4aa603e6448b99f9371397d439795c91a93637eand if (!strncmp(buffer, "uri:", 4)) {
17ade6df5ec233536985eb1c130a906c725dd614humbedooh mime_info.file_name = ap_get_token(neg->pool, &body, 0);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin }
cf663b70eeea0b51582be629e148a697aa2a116eminfrin else if (!strncmp(buffer, "content-type:", 13)) {
cf663b70eeea0b51582be629e148a697aa2a116eminfrin struct accept_rec accept_info;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin
cf663b70eeea0b51582be629e148a697aa2a116eminfrin get_entry(neg->pool, &accept_info, body);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin set_mime_fields(&mime_info, &accept_info);
4aa603e6448b99f9371397d439795c91a93637eand has_content = 1;
17ade6df5ec233536985eb1c130a906c725dd614humbedooh }
4aa603e6448b99f9371397d439795c91a93637eand else if (!strncmp(buffer, "content-length:", 15)) {
17ade6df5ec233536985eb1c130a906c725dd614humbedooh mime_info.bytes = atol(body);
cf663b70eeea0b51582be629e148a697aa2a116eminfrin has_content = 1;
cf663b70eeea0b51582be629e148a697aa2a116eminfrin }
cf663b70eeea0b51582be629e148a697aa2a116eminfrin else if (!strncmp(buffer, "content-language:", 17)) {
cf663b70eeea0b51582be629e148a697aa2a116eminfrin mime_info.content_languages = do_languages_line(neg->pool,
cf663b70eeea0b51582be629e148a697aa2a116eminfrin &body);
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin has_content = 1;
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin }
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin else if (!strncmp(buffer, "content-encoding:", 17)) {
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin mime_info.content_encoding = ap_get_token(neg->pool, &body, 0);
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin has_content = 1;
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin }
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin else if (!strncmp(buffer, "description:", 12)) {
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin char *desc = apr_pstrdup(neg->pool, body);
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin char *cp;
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin for (cp = desc; *cp; ++cp) {
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin if (*cp=='\n') *cp=' ';
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin }
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin if (cp>desc) *(cp-1)=0;
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf mime_info.description = desc;
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf }
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf else if (!strncmp(buffer, "body:", 5)) {
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf char *tag = apr_pstrdup(neg->pool, body);
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin char *eol = strchr(tag, '\0');
4aa603e6448b99f9371397d439795c91a93637eand apr_size_t len = MAX_STRING_LEN;
4aa603e6448b99f9371397d439795c91a93637eand while (--eol >= tag && apr_isspace(*eol))
17ade6df5ec233536985eb1c130a906c725dd614humbedooh *eol = '\0';
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin if ((mime_info.body = get_body(buffer, &len, tag, *map)) < 0) {
e80f65dadea03d04ee5b9e2d3de8c5fcef9e6714minfrin ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen "Syntax error in type map, no end tag '%s'"
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen "found in %s for Body: content.",
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen tag, r->filename);
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen break;
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen }
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen mime_info.bytes = len;
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen mime_info.file_name = rr->filename;
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin }
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen }
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen else {
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen if (*mime_info.file_name && has_content) {
a04dcd330b05345681a4057702b2d43b2042e21dtrawick void *new_var = apr_array_push(neg->avail_vars);
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen memcpy(new_var, (void *) &mime_info, sizeof(var_rec));
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen }
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen clean_var_rec(&mime_info);
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen has_content = 0;
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen }
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen } while (hstate != header_eof);
20f499565e77defe9dab24dd85c02f38a1175855nd
17ade6df5ec233536985eb1c130a906c725dd614humbedooh if (map_)
8305f030deafc6022193cff1cb11b6f69a2d4f3arbowen apr_file_close(map_);
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd set_vlist_validator(r, rr);
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd return OK;
cf2c758d9a7837808846a4bbf5611f268954c79dnd}
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin/* Sort function used by read_types_multi. */
cf2c758d9a7837808846a4bbf5611f268954c79dndstatic int variantsortf(var_rec *a, var_rec *b) {
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd /* First key is the source quality, sort in descending order. */
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd /* XXX: note that we currently implement no method of setting the
12a677ce91b7bd5aa918271de5d725635638f4edrbowen * source quality for multiviews variants, so we are always comparing
12a677ce91b7bd5aa918271de5d725635638f4edrbowen * 1.0 to 1.0 for now
cf2c758d9a7837808846a4bbf5611f268954c79dnd */
51e213aed94e1ca678a7551c8a215b8caed301e7jim if (a->source_quality < b->source_quality)
cf2c758d9a7837808846a4bbf5611f268954c79dnd return 1;
20f499565e77defe9dab24dd85c02f38a1175855nd if (a->source_quality > b->source_quality)
17ade6df5ec233536985eb1c130a906c725dd614humbedooh return -1;
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd /* Second key is the variant name */
cf2c758d9a7837808846a4bbf5611f268954c79dnd return strcmp(a->file_name, b->file_name);
cf2c758d9a7837808846a4bbf5611f268954c79dnd}
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd/*****************************************************************
cf2c758d9a7837808846a4bbf5611f268954c79dnd *
cf2c758d9a7837808846a4bbf5611f268954c79dnd * Same as read_type_map, except we use a filtered directory listing
cf2c758d9a7837808846a4bbf5611f268954c79dnd * as the map...
cf2c758d9a7837808846a4bbf5611f268954c79dnd */
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dndstatic int read_types_multi(negotiation_state *neg)
cf2c758d9a7837808846a4bbf5611f268954c79dnd{
cf2c758d9a7837808846a4bbf5611f268954c79dnd request_rec *r = neg->r;
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd char *filp;
cf2c758d9a7837808846a4bbf5611f268954c79dnd int prefix_len;
cf2c758d9a7837808846a4bbf5611f268954c79dnd apr_dir_t *dirp;
cf2c758d9a7837808846a4bbf5611f268954c79dnd apr_finfo_t dirent;
cf2c758d9a7837808846a4bbf5611f268954c79dnd apr_status_t status;
26ec660d801fc7f7dd96146cea051df0a9b4193eminfrin struct var_rec mime_info;
cf2c758d9a7837808846a4bbf5611f268954c79dnd struct accept_rec accept_info;
cf2c758d9a7837808846a4bbf5611f268954c79dnd void *new_var;
cf2c758d9a7837808846a4bbf5611f268954c79dnd int anymatch = 0;
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd clean_var_rec(&mime_info);
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd if (!(filp = strrchr(r->filename, '/'))) {
cf2c758d9a7837808846a4bbf5611f268954c79dnd return DECLINED; /* Weird... */
51e213aed94e1ca678a7551c8a215b8caed301e7jim }
cf2c758d9a7837808846a4bbf5611f268954c79dnd
20f499565e77defe9dab24dd85c02f38a1175855nd /* XXX this should be more general, and quit using 'specials' */
17ade6df5ec233536985eb1c130a906c725dd614humbedooh if (strncmp(r->filename, "proxy:", 6) == 0) {
cf2c758d9a7837808846a4bbf5611f268954c79dnd return DECLINED;
cf2c758d9a7837808846a4bbf5611f268954c79dnd }
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd ++filp;
cf2c758d9a7837808846a4bbf5611f268954c79dnd prefix_len = strlen(filp);
cf2c758d9a7837808846a4bbf5611f268954c79dnd
cf2c758d9a7837808846a4bbf5611f268954c79dnd if ((status = apr_dir_open(&dirp, neg->dir_name, neg->pool)) != APR_SUCCESS) {
cf2c758d9a7837808846a4bbf5611f268954c79dnd ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
cf2c758d9a7837808846a4bbf5611f268954c79dnd "cannot read directory for multi: %s", neg->dir_name);
cf2c758d9a7837808846a4bbf5611f268954c79dnd return HTTP_FORBIDDEN;
cf2c758d9a7837808846a4bbf5611f268954c79dnd }
cf2c758d9a7837808846a4bbf5611f268954c79dnd
fe64b2ba25510d8c9dba5560a2d537763566cf40nd while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
fe64b2ba25510d8c9dba5560a2d537763566cf40nd apr_array_header_t *exception_list;
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd request_rec *sub_req;
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
3e6b8c7840a46cdccd46b7a4b1902c2bc82f6cc2gryzor /* Do we have a match? */
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd#ifdef CASE_BLIND_FILESYSTEM
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung if (strncasecmp(dirent.name, filp, prefix_len)) {
727872d18412fc021f03969b8641810d8896820bhumbedooh#else
0d0ba3a410038e179b695446bb149cce6264e0abnd if (strncmp(dirent.name, filp, prefix_len)) {
727872d18412fc021f03969b8641810d8896820bhumbedooh#endif
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh continue;
0d0ba3a410038e179b695446bb149cce6264e0abnd }
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh if (dirent.name[prefix_len] != '.') {
727872d18412fc021f03969b8641810d8896820bhumbedooh continue;
0d0ba3a410038e179b695446bb149cce6264e0abnd }
0d0ba3a410038e179b695446bb149cce6264e0abnd
0d0ba3a410038e179b695446bb149cce6264e0abnd /* Ok, something's here. Maybe nothing useful. Remember that
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh * we tried, if we completely fail, so we can reject the request!
0d0ba3a410038e179b695446bb149cce6264e0abnd */
0d0ba3a410038e179b695446bb149cce6264e0abnd anymatch = 1;
0d0ba3a410038e179b695446bb149cce6264e0abnd
727872d18412fc021f03969b8641810d8896820bhumbedooh /* Yep. See if it's something which we have access to, and
0d0ba3a410038e179b695446bb149cce6264e0abnd * which has a known type and encoding (as opposed to something
0d0ba3a410038e179b695446bb149cce6264e0abnd * which we'll be slapping default_type on later).
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh */
205f749042ed530040a4f0080dbcb47ceae8a374rjung
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen sub_req = ap_sub_req_lookup_dirent(&dirent, r, NULL);
0d0ba3a410038e179b695446bb149cce6264e0abnd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd /* BLECH --- don't multi-resolve non-ordinary files */
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd if (sub_req->finfo.filetype != APR_REG)
fe64b2ba25510d8c9dba5560a2d537763566cf40nd continue;
/* If it has a handler, we'll pretend it's a CGI script,
* since that's a good indication of the sort of thing it
* might be doing.
*/
if (sub_req->handler && !sub_req->content_type) {
sub_req->content_type = CGI_MAGIC_TYPE;
}
/*
* mod_mime will _always_ provide us the base name in the
* ap-mime-exception-list, if it processed anything. If
* this list is empty, give up immediately, there was
* nothing interesting. For example, looking at the files
* readme.txt and readme.foo, we will throw away .foo if
* it's an insignificant file (e.g. did not identify a
* language, charset, encoding, content type or handler,)
*/
exception_list =
(apr_array_header_t *)apr_table_get(sub_req->notes,
"ap-mime-exceptions-list");
if (!exception_list) {
ap_destroy_sub_req(sub_req);
continue;
}
/* Each unregonized bit better match our base name, in sequence.
* A test of index.html.foo will match index.foo or index.html.foo,
* but it will never transpose the segments and allow index.foo.html
* because that would introduce too much CPU consumption. Better that
* we don't attempt a many-to-many match here.
*/
{
int nexcept = exception_list->nelts;
char **cur_except = (char**)exception_list->elts;
char *segstart = filp, *segend, saveend;
while (*segstart && nexcept) {
if (!(segend = strchr(segstart, '.')))
segend = strchr(segstart, '\0');
saveend = *segend;
*segend = '\0';
#ifdef CASE_BLIND_FILESYSTEM
if (strcasecmp(segstart, *cur_except) == 0) {
#else
if (strcmp(segstart, *cur_except) == 0) {
#endif
--nexcept;
++cur_except;
}
if (!saveend)
break;
*segend = saveend;
segstart = segend + 1;
}
if (nexcept) {
/* Something you don't know is, something you don't know...
*/
ap_destroy_sub_req(sub_req);
continue;
}
}
/*
* ###: be warned, the _default_ content type is already
* picked up here! If we failed the subrequest, or don't
* know what we are serving, then continue.
*/
if (sub_req->status != HTTP_OK || (!sub_req->content_type)) {
ap_destroy_sub_req(sub_req);
continue;
}
/* If it's a map file, we use that instead of the map
* we're building...
*/
if (((sub_req->content_type) &&
!strcmp(sub_req->content_type, MAP_FILE_MAGIC_TYPE)) ||
((sub_req->handler) &&
!strcmp(sub_req->handler, "type-map"))) {
apr_dir_close(dirp);
neg->avail_vars->nelts = 0;
if (sub_req->status != HTTP_OK) {
return sub_req->status;
}
return read_type_map(NULL, neg, sub_req);
}
/* Have reasonable variant --- gather notes. */
mime_info.sub_req = sub_req;
mime_info.file_name = apr_pstrdup(neg->pool, dirent.name);
if (sub_req->content_encoding) {
mime_info.content_encoding = sub_req->content_encoding;
}
if (sub_req->content_languages) {
mime_info.content_languages = sub_req->content_languages;
}
get_entry(neg->pool, &accept_info, sub_req->content_type);
set_mime_fields(&mime_info, &accept_info);
new_var = apr_array_push(neg->avail_vars);
memcpy(new_var, (void *) &mime_info, sizeof(var_rec));
neg->count_multiviews_variants++;
clean_var_rec(&mime_info);
}
apr_dir_close(dirp);
/* We found some file names that matched. None could be served.
* Rather than fall out to autoindex or some other mapper, this
* request must die.
*/
if (anymatch && !neg->avail_vars->nelts) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"Negotiation: discovered file(s) matching request: %s"
" (None could be negotiated).",
r->filename);
return HTTP_NOT_FOUND;
}
set_vlist_validator(r, r);
/* Sort the variants into a canonical order. The negotiation
* result sometimes depends on the order of the variants. By
* sorting the variants into a canonical order, rather than using
* the order in which readdir() happens to return them, we ensure
* that the negotiation result will be consistent over filesystem
* backup/restores and over all mirror sites.
*/
qsort((void *) neg->avail_vars->elts, neg->avail_vars->nelts,
sizeof(var_rec), (int (*)(const void *, const void *)) variantsortf);
return OK;
}
/*****************************************************************
* And now for the code you've been waiting for... actually
* finding a match to the client's requirements.
*/
/* Matching MIME types ... the star/star and foo/star commenting conventions
* are implemented here. (You know what I mean by star/star, but just
* try mentioning those three characters in a C comment). Using strcmp()
* is legit, because everything has already been smashed to lowercase.
*
* Note also that if we get an exact match on the media type, we update
* level_matched for use in level_cmp below...
*
* We also give a value for mime_stars, which is used later. It should
* be 1 for star/star, 2 for type/star and 3 for type/subtype.
*/
static int mime_match(accept_rec *accept_r, var_rec *avail)
{
const char *accept_type = accept_r->name;
const char *avail_type = avail->mime_type;
int len = strlen(accept_type);
if (accept_type[0] == '*') { /* Anything matches star/star */
if (avail->mime_stars < 1) {
avail->mime_stars = 1;
}
return 1;
}
else if ((accept_type[len - 1] == '*') &&
!strncmp(accept_type, avail_type, len - 2)) {
if (avail->mime_stars < 2) {
avail->mime_stars = 2;
}
return 1;
}
else if (!strcmp(accept_type, avail_type)
|| (!strcmp(accept_type, "text/html")
&& (!strcmp(avail_type, INCLUDES_MAGIC_TYPE)
|| !strcmp(avail_type, INCLUDES_MAGIC_TYPE3)))) {
if (accept_r->level >= avail->level) {
avail->level_matched = avail->level;
avail->mime_stars = 3;
return 1;
}
}
return OK;
}
/* This code implements a piece of the tie-breaking algorithm between
* variants of equal quality. This piece is the treatment of variants
* of the same base media type, but different levels. What we want to
* return is the variant at the highest level that the client explicitly
* claimed to accept.
*
* If all the variants available are at a higher level than that, or if
* the client didn't say anything specific about this media type at all
* and these variants just got in on a wildcard, we prefer the lowest
* level, on grounds that that's the one that the client is least likely
* to choke on.
*
* (This is all motivated by treatment of levels in HTML --- we only
* want to give level 3 to browsers that explicitly ask for it; browsers
* that don't, including HTTP/0.9 browsers that only get the implicit
* "Accept: * / *" [space added to avoid confusing cpp --- no, that
* syntax doesn't really work] should get HTML2 if available).
*
* (Note that this code only comes into play when we are choosing among
* variants of equal quality, where the draft standard gives us a fair
* bit of leeway about what to do. It ain't specified by the standard;
* rather, it is a choice made by this server about what to do in cases
* where the standard does not specify a unique course of action).
*/
static int level_cmp(var_rec *var1, var_rec *var2)
{
/* Levels are only comparable between matching media types */
if (var1->is_pseudo_html && !var2->is_pseudo_html) {
return 0;
}
if (!var1->is_pseudo_html && strcmp(var1->mime_type, var2->mime_type)) {
return 0;
}
/* The result of the above if statements is that, if we get to
* here, both variants have the same mime_type or both are
* pseudo-html.
*/
/* Take highest level that matched, if either did match. */
if (var1->level_matched > var2->level_matched) {
return 1;
}
if (var1->level_matched < var2->level_matched) {
return -1;
}
/* Neither matched. Take lowest level, if there's a difference. */
if (var1->level < var2->level) {
return 1;
}
if (var1->level > var2->level) {
return -1;
}
/* Tied */
return 0;
}
/* Finding languages. The main entry point is set_language_quality()
* which is called for each variant. It sets two elements in the
* variant record:
* language_quality - the 'q' value of the 'best' matching language
* from Accept-Language: header (HTTP/1.1)
* lang_index - Pre HTTP/1.1 language priority, using
* position of language on the Accept-Language:
* header, if present, else LanguagePriority
* directive order.
*
* When we do the variant checking for best variant, we use language
* quality first, and if a tie, language_index next (this only applies
* when _not_ using the RVSA/1.0 algorithm). If using the RVSA/1.0
* algorithm, lang_index is never used.
*
* set_language_quality() calls find_lang_index() and find_default_index()
* to set lang_index.
*/
static int find_lang_index(apr_array_header_t *accept_langs, char *lang)
{
accept_rec *accs;
int i;
if (!lang || !accept_langs) {
return -1;
}
accs = (accept_rec *) accept_langs->elts;
for (i = 0; i < accept_langs->nelts; ++i) {
if (!strncmp(lang, accs[i].name, strlen(accs[i].name))) {
return i;
}
}
return -1;
}
/* This function returns the priority of a given language
* according to LanguagePriority. It is used in case of a tie
* between several languages.
*/
static int find_default_index(neg_dir_config *conf, char *lang)
{
apr_array_header_t *arr;
int nelts;
char **elts;
int i;
if (!lang) {
return -1;
}
arr = conf->language_priority;
nelts = arr->nelts;
elts = (char **) arr->elts;
for (i = 0; i < nelts; ++i) {
if (!strcasecmp(elts[i], lang)) {
return i;
}
}
return -1;
}
/* set_default_lang_quality() sets the quality we apply to variants
* which have no language assigned to them. If none of the variants
* have a language, we are not negotiating on language, so all are
* acceptable, and we set the default q value to 1.0. However if
* some of the variants have languages, we set this default to 0.001.
* The value of this default will be applied to all variants with
* no explicit language -- which will have the effect of making them
* acceptable, but only if no variants with an explicit language
* are acceptable. The default q value set here is assigned to variants
* with no language type in set_language_quality().
*
* Note that if using the RVSA/1.0 algorithm, we don't use this
* fiddle.
*/
static void set_default_lang_quality(negotiation_state *neg)
{
var_rec *avail_recs = (var_rec *) neg->avail_vars->elts;
int j;
if (!neg->dont_fiddle_headers) {
for (j = 0; j < neg->avail_vars->nelts; ++j) {
var_rec *variant = &avail_recs[j];
if (variant->content_languages &&
variant->content_languages->nelts) {
neg->default_lang_quality = 0.001f;
return;
}
}
}
neg->default_lang_quality = 1.0f;
}
/* Set the language_quality value in the variant record. Also
* assigns lang_index for back-compat.
*
* To find the language_quality value, we look for the 'q' value
* of the 'best' matching language on the Accept-Language
* header. The 'best' match is the language on Accept-Language
* header which matches the language of this variant either fully,
* or as far as the prefix marker (-). If two or more languages
* match, use the longest string from the Accept-Language header
* (see HTTP/1.1 [14.4])
*
* When a variant has multiple languages, we find the 'best'
* match for each variant language tag as above, then select the
* one with the highest q value. Because both the accept-header
* and variant can have multiple languages, we now have a hairy
* loop-within-a-loop here.
*
* If the variant has no language and we have no Accept-Language
* items, leave the quality at 1.0 and return.
*
* If the variant has no language, we use the default as set by
* set_default_lang_quality() (1.0 if we are not negotiating on
* language, 0.001 if we are).
*
* Following the setting of the language quality, we drop through to
* set the old 'lang_index'. This is set based on either the order
* of the languages on the Accept-Language header, or the
* order on the LanguagePriority directive. This is only used
* in the negotiation if the language qualities tie.
*/
static void set_language_quality(negotiation_state *neg, var_rec *variant)
{
char *firstlang;
int idx;
if (!variant->content_languages || !variant->content_languages->nelts) {
/* This variant has no content-language, so use the default
* quality factor for variants with no content-language
* (previously set by set_default_lang_quality()).
* Leave the factor alone (it remains at 1.0) when we may not fiddle
* with the headers.
*/
if (!neg->dont_fiddle_headers) {
variant->lang_quality = neg->default_lang_quality;
}
if (!neg->accept_langs) {
return; /* no accept-language header */
}
}
else {
/* Variant has one (or more) languages. Look for the best
* match. We do this by going through each language on the
* variant description looking for a match on the
* Accept-Language header. The best match is the longest
* matching language on the header. The final result is the
* best q value from all the languages on the variant
* description.
*/
if (!neg->accept_langs) {
/* no accept-language header makes the variant indefinite */
variant->definite = 0;
}
else { /* There is an accept-language with 0 or more items */
accept_rec *accs = (accept_rec *) neg->accept_langs->elts;
accept_rec *best = NULL, *star = NULL;
accept_rec *bestthistag;
char *lang, *p;
float fiddle_q = 0.0f;
int any_match_on_star = 0;
int i, j;
size_t alen, longest_lang_range_len;
for (j = 0; j < variant->content_languages->nelts; ++j) {
p = NULL;
bestthistag = NULL;
longest_lang_range_len = 0;
alen = 0;
/* lang is the variant's language-tag, which is the one
* we are allowed to use the prefix of in HTTP/1.1
*/
lang = ((char **) (variant->content_languages->elts))[j];
/* now find the best (i.e. longest) matching
* Accept-Language header language. We put the best match
* for this tag in bestthistag. We cannot update the
* overall best (based on q value) because the best match
* for this tag is the longest language item on the accept
* header, not necessarily the highest q.
*/
for (i = 0; i < neg->accept_langs->nelts; ++i) {
if (!strcmp(accs[i].name, "*")) {
if (!star) {
star = &accs[i];
}
continue;
}
/* Find language. We match if either the variant
* language tag exactly matches the language range
* from the accept header, or a prefix of the variant
* language tag up to a '-' character matches the
* whole of the language range in the Accept-Language
* header. Note that HTTP/1.x allows any number of
* '-' characters in a tag or range, currently only
* tags with zero or one '-' characters are defined
* for general use (see rfc1766).
*
* We only use language range in the Accept-Language
* header the best match for the variant language tag
* if it is longer than the previous best match.
*/
alen = strlen(accs[i].name);
if ((strlen(lang) >= alen) &&
!strncmp(lang, accs[i].name, alen) &&
((lang[alen] == 0) || (lang[alen] == '-')) ) {
if (alen > longest_lang_range_len) {
longest_lang_range_len = alen;
bestthistag = &accs[i];
}
}
if (!bestthistag && !neg->dont_fiddle_headers) {
/* The next bit is a fiddle. Some browsers might
* be configured to send more specific language
* ranges than desirable. For example, an
* Accept-Language of en-US should never match
* variants with languages en or en-GB. But US
* English speakers might pick en-US as their
* language choice. So this fiddle checks if the
* language range has a prefix, and if so, it
* matches variants which match that prefix with a
* priority of 0.001. So a request for en-US would
* match variants of types en and en-GB, but at
* much lower priority than matches of en-US
* directly, or of any other language listed on
* the Accept-Language header. Note that this
* fiddle does not handle multi-level prefixes.
*/
if ((p = strchr(accs[i].name, '-'))) {
int plen = p - accs[i].name;
if (!strncmp(lang, accs[i].name, plen)) {
fiddle_q = 0.001f;
}
}
}
}
/* Finished looking at Accept-Language headers, the best
* (longest) match is in bestthistag, or NULL if no match
*/
if (!best ||
(bestthistag && bestthistag->quality > best->quality)) {
best = bestthistag;
}
/* See if the tag matches on a * in the Accept-Language
* header. If so, record this fact for later use
*/
if (!bestthistag && star) {
any_match_on_star = 1;
}
}
/* If one of the language tags of the variant matched on *, we
* need to see if its q is better than that of any non-* match
* on any other tag of the variant. If so the * match takes
* precedence and the overall match is not definite.
*/
if ( any_match_on_star &&
((best && star->quality > best->quality) ||
(!best)) ) {
best = star;
variant->definite = 0;
}
variant->lang_quality = best ? best->quality : fiddle_q;
}
}
/* Now set the old lang_index field. Since this is old
* stuff anyway, don't bother with handling multiple languages
* per variant, just use the first one assigned to it
*/
idx = 0;
if (variant->content_languages && variant->content_languages->nelts) {
firstlang = ((char **) variant->content_languages->elts)[0];
}
else {
firstlang = "";
}
if (!neg->accept_langs) { /* Client doesn't care */
idx = find_default_index((neg_dir_config *) ap_get_module_config(
neg->r->per_dir_config, &negotiation_module),
firstlang);
}
else { /* Client has Accept-Language */
idx = find_lang_index(neg->accept_langs, firstlang);
}
variant->lang_index = idx;
return;
}
/* Determining the content length --- if the map didn't tell us,
* we have to do a stat() and remember for next time.
*/
static apr_off_t find_content_length(negotiation_state *neg, var_rec *variant)
{
apr_finfo_t statb;
if (variant->bytes < 0) {
if (variant->sub_req && (variant->sub_req->finfo.valid & APR_FINFO_SIZE)) {
variant->bytes = variant->sub_req->finfo.size;
}
else {
char *fullname = ap_make_full_path(neg->pool, neg->dir_name,
variant->file_name);
if (apr_stat(&statb, fullname,
APR_FINFO_SIZE, neg->pool) == APR_SUCCESS) {
variant->bytes = statb.size;
}
}
}
return variant->bytes;
}
/* For a given variant, find the best matching Accept: header
* and assign the Accept: header's quality value to the
* mime_type_quality field of the variant, for later use in
* determining the best matching variant.
*/
static void set_accept_quality(negotiation_state *neg, var_rec *variant)
{
int i;
accept_rec *accept_recs;
float q = 0.0f;
int q_definite = 1;
/* if no Accept: header, leave quality alone (will
* remain at the default value of 1)
*
* XXX: This if is currently never true because of the effect of
* maybe_add_default_accepts().
*/
if (!neg->accepts) {
if (variant->mime_type && *variant->mime_type)
variant->definite = 0;
return;
}
accept_recs = (accept_rec *) neg->accepts->elts;
/*
* Go through each of the ranges on the Accept: header,
* looking for the 'best' match with this variant's
* content-type. We use the best match's quality
* value (from the Accept: header) for this variant's
* mime_type_quality field.
*
* The best match is determined like this:
* type/type is better than type/ * is better than * / *
* if match is type/type, use the level mime param if available
*/
for (i = 0; i < neg->accepts->nelts; ++i) {
accept_rec *type = &accept_recs[i];
int prev_mime_stars;
prev_mime_stars = variant->mime_stars;
if (!mime_match(type, variant)) {
continue; /* didn't match the content type at all */
}
else {
/* did match - see if there were less or more stars than
* in previous match
*/
if (prev_mime_stars == variant->mime_stars) {
continue; /* more stars => not as good a match */
}
}
/* If we are allowed to mess with the q-values
* and have no explicit q= parameters in the accept header,
* make wildcards very low, so we have a low chance
* of ending up with them if there's something better.
*/
if (!neg->dont_fiddle_headers && !neg->accept_q &&
variant->mime_stars == 1) {
q = 0.01f;
}
else if (!neg->dont_fiddle_headers && !neg->accept_q &&
variant->mime_stars == 2) {
q = 0.02f;
}
else {
q = type->quality;
}
q_definite = (variant->mime_stars == 3);
}
variant->mime_type_quality = q;
variant->definite = variant->definite && q_definite;
}
/* For a given variant, find the 'q' value of the charset given
* on the Accept-Charset line. If no charsets are listed,
* assume value of '1'.
*/
static void set_charset_quality(negotiation_state *neg, var_rec *variant)
{
int i;
accept_rec *accept_recs;
const char *charset = variant->content_charset;
accept_rec *star = NULL;
/* if no Accept-Charset: header, leave quality alone (will
* remain at the default value of 1)
*/
if (!neg->accept_charsets) {
if (charset && *charset)
variant->definite = 0;
return;
}
accept_recs = (accept_rec *) neg->accept_charsets->elts;
if (charset == NULL || !*charset) {
/* Charset of variant not known */
/* if not a text / * type, leave quality alone */
if (!(!strncmp(variant->mime_type, "text/", 5)
|| !strcmp(variant->mime_type, INCLUDES_MAGIC_TYPE)
|| !strcmp(variant->mime_type, INCLUDES_MAGIC_TYPE3)
))
return;
/* Don't go guessing if we are in strict header mode,
* e.g. when running the rvsa, as any guess won't be reflected
* in the variant list or content-location headers.
*/
if (neg->dont_fiddle_headers)
return;
charset = "iso-8859-1"; /* The default charset for HTTP text types */
}
/*
* Go through each of the items on the Accept-Charset header,
* looking for a match with this variant's charset. If none
* match, charset is unacceptable, so set quality to 0.
*/
for (i = 0; i < neg->accept_charsets->nelts; ++i) {
accept_rec *type = &accept_recs[i];
if (!strcmp(type->name, charset)) {
variant->charset_quality = type->quality;
return;
}
else if (strcmp(type->name, "*") == 0) {
star = type;
}
}
/* No explicit match */
if (star) {
variant->charset_quality = star->quality;
variant->definite = 0;
return;
}
/* If this variant is in charset iso-8859-1, the default is 1.0 */
if (strcmp(charset, "iso-8859-1") == 0) {
variant->charset_quality = 1.0f;
}
else {
variant->charset_quality = 0.0f;
}
}
/* is_identity_encoding is included for back-compat, but does anyone
* use 7bit, 8bin or binary in their var files??
*/
static int is_identity_encoding(const char *enc)
{
return (!enc || !enc[0] || !strcmp(enc, "7bit") || !strcmp(enc, "8bit")
|| !strcmp(enc, "binary"));
}
/*
* set_encoding_quality determines whether the encoding for a particular
* variant is acceptable for the user-agent.
*
* The rules for encoding are that if the user-agent does not supply
* any Accept-Encoding header, then all encodings are allowed but a
* variant with no encoding should be preferred.
* If there is an empty Accept-Encoding header, then no encodings are
* acceptable. If there is a non-empty Accept-Encoding header, then
* any of the listed encodings are acceptable, as well as no encoding
* unless the "identity" encoding is specifically excluded.
*/
static void set_encoding_quality(negotiation_state *neg, var_rec *variant)
{
accept_rec *accept_recs;
const char *enc = variant->content_encoding;
accept_rec *star = NULL;
float value_if_not_found = 0.0f;
int i;
if (!neg->accept_encodings) {
/* We had no Accept-Encoding header, assume that all
* encodings are acceptable with a low quality,
* but we prefer no encoding if available.
*/
if (!enc || is_identity_encoding(enc))
variant->encoding_quality = 1.0f;
else
variant->encoding_quality = 0.5f;
return;
}
if (!enc || is_identity_encoding(enc)) {
enc = "identity";
value_if_not_found = 0.0001f;
}
accept_recs = (accept_rec *) neg->accept_encodings->elts;
/* Go through each of the encodings on the Accept-Encoding: header,
* looking for a match with our encoding. x- prefixes are ignored.
*/
if (enc[0] == 'x' && enc[1] == '-') {
enc += 2;
}
for (i = 0; i < neg->accept_encodings->nelts; ++i) {
char *name = accept_recs[i].name;
if (name[0] == 'x' && name[1] == '-') {
name += 2;
}
if (!strcmp(name, enc)) {
variant->encoding_quality = accept_recs[i].quality;
return;
}
if (strcmp(name, "*") == 0) {
star = &accept_recs[i];
}
}
/* No explicit match */
if (star) {
variant->encoding_quality = star->quality;
return;
}
/* Encoding not found on Accept-Encoding: header, so it is
* _not_ acceptable unless it is the identity (no encoding)
*/
variant->encoding_quality = value_if_not_found;
}
/*************************************************************
* Possible results of the variant selection algorithm
*/
enum algorithm_results {
alg_choice = 1, /* choose variant */
alg_list /* list variants */
};
/* Below is the 'best_match' function. It returns an int, which has
* one of the two values alg_choice or alg_list, which give the result
* of the variant selection algorithm. alg_list means that no best
* variant was found by the algorithm, alg_choice means that a best
* variant was found and should be returned. The list/choice
* terminology comes from TCN (rfc2295), but is used in a more generic
* way here. The best variant is returned in *pbest. best_match has
* two possible algorithms for determining the best variant: the
* RVSA/1.0 algorithm (from RFC2296), and the standard Apache
* algorithm. These are split out into separate functions
* (is_variant_better_rvsa() and is_variant_better()). Selection of
* one is through the neg->use_rvsa flag.
*
* The call to best_match also creates full information, including
* language, charset, etc quality for _every_ variant. This is needed
* for generating a correct Vary header, and can be used for the
* Alternates header, the human-readable list responses and 406 errors.
*/
/* Firstly, the RVSA/1.0 (HTTP Remote Variant Selection Algorithm
* v1.0) from rfc2296. This is the algorithm that goes together with
* transparent content negotiation (TCN).
*/
static int is_variant_better_rvsa(negotiation_state *neg, var_rec *variant,
var_rec *best, float *p_bestq)
{
float bestq = *p_bestq, q;
/* TCN does not cover negotiation on content-encoding. For now,
* we ignore the encoding unless it was explicitly excluded.
*/
if (variant->encoding_quality == 0.0f)
return 0;
q = variant->mime_type_quality *
variant->source_quality *
variant->charset_quality *
variant->lang_quality;
/* RFC 2296 calls for the result to be rounded to 5 decimal places,
* but we don't do that because it serves no useful purpose other
* than to ensure that a remote algorithm operates on the same
* precision as ours. That is silly, since what we obviously want
* is for the algorithm to operate on the best available precision
* regardless of who runs it. Since the above calculation may
* result in significant variance at 1e-12, rounding would be bogus.
*/
#ifdef NEG_DEBUG
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Variant: file=%s type=%s lang=%s sourceq=%1.3f "
"mimeq=%1.3f langq=%1.3f charq=%1.3f encq=%1.3f "
"q=%1.5f definite=%d",
(variant->file_name ? variant->file_name : ""),
(variant->mime_type ? variant->mime_type : ""),
(variant->content_languages
? apr_array_pstrcat(neg->pool, variant->content_languages, ',')
: ""),
variant->source_quality,
variant->mime_type_quality,
variant->lang_quality,
variant->charset_quality,
variant->encoding_quality,
q,
variant->definite);
#endif
if (q <= 0.0f) {
return 0;
}
if (q > bestq) {
*p_bestq = q;
return 1;
}
if (q == bestq) {
/* If the best variant's encoding is of lesser quality than
* this variant, then we prefer this variant
*/
if (variant->encoding_quality > best->encoding_quality) {
*p_bestq = q;
return 1;
}
}
return 0;
}
/* Negotiation algorithm as used by previous versions of Apache
* (just about).
*/
static int is_variant_better(negotiation_state *neg, var_rec *variant,
var_rec *best, float *p_bestq)
{
float bestq = *p_bestq, q;
int levcmp;
/* For non-transparent negotiation, server can choose how
* to handle the negotiation. We'll use the following in
* order: content-type, language, content-type level, charset,
* content encoding, content length.
*
* For each check, we have three possible outcomes:
* This variant is worse than current best: return 0
* This variant is better than the current best:
* assign this variant's q to *p_bestq, and return 1
* This variant is just as desirable as the current best:
* drop through to the next test.
*
* This code is written in this long-winded way to allow future
* customisation, either by the addition of additional
* checks, or to allow the order of the checks to be determined
* by configuration options (e.g. we might prefer to check
* language quality _before_ content type).
*/
/* First though, eliminate this variant if it is not
* acceptable by type, charset, encoding or language.
*/
#ifdef NEG_DEBUG
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Variant: file=%s type=%s lang=%s sourceq=%1.3f "
"mimeq=%1.3f langq=%1.3f langidx=%d charq=%1.3f encq=%1.3f ",
(variant->file_name ? variant->file_name : ""),
(variant->mime_type ? variant->mime_type : ""),
(variant->content_languages
? apr_array_pstrcat(neg->pool, variant->content_languages, ',')
: ""),
variant->source_quality,
variant->mime_type_quality,
variant->lang_quality,
variant->lang_index,
variant->charset_quality,
variant->encoding_quality);
#endif
if (variant->encoding_quality == 0.0f ||
variant->lang_quality == 0.0f ||
variant->source_quality == 0.0f ||
variant->charset_quality == 0.0f ||
variant->mime_type_quality == 0.0f) {
return 0; /* don't consider unacceptables */
}
q = variant->mime_type_quality * variant->source_quality;
if (q == 0.0 || q < bestq) {
return 0;
}
if (q > bestq || !best) {
*p_bestq = q;
return 1;
}
/* language */
if (variant->lang_quality < best->lang_quality) {
return 0;
}
if (variant->lang_quality > best->lang_quality) {
*p_bestq = q;
return 1;
}
/* if language qualities were equal, try the LanguagePriority stuff */
if (best->lang_index != -1 &&
(variant->lang_index == -1 || variant->lang_index > best->lang_index)) {
return 0;
}
if (variant->lang_index != -1 &&
(best->lang_index == -1 || variant->lang_index < best->lang_index)) {
*p_bestq = q;
return 1;
}
/* content-type level (sometimes used with text/html, though we
* support it on other types too)
*/
levcmp = level_cmp(variant, best);
if (levcmp == -1) {
return 0;
}
if (levcmp == 1) {
*p_bestq = q;
return 1;
}
/* charset */
if (variant->charset_quality < best->charset_quality) {
return 0;
}
/* If the best variant's charset is ISO-8859-1 and this variant has
* the same charset quality, then we prefer this variant
*/
if (variant->charset_quality > best->charset_quality ||
((variant->content_charset != NULL &&
*variant->content_charset != '\0' &&
strcmp(variant->content_charset, "iso-8859-1") != 0) &&
(best->content_charset == NULL ||
*best->content_charset == '\0' ||
strcmp(best->content_charset, "iso-8859-1") == 0))) {
*p_bestq = q;
return 1;
}
/* Prefer the highest value for encoding_quality.
*/
if (variant->encoding_quality < best->encoding_quality) {
return 0;
}
if (variant->encoding_quality > best->encoding_quality) {
*p_bestq = q;
return 1;
}
/* content length if all else equal */
if (find_content_length(neg, variant) >= find_content_length(neg, best)) {
return 0;
}
/* ok, to get here means every thing turned out equal, except
* we have a shorter content length, so use this variant
*/
*p_bestq = q;
return 1;
}
static int best_match(negotiation_state *neg, var_rec **pbest)
{
int j;
var_rec *best = NULL;
float bestq = 0.0f;
enum algorithm_results algorithm_result;
var_rec *avail_recs = (var_rec *) neg->avail_vars->elts;
set_default_lang_quality(neg);
/*
* Find the 'best' variant
*/
for (j = 0; j < neg->avail_vars->nelts; ++j) {
var_rec *variant = &avail_recs[j];
/* Find all the relevant 'quality' values from the
* Accept... headers, and store in the variant. This also
* prepares for sending an Alternates header etc so we need to
* do it even if we do not actually plan to find a best
* variant.
*/
set_accept_quality(neg, variant);
set_language_quality(neg, variant);
set_encoding_quality(neg, variant);
set_charset_quality(neg, variant);
/* Only do variant selection if we may actually choose a
* variant for the client
*/
if (neg->may_choose) {
/* Now find out if this variant is better than the current
* best, either using the RVSA/1.0 algorithm, or Apache's
* internal server-driven algorithm. Presumably other
* server-driven algorithms are possible, and could be
* implemented here.
*/
if (neg->use_rvsa) {
if (is_variant_better_rvsa(neg, variant, best, &bestq)) {
best = variant;
}
}
else {
if (is_variant_better(neg, variant, best, &bestq)) {
best = variant;
}
}
}
}
/* We now either have a best variant, or no best variant */
if (neg->use_rvsa) {
/* calculate result for RVSA/1.0 algorithm:
* only a choice response if the best variant has q>0
* and is definite
*/
algorithm_result = (best && best->definite) && (bestq > 0) ?
alg_choice : alg_list;
}
else {
/* calculate result for Apache negotiation algorithm */
algorithm_result = bestq > 0 ? alg_choice : alg_list;
}
/* Returning a choice response with a non-neighboring variant is a
* protocol security error in TCN (see rfc2295). We do *not*
* verify here that the variant and URI are neighbors, even though
* we may return alg_choice. We depend on the environment (the
* caller) to only declare the resource transparently negotiable if
* all variants are neighbors.
*/
*pbest = best;
return algorithm_result;
}
/* Sets response headers for a negotiated response.
* neg->is_transparent determines whether a transparently negotiated
* response or a plain `server driven negotiation' response is
* created. Applicable headers are Alternates, Vary, and TCN.
*
* The Vary header we create is sometimes longer than is required for
* the correct caching of negotiated results by HTTP/1.1 caches. For
* example if we have 3 variants x.html, x.ps.en and x.ps.nl, and if
* the Accept: header assigns a 0 quality to .ps, then the results of
* the two server-side negotiation algorithms we currently implement
* will never depend on Accept-Language so we could return `Vary:
* negotiate, accept' instead of the longer 'Vary: negotiate, accept,
* accept-language' which the code below will return. A routine for
* computing the exact minimal Vary header would be a huge pain to code
* and maintain though, especially because we need to take all possible
* twiddles in the server-side negotiation algorithms into account.
*/
static void set_neg_headers(request_rec *r, negotiation_state *neg,
int alg_result)
{
apr_table_t *hdrs;
var_rec *avail_recs = (var_rec *) neg->avail_vars->elts;
const char *sample_type = NULL;
const char *sample_language = NULL;
const char *sample_encoding = NULL;
const char *sample_charset = NULL;
char *lang;
char *qstr;
char *lenstr;
apr_off_t len;
apr_array_header_t *arr;
int max_vlist_array = (neg->avail_vars->nelts * 21);
int first_variant = 1;
int vary_by_type = 0;
int vary_by_language = 0;
int vary_by_charset = 0;
int vary_by_encoding = 0;
int j;
/* In order to avoid O(n^2) memory copies in building Alternates,
* we preallocate a apr_table_t with the maximum substrings possible,
* fill it with the variant list, and then concatenate the entire array.
* Note that if you change the number of substrings pushed, you also
* need to change the calculation of max_vlist_array above.
*/
if (neg->send_alternates && neg->avail_vars->nelts)
arr = apr_array_make(r->pool, max_vlist_array, sizeof(char *));
else
arr = NULL;
/* Put headers into err_headers_out, since send_http_header()
* outputs both headers_out and err_headers_out.
*/
hdrs = r->err_headers_out;
for (j = 0; j < neg->avail_vars->nelts; ++j) {
var_rec *variant = &avail_recs[j];
if (variant->content_languages && variant->content_languages->nelts) {
lang = apr_array_pstrcat(r->pool, variant->content_languages, ',');
}
else {
lang = NULL;
}
/* Calculate Vary by looking for any difference between variants */
if (first_variant) {
sample_type = variant->mime_type;
sample_charset = variant->content_charset;
sample_language = lang;
sample_encoding = variant->content_encoding;
}
else {
if (!vary_by_type &&
strcmp(sample_type ? sample_type : "",
variant->mime_type ? variant->mime_type : "")) {
vary_by_type = 1;
}
if (!vary_by_charset &&
strcmp(sample_charset ? sample_charset : "",
variant->content_charset ?
variant->content_charset : "")) {
vary_by_charset = 1;
}
if (!vary_by_language &&
strcmp(sample_language ? sample_language : "",
lang ? lang : "")) {
vary_by_language = 1;
}
if (!vary_by_encoding &&
strcmp(sample_encoding ? sample_encoding : "",
variant->content_encoding ?
variant->content_encoding : "")) {
vary_by_encoding = 1;
}
}
first_variant = 0;
if (!neg->send_alternates)
continue;
/* Generate the string components for this Alternates entry */
*((const char **) apr_array_push(arr)) = "{\"";
*((const char **) apr_array_push(arr)) = variant->file_name;
*((const char **) apr_array_push(arr)) = "\" ";
qstr = (char *) apr_palloc(r->pool, 6);
apr_snprintf(qstr, 6, "%1.3f", variant->source_quality);
/* Strip trailing zeros (saves those valuable network bytes) */
if (qstr[4] == '0') {
qstr[4] = '\0';
if (qstr[3] == '0') {
qstr[3] = '\0';
if (qstr[2] == '0') {
qstr[1] = '\0';
}
}
}
*((const char **) apr_array_push(arr)) = qstr;
if (variant->mime_type && *variant->mime_type) {
*((const char **) apr_array_push(arr)) = " {type ";
*((const char **) apr_array_push(arr)) = variant->mime_type;
*((const char **) apr_array_push(arr)) = "}";
}
if (variant->content_charset && *variant->content_charset) {
*((const char **) apr_array_push(arr)) = " {charset ";
*((const char **) apr_array_push(arr)) = variant->content_charset;
*((const char **) apr_array_push(arr)) = "}";
}
if (lang) {
*((const char **) apr_array_push(arr)) = " {language ";
*((const char **) apr_array_push(arr)) = lang;
*((const char **) apr_array_push(arr)) = "}";
}
if (variant->content_encoding && *variant->content_encoding) {
/* Strictly speaking, this is non-standard, but so is TCN */
*((const char **) apr_array_push(arr)) = " {encoding ";
*((const char **) apr_array_push(arr)) = variant->content_encoding;
*((const char **) apr_array_push(arr)) = "}";
}
/* Note that the Alternates specification (in rfc2295) does
* not require that we include {length x}, so we could omit it
* if determining the length is too expensive. We currently
* always include it though. 22 bytes is enough for 2^64.
*
* If the variant is a CGI script, find_content_length would
* return the length of the script, not the output it
* produces, so we check for the presence of a handler and if
* there is one we don't add a length.
*
* XXX: TODO: This check does not detect a CGI script if we
* get the variant from a type map. This needs to be fixed
* (without breaking things if the type map specifies a
* content-length, which currently leads to the correct result).
*/
if (!(variant->sub_req && variant->sub_req->handler)
&& (len = find_content_length(neg, variant)) >= 0) {
lenstr = (char *) apr_palloc(r->pool, 22);
apr_snprintf(lenstr, 22, "%" APR_OFF_T_FMT, len);
*((const char **) apr_array_push(arr)) = " {length ";
*((const char **) apr_array_push(arr)) = lenstr;
*((const char **) apr_array_push(arr)) = "}";
}
*((const char **) apr_array_push(arr)) = "}";
*((const char **) apr_array_push(arr)) = ", "; /* trimmed below */
}
if (neg->send_alternates && neg->avail_vars->nelts) {
arr->nelts--; /* remove last comma */
apr_table_mergen(hdrs, "Alternates",
apr_array_pstrcat(r->pool, arr, '\0'));
}
if (neg->is_transparent || vary_by_type || vary_by_language ||
vary_by_language || vary_by_charset || vary_by_encoding) {
apr_table_mergen(hdrs, "Vary", 2 + apr_pstrcat(r->pool,
neg->is_transparent ? ", negotiate" : "",
vary_by_type ? ", accept" : "",
vary_by_language ? ", accept-language" : "",
vary_by_charset ? ", accept-charset" : "",
vary_by_encoding ? ", accept-encoding" : "", NULL));
}
if (neg->is_transparent) { /* Create TCN response header */
apr_table_setn(hdrs, "TCN",
alg_result == alg_list ? "list" : "choice");
}
}
/**********************************************************************
*
* Return an HTML list of variants. This is output as part of the
* choice response or 406 status body.
*/
static char *make_variant_list(request_rec *r, negotiation_state *neg)
{
apr_array_header_t *arr;
int i;
int max_vlist_array = (neg->avail_vars->nelts * 15) + 2;
/* In order to avoid O(n^2) memory copies in building the list,
* we preallocate a apr_table_t with the maximum substrings possible,
* fill it with the variant list, and then concatenate the entire array.
*/
arr = apr_array_make(r->pool, max_vlist_array, sizeof(char *));
*((const char **) apr_array_push(arr)) = "Available variants:\n<ul>\n";
for (i = 0; i < neg->avail_vars->nelts; ++i) {
var_rec *variant = &((var_rec *) neg->avail_vars->elts)[i];
const char *filename = variant->file_name ? variant->file_name : "";
apr_array_header_t *languages = variant->content_languages;
const char *description = variant->description ? variant->description : "";
/* The format isn't very neat, and it would be nice to make
* the tags human readable (eg replace 'language en' with 'English').
* Note that if you change the number of substrings pushed, you also
* need to change the calculation of max_vlist_array above.
*/
*((const char **) apr_array_push(arr)) = "<li><a href=\"";
*((const char **) apr_array_push(arr)) = filename;
*((const char **) apr_array_push(arr)) = "\">";
*((const char **) apr_array_push(arr)) = filename;
*((const char **) apr_array_push(arr)) = "</a> ";
*((const char **) apr_array_push(arr)) = description;
if (variant->mime_type && *variant->mime_type) {
*((const char **) apr_array_push(arr)) = ", type ";
*((const char **) apr_array_push(arr)) = variant->mime_type;
}
if (languages && languages->nelts) {
*((const char **) apr_array_push(arr)) = ", language ";
*((const char **) apr_array_push(arr)) = apr_array_pstrcat(r->pool,
languages, ',');
}
if (variant->content_charset && *variant->content_charset) {
*((const char **) apr_array_push(arr)) = ", charset ";
*((const char **) apr_array_push(arr)) = variant->content_charset;
}
if (variant->content_encoding) {
*((const char **) apr_array_push(arr)) = ", encoding ";
*((const char **) apr_array_push(arr)) = variant->content_encoding;
}
*((const char **) apr_array_push(arr)) = "</li>\n";
}
*((const char **) apr_array_push(arr)) = "</ul>\n";
return apr_array_pstrcat(r->pool, arr, '\0');
}
static void store_variant_list(request_rec *r, negotiation_state *neg)
{
if (r->main == NULL) {
apr_table_setn(r->notes, "variant-list", make_variant_list(r, neg));
}
else {
apr_table_setn(r->main->notes, "variant-list",
make_variant_list(r->main, neg));
}
}
/* Called if we got a "Choice" response from the variant selection algorithm.
* It checks the result of the chosen variant to see if it
* is itself negotiated (if so, return error HTTP_VARIANT_ALSO_VARIES).
* Otherwise, add the appropriate headers to the current response.
*/
static int setup_choice_response(request_rec *r, negotiation_state *neg,
var_rec *variant)
{
request_rec *sub_req;
const char *sub_vary;
if (!variant->sub_req) {
int status;
sub_req = ap_sub_req_lookup_file(variant->file_name, r, NULL);
status = sub_req->status;
if (status != HTTP_OK &&
!apr_table_get(sub_req->err_headers_out, "TCN")) {
ap_destroy_sub_req(sub_req);
return status;
}
variant->sub_req = sub_req;
}
else {
sub_req = variant->sub_req;
}
/* The variant selection algorithm told us to return a "Choice"
* response. This is the normal variant response, with
* some extra headers. First, ensure that the chosen
* variant did or will not itself engage in transparent negotiation.
* If not, set the appropriate headers, and fall through to
* the normal variant handling
*/
/* This catches the error that a transparent type map selects a
* transparent multiviews resource as the best variant.
*
* XXX: We do not signal an error if a transparent type map
* selects a _non_transparent multiviews resource as the best
* variant, because we can generate a legal negotiation response
* in this case. In this case, the vlist_validator of the
* nontransparent subrequest will be lost however. This could
* lead to cases in which a change in the set of variants or the
* negotiation algorithm of the nontransparent resource is never
* propagated up to a HTTP/1.1 cache which interprets Vary. To be
* completely on the safe side we should return HTTP_VARIANT_ALSO_VARIES
* for this type of recursive negotiation too.
*/
if (neg->is_transparent &&
apr_table_get(sub_req->err_headers_out, "TCN")) {
return HTTP_VARIANT_ALSO_VARIES;
}
/* This catches the error that a transparent type map recursively
* selects, as the best variant, another type map which itself
* causes transparent negotiation to be done.
*
* XXX: Actually, we catch this error by catching all cases of
* type map recursion. There are some borderline recursive type
* map arrangements which would not produce transparent
* negotiation protocol errors or lack of cache propagation
* problems, but such arrangements are very hard to detect at this
* point in the control flow, so we do not bother to single them
* out.
*
* Recursive type maps imply a recursive arrangement of negotiated
* resources which is visible to outside clients, and this is not
* supported by the transparent negotiation caching protocols, so
* if we are to have generic support for recursive type maps, we
* have to create some configuration setting which makes all type
* maps non-transparent when recursion is enabled. Also, if we
* want recursive type map support which ensures propagation of
* type map changes into HTTP/1.1 caches that handle Vary, we
* would have to extend the current mechanism for generating
* variant list validators.
*/
if (sub_req->handler && strcmp(sub_req->handler, "type-map") == 0) {
return HTTP_VARIANT_ALSO_VARIES;
}
/* This adds an appropriate Variant-Vary header if the subrequest
* is a multiviews resource.
*
* XXX: TODO: Note that this does _not_ handle any Vary header
* returned by a CGI if sub_req is a CGI script, because we don't
* see that Vary header yet at this point in the control flow.
* This won't cause any cache consistency problems _unless_ the
* CGI script also returns a Cache-Control header marking the
* response as cachable. This needs to be fixed, also there are
* problems if a CGI returns an Etag header which also need to be
* fixed.
*/
if ((sub_vary = apr_table_get(sub_req->err_headers_out, "Vary")) != NULL) {
apr_table_setn(r->err_headers_out, "Variant-Vary", sub_vary);
/* Move the subreq Vary header into the main request to
* prevent having two Vary headers in the response, which
* would be legal but strange.
*/
apr_table_setn(r->err_headers_out, "Vary", sub_vary);
apr_table_unset(sub_req->err_headers_out, "Vary");
}
apr_table_setn(r->err_headers_out, "Content-Location",
apr_pstrdup(r->pool, variant->file_name));
set_neg_headers(r, neg, alg_choice); /* add Alternates and Vary */
/* Still to do by caller: add Expires */
return 0;
}
/****************************************************************
*
* Executive...
*/
static int do_negotiation(request_rec *r, negotiation_state *neg,
var_rec **bestp, int prefer_scripts)
{
var_rec *avail_recs = (var_rec *) neg->avail_vars->elts;
int alg_result; /* result of variant selection algorithm */
int res;
int j;
/* Decide if resource is transparently negotiable */
/* GET or HEAD? (HEAD has same method number as GET) */
if (r->method_number == M_GET) {
/* maybe this should be configurable, see also the comment
* about recursive type maps in setup_choice_response()
*/
neg->is_transparent = 1;
/* We can't be transparent if we are a map file in the middle
* of the request URI.
*/
if (r->path_info && *r->path_info)
neg->is_transparent = 0;
for (j = 0; j < neg->avail_vars->nelts; ++j) {
var_rec *variant = &avail_recs[j];
/* We can't be transparent, because of internal
* assumptions in best_match(), if there is a
* non-neighboring variant. We can have a non-neighboring
* variant when processing a type map.
*/
if (ap_strchr_c(variant->file_name, '/'))
neg->is_transparent = 0;
}
}
if (neg->is_transparent) {
parse_negotiate_header(r, neg);
}
else { /* configure negotiation on non-transparent resource */
neg->may_choose = 1;
}
maybe_add_default_accepts(neg, prefer_scripts);
alg_result = best_match(neg, bestp);
/* alg_result is one of
* alg_choice: a best variant is chosen
* alg_list: no best variant is chosen
*/
if (alg_result == alg_list) {
/* send a list response or HTTP_NOT_ACCEPTABLE error response */
neg->send_alternates = 1; /* always include Alternates header */
set_neg_headers(r, neg, alg_result);
store_variant_list(r, neg);
if (neg->is_transparent && neg->ua_supports_trans) {
/* XXX todo: expires? cachability? */
/* Some HTTP/1.0 clients are known to choke when they get
* a 300 (multiple choices) response without a Location
* header. However the 300 code response we are are about
* to generate will only reach 1.0 clients which support
* transparent negotiation, and they should be OK. The
* response should never reach older 1.0 clients, even if
* we have CacheNegotiatedDocs enabled, because no 1.0
* proxy cache (we know of) will cache and return 300
* responses (they certainly won't if they conform to the
* HTTP/1.0 specification).
*/
return HTTP_MULTIPLE_CHOICES;
}
if (!*bestp) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"no acceptable variant: %s", r->filename);
return HTTP_NOT_ACCEPTABLE;
}
}
/* Variant selection chose a variant */
/* XXX todo: merge the two cases in the if statement below */
if (neg->is_transparent) {
if ((res = setup_choice_response(r, neg, *bestp)) != 0) {
return res; /* return if error */
}
}
else {
set_neg_headers(r, neg, alg_result);
}
/* Make sure caching works - Vary should handle HTTP/1.1, but for
* HTTP/1.0, we can't allow caching at all.
*/
/* XXX: Note that we only set r->no_cache to 1, which causes
* Expires: <now> to be added, when responding to a HTTP/1.0
* client. If we return the response to a 1.1 client, we do not
* add Expires <now>, because doing so would degrade 1.1 cache
* performance by preventing re-use of the response without prior
* revalidation. On the other hand, if the 1.1 client is a proxy
* which was itself contacted by a 1.0 client, or a proxy cache
* which can be contacted later by 1.0 clients, then we currently
* rely on this 1.1 proxy to add the Expires: <now> when it
* forwards the response.
*
* XXX: TODO: Find out if the 1.1 spec requires proxies and
* tunnels to add Expires: <now> when forwarding the response to
* 1.0 clients. I (kh) recall it is rather vague on this point.
* Testing actual 1.1 proxy implementations would also be nice. If
* Expires: <now> is not added by proxies then we need to always
* include Expires: <now> ourselves to ensure correct caching, but
* this would degrade HTTP/1.1 cache efficiency unless we also add
* Cache-Control: max-age=N, which we currently don't.
*
* Roy: No, we are not going to screw over HTTP future just to
* ensure that people who can't be bothered to upgrade their
* clients will always receive perfect server-side negotiation.
* Hell, those clients are sending bogus accept headers anyway.
*
* Manual setting of cache-control/expires always overrides this
* automated kluge, on purpose.
*/
if ((!do_cache_negotiated_docs(r->server)
&& (r->proto_num < HTTP_VERSION(1,1)))
&& neg->count_multiviews_variants != 1) {
r->no_cache = 1;
}
return OK;
}
static int handle_map_file(request_rec *r)
{
negotiation_state *neg;
apr_file_t *map;
var_rec *best;
int res;
char *udir;
if(strcmp(r->handler,MAP_FILE_MAGIC_TYPE) && strcmp(r->handler,"type-map"))
return DECLINED;
neg = parse_accept_headers(r);
if ((res = read_type_map(&map, neg, r))) {
return res;
}
res = do_negotiation(r, neg, &best, 0);
if (res != 0) return res;
if (best->body)
{
apr_bucket_brigade *bb;
apr_bucket *e;
ap_allow_standard_methods(r, REPLACE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
if ((res = ap_discard_request_body(r)) != OK) {
return res;
}
/*if (r->method_number == M_OPTIONS) {
* return ap_send_http_options(r);
*}
*/
if (r->method_number != M_GET && r->method_number != M_POST) {
return HTTP_METHOD_NOT_ALLOWED;
}
/* ### These may be implemented by adding some 'extra' info
* of the file offset onto the etag
* ap_update_mtime(r, r->finfo.mtime);
* ap_set_last_modified(r);
* ap_set_etag(r);
*/
apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
ap_set_content_length(r, best->bytes);
if ((res = ap_meets_conditions(r)) != OK) {
return res;
}
bb = apr_brigade_create(r->pool);
e = apr_bucket_file_create(map, best->body,
(apr_size_t)best->bytes, r->pool);
APR_BRIGADE_INSERT_TAIL(bb, e);
e = apr_bucket_eos_create();
APR_BRIGADE_INSERT_TAIL(bb, e);
return ap_pass_brigade(r->output_filters, bb);
}
/* XXX This looks entirely bogus. Why can't negotiated content
* have additional path_info? Just because I choose a given
* script based on content type doesn't mean I don't want it
* to run the same set of actions (think three languages of
* viewcvs.cgi.xx, all serving the same repository.)
*/
if (r->path_info && *r->path_info) {
r->uri[ap_find_path_info(r->uri, r->path_info)] = '\0';
}
udir = ap_make_dirstr_parent(r->pool, r->uri);
udir = ap_escape_uri(r->pool, udir);
ap_internal_redirect(apr_pstrcat(r->pool, udir, best->file_name,
r->path_info, NULL), r);
return OK;
}
static int handle_multi(request_rec *r)
{
negotiation_state *neg;
var_rec *best, *avail_recs;
request_rec *sub_req;
int res;
int j;
if (r->finfo.filetype != 0 || !(ap_allow_options(r) & OPT_MULTI)) {
return DECLINED;
}
neg = parse_accept_headers(r);
if ((res = read_types_multi(neg))) {
return_from_multi:
/* free all allocated memory from subrequests */
avail_recs = (var_rec *) neg->avail_vars->elts;
for (j = 0; j < neg->avail_vars->nelts; ++j) {
var_rec *variant = &avail_recs[j];
if (variant->sub_req) {
ap_destroy_sub_req(variant->sub_req);
}
}
return res;
}
if (neg->avail_vars->nelts == 0) {
return DECLINED;
}
res = do_negotiation(r, neg, &best,
(r->method_number != M_GET) || r->args ||
(r->path_info && *r->path_info));
if (res != 0)
goto return_from_multi;
if (!(sub_req = best->sub_req)) {
/* We got this out of a map file, so we don't actually have
* a sub_req structure yet. Get one now.
*/
sub_req = ap_sub_req_lookup_file(best->file_name, r, NULL);
if (sub_req->status != HTTP_OK) {
res = sub_req->status;
ap_destroy_sub_req(sub_req);
goto return_from_multi;
}
}
/* now do a "fast redirect" ... promotes the sub_req into the main req */
ap_internal_fast_redirect(sub_req, r);
/* give no advise for time on this subrequest. Perhaps we
* should tally the last mtime amoung all variants, and date
* the most recent, but that could confuse the proxies.
*/
r->mtime = 0;
/* clean up all but our favorite variant, since that sub_req
* is now merged into the main request!
*/
avail_recs = (var_rec *) neg->avail_vars->elts;
for (j = 0; j < neg->avail_vars->nelts; ++j) {
var_rec *variant = &avail_recs[j];
if (variant != best && variant->sub_req) {
ap_destroy_sub_req(variant->sub_req);
}
}
return OK;
}
/**********************************************************************
* There is a problem with content-encoding, as some clients send and
* expect an x- token (e.g. x-gzip) while others expect the plain token
* (i.e. gzip). To try and deal with this as best as possible we do
* the following: if the client sent an Accept-Encoding header and it
* contains a plain token corresponding to the content encoding of the
* response, then set content encoding using the plain token. Else if
* the A-E header contains the x- token use the x- token in the C-E
* header. Else don't do anything.
*
* Note that if no A-E header was sent, or it does not contain a token
* compatible with the final content encoding, then the token in the
* C-E header will be whatever was specified in the AddEncoding
* directive.
*/
static int fix_encoding(request_rec *r)
{
const char *enc = r->content_encoding;
char *x_enc = NULL;
apr_array_header_t *accept_encodings;
accept_rec *accept_recs;
int i;
if (!enc || !*enc) {
return DECLINED;
}
if (enc[0] == 'x' && enc[1] == '-') {
enc += 2;
}
if ((accept_encodings = do_header_line(r->pool,
apr_table_get(r->headers_in, "Accept-Encoding"))) == NULL) {
return DECLINED;
}
accept_recs = (accept_rec *) accept_encodings->elts;
for (i = 0; i < accept_encodings->nelts; ++i) {
char *name = accept_recs[i].name;
if (!strcmp(name, enc)) {
r->content_encoding = name;
return OK;
}
if (name[0] == 'x' && name[1] == '-' && !strcmp(name+2, enc)) {
x_enc = name;
}
}
if (x_enc) {
r->content_encoding = x_enc;
return OK;
}
return DECLINED;
}
static void register_hooks(apr_pool_t *p)
{
ap_hook_fixups(fix_encoding,NULL,NULL,APR_HOOK_MIDDLE);
ap_hook_type_checker(handle_multi,NULL,NULL,APR_HOOK_FIRST);
ap_hook_handler(handle_map_file,NULL,NULL,APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA negotiation_module =
{
STANDARD20_MODULE_STUFF,
create_neg_dir_config, /* dir config creator */
merge_neg_dir_configs, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
negotiation_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};