<-
Apache > HTTP Server > Documentation > Version 2.5 > Modules

Apache Module mod_mime_libmagic

Available Languages:  en 

Description:Determines the MIME type of a file from its contents using libmagic
Status:Extension
Module Identifier:mime_libmagic_module
Source File:mod_mime_libmagic.c
Compatibility:Available in Apache 2.5.1 and later

Summary

This module determines the MIME type of a file by examining its contents, in the same way the file(1) command does. It is intended as a "second line of defense" for files which mod_mime cannot type from their extension: it runs after mod_mime and does nothing if a type has already been set.

Unlike mod_mime_magic, which has its own copy of the file(1) engine and its own magic database, this module uses the system's libmagic(3) library and magic database from the file package, so the types it reports are those which file --mime-type would report. The module is built only if libmagic is found at configure time, and is only active in a server or virtual host where MimeLibmagic is On.

Where libmagic reports application/octet-stream the module sets no type, leaving the response without a Content-Type header, as mod_mime_magic does for unrecognised content. Directories are typed as httpd/unix-directory and empty files as text/plain.

The module does not look inside compressed files and never sets a Content-Encoding: a gzip file is reported as application/gzip. This differs from mod_mime_magic, which reports the compressed file's encoding.

Topics

Directives

Bugfix checklist

See also

top

Performance and memory overhead

A libmagic handle is created, loaded with the magic database and destroyed for every request examined. Loading a compiled database (magic.mgc) maps the file into memory rather than parsing it, and the mapped pages are shared between all server processes, so this is inexpensive. If the configured database is a text file rather than a compiled one, it is parsed on every request.

Only the first MimeLibmagicBytes bytes of a file are read and examined, whatever its size.

Examining text content in particular can allocate a few megabytes of memory temporarily. With a threaded MPM and a C library which keeps a separate heap per thread, each server thread retains its peak allocation, so the resident size of a busy child process can grow by that much per thread before levelling off. This is not a leak.

top

MimeLibmagic Directive

Description:Enable MIME type determination from file contents
Syntax:MimeLibmagic On|Off
Default:MimeLibmagic Off
Context:server config, virtual host
Status:Extension
Module:mod_mime_libmagic

The MimeLibmagic directive enables the module for a server or virtual host. Files which mod_mime leaves untyped, typically those without a recognised extension, then have their contents examined by libmagic.

Example

MimeLibmagic On
top

MimeLibmagicBytes Directive

Description:Number of bytes of a file examined
Syntax:MimeLibmagicBytes bytes
Default:MimeLibmagicBytes 16384
Context:server config, virtual host
Status:Extension
Module:mod_mime_libmagic

The MimeLibmagicBytes directive sets how many bytes from the start of a file are read and examined. Larger values allow magic rules which look deeper into a file to match, at the cost of reading more of every examined file. The maximum is 1048576.

top

MimeLibmagicCharset Directive

Description:Add a charset parameter to text types
Syntax:MimeLibmagicCharset On|Off
Default:MimeLibmagicCharset Off
Context:server config, virtual host
Status:Extension
Module:mod_mime_libmagic

When MimeLibmagicCharset is On, the character encoding libmagic detects (such as us-ascii, utf-8 or iso-8859-1) is added as a charset parameter to text/* types, giving for example text/plain; charset=utf-8. Other types are not affected. As with AddDefaultCharset, the detected charset should be trusted only where the content is known to be encoded consistently.

top

MimeLibmagicFile Directive

Description:The magic database to use
Syntax:MimeLibmagicFile file-path
Default:The default database of the installed libmagic
Context:server config, virtual host
Status:Extension
Module:mod_mime_libmagic

The MimeLibmagicFile directive names the magic database, in place of the default compiled into libmagic (typically /usr/share/misc/magic, for which the compiled magic.mgc alongside it is loaded). The database must be in the format described in magic(5); the file distributed with mod_mime_magic is not suitable. A relative path is taken relative to the ServerRoot. If the database cannot be loaded, an error is logged for each request examined and the file is left untyped.

Note that the MAGIC environment variable, if set when the server is started, overrides the library default, but not a database given with this directive.

Example

MimeLibmagicFile /usr/share/misc/magic.mgc

Available Languages:  en