2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
6 Copyright 2002-2004 The Apache Software Foundation
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
21 <modulesynopsis metafile="mod_mime_magic.xml.meta">
23 <name>mod_mime_magic</name>
24 <description>Determines the MIME type of a file
25 by looking at a few bytes of its contents</description>
26 <status>Extension</status>
27 <sourcefile>mod_mime_magic.c</sourcefile>
28 <identifier>mime_magic_module</identifier>
31 <p>This module determines the MIME type of files in the same
32 way the Unix <code>file(1)</code> command works: it looks at the first
33 few bytes of the file. It is intended as a "second line of defense"
34 for cases that <module>mod_mime</module> can't resolve.</p>
36 <p>This module is derived from a free version of the
37 <code>file(1)</code> command for Unix, which uses "magic
38 numbers" and other hints from a file's contents to figure out
39 what the contents are. This module is active only if the magic
40 file is specified by the <directive module="mod_mime_magic"
41 >MimeMagicFile</directive> directive.</p>
44 <section id="format"><title>Format of the Magic File</title>
46 <p>The contents of the file are plain ASCII text in 4-5
47 columns. Blank lines are allowed but ignored. Commented lines
48 use a hash mark (<code>#</code>). The remaining lines are parsed for
49 the following columns:</p>
51 <table style="zebra" border="1">
52 <tr><th>Column</th><th>Description</th></tr>
54 <td>byte number to begin checking from<br />
55 "<code>></code>" indicates a dependency upon the previous
56 non-"<code>></code>" line</td></tr>
59 <td>type of data to match
61 <tr><td><code>byte</code></td>
62 <td>single character</td></tr>
63 <tr><td><code>short</code></td>
64 <td>machine-order 16-bit integer</td></tr>
65 <tr><td><code>long</code></td>
66 <td>machine-order 32-bit integer</td></tr>
67 <tr><td><code>string</code></td>
68 <td>arbitrary-length string</td></tr>
69 <tr><td><code>date</code></td>
70 <td>long integer date (seconds since Unix epoch/1970)</td></tr>
71 <tr><td><code>beshort</code></td>
72 <td>big-endian 16-bit integer</td></tr>
73 <tr><td><code>belong</code></td>
74 <td>big-endian 32-bit integer</td></tr>
75 <tr><td><code>bedate</code></td>
76 <td>big-endian 32-bit integer date</td></tr>
77 <tr><td><code>leshort</code></td>
78 <td>little-endian 16-bit integer</td></tr>
79 <tr><td><code>lelong</code></td>
80 <td>little-endian 32-bit integer</td></tr>
81 <tr><td><code>ledate</code></td>
82 <td>little-endian 32-bit integer date</td></tr>
86 <td>contents of data to match</td></tr>
89 <td>MIME type if matched</td></tr>
92 <td>MIME encoding if matched (optional)</td></tr>
95 <p>For example, the following magic file lines would recognize
96 some audio formats:</p>
99 <pre># Sun/NeXT audio data
101 >12 belong 1 audio/basic
102 >12 belong 2 audio/basic
103 >12 belong 3 audio/basic
104 >12 belong 4 audio/basic
105 >12 belong 5 audio/basic
106 >12 belong 6 audio/basic
107 >12 belong 7 audio/basic
108 >12 belong 23 audio/x-adpcm</pre>
111 <p>Or these would recognize the difference between <code>*.doc</code>
112 files containing Microsoft Word or FrameMaker documents. (These are
113 incompatible file formats which use the same file suffix.)</p>
117 0 string \<MakerFile application/x-frame
118 0 string \<MIFFile application/x-frame
119 0 string \<MakerDictionary application/x-frame
120 0 string \<MakerScreenFon application/x-frame
121 0 string \<MML application/x-frame
122 0 string \<Book application/x-frame
123 0 string \<Maker application/x-frame
126 0 string \376\067\0\043 application/msword
127 0 string \320\317\021\340\241\261 application/msword
128 0 string \333\245-\0\0\0 application/msword</pre>
131 <p>An optional MIME encoding can be included as a fifth column.
132 For example, this can recognize gzipped files and set the
133 encoding for them.</p>
136 <pre># gzip (GNU zip, not to be confused with
137 # [Info-ZIP/PKWARE] zip archiver)
139 0 string \037\213 application/octet-stream x-gzip</pre>
143 <section id="performance"><title>Performance Issues</title>
144 <p>This module is not for every system. If your system is barely
145 keeping up with its load or if you're performing a web server
146 benchmark, you may not want to enable this because the
147 processing is not free.</p>
149 <p>However, an effort was made to improve the performance of
150 the original <code>file(1)</code> code to make it fit in a busy web
151 server. It was designed for a server where there are thousands of users
152 who publish their own documents. This is probably very common
153 on intranets. Many times, it's helpful if the server can make
154 more intelligent decisions about a file's contents than the
155 file name allows ...even if just to reduce the "why doesn't my
156 page work" calls when users improperly name their own files.
157 You have to decide if the extra work suits your
161 <section id="notes"><title>Notes</title>
162 <p>The following notes apply to the <module>mod_mime_magic</module>
163 module and are included here for compliance with contributors'
164 copyright restrictions that require their acknowledgment.</p>
167 <p>mod_mime_magic: MIME type lookup via file magic numbers<br />
168 Copyright (c) 1996-1997 Cisco Systems, Inc.</p>
170 <p>This software was submitted by Cisco Systems to the Apache Group
171 in July 1997. Future revisions and derivatives of this source code
172 must acknowledge Cisco Systems as the original contributor of this
173 module. All other licensing and usage conditions are those of the
176 <p>Some of this code is derived from the free version of the file
177 command originally posted to comp.sources.unix. Copyright info for
178 that program is included below as required.</p>
182 <p> - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.</p>
184 <p>This software is not subject to any license of the American
185 Telephone and Telegraph Company or of the Regents of the University
188 <p>Permission is granted to anyone to use this software for any
189 purpose on any computer system, and to alter it and redistribute it
190 freely, subject to the following restrictions:</p>
193 <li>The author is not responsible for the consequences of use of
194 this software, no matter how awful, even if they arise from flaws
197 <li>The origin of this software must not be misrepresented, either
198 by explicit claim or by omission. Since few users ever read
199 sources, credits must appear in the documentation.</li>
201 <li>Altered versions must be plainly marked as such, and must not
202 be misrepresented as being the original software. Since few users
203 ever read sources, credits must appear in the documentation.</li>
205 <li>This notice may not be removed or altered.</li>
210 <p>For compliance with Mr Darwin's terms: this has been very
211 significantly modified from the free "file" command.</p>
214 <li>all-in-one file for compilation convenience when moving from
215 one version of Apache to the next.</li>
217 <li>Memory allocation is done through the Apache API's pool
220 <li>All functions have had necessary Apache API request or server
221 structures passed to them where necessary to call other Apache API
222 routines. (<em>i.e.</em>, usually for logging, files, or memory
223 allocation in itself or a called function.)</li>
225 <li>struct magic has been converted from an array to a single-ended
226 linked list because it only grows one record at a time, it's only
227 accessed sequentially, and the Apache API has no equivalent of
228 <code>realloc()</code>.</li>
230 <li>Functions have been changed to get their parameters from the
231 server configuration instead of globals. (It should be reentrant
232 now but has not been tested in a threaded environment.)</li>
234 <li>Places where it used to print results to stdout now saves them
235 in a list where they're used to set the MIME type in the Apache
238 <li>Command-line flags have been removed since they will never be
245 <name>MimeMagicFile</name>
246 <description>Enable MIME-type determination based on file contents
247 using the specified magic file</description>
248 <syntax>MimeMagicFile <var>file-path</var></syntax>
249 <contextlist><context>server config</context><context>virtual host</context>
253 <p>The <directive>MimeMagicFile</directive> directive can be used to
254 enable this module, the default file is distributed at
255 <code>conf/magic</code>. Non-rooted paths are relative to the
256 <directive module="core">ServerRoot</directive>. Virtual hosts will use
257 the same file as the main server unless a more specific setting is
258 used, in which case the more specific setting overrides the main
261 <example><title>Example</title>
262 MimeMagicFile conf/magic