mod_mime.c revision 32c4bc04f89b16521718145dc731f750144d7b38
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* http_mime.c: Sends/gets MIME headers for requests
*
* Rob McCool
*
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_hash.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_request.h"
/* XXXX - fix me / EBCDIC
* there was a cludge here which would use its
* own version apr_isascii(). Indicating that
* on some platforms that might be needed.
*
* #define OS_ASC(c) (c) -- for mere mortals
* or
* #define OS_ASC(c) (ebcdic2ascii[c]) -- for dino's
*
* #define apr_isascii(c) ((OS_ASC(c) & 0x80) == 0)
*/
/* XXXXX - fix me - See note with NOT_PROXY
*/
typedef struct attrib_info {
char *name;
int offset;
} attrib_info;
/* Information to which an extension can be mapped
*/
typedef struct extension_info {
char *forced_type; /* Additional AddTyped stuff */
char *encoding_type; /* Added with AddEncoding... */
char *language_type; /* Added with AddLanguage... */
char *handler; /* Added with AddHandler... */
char *charset_type; /* Added with AddCharset... */
char *input_filters; /* Added with AddInputFilter... */
char *output_filters; /* Added with AddOutputFilter... */
#define MULTIMATCH_UNSET 0
#define MULTIMATCH_ANY 1
#define MULTIMATCH_NEGOTIATED 2
#define MULTIMATCH_HANDLERS 4
#define MULTIMATCH_FILTERS 8
typedef struct {
* extension_info structure */
char *default_language; /* Language if no AddLanguage ext found */
int multimatch; /* Extensions to include in multiview matching
* for filenames, e.g. Filters and Handlers
*/
typedef struct param_s {
char *attr;
char *val;
} param;
typedef struct {
char *type;
char *subtype;
} content_type;
static char tspecial[] = {
'(', ')', '<', '>', '@', ',', ';', ':',
'\\', '"', '/', '[', ']', '?', '=',
'\0'
};
{
return new;
}
/*
* Overlay one hash table of extension_mappings onto another
*/
static void *overlay_extension_mappings(apr_pool_t *p,
const void *key,
const void *overlay_val,
const void *base_val,
const void *data)
{
if (overlay_info->forced_type) {
}
if (overlay_info->encoding_type) {
}
if (overlay_info->language_type) {
}
if (overlay_info->handler) {
}
if (overlay_info->charset_type) {
}
if (overlay_info->input_filters) {
}
if (overlay_info->output_filters) {
}
return new_info;
}
/* Member is the offset within an extension_info of the pointer to reset
*/
{
int i;
}
}
}
{
NULL);
}
else {
}
else {
}
/* We may not be merging the tables, but if we potentially will change
* an exinfo member, then we are about to trounce it anyways.
* We must have a copy for safety.
*/
}
}
if (new->extension_mappings) {
if (add->remove_mappings)
}
return new;
}
{
mime_dir_config *m=m_;
#ifdef CASE_BLIND_FILESYSTEM
#endif
if (*key == '.')
++key;
if (!m->extension_mappings) {
}
else
if (!exinfo) {
}
return NULL;
}
/*
* Note handler names are un-added with each per_dir_config merge.
* This keeps the association from being inherited, but not
* from being re-added at a subordinate level.
*/
const char *ext)
{
if (*ext == '.')
++ext;
if (!m->remove_mappings) {
}
#ifdef CASE_BLIND_FILESYSTEM
#endif
return NULL;
}
/* The sole bit of server configuration that the MIME module has is
* the name of its config file, so...
*/
const char *arg)
{
(void *)arg);
return NULL;
}
const char *include)
{
if (strcasecmp(include, "Any") == 0) {
return "Any is incompatible with NegotiatedOnly, "
"Filters and Handlers";
m->multimatch |= MULTIMATCH_ANY;
}
else if (strcasecmp(include, "NegotiatedOnly") == 0) {
return "Any is incompatible with NegotiatedOnly, "
"Filters and Handlers";
m->multimatch |= MULTIMATCH_NEGOTIATED;
}
else if (strcasecmp(include, "Filters") == 0) {
| MULTIMATCH_ANY)))
return "Filters is incompatible with Any and NegotiatedOnly";
m->multimatch |= MULTIMATCH_FILTERS;
}
else if (strcasecmp(include, "Handlers") == 0) {
| MULTIMATCH_ANY)))
return "Handlers is incompatible with Any and NegotiatedOnly";
m->multimatch |= MULTIMATCH_HANDLERS;
}
else
return "Unrecognized option";
return NULL;
}
static const command_rec mime_cmds[] =
{
"a charset (e.g., iso-2022-jp), followed by one or more file extensions"),
"an encoding (e.g., gzip), followed by one or more file extensions"),
"a handler name followed by one or more file extensions"),
"input filter name (or ; delimited names) followed by one or more file extensions"),
"a language (e.g., fr), followed by one or more file extensions"),
"output filter name (or ; delimited names) followed by one or more file extensions"),
"a mime type followed by one or more file extensions"),
"language to use for documents with no other language file extension"),
"one or more file extensions"),
"one or more file extensions"),
"one or more file extensions"),
"one or more file extensions"),
"one or more file extensions"),
"one or more file extensions"),
"one or more file extensions"),
"the MIME types config file"),
{NULL}
};
static apr_hash_t *mime_type_extensions;
{
ap_configfile_t *f;
char l[MAX_STRING_LEN];
if (!types_confname)
if (!types_confname) {
"Invalid mime types config path %s",
return HTTP_INTERNAL_SERVER_ERROR;
}
!= APR_SUCCESS) {
"could not open mime types config file %s.",
return HTTP_INTERNAL_SERVER_ERROR;
}
while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
if (l[0] == '#')
continue;
while (ll[0]) {
}
}
ap_cfg_closefile(f);
return OK;
}
static const char *zap_sp(const char *s)
{
if (s == NULL) {
return (NULL);
}
if (*s == '\0') {
return (s);
}
/* skip prefixed white space */
for (; *s == ' ' || *s == '\t' || *s == '\n'; s++);
return (s);
}
{
start++;
}
end--;
}
if (len) {
}
}
static int is_token(char c)
{
int res;
return res;
}
static int is_qtext(char c)
{
int res;
? 1 : -1;
return res;
}
static int is_quoted_pair(const char *s)
{
int res = -1;
int c;
c = (int) *(s + 1);
if (apr_isascii(c)) {
res = 1;
}
}
return (res);
}
{
int quoted = 0;
apr_pool_t * p = r->pool;
/* initialize ctp */
mp = s;
/* getting a type */
"mod_mime: analyze_ct: cannot get media type from '%s'",
(const char *) mp);
return (NULL);
}
"Cannot get media subtype.");
return (NULL);
}
"Cannot get media subtype.");
return (NULL);
}
}
/* getting a subtype */
cp++;
continue;
"Cannot get media subtype.");
return (NULL);
}
"Cannot get media subtype.");
return (NULL);
}
}
if (*cp == '\0') {
return (ctp);
}
/* getting parameters */
cp++;
"Cannot get media parameter.");
return (NULL);
}
cp++;
continue;
}
cp++;
continue;
}
else if (*cp == '=') {
"Cannot get media parameter.");
return (NULL);
}
cp++;
"Cannot get media parameter.");
return (NULL);
}
continue;
}
else {
"Cannot get media parameter.");
return (NULL);
}
}
else {
if (*cp == '"') {
quoted = 1;
cp++;
}
else {
quoted = 0;
}
}
if (quoted > 0) {
cp++;
}
else if (is_quoted_pair(cp) > 0) {
cp += 2;
}
else if (*cp == '"') {
cp++;
cp++;
}
"Cannot get media parameter.");
return(NULL);
}
quoted = 0;
}
else {
"Cannot get media parameter.");
return (NULL);
}
}
}
else {
while (1) {
cp++;
}
break;
}
else {
"Cannot get media parameter.");
return (NULL);
}
}
}
"Cannot get media parameter.");
return (NULL);
}
}
else {
}
}
quoted = 0;
if (*cp == '\0') {
break;
}
cp++;
}
}
return (ctp);
}
/*
* find_ct is the hook routine for determining content-type and other
* MIME-related metadata. It assumes that r->filename has already been
* set and stat has been called for r->finfo. It also assumes that the
* non-path base file name is not the empty string unless it is a dir.
*/
static int find_ct(request_rec *r)
{
char *ext;
int found_metadata = 0;
r->content_type = DIR_MAGIC_TYPE;
return OK;
}
&mime_module);
/* Always drop the path leading up to the file name.
*/
else
++fn;
/* The exception list keeps track of those filename components that
* are not associated with extensions indicating metadata.
* The base name is always the first exception (i.e., "txt.html" has
* a basename of "txt" even though it might look like an extension).
*/
/* Parse filename extensions which can be in any order
*/
int found;
continue;
found = 0;
#ifdef CASE_BLIND_FILESYSTEM
/* We have a basic problem that folks on case-crippled systems
* expect anything and everything to succeed
*/
#endif
}
APR_HASH_KEY_STRING)) != NULL) {
r->content_type = type;
found = 1;
}
}
if (exinfo->forced_type) {
found = 1;
}
if (exinfo->charset_type) {
found = 1;
}
if (exinfo->language_type) {
if (!r->content_languages)
sizeof(char *));
*((const char **) apr_array_push(r->content_languages))
= exinfo->language_type;
found = 1;
}
if (exinfo->encoding_type) {
if (!r->content_encoding)
else {
/* XXX should eliminate duplicate entities */
r->content_encoding,
", ",
NULL);
}
found = 1;
}
/* The following extensions are not 'Found'. That is, they don't
* make any contribution to metadata negotation, so they must have
* been explicitly requested by name.
*/
found = 1;
}
/* XXX Two significant problems; 1, we don't check to see if we are
* setting redundant filters. 2, we insert these in the types config
* hook, which may be too early (dunno.)
*/
while (*filters
}
found = 1;
}
while (*filters
}
found = 1;
}
}
found_metadata = 1;
else
}
/*
* Need to set a notes entry on r for unrecognized elements.
* Somebody better claim them! If we did absolutely nothing,
* skip the notes to alert mod_negotiation we are clueless.
*/
if (found_metadata) {
(void *)exception_list);
}
if (r->content_type) {
int override = 0;
if (!override) {
"; charset=", charset,
NULL);
override = 1;
}
}
else {
NULL);
}
}
"; charset=", charset,
NULL);
}
}
}
/* Set default language, if none was specified by the extensions
* and we have a DefaultLanguage setting in force
*/
const char **new;
if (!r->content_languages)
}
if (!r->content_type)
return DECLINED;
return OK;
}
static void register_hooks(apr_pool_t *p)
{
/*
* this hook seems redundant ... is there any reason a type checker isn't
* allowed to do this already? I'd think that fixups in general would be
* the last opportunity to get the filters right.
* ap_hook_insert_filter(mime_insert_filters,NULL,NULL,APR_HOOK_MIDDLE);
*/
}
create_mime_dir_config, /* create per-directory config structure */
merge_mime_dir_configs, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
mime_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};