summaryrefslogtreecommitdiff
path: root/docs/manual/mod/mod_mime_magic.xml
blob: e868fefc0f23d32a0ede66fd6161d9771e34279f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?xml version="1.0"?>
<!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
<!-- $LastChangedRevision$ -->

<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<modulesynopsis metafile="mod_mime_magic.xml.meta">

<name>mod_mime_magic</name>
<description>Determines the MIME type of a file
    by looking at a few bytes of its contents</description>
<status>Extension</status>
<sourcefile>mod_mime_magic.c</sourcefile>
<identifier>mime_magic_module</identifier>

<summary>
    <p>This module determines the MIME type of files in the same
    way the Unix <code>file(1)</code> command works: it looks at the first
    few bytes of the file. It is intended as a "second line of defense"
    for cases that <module>mod_mime</module> can't resolve.</p>

    <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 <directive module="mod_mime_magic"
    >MimeMagicFile</directive> directive.</p>
</summary>

<section id="format"><title>Format of the Magic File</title>

    <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 (<code>#</code>). The remaining lines are parsed for
    the following columns:</p>

    <table style="zebra" border="1">
    <columnspec><column width=".15"/><column width=".8"/></columnspec>
    <tr><th>Column</th><th>Description</th></tr>
    <tr><td>1</td>
        <td>byte number to begin checking from<br />
         "<code>&gt;</code>" indicates a dependency upon the previous
         non-"<code>&gt;</code>" line</td></tr>

    <tr><td>2</td>
        <td><p>type of data to match</p>
        <table border="1">
        <columnspec><column width=".2"/><column width=".7"/></columnspec>
        <tr><td><code>byte</code></td>
            <td>single character</td></tr>
        <tr><td><code>short</code></td>
            <td>machine-order 16-bit integer</td></tr>
        <tr><td><code>long</code></td>
            <td>machine-order 32-bit integer</td></tr>
        <tr><td><code>string</code></td>
            <td>arbitrary-length string</td></tr>
        <tr><td><code>date</code></td>
            <td>long integer date (seconds since Unix epoch/1970)</td></tr>
        <tr><td><code>beshort</code></td>
            <td>big-endian 16-bit integer</td></tr>
        <tr><td><code>belong</code></td>
            <td>big-endian 32-bit integer</td></tr>
        <tr><td><code>bedate</code></td>
            <td>big-endian 32-bit integer date</td></tr>
        <tr><td><code>leshort</code></td>
            <td>little-endian 16-bit integer</td></tr>
        <tr><td><code>lelong</code></td>
            <td>little-endian 32-bit integer</td></tr>
        <tr><td><code>ledate</code></td>
            <td>little-endian 32-bit integer date</td></tr>
        </table></td></tr>

    <tr><td>3</td>
        <td>contents of data to match</td></tr>

    <tr><td>4</td>
        <td>MIME type if matched</td></tr>

    <tr><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:</p>

    <example>
<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>
    </example>

    <p>Or these would recognize the difference between <code>*.doc</code>
    files containing Microsoft Word or FrameMaker documents. (These are
    incompatible file formats which use the same file suffix.)</p>

    <example>
<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>
    </example>

    <p>An optional MIME encoding can be included as a fifth column.
    For example, this can recognize gzipped files and set the
    encoding for them.</p>

    <example>
<pre># gzip (GNU zip, not to be confused with
#       [Info-ZIP/PKWARE] zip archiver)

0  string  \037\213  application/octet-stream  x-gzip</pre>
    </example>
</section>

<section id="performance"><title>Performance Issues</title>
    <p>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>

    <p>However, an effort was made to improve the performance of
    the original <code>file(1)</code> 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>
</section>

<section id="notes"><title>Notes</title>
    <p>The following notes apply to the <module>mod_mime_magic</module>
    module and are included here for compliance with contributors'
    copyright restrictions that require their acknowledgment.</p>

    <note>
      <p>mod_mime_magic: MIME type lookup via file magic numbers<br />
      Copyright (c) 1996-1997 Cisco Systems, Inc.</p>

      <p>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.</p>

      <p>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.</p>
    </note>

    <note>
      <p> - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.</p>

      <p>This software is not subject to any license of the American
      Telephone and Telegraph Company or of the Regents of the University
      of California.</p>

      <p>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:</p>

      <ol>
        <li>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.</li>

        <li>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.</li>

        <li>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.</li>

        <li>This notice may not be removed or altered.</li>
      </ol>
    </note>

    <note>
      <p>For compliance with Mr Darwin's terms: this has been very
      significantly modified from the free "file" command.</p>

      <ul>
        <li>all-in-one file for compilation convenience when moving from
        one version of Apache to the next.</li>

        <li>Memory allocation is done through the Apache API's pool
        structure.</li>

        <li>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.)</li>

        <li>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
        <code>realloc()</code>.</li>

        <li>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.)</li>

        <li>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.</li>

        <li>Command-line flags have been removed since they will never be
        used here.</li>
      </ul>
    </note>
</section>

<directivesynopsis>
<name>MimeMagicFile</name>
<description>Enable MIME-type determination based on file contents
using the specified magic file</description>
<syntax>MimeMagicFile <var>file-path</var></syntax>
<contextlist><context>server config</context><context>virtual host</context>
</contextlist>

<usage>
    <p>The <directive>MimeMagicFile</directive> 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
    <directive module="core">ServerRoot</directive>. 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>

    <example><title>Example</title>
      MimeMagicFile conf/magic
    </example>
</usage>
</directivesynopsis>

</modulesynopsis>