mod_mime_magic.html revision fc891500135b18740c60ea32a7ea0a069e8eafd7
0N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2362N/A<HTML>
0N/A <HEAD>
0N/A <TITLE>Apache module mod_mime_magic</TITLE>
0N/A </HEAD>
0N/A <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
2362N/A <BODY
0N/A BGCOLOR="#FFFFFF"
2362N/A TEXT="#000000"
0N/A LINK="#0000FF"
0N/A VLINK="#000080"
0N/A ALINK="#FF0000"
0N/A >
0N/A <DIV ALIGN="CENTER">
0N/A <IMG SRC="/images/sub.gif" ALT="[APACHE DOCUMENTATION]">
0N/A </DIV>
0N/A
0N/A <H1 align="CENTER">Module mod_mime_magic</H1>
0N/A
0N/A This module is contained in the mod_mime_magic.c file, and is
2362N/A an optional extension to the Apache HTTPD server.
2362N/A It can be used to determine the MIME type of a file by looking at a
2362N/A few bytes of its contents, the same way the Unix file(1) command works.
0N/A To use mod_mime_magic you have to enable the following line in the
0N/A server build <TT>Configuration</TT> file:
0N/A
0N/A <PRE>
0N/A AddModule modules/standard/mod_mime_magic.o
0N/A </PRE>
0N/A
0N/A This should be listed <EM>before</EM> mod_mime in the build
0N/A <TT>Configuration</TT> file so that it will be used after mod_mime.
0N/A mod_mime_magic is intended as a "second line of defense" for cases
0N/A mod_mime cannot resolve.
0N/A
0N/A <H2>Summary</H2>
0N/A
0N/A This module is derived from a free version of the <CODE>file(1)</CODE>
0N/A command for Unix,
0N/A which uses "magic numbers" and other hints from a file's contents to
0N/A figure out what the contents are.
0N/A In the case of this module,
0N/A it tries to figure out the MIME type of the file.
0N/A <P>
0N/A This module active only if the magic file is specified by the
0N/A <A HREF="#mimemagicfile"><CODE>MimeMagicFile</CODE></A> directive.
0N/A <P>
0N/A The contents of the file are plain ASCII text in 4-5 columns.
0N/A Blank lines are allowed but ignored.
0N/A Commented lines use a hash mark "#".
0N/A The remaining lines are parsed for the following columns:
0N/A <table border=1>
0N/A <tr valign=top>
0N/A <TH>Column</TH>
0N/A <TH>Description</TH>
0N/A </TR>
0N/A <tr valign=top>
0N/A <TD>1</TD>
0N/A <TD>byte number to begin checking from
0N/A <BR>
0N/A "&gt;" indicates a dependency upon the previous non-"&gt;" line</TD>
0N/A </TR><tr valign=top>
0N/A <TD>2</TD>
0N/A <TD>type of data to match
0N/A <table border=1>
0N/A <TR><TD>byte</TD><TD>single character</TD></TR>
0N/A <TR><TD>short</TD><TD>machine-order 16-bit integer</TD></TR>
0N/A <TR><TD>long</TD><TD>machine-order 32-bit integer</TD></TR>
0N/A <TR><TD>string</TD><TD>arbitrary-length string</TD></TR>
0N/A <TR><TD>date</TD><TD>long integer date
0N/A (seconds since Unix epoch/1970)</TD></TR>
0N/A <TR><TD>beshort</TD><TD>big-endian 16-bit integer</TD></TR>
0N/A <TR><TD>belong</TD><TD>big-endian 32-bit integer</TD></TR>
0N/A <TR><TD>bedate</TD><TD>big-endian 32-bit integer date</TD></TR>
0N/A <TR><TD>leshort</TD><TD>little-endian 16-bit integer</TD></TR>
0N/A <TR><TD>lelong</TD><TD>little-endian 32-bit integer</TD></TR>
0N/A <TR><TD>ledate</TD><TD>little-endian 32-bit integer date</TD></TR>
0N/A </TABLE>
0N/A </TD>
0N/A </TR><tr valign=top>
0N/A <TD>3</TD>
0N/A <TD>contents of data to match</TD>
0N/A </TR><tr valign=top>
0N/A <TD>4</TD>
0N/A <TD>MIME type if matched</TD>
0N/A </TR><tr valign=top>
0N/A <TD>5</TD>
0N/A <TD>MIME encoding if matched (optional)</TD>
0N/A </TR>
0N/A </TABLE>
0N/A
0N/A <P>
0N/A For example, the following magic file lines
0N/A would recognize some audio formats.
0N/A
0N/A<PRE>
0N/A# Sun/NeXT audio data
0N/A0 string .snd
0N/A&gt;12 belong 1 audio/basic
0N/A&gt;12 belong 2 audio/basic
0N/A&gt;12 belong 3 audio/basic
0N/A&gt;12 belong 4 audio/basic
0N/A&gt;12 belong 5 audio/basic
0N/A&gt;12 belong 6 audio/basic
0N/A&gt;12 belong 7 audio/basic
0N/A&gt;12 belong 23 audio/x-adpcm
0N/A</PRE>
0N/A
0N/A Or these would recognize the difference between "*.doc" files containing
0N/A Microsoft Word or FrameMaker documents. (These are incompatible file
0N/A formats which use the same file suffix.)
0N/A
0N/A<PRE>
0N/A# Frame
0N/A0 string \&lt;MakerFile application/x-frame
0N/A0 string \&lt;MIFFile application/x-frame
0N/A0 string \&lt;MakerDictionary application/x-frame
0N/A0 string \&lt;MakerScreenFon application/x-frame
0N/A0 string \&lt;MML application/x-frame
0N/A0 string \&lt;Book application/x-frame
0N/A0 string \&lt;Maker application/x-frame
0N/A
0N/A# MS-Word
0N/A0 string \376\067\0\043 application/msword
0N/A0 string \320\317\021\340\241\261 application/msword
0N/A0 string \333\245-\0\0\0 application/msword
0N/A</PRE>
0N/A
0N/A An optional MIME encoding can be included as a fifth column.
0N/A For example, this can recognize gzipped files and set the encoding
0N/A for them.
0N/A
0N/A<PRE>
0N/A# gzip (GNU zip, not to be confused with [Info-ZIP/PKWARE] zip archiver)
0N/A0 string \037\213 application/octet-stream x-gzip
0N/A</PRE>
0N/A
0N/A <H3>Performance Issues</H3>
0N/A
0N/A This module is not for every system. If your system is barely keeping
0N/A up with its load or if you're performing a web server benchmark,
0N/A you may not want to enable this because the processing is not free.
0N/A <P>
0N/A However, an effort was made to improve the performance of the original
0N/A file(1) code to make it fit in a busy web server.
0N/A It was designed for a server where there are thousands of users who
0N/A publish their own documents.
0N/A This is probably very common on intranets.
0N/A Many times, it's helpful
0N/A if the server can make more intelligent decisions about a file's
0N/A contents than the file name allows
0N/A ...even if just to reduce the "why doesn't my page work" calls
0N/A when users improperly name their own files.
0N/A You have to decide if the extra work suits your environment.
0N/A <P>
0N/A When compiling an Apache server, this module should be at or near the
0N/A top of the list of modules in the Configuration file. The modules are
0N/A listed in increasing priority so that will mean this one is used only
0N/A as a last resort, just like it was designed to.
0N/A
0N/A <H2>Directives</H2>
0N/A <P>
0N/A <UL>
0N/A <LI><A HREF="#mimemagicfile">MimeMagicFile</A>
0N/A </LI>
0N/A </UL>
0N/A </P>
0N/A <HR>
0N/A <H2><A NAME="mimemagicfile">
0N/A MimeMagicFile
0N/A </A></H2>
0N/A <P>
0N/A <A
0N/A HREF="directive-dict.html#Syntax"
0N/A REL="Help"
0N/A ><STRONG>Syntax:</STRONG></A> MimeMagicFile <EM>magic-file-name</EM>
0N/A <BR>
0N/A <A
0N/A HREF="directive-dict.html#Default"
0N/A REL="Help"
0N/A ><STRONG>Default:</STRONG></A> none
0N/A <BR>
0N/A <A
0N/A HREF="directive-dict.html#Context"
0N/A REL="Help"
0N/A ><STRONG>Context:</STRONG></A> server config, virtual host
0N/A <BR>
0N/A <A
0N/A HREF="directive-dict.html#Status"
0N/A REL="Help"
0N/A ><STRONG>Status:</STRONG></A> Extension
0N/A <BR>
0N/A <A
0N/A HREF="directive-dict.html#Module"
0N/A REL="Help"
0N/A ><STRONG>Module:</STRONG></A> mod_mime_magic
0N/A <P>
0N/A
0N/A The <CODE>MimeMagicFile</CODE> directive can be used to enable this module,
0N/A the default file is distributed at <CODE>conf/magic</CODE>.
0N/A Non-rooted paths are relative to the ServerRoot. Virtual hosts
0N/A will use the same file as the main server unless a more specific setting
0N/A is used, in which case the more specific setting overrides the main server's
0N/A file.
0N/A <P>
0N/A <HR>
0N/A
0N/A <H2><A NAME="notes">Notes</A></H2>
0N/A
0N/A The following notes apply to the mod_mime_magic module and are
0N/A included here for compliance with contributors' copyright restrictions
0N/A that require their acknowledgment.
0N/A
0N/A<PRE>
0N/A/*
0N/A * mod_mime_magic: MIME type lookup via file magic numbers
0N/A * Copyright (c) 1996-1997 Cisco Systems, Inc.
0N/A *
0N/A * This software was submitted by Cisco Systems to the Apache Group in July
0N/A * 1997. Future revisions and derivatives of this source code must
0N/A * acknowledge Cisco Systems as the original contributor of this module.
0N/A * All other licensing and usage conditions are those of the Apache Group.
0N/A *
0N/A * Some of this code is derived from the free version of the file command
0N/A * originally posted to comp.sources.unix. Copyright info for that program
0N/A * is included below as required.
0N/A * ---------------------------------------------------------------------------
0N/A * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
0N/A *
0N/A * This software is not subject to any license of the American Telephone and
0N/A * Telegraph Company or of the Regents of the University of California.
0N/A *
0N/A * Permission is granted to anyone to use this software for any purpose on any
0N/A * computer system, and to alter it and redistribute it freely, subject to
0N/A * the following restrictions:
0N/A *
0N/A * 1. The author is not responsible for the consequences of use of this
0N/A * software, no matter how awful, even if they arise from flaws in it.
0N/A *
0N/A * 2. The origin of this software must not be misrepresented, either by
0N/A * explicit claim or by omission. Since few users ever read sources, credits
0N/A * must appear in the documentation.
0N/A *
0N/A * 3. Altered versions must be plainly marked as such, and must not be
0N/A * misrepresented as being the original software. Since few users ever read
0N/A * sources, credits must appear in the documentation.
0N/A *
0N/A * 4. This notice may not be removed or altered.
0N/A * -------------------------------------------------------------------------
0N/A *
0N/A * For compliance with Mr Darwin's terms: this has been very significantly
0N/A * modified from the free "file" command.
0N/A * - all-in-one file for compilation convenience when moving from one
0N/A * version of Apache to the next.
0N/A * - Memory allocation is done through the Apache API's pool structure.
0N/A * - All functions have had necessary Apache API request or server
0N/A * structures passed to them where necessary to call other Apache API
0N/A * routines. (<EM>i.e.</EM>, usually for logging, files, or memory allocation in
0N/A * itself or a called function.)
0N/A * - struct magic has been converted from an array to a single-ended linked
0N/A * list because it only grows one record at a time, it's only accessed
0N/A * sequentially, and the Apache API has no equivalent of realloc().
0N/A * - Functions have been changed to get their parameters from the server
0N/A * configuration instead of globals. (It should be reentrant now but has
0N/A * not been tested in a threaded environment.)
0N/A * - Places where it used to print results to stdout now saves them in a
0N/A * list where they're used to set the MIME type in the Apache request
0N/A * record.
0N/A * - Command-line flags have been removed since they will never be used here.
0N/A *
0N/A */
0N/A</PRE>
0N/A
0N/A </BODY>
0N/A</HTML>
0N/A