2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.xsl"?>
6 <name>mod_mime_magic</name>
7 <description>Determines the MIME type of a file
8 by looking at a few bytes of its contents</description>
9 <status>Extension</status>
10 <sourcefile>mod_mime_magic.c</sourcefile>
11 <identifier>mime_magic_module</identifier>
14 <p>This module determines the MIME type of files in the same
15 way the Unix file(1) command works: it looks at the first few
16 bytes of the file. It is intended as a "second line of defense"
17 for cases that <module>mod_mime</module> can't
18 resolve. To assure that mod_mime gets first try at determining
19 a file's MIME type, be sure to list mod_mime_magic
20 <strong>before</strong> mod_mime in the configuration.</p>
22 <p>This module is derived from a free version of the
23 <code>file(1)</code> command for Unix, which uses "magic
24 numbers" and other hints from a file's contents to figure out
25 what the contents are. This module is active only if the magic
26 file is specified by the <directive module="mod_mime_magic"
27 >MimeMagicFile</directive> directive.</p>
30 <section><title>Format of the Magic File</title>
32 <p>The contents of the file are plain ASCII text in 4-5
33 columns. Blank lines are allowed but ignored. Commented lines
34 use a hash mark "#". The remaining lines are parsed for the
35 following columns:</p>
47 <td>byte number to begin checking from<br />
48 ">" indicates a dependency upon the previous non-">"
62 <td>single character</td>
68 <td>machine-order 16-bit integer</td>
74 <td>machine-order 32-bit integer</td>
80 <td>arbitrary-length string</td>
86 <td>long integer date (seconds since Unix
93 <td>big-endian 16-bit integer</td>
99 <td>big-endian 32-bit integer</td>
105 <td>big-endian 32-bit integer date</td>
111 <td>little-endian 16-bit integer</td>
117 <td>little-endian 32-bit integer</td>
123 <td>little-endian 32-bit integer date</td>
132 <td>contents of data to match</td>
138 <td>MIME type if matched</td>
144 <td>MIME encoding if matched (optional)</td>
148 <p>For example, the following magic file lines would recognize
149 some audio formats.</p>
152 # Sun/NeXT audio data
154 >12 belong 1 audio/basic
155 >12 belong 2 audio/basic
156 >12 belong 3 audio/basic
157 >12 belong 4 audio/basic
158 >12 belong 5 audio/basic
159 >12 belong 6 audio/basic
160 >12 belong 7 audio/basic
161 >12 belong 23 audio/x-adpcm
164 <p>Or these would recognize the difference between "*.doc" files
165 containing Microsoft Word or FrameMaker documents. (These are
166 incompatible file formats which use the same file suffix.)</p>
170 0 string \<MakerFile application/x-frame
171 0 string \<MIFFile application/x-frame
172 0 string \<MakerDictionary application/x-frame
173 0 string \<MakerScreenFon application/x-frame
174 0 string \<MML application/x-frame
175 0 string \<Book application/x-frame
176 0 string \<Maker application/x-frame
179 0 string \376\067\0\043 application/msword
180 0 string \320\317\021\340\241\261 application/msword
181 0 string \333\245-\0\0\0 application/msword
184 <p>An optional MIME encoding can be included as a fifth column.
185 For example, this can recognize gzipped files and set the
186 encoding for them.</p>
189 # gzip (GNU zip, not to be confused with [Info-ZIP/PKWARE] zip archiver)
190 0 string \037\213 application/octet-stream x-gzip
195 <section><title>Performance Issues</title>
196 <p>This module is not for every system. If your system is barely
197 keeping up with its load or if you're performing a web server
198 benchmark, you may not want to enable this because the
199 processing is not free.</p>
201 <p>However, an effort was made to improve the performance of
202 the original file(1) code to make it fit in a busy web server.
203 It was designed for a server where there are thousands of users
204 who publish their own documents. This is probably very common
205 on intranets. Many times, it's helpful if the server can make
206 more intelligent decisions about a file's contents than the
207 file name allows ...even if just to reduce the "why doesn't my
208 page work" calls when users improperly name their own files.
209 You have to decide if the extra work suits your
212 <p>When compiling an Apache server, this module should be at or
213 near the top of the list of modules in the Configuration file.
214 The modules are listed in increasing priority so that will mean
215 this one is used only as a last resort, just like it was
220 <section id="notes"><title>Notes</title>
222 <p>The following notes apply to the mod_mime_magic module and are
223 included here for compliance with contributors' copyright
224 restrictions that require their acknowledgment. </p>
227 * mod_mime_magic: MIME type lookup via file magic numbers
228 * Copyright (c) 1996-1997 Cisco Systems, Inc.
230 * This software was submitted by Cisco Systems to the Apache Group in July
231 * 1997. Future revisions and derivatives of this source code must
232 * acknowledge Cisco Systems as the original contributor of this module.
233 * All other licensing and usage conditions are those of the Apache Group.
235 * Some of this code is derived from the free version of the file command
236 * originally posted to comp.sources.unix. Copyright info for that program
237 * is included below as required.
238 * ---------------------------------------------------------------------------
239 * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
241 * This software is not subject to any license of the American Telephone and
242 * Telegraph Company or of the Regents of the University of California.
244 * Permission is granted to anyone to use this software for any purpose on any
245 * computer system, and to alter it and redistribute it freely, subject to
246 * the following restrictions:
248 * 1. The author is not responsible for the consequences of use of this
249 * software, no matter how awful, even if they arise from flaws in it.
251 * 2. The origin of this software must not be misrepresented, either by
252 * explicit claim or by omission. Since few users ever read sources, credits
253 * must appear in the documentation.
255 * 3. Altered versions must be plainly marked as such, and must not be
256 * misrepresented as being the original software. Since few users ever read
257 * sources, credits must appear in the documentation.
259 * 4. This notice may not be removed or altered.
260 * -------------------------------------------------------------------------
262 * For compliance with Mr Darwin's terms: this has been very significantly
263 * modified from the free "file" command.
264 * - all-in-one file for compilation convenience when moving from one
265 * version of Apache to the next.
266 * - Memory allocation is done through the Apache API's pool structure.
267 * - All functions have had necessary Apache API request or server
268 * structures passed to them where necessary to call other Apache API
269 * routines. (<em>i.e.</em>, usually for logging, files, or memory allocation in
270 * itself or a called function.)
271 * - struct magic has been converted from an array to a single-ended linked
272 * list because it only grows one record at a time, it's only accessed
273 * sequentially, and the Apache API has no equivalent of realloc().
274 * - Functions have been changed to get their parameters from the server
275 * configuration instead of globals. (It should be reentrant now but has
276 * not been tested in a threaded environment.)
277 * - Places where it used to print results to stdout now saves them in a
278 * list where they're used to set the MIME type in the Apache request
280 * - Command-line flags have been removed since they will never be used here.
287 <name>MimeMagicFile</name>
288 <description>Enable MIME-type determination based on file contents
289 using the specified magic file</description>
290 <syntax>MimeMagicFile <em>file-path</em></syntax>
291 <contextlist><context>server config</context>
292 <context>virtual host</context></contextlist>
295 <p>The <directive>MimeMagicFile</directive> directive can be used to
296 enable this module, the default file is distributed at
297 <code>conf/magic</code>. Non-rooted paths are relative to the
298 ServerRoot. Virtual hosts will use the same file as the main
299 server unless a more specific setting is used, in which case
300 the more specific setting overrides the main server's file.</p>