mod_mime.c revision 36ef8f77bffe75d1aa327882be1b5bdbe2ff567a
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* 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"
#include "http_protocol.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
*/
int use_path_info; /* If set to 0, only use filename.
* If set to 1, append PATH_INFO to filename for
* lookups.
* If set to 2, this value is unset and is
* effectively 0.
*/
typedef struct param_s {
char *attr;
char *val;
} param;
typedef struct {
const char *type;
const 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)
}
}
else {
}
return new;
}
{
mime_dir_config *m=m_;
if (*key == '.') {
++key;
}
if (!m->extension_mappings) {
}
else {
}
if (!exinfo) {
}
return NULL;
}
/*
* As RemoveType should also override the info from TypesConfig, we add an
* empty string as type instead of actually removing the type.
*/
const char *ext)
{
}
/*
* 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) {
}
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)
{
const char *errmsg;
return errmsg;
}
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 "NegotiatedOnly is incompatible with Any, "
"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 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"),
"Set to 'yes' to allow mod_mime to use path info for type checking"),
{NULL}
};
static apr_hash_t *mime_type_extensions;
{
ap_configfile_t *f;
char l[MAX_STRING_LEN];
&mime_module);
if (!types_confname) {
}
if (!types_confname) {
"Invalid mime types config path %s",
(const char *)ap_get_module_config(s->module_config,
&mime_module));
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 */
while (apr_isspace(*cp)) {
cp++;
}
if (!*cp) {
"mod_mime: analyze_ct: cannot get media type from '%s'",
(const char *) mp);
return (NULL);
}
do {
cp++;
"Cannot get media type from '%s'",
(const char *) mp);
return (NULL);
}
while (apr_isspace(*cp)) {
cp++;
}
if (*cp != '/') {
"mod_mime: analyze_ct: cannot get media type from '%s'",
(const char *) mp);
return (NULL);
}
cp++; /* skip the '/' */
/* getting a subtype */
while (apr_isspace(*cp)) {
cp++;
}
if (!*cp) {
"Cannot get media subtype.");
return (NULL);
}
do {
cp++;
while (apr_isspace(*cp)) {
cp++;
}
if (*cp == '\0') {
return (ctp);
}
/* getting parameters */
cp++; /* skip the ';' */
"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;
return OK;
}
if (!r->filename) {
return DECLINED;
}
&mime_module);
/* If use_path_info is explicitly set to on (value & 1 == 1), append. */
}
else {
resource_name = r->filename;
}
/* Always drop the path leading up to the file name.
*/
fn = resource_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;
char *extcase;
continue;
}
found = 0;
/* Save the ext in extcase before converting it to lower case.
*/
}
APR_HASH_KEY_STRING)) != NULL) {
ap_set_content_type(r, (char*) type);
found = 1;
}
}
/* empty string is treated as special case for RemoveType */
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
*
* ah no. Order is important and double encoding is neither
* forbidden nor impossible. -- nd
*/
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;
ctp->subtype_len +
sizeof("/"));
char *tmp = base_content_type;
*tmp++ = '/';
*tmp = 0;
if (!override) {
apr_pstrcat(r->pool,
r->content_type,
"; charset=",
NULL));
override = 1;
}
}
else {
apr_pstrcat(r->pool,
r->content_type,
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);
*/
}
AP_DECLARE_MODULE(mime) = {
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 */
};