mod_mime_magic.c revision 99b130dd1f797c0acf3ffa960b3c9acefd29f0fc
6ae232055d4d8a97267517c5e50074c2c819941and/* ====================================================================
6ae232055d4d8a97267517c5e50074c2c819941and * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * Redistribution and use in source and binary forms, with or without
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * modification, are permitted provided that the following conditions
6ae232055d4d8a97267517c5e50074c2c819941and * are met:
6ae232055d4d8a97267517c5e50074c2c819941and * 1. Redistributions of source code must retain the above copyright
6ae232055d4d8a97267517c5e50074c2c819941and * notice, this list of conditions and the following disclaimer.
6ae232055d4d8a97267517c5e50074c2c819941and * 2. Redistributions in binary form must reproduce the above copyright
6ae232055d4d8a97267517c5e50074c2c819941and * notice, this list of conditions and the following disclaimer in
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * the documentation and/or other materials provided with the
2e545ce2450a9953665f701bb05350f0d3f26275nd * distribution.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 3. All advertising materials mentioning features or use of this
6ae232055d4d8a97267517c5e50074c2c819941and * software must display the following acknowledgment:
6ae232055d4d8a97267517c5e50074c2c819941and * "This product includes software developed by the Apache Group
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen * for use in the Apache HTTP server project (http://www.apache.org/)."
6ae232055d4d8a97267517c5e50074c2c819941and * 4. The names "Apache Server" and "Apache Group" must not be used to
6ae232055d4d8a97267517c5e50074c2c819941and * endorse or promote products derived from this software without
6ae232055d4d8a97267517c5e50074c2c819941and * prior written permission. For written permission, please contact
b43f840409794ed298e8634f6284741f193b6c4ftakashi * apache@apache.org.
b43f840409794ed298e8634f6284741f193b6c4ftakashi * 5. Products derived from this software may not be called "Apache"
6ae232055d4d8a97267517c5e50074c2c819941and * nor may "Apache" appear in their names without prior written
6ae232055d4d8a97267517c5e50074c2c819941and * permission of the Apache Group.
1ac39787115a288f5e848344b1b1e8dccb1c58f1nd * 6. Redistributions of any form whatsoever must retain the following
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * acknowledgment:
6ae232055d4d8a97267517c5e50074c2c819941and * "This product includes software developed by the Apache Group
b43f840409794ed298e8634f6284741f193b6c4ftakashi * for use in the Apache HTTP server project (http://www.apache.org/)."
b43f840409794ed298e8634f6284741f193b6c4ftakashi * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
b43f840409794ed298e8634f6284741f193b6c4ftakashi * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6ae232055d4d8a97267517c5e50074c2c819941and * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
6ae232055d4d8a97267517c5e50074c2c819941and * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
6ae232055d4d8a97267517c5e50074c2c819941and * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6ae232055d4d8a97267517c5e50074c2c819941and * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6ae232055d4d8a97267517c5e50074c2c819941and * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
6ae232055d4d8a97267517c5e50074c2c819941and * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6ae232055d4d8a97267517c5e50074c2c819941and * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6ae232055d4d8a97267517c5e50074c2c819941and * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6ae232055d4d8a97267517c5e50074c2c819941and * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
6ae232055d4d8a97267517c5e50074c2c819941and * OF THE POSSIBILITY OF SUCH DAMAGE.
6ae232055d4d8a97267517c5e50074c2c819941and * ====================================================================
6ae232055d4d8a97267517c5e50074c2c819941and * This software consists of voluntary contributions made by many
6ae232055d4d8a97267517c5e50074c2c819941and * individuals on behalf of the Apache Group and was originally based
6ae232055d4d8a97267517c5e50074c2c819941and * on public domain software written at the National Center for
6ae232055d4d8a97267517c5e50074c2c819941and * Supercomputing Applications, University of Illinois, Urbana-Champaign.
6ae232055d4d8a97267517c5e50074c2c819941and * For more information on the Apache Group and the Apache HTTP server
6ae232055d4d8a97267517c5e50074c2c819941and * project, please see <http://www.apache.org/>.
6ae232055d4d8a97267517c5e50074c2c819941and * mod_mime_magic: MIME type lookup via file magic numbers
6ae232055d4d8a97267517c5e50074c2c819941and * Copyright (c) 1996-1997 Cisco Systems, Inc.
6ae232055d4d8a97267517c5e50074c2c819941and * This software was submitted by Cisco Systems to the Apache Group in July
6ae232055d4d8a97267517c5e50074c2c819941and * 1997. Future revisions and derivatives of this source code must
6ae232055d4d8a97267517c5e50074c2c819941and * acknowledge Cisco Systems as the original contributor of this module.
6ae232055d4d8a97267517c5e50074c2c819941and * All other licensing and usage conditions are those of the Apache Group.
6ae232055d4d8a97267517c5e50074c2c819941and * Some of this code is derived from the free version of the file command
6ae232055d4d8a97267517c5e50074c2c819941and * originally posted to comp.sources.unix. Copyright info for that program
6ae232055d4d8a97267517c5e50074c2c819941and * is included below as required.
6ae232055d4d8a97267517c5e50074c2c819941and * ---------------------------------------------------------------------------
6ae232055d4d8a97267517c5e50074c2c819941and * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
6ae232055d4d8a97267517c5e50074c2c819941and * This software is not subject to any license of the American Telephone and
6ae232055d4d8a97267517c5e50074c2c819941and * Telegraph Company or of the Regents of the University of California.
6ae232055d4d8a97267517c5e50074c2c819941and * Permission is granted to anyone to use this software for any purpose on any
6ae232055d4d8a97267517c5e50074c2c819941and * computer system, and to alter it and redistribute it freely, subject to
6ae232055d4d8a97267517c5e50074c2c819941and * the following restrictions:
6ae232055d4d8a97267517c5e50074c2c819941and * 1. The author is not responsible for the consequences of use of this
6ae232055d4d8a97267517c5e50074c2c819941and * software, no matter how awful, even if they arise from flaws in it.
6ae232055d4d8a97267517c5e50074c2c819941and * 2. The origin of this software must not be misrepresented, either by
6ae232055d4d8a97267517c5e50074c2c819941and * explicit claim or by omission. Since few users ever read sources, credits
6ae232055d4d8a97267517c5e50074c2c819941and * must appear in the documentation.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * 3. Altered versions must be plainly marked as such, and must not be
727872d18412fc021f03969b8641810d8896820bhumbedooh * misrepresented as being the original software. Since few users ever read
0d0ba3a410038e179b695446bb149cce6264e0abnd * sources, credits must appear in the documentation.
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh * 4. This notice may not be removed or altered.
0d0ba3a410038e179b695446bb149cce6264e0abnd * -------------------------------------------------------------------------
727872d18412fc021f03969b8641810d8896820bhumbedooh * For compliance with Mr Darwin's terms: this has been very significantly
0d0ba3a410038e179b695446bb149cce6264e0abnd * modified from the free "file" command.
0d0ba3a410038e179b695446bb149cce6264e0abnd * - all-in-one file for compilation convenience when moving from one
0d0ba3a410038e179b695446bb149cce6264e0abnd * version of Apache to the next.
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh * - Memory allocation is done through the Apache API's ap_context_t structure.
0d0ba3a410038e179b695446bb149cce6264e0abnd * - All functions have had necessary Apache API request or server
0d0ba3a410038e179b695446bb149cce6264e0abnd * structures passed to them where necessary to call other Apache API
0d0ba3a410038e179b695446bb149cce6264e0abnd * routines. (i.e. usually for logging, files, or memory allocation in
727872d18412fc021f03969b8641810d8896820bhumbedooh * itself or a called function.)
0d0ba3a410038e179b695446bb149cce6264e0abnd * - struct magic has been converted from an array to a single-ended linked
0d0ba3a410038e179b695446bb149cce6264e0abnd * list because it only grows one record at a time, it's only accessed
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh * sequentially, and the Apache API has no equivalent of realloc().
205f749042ed530040a4f0080dbcb47ceae8a374rjung * - Functions have been changed to get their parameters from the server
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen * configuration instead of globals. (It should be reentrant now but has
0d0ba3a410038e179b695446bb149cce6264e0abnd * not been tested in a threaded environment.)
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd * - Places where it used to print results to stdout now saves them in a
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd * list where they're used to set the MIME type in the Apache request
6ae232055d4d8a97267517c5e50074c2c819941and * - Command-line flags have been removed since they will never be used here.
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "util_script.h"
#include <utime.h>
#define MIME_MAGIC_DEBUG 0
struct magic {
short flag;
} in;
union VALUETYPE {
char s[MAXstring];
* @(#)tar.h 1.20 86/10/29 Public Domain. Created 25 August 1985 by John
union record {
struct header {
char linkflag;
} header;
static int is_tar(unsigned char *, int);
static int hextoint(int);
struct magic *, int);
static long from_oct(int, char *);
* includes for ASCII substring recognition formerly "names.h" in file
static char *types[] =
static struct names {
char *name;
short type;
} names[] = {
#ifdef NOTDEF
NULL, 0
typedef struct magic_rsl_s {
} magic_rsl;
return new;
if (!conf) {
return NULL;
{NULL}
sizeof(magic_req_rec));
return req_dat;
if (!req_dat) {
str[0] = c;
res_pos = 0;
cur_pos++) {
#if MIME_MAGIC_DEBUG
return result;
} rsl_states;
return DECLINED;
return DECLINED;
state++;
return DECLINED;
type_len++;
state++;
state++;
type_len++;
state++;
encoding_len++;
return DECLINED;
return DECLINED;
char *tmp;
char *tmp;
if (!r->content_type ||
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
int result;
case DONE:
return OK;
case OK:
return result;
return DECLINED;
return HTTP_INTERNAL_SERVER_ERROR;
if (nbytes == 0)
return OK;
ap_file_t *f;
int errs = 0;
int lineno;
#if MIME_MAGIC_DEBUG
int rule = 0;
int ws_offset;
if (line[0]) {
ws_offset = 0;
ws_offset++;
#if MIME_MAGIC_DEBUG
rule++;
++errs;
(void) ap_close(f);
#if MIME_MAGIC_DEBUG
conf,
#if MIME_MAGIC_DEBUG
prevm = 0;
prevm = m;
switch (m->type) {
case BYTE:
case SHORT:
case BESHORT:
case LESHORT:
case DATE:
case BEDATE:
case LEDATE:
case LONG:
case BELONG:
case LELONG:
case STRING:
struct magic *m;
m->flag = 0;
m->cont_level = 0;
m->cont_level++;
if (ap_isdigit((unsigned char) *l)) {
while (ap_isdigit((unsigned char) *l))
l += NBYTE;
l += NSHORT;
l += NLONG;
l += NSTRING;
l += NDATE;
l += NBESHORT;
l += NBELONG;
l += NBEDATE;
l += NLESHORT;
l += NLELONG;
l += NLEDATE;
m->mask = ~0L;
m->reln = *l;
m->reln = *l;
m->reln = *l;
m->nospflag = 0;
#if MIME_MAGIC_DEBUG
int slen;
register int val;
if (ap_isspace((unsigned char) c))
if (p >= pmax) {
goto out;
*p++ = (char) val;
val = c;
c = hextoint(*s++);
c = hextoint(*s++);
*p++ = (char) val;
out:
static int hextoint(int c)
if (ap_isdigit((unsigned char) c))
case S_IFDIR:
return DONE;
case S_IFCHR:
return DONE;
#ifdef S_IFBLK
case S_IFBLK:
return DONE;
#ifdef S_IFIFO
case S_IFIFO:
return DONE;
#ifdef S_IFLNK
case S_IFLNK:
return HTTP_INTERNAL_SERVER_ERROR;
#ifdef S_IFSOCK
#ifndef __COHERENT__
case S_IFSOCK:
return DONE;
case S_IFREG:
return HTTP_INTERNAL_SERVER_ERROR;
return DONE;
return OK;
* apprentice.c). Passed the name and FILE * of one file to be typed.
#if MIME_MAGIC_DEBUG
int rule_counter = 0;
int cont_level = 0;
int need_separator = 0;
union VALUETYPE p;
struct magic *m;
#if MIME_MAGIC_DEBUG
conf,
#if MIME_MAGIC_DEBUG
#if MIME_MAGIC_DEBUG
rule_counter++;
!mcheck(r, &p, m)) {
#if MIME_MAGIC_DEBUG
rule_counter++;
m = m_cont;
#if MIME_MAGIC_DEBUG
mprint(r, &p, m);
if (m->desc[0])
cont_level++;
m = m->next;
while (m && (m->cont_level != 0)) {
#if MIME_MAGIC_DEBUG
mcheck(r, &p, m)) {
if (need_separator
&& (m->nospflag == 0)
need_separator = 0;
mprint(r, &p, m);
if (m->desc[0])
cont_level++;
m = m->next;
#if MIME_MAGIC_DEBUG
#if MIME_MAGIC_DEBUG
switch (m->type) {
case BYTE:
case SHORT:
case BESHORT:
case LESHORT:
case LONG:
case BELONG:
case LELONG:
case STRING:
case DATE:
case BEDATE:
case LEDATE:
m->type);
char *rt;
switch (m->type) {
case BYTE:
case SHORT:
case LONG:
case DATE:
case STRING:
case BESHORT:
case BELONG:
case BEDATE:
case LESHORT:
case LELONG:
case LEDATE:
if (!mconvert(r, p, m))
case BYTE:
case SHORT:
case LONG:
if (!mconvert(r, p, m))
register unsigned long l = m->value.l;
int matched;
switch (m->type) {
case BYTE:
case SHORT:
case BESHORT:
case LESHORT:
case LONG:
case BELONG:
case LELONG:
case DATE:
case BEDATE:
case LEDATE:
case STRING:
* What we want here is: v = strncmp(m->value.s, p->s, m->vallen);
register unsigned char *a = (unsigned char *) m->value.s;
while (--len >= 0)
switch (m->reln) {
#if MIME_MAGIC_DEBUG
matched = v != l;
#if MIME_MAGIC_DEBUG
matched = v == l;
#if MIME_MAGIC_DEBUG
matched = v > l;
#if MIME_MAGIC_DEBUG
matched = (long) v > (long) l;
#if MIME_MAGIC_DEBUG
matched = v < l;
#if MIME_MAGIC_DEBUG
matched = (long) v < (long) l;
#if MIME_MAGIC_DEBUG
matched = (v & l) == l;
#if MIME_MAGIC_DEBUG
matched = (v & l) != l;
#if MIME_MAGIC_DEBUG
matched = 0;
m->reln);
return matched;
int has_escapes = 0;
char *token;
register struct names *p;
int small_nbytes;
/* look for tokens from names.h - this is expensive!, so we'll limit
if (has_escapes)
if (has_escapes) {
char *magic;
int maglen;
int silent;
} compr[] = {
unsigned char *newbuf;
int newsize;
for (i = 0; i < ncompr; i++) {
if (i == ncompr)
struct uncompress_parms {
request_rec *r;
int method;
char **env;
if (!iol)
return APR_EBADF;
if (script_in) {
return (rc);
unsigned char **newch, int n)
parm.r = r;
* @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
sum = 0;
for (i = sizeof(union record); --i >= 0;) {
register long value;
where++;
if (--digs <= 0)
value = 0;
--digs;
return value;
char *sub_filename;
#if MIME_MAGIC_DEBUG
suffix_pos--;
result = 0;
#if MIME_MAGIC_DEBUG
#if MIME_MAGIC_DEBUG
r->content_encoding =
r->content_language =
return result;
static void magic_init(ap_context_t *p, ap_context_t *plog, ap_context_t *ptemp, server_rec *main_server)
int result;
server_rec *s;
#if MIME_MAGIC_DEBUG
#if MIME_MAGIC_DEBUG
prevm = 0;
prevm = m;
int result;
return DECLINED;
if (r->content_type) {
return DECLINED;
return DECLINED;
if (!magic_set_config(r)) {
return HTTP_INTERNAL_SERVER_ERROR;
return result;
return magic_rsl_to_request(r);
static void register_hooks(void)