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