• 24/08/2003

Apache module mod_mime_magic

This module provides for determining the MIME type of a file by looking at a few bytes of its contents.

Status: Extension
Source File: mod_mime_magic.c
Module Identifier: mime_magic_module

Summary

This module determines the MIME type of files in the same way the Unix file(1) command works: it looks at the first few bytes of the file. It is intended as a “second line of defense” for cases that mod_mime can’t resolve. To assure that mod_mime gets first try at determining a file’s MIME type, be sure to list mod_mime_magic before mod_mime in the configuration.

This module is derived from a free version of the file(1) 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 MimeMagicFile directive.

Directives

Format of the Magic File

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:

Column Description
1 byte number to begin checking from
“>” indicates a dependency upon the previous non-“>” line
2 type of data to match

byte single character
short machine-order 16-bit integer
long machine-order 32-bit integer
string arbitrary-length string
date long integer date (seconds since Unix epoch/1970)
beshort big-endian 16-bit integer
belong big-endian 32-bit integer
bedate big-endian 32-bit integer date
leshort little-endian 16-bit integer
lelong little-endian 32-bit integer
ledate little-endian 32-bit integer date
3 contents of data to match
4 MIME type if matched
5 MIME encoding if matched (optional)

For example, the following magic file lines would recognize some audio formats.

# Sun/NeXT audio data
0       string          .snd
>12     belong          1               audio/basic
>12     belong          2               audio/basic
>12     belong          3               audio/basic
>12     belong          4               audio/basic
>12     belong          5               audio/basic
>12     belong          6               audio/basic
>12     belong          7               audio/basic
>12     belong          23              audio/x-adpcm

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

# Frame
0       string          \
Close Bitnami banner
Bitnami