httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dean Gaudet <dgau...@hyperreal.org>
Subject cvs commit: apache/src mod_mime_magic.c CHANGES Configuration.tmpl
Date Mon, 07 Jul 1997 18:18:48 GMT
dgaudet     97/07/07 11:18:46

  Modified:    conf      srm.conf-dist
               htdocs/manual/mod  directives.html index.html
               src       CHANGES Configuration.tmpl
  Added:       conf      magic
               htdocs/manual/mod  mod_mime_magic.html
               src       mod_mime_magic.c
  Log:
  Added mod_mime_magic.
  
  Submitted by:	Ian Kluft <ikluft@cisco.com>
  
  Revision  Changes    Path
  1.14      +4 -0      apache/conf/srm.conf-dist
  
  Index: srm.conf-dist
  ===================================================================
  RCS file: /export/home/cvs/apache/conf/srm.conf-dist,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -C3 -r1.13 -r1.14
  *** srm.conf-dist	1997/06/30 01:21:15	1.13
  --- srm.conf-dist	1997/07/07 18:18:36	1.14
  ***************
  *** 204,206 ****
  --- 204,210 ----
    #    3) external redirects
    #ErrorDocument 402 http://some.other_server.com/subscription_info.html
    #
  + 
  + # mod_mime_magic allows the server to use various hints from the file itself
  + # to determine its type.
  + #MimeMagicFile conf/magic
  
  
  
  1.1                  apache/conf/magic
  
  Index: magic
  ===================================================================
  # Magic data for mod_mime_magic Apache module (originally for file(1) command)
  # The module is described in htdocs/manual/mod/mod_mime_magic.html
  #
  # The format is 4-5 columns:
  #    Column #1: byte number to begin checking from, ">" indicates continuation
  #    Column #2: type of data to match
  #    Column #3: contents of data to match
  #    Column #4: MIME type of result
  #    Column #5: MIME encoding of result (optional)
  
  #------------------------------------------------------------------------------
  # Localstuff:  file(1) magic for locally observed files
  # Add any locally observed files here.
  
  #------------------------------------------------------------------------------
  # end local stuff
  #------------------------------------------------------------------------------
  
  #------------------------------------------------------------------------------
  # Java
  
  0	short		0xcafe
  >2	short		0xbabe		application/java
  
  #------------------------------------------------------------------------------
  # audio:  file(1) magic for sound formats
  #
  # from Jan Nicolai Langfeldt <janl@ifi.uio.no>,
  #
  
  # 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
  
  # DEC systems (e.g. DECstation 5000) use a variant of the Sun/NeXT format
  # that uses little-endian encoding and has a different magic number
  # (0x0064732E in little-endian encoding).
  0	lelong		0x0064732E	
  >12	lelong		1		audio/x-dec-basic
  >12	lelong		2		audio/x-dec-basic
  >12	lelong		3		audio/x-dec-basic
  >12	lelong		4		audio/x-dec-basic
  >12	lelong		5		audio/x-dec-basic
  >12	lelong		6		audio/x-dec-basic
  >12	lelong		7		audio/x-dec-basic
  #                                       compressed (G.721 ADPCM)
  >12	lelong		23		audio/x-dec-adpcm
  
  # Bytes 0-3 of AIFF, AIFF-C, & 8SVX audio files are "FORM"
  #					AIFF audio data
  8	string		AIFF		audio/x-aiff	
  #					AIFF-C audio data
  8	string		AIFC		audio/x-aiff	
  #					IFF/8SVX audio data
  8	string		8SVX		audio/x-aiff	
  
  # Creative Labs AUDIO stuff
  #					Standard MIDI data
  0	string	MThd			audio/unknown	
  #>9 	byte	>0			(format %d)
  #>11	byte	>1			using %d channels
  #					Creative Music (CMF) data
  0	string	CTMF			audio/unknown	
  #					SoundBlaster instrument data
  0	string	SBI			audio/unknown	
  #					Creative Labs voice data
  0	string	Creative\ Voice\ File	audio/unknown	
  ## is this next line right?  it came this way...
  #>19	byte	0x1A
  #>23	byte	>0			- version %d
  #>22	byte	>0			\b.%d
  
  # [GRR 950115:  is this also Creative Labs?  Guessing that first line
  #  should be string instead of unknown-endian long...]
  #0	long		0x4e54524b	MultiTrack sound data
  #0	string		NTRK		MultiTrack sound data
  #>4	long		x		- version %ld
  
  # Microsoft WAVE format (*.wav)
  # [GRR 950115:  probably all of the shorts and longs should be leshort/lelong]
  #					Microsoft RIFF
  0	string		RIFF		audio/unknown	
  #					- WAVE format
  >8	string		WAVE		audio/x-wav	
  
  #------------------------------------------------------------------------------
  # c-lang:  file(1) magic for C programs or various scripts
  #
  
  # XPM icons (Greg Roelofs, newt@uchicago.edu)
  # ideally should go into "images", but entries below would tag XPM as C source
  0	string		/*\ XPM		image/x-xbm	7bit
  
  # this first will upset you if you're a PL/1 shop... (are there any left?)
  # in which case rm it; ascmagic will catch real C programs
  #					C or REXX program text
  0	string		/*		text/plain
  #					C++ program text
  0	string		//		text/plain
  
  #------------------------------------------------------------------------------
  # compress:  file(1) magic for pure-compression formats (no archives)
  #
  # compress, gzip, pack, compact, huf, squeeze, crunch, freeze, yabba, whap, etc.
  #
  # Formats for various forms of compressed data
  # Formats for "compress" proper have been moved into "compress.c",
  # because it tries to uncompress it to figure out what's inside.
  
  # standard unix compress
  0	string		\037\235	application/octet-stream	x-compress
  
  # gzip (GNU zip, not to be confused with [Info-ZIP/PKWARE] zip archiver)
  0       string          \037\213        application/octet-stream	x-gzip
  
  # According to gzip.h, this is the correct byte order for packed data.
  0	string		\037\036	application/octet-stream
  #
  # This magic number is byte-order-independent.
  #
  0	short		017437		application/octet-stream
  
  # XXX - why *two* entries for "compacted data", one of which is
  # byte-order independent, and one of which is byte-order dependent?
  #
  # compacted data
  0	short		0x1fff		application/octet-stream
  0	string		\377\037	application/octet-stream
  # huf output
  0	short		0145405		application/octet-stream
  
  # Squeeze and Crunch...
  # These numbers were gleaned from the Unix versions of the programs to
  # handle these formats.  Note that I can only uncrunch, not crunch, and
  # I didn't have a crunched file handy, so the crunch number is untested.
  #				Keith Waclena <keith@cerberus.uchicago.edu>
  #0	leshort		0x76FF		squeezed data (CP/M, DOS)
  #0	leshort		0x76FE		crunched data (CP/M, DOS)
  
  # Freeze
  #0	string		\037\237	Frozen file 2.1
  #0	string		\037\236	Frozen file 1.0 (or gzip 0.5)
  
  # lzh?
  #0	string		\037\240	LZH compressed data
  
  #------------------------------------------------------------------------------
  # frame:  file(1) magic for FrameMaker files
  #
  # This stuff came on a FrameMaker demo tape, most of which is
  # copyright, but this file is "published" as witness the following:
  #
  0	string		\<MakerFile	application/x-frame
  0	string		\<MIFFile	application/x-frame
  0	string		\<MakerDictionary	application/x-frame
  0	string		\<MakerScreenFon	application/x-frame
  0	string		\<MML		application/x-frame
  0	string		\<Book		application/x-frame
  0	string		\<Maker		application/x-frame
  
  #------------------------------------------------------------------------------
  # html:  file(1) magic for HTML (HyperText Markup Language) docs
  #
  # from Daniel Quinlan <quinlan@yggdrasil.com>
  #
  0	string		\<HEAD		text/html
  0	string		\<head		text/html
  0	string		\<TITLE		text/html
  0	string		\<title		text/html
  0       string          \<html          text/html
  0       string          \<HTML          text/html
  0	string		\<!--		text/html
  0	string		\<h1		text/html
  0	string		\<H1		text/html
  
  #------------------------------------------------------------------------------
  # images:  file(1) magic for image formats (see also "c-lang" for XPM bitmaps)
  #
  # originally from jef@helios.ee.lbl.gov (Jef Poskanzer),
  # additions by janl@ifi.uio.no as well as others. Jan also suggested
  # merging several one- and two-line files into here.
  #
  # XXX - byte order for GIF and TIFF fields?
  # [GRR:  TIFF allows both byte orders; GIF is probably little-endian]
  #
  
  # [GRR:  what the hell is this doing in here?]
  #0	string		xbtoa		btoa'd file
  
  # PBMPLUS
  #					PBM file
  0	string		P1		image/x-portable-bitmap	7bit
  #					PGM file
  0	string		P2		image/x-portable-greymap	7bit
  #					PPM file
  0	string		P3		image/x-portable-pixmap	7bit
  #					PBM "rawbits" file
  0	string		P4		image/x-portable-bitmap
  #					PGM "rawbits" file
  0	string		P5		image/x-portable-greymap
  #					PPM "rawbits" file
  0	string		P6		image/x-portable-pixmap
  
  # NIFF (Navy Interchange File Format, a modification of TIFF)
  # [GRR:  this *must* go before TIFF]
  0	string		IIN1		image/x-niff
  
  # TIFF and friends
  #					TIFF file, big-endian
  0	string		MM		image/tiff
  #					TIFF file, little-endian
  0	string		II		image/tiff
  
  # possible GIF replacements; none yet released!
  # (Greg Roelofs, newt@uchicago.edu)
  #
  # GRR 950115:  this was mine ("Zip GIF"):
  #					ZIF image (GIF+deflate alpha)
  0	string		GIF94z		image/unknown
  #
  # GRR 950115:  this is Jeremy Wohl's Free Graphics Format (better):
  #					FGF image (GIF+deflate beta)
  0	string		FGF95a		image/unknown
  #
  # GRR 950115:  this is Thomas Boutell's Portable Bitmap Format proposal
  # (best; not yet implemented):
  #					PBF image (deflate compression)
  0	string		PBF		image/unknown
  
  # GIF
  0	string		GIF		image/gif
  
  # JPEG images
  0	beshort		0xffd8		image/jpeg
  
  # PC bitmaps (OS/2, Windoze BMP files)  (Greg Roelofs, newt@uchicago.edu)
  0	string		BM		image/bmp
  #>14	byte		12		(OS/2 1.x format)
  #>14	byte		64		(OS/2 2.x format)
  #>14	byte		40		(Windows 3.x format)
  #0	string		IC		icon
  #0	string		PI		pointer
  #0	string		CI		color icon
  #0	string		CP		color pointer
  #0	string		BA		bitmap array
  
  
  #------------------------------------------------------------------------------
  # lisp:  file(1) magic for lisp programs
  #
  # various lisp types, from Daniel Quinlan (quinlan@yggdrasil.com)
  0	string	;;			text/plain	8bit
  # Emacs 18 - this is always correct, but not very magical.
  0	string	\012(			application/x-elc
  # Emacs 19
  0	string	;ELC\023\000\000\000	application/x-elc
  
  #------------------------------------------------------------------------------
  # mail.news:  file(1) magic for mail and news
  #
  # There are tests to ascmagic.c to cope with mail and news.
  0	string		Relay-Version: 	message/rfc822	7bit
  0	string		#!\ rnews	message/rfc822	7bit
  0	string		N#!\ rnews	message/rfc822	7bit
  0	string		Forward\ to 	message/rfc822	7bit
  0	string		Pipe\ to 	message/rfc822	7bit
  0	string		Return-Path:	message/rfc822	7bit
  0	string		Path:		message/news	8bit
  0	string		Xref:		message/news	8bit
  0	string		From:		message/rfc822	7bit
  0	string		Article 	message/news	8bit
  #------------------------------------------------------------------------------
  # msword: file(1) magic for MS Word files
  #
  # Contributor claims:
  # Reversed-engineered MS Word magic numbers
  #
  
  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
  
  
  
  #------------------------------------------------------------------------------
  # printer:  file(1) magic for printer-formatted files
  #
  
  # PostScript
  0	string		%!		application/postscript
  0	string		\004%!		application/postscript
  
  # Acrobat
  # (due to clamen@cs.cmu.edu)
  0	string		%PDF-		application/pdf
  
  #------------------------------------------------------------------------------
  # sc:  file(1) magic for "sc" spreadsheet
  #
  38	string		Spreadsheet	application/x-sc
  
  #------------------------------------------------------------------------------
  # tex:  file(1) magic for TeX files
  #
  # XXX - needs byte-endian stuff (big-endian and little-endian DVI?)
  #
  # From <conklin@talisman.kaleida.com>
  
  # Although we may know the offset of certain text fields in TeX DVI
  # and font files, we can't use them reliably because they are not
  # zero terminated. [but we do anyway, christos]
  0	string		\367\002	application/x-dvi
  #0	string		\367\203	TeX generic font data
  #0	string		\367\131	TeX packed font data
  #0	string		\367\312	TeX virtual font data
  #0	string		This\ is\ TeX,	TeX transcript text	
  #0	string		This\ is\ METAFONT,	METAFONT transcript text
  
  # There is no way to detect TeX Font Metric (*.tfm) files without
  # breaking them apart and reading the data.  The following patterns
  # match most *.tfm files generated by METAFONT or afm2tfm.
  #2	string		\000\021	TeX font metric data
  #2	string		\000\022	TeX font metric data
  #>34	string		>\0		(%s)
  
  # Texinfo and GNU Info, from Daniel Quinlan (quinlan@yggdrasil.com)
  #0	string		\\input\ texinfo	Texinfo source text
  #0	string		This\ is\ Info\ file	GNU Info text
  
  # correct TeX magic for Linux (and maybe more)
  # from Peter Tobias (tobias@server.et-inf.fho-emden.de)
  #
  0	leshort		0x02f7		application/x-dvi
  
  # RTF - Rich Text Format
  0	string		{\\rtf		application/rtf
  
  #------------------------------------------------------------------------------
  # animation:  file(1) magic for animation/movie formats
  #
  # animation formats, originally from vax@ccwf.cc.utexas.edu (VaX#n8)
  #						MPEG file
  0	string		\000\000\001\263	video/mpeg
  #
  # The contributor claims:
  #   I couldn't find a real magic number for these, however, this
  #   -appears- to work.  Note that it might catch other files, too,
  #   so BE CAREFUL!
  #
  # Note that title and author appear in the two 20-byte chunks
  # at decimal offsets 2 and 22, respectively, but they are XOR'ed with
  # 255 (hex FF)! DL format SUCKS BIG ROCKS.
  #
  #						DL file version 1 , medium format (160x100, 4 images/screen)
  0	byte		1			video/unknown
  0	byte		2			video/unknown
  
  
  
  
  1.24      +1 -0      apache/htdocs/manual/mod/directives.html
  
  Index: directives.html
  ===================================================================
  RCS file: /export/home/cvs/apache/htdocs/manual/mod/directives.html,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -C3 -r1.23 -r1.24
  *** directives.html	1997/07/06 17:19:14	1.23
  --- directives.html	1997/07/07 18:18:39	1.24
  ***************
  *** 115,120 ****
  --- 115,121 ----
    <li><A HREF="core.html#maxspareservers">MaxSpareServers</A>
    <li><A HREF="mod_cern_meta.html#metadir">MetaDir</A>
    <li><A HREF="mod_cern_meta.html#metasuffix">MetaSuffix</A>
  + <li><A HREF="mod_mime_magic.html#mimemagicfile">MimeMagicFile</A>
    <li><A HREF="core.html#minspareservers">MinSpareServers</A>
    <li><A HREF="mod_proxy.html#nocache">NoCache</A>
    <li><A HREF="core.html#options">Options</A>
  
  
  
  1.15      +3 -1      apache/htdocs/manual/mod/index.html
  
  Index: index.html
  ===================================================================
  RCS file: /export/home/cvs/apache/htdocs/manual/mod/index.html,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -C3 -r1.14 -r1.15
  *** index.html	1997/07/06 17:19:14	1.14
  --- index.html	1997/07/07 18:18:39	1.15
  ***************
  *** 83,89 ****
    <dt><A HREF="mod_log_referer.html">mod_log_referer</A>
    <dd>Logging of document references.
    <dt><A HREF="mod_mime.html">mod_mime</A>
  ! <dd>Determining document types.
    <dt><A HREF="mod_negotiation.html">mod_negotiation</A>
    <dd>Content negotiation.
    <dt><A HREF="mod_rewrite.html">mod_rewrite</a> Apache 1.2 and up
  --- 83,91 ----
    <dt><A HREF="mod_log_referer.html">mod_log_referer</A>
    <dd>Logging of document references.
    <dt><A HREF="mod_mime.html">mod_mime</A>
  ! <dd>Determining document types using file extensions.
  ! <dt><A HREF="mod_mime_magic.html">mod_mime_magic</A>
  ! <dd>Determining document types using "magic numbers".
    <dt><A HREF="mod_negotiation.html">mod_negotiation</A>
    <dd>Content negotiation.
    <dt><A HREF="mod_rewrite.html">mod_rewrite</a> Apache 1.2 and up
  
  
  
  1.1                  apache/htdocs/manual/mod/mod_mime_magic.html
  
  Index: mod_mime_magic.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  <HTML>
   <HEAD>
    <TITLE>Apache module mod_mime_magic</TITLE>
   </HEAD>
   <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
   <BODY
    BGCOLOR="#FFFFFF"
    TEXT="#000000"
    LINK="#0000FF"
    VLINK="#000080"
    ALINK="#FF0000"
   >
    <h1 align="CENTER">Module mod_mime_magic</h1>
  
    This module is an optional extension to the Apache HTTPD server.
    The current version can be obtained from
    <a href="http://www.employees.org/~ikluft/apache/mod_mime_magic/">
    http://www.employees.org/~ikluft/apache/mod_mime_magic/</a>.
  
    <h2>Summary</h2>
  
    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.
    In the case of this module,
    it tries to figure out the MIME type of the file.
    <p>
    This module is only active if the magic file exists and
    was successfully opened at server-configuration time.
    The magic file can be named by the
    <A HREF="#mimemagicfile"><code>MimeMagicFile</code></a>
    directive or defaults to conf/magic.
    <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 "#".
    The remaining lines are parsed for the following columns:
    <table border=1>
    <tr valign=top>
    <th>Column</th>
    <th>Description</th>
    </tr>
    <tr valign=top>
    <td>1</td>
    <td>byte number to begin checking from
    <br>
    "&gt;" indicates a dependency upon the previous non-"&gt;" line</td>
    </tr><tr valign=top>
    <td>2</td>
    <td>type of data to match
      <table border=1>
      <tr><td>byte</td><td>single character</td></tr>
      <tr><td>short</td><td>machine-order 16-bit integer</td></tr>
      <tr><td>long</td><td>machine-order 32-bit integer</td></tr>
      <tr><td>string</td><td>arbitrary-length string</td></tr>
      <tr><td>date</td><td>long integer date
        (seconds since Unix epoch/1970)</td></tr>
      <tr><td>beshort</td><td>big-endian 16-bit integer</td></tr>
      <tr><td>belong</td><td>big-endian 32-bit integer</td></tr>
      <tr><td>bedate</td><td>big-endian 32-bit integer date</td></tr>
      <tr><td>leshort</td><td>little-endian 16-bit integer</td></tr>
      <tr><td>lelong</td><td>little-endian 32-bit integer</td></tr>
      <tr><td>ledate</td><td>little-endian 32-bit integer date</td></tr>
      </table>
    </td>
    </tr><tr valign=top>
    <td>3</td>
    <td>contents of data to match</td>
    </tr><tr valign=top>
    <td>4</td>
    <td>MIME type if matched</td>
    </tr><tr valign=top>
    <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.
  
  <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>
  
    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.)
  
  <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>
  
    An optional MIME encoding can be included as a fifth column.
    For example, this can recognize gzipped files and set the encoding
    for them.
  
  <pre>
  # gzip (GNU zip, not to be confused with [Info-ZIP/PKWARE] zip archiver)
  0       string          \037\213        application/octet-stream        x-gzip
  </pre>
  
    <h3>Performance Issues</h3>
  
    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>
    However, an effort was made to improve the performance of the original
    file(1) 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>
    When compiling an Apache server, this module should be at or near the
    top of the list of modules in the Configuration file.  The modules are
    listed in increasing priority so that will mean this one is used only
    as a last resort, just like it was designed to.
  
    <h2>Directives</h2>
    <p>
    <UL>
     <LI><A HREF="#mimemagicfile">MimeMagicFile</A>
     </LI>
    </UL>
    </P>
    <HR>
    <H2><A NAME="mimemagicfile">
     MimeMagicFile
    </A></H2>
    <P>
    <STRONG>Syntax:</STRONG> MimeMagicFile <em>magic-file-name</em>
    <BR>
    <STRONG>Default:</STRONG> conf/magic
    <BR>
    <STRONG>Context:</STRONG> server config, virtual host
    <BR>
    <STRONG>Status:</STRONG> Extension
    <BR>
    <STRONG>Module:</STRONG> mod_mime_magic
    <p>
  
    The MimeMagicFile directive can be used to change the location of the
    magic file from its default location at <code>conf/magic</code>.
    Non-rooted paths are relative to the ServerRoot.
    <p>
    <HR>
  
    <h2><a name="notes">Notes</a></h2>
  
    Patches and suggestions for mod_mime_magic should be sent to
    Ian Kluft &lt;ikluft<!--- comment inserted to discourage spam --->@cisco.com&gt;.
    Note that enhancements are done on a volunteer basis so no timetable can
    be committed for any particular request.
    Obviously, patches are given much higher priority over plain requests.
    <p>
    The following notes apply to the mod_mime_magic module and are
    included here for compliance with contributors' copyright restrictions
    that require their acknowledgement.
  
  <pre>
  /*
   * mod_mime_magic: MIME type lookup via file magic numbers
   * Copyright (c) 1996-1997 Cisco Systems, Inc.
   *
   * 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.
   *
   * 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.
   * ---------------------------------------------------------------------------
   * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
   *
   * This software is not subject to any license of the American Telephone and
   * Telegraph Company or of the Regents of the University of California.
   *
   * 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:
   *
   * 1. 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.
   *
   * 2. 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.
   *
   * 3. 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.
   *
   * 4. This notice may not be removed or altered.
   * -------------------------------------------------------------------------
   *
   * For complicance with Mr Darwin's terms: this has been very significantly
   * modified from the free "file" command.
   * - all-in-one file for compilation convenience when moving from one
   *   version of Apache to the next.
   * - Memory allocation is done through the Apache API's pool structure.
   * - All functions have had necessary Apache API request or server
   *   structures passed to them where necessary to call other Apache API
   *   routines.  (i.e. usually for logging, files, or memory allocation in
   *   itself or a called function.)
   * - 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 realloc().
   * - 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.)
   * - 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.
   * - Command-line flags have been removed since they will never be used here.
   *
   */
  </pre>
  
   </BODY>
  </HTML>
  
  
  
  1.325     +4 -0      apache/src/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /export/home/cvs/apache/src/CHANGES,v
  retrieving revision 1.324
  retrieving revision 1.325
  diff -C3 -r1.324 -r1.325
  *** CHANGES	1997/07/07 14:34:24	1.324
  --- CHANGES	1997/07/07 18:18:42	1.325
  ***************
  *** 1,4 ****
  --- 1,8 ----
    Changes with Apache 1.3
  +   
  +   *) The new mod_mime_magic can be used to "magically" determine the type
  +      of a file if the extension is unknown.  Based on the unix file(1)
  +      command.  [Ian Kluft <ikluft@cisco.com>]
    
      *) We now determine and display the time spent processing a
         request if desired.
  
  
  
  1.65      +6 -1      apache/src/Configuration.tmpl
  
  Index: Configuration.tmpl
  ===================================================================
  RCS file: /export/home/cvs/apache/src/Configuration.tmpl,v
  retrieving revision 1.64
  retrieving revision 1.65
  diff -C3 -r1.64 -r1.65
  *** Configuration.tmpl	1997/06/30 21:42:30	1.64
  --- Configuration.tmpl	1997/07/07 18:18:42	1.65
  ***************
  *** 147,156 ****
    ##
    ## Type checking modules
    ##
    ## mod_mime maps filename extensions to content types, encodings, and
  ! ## magic type handlers (the latter is obsoleted by mod_actions).
    ## mod_negotiation allows content selection based on the Accept* headers.
    
    Module mime_module         mod_mime.o
    Module negotiation_module  mod_negotiation.o
    
  --- 147,161 ----
    ##
    ## Type checking modules
    ##
  + ## mod_mime_magic determines the type of a file by examining a few bytes
  + ## of it and testing against a database of filetype signatures.  It is
  + ## based on the unix file(1) command.
    ## mod_mime maps filename extensions to content types, encodings, and
  ! ## "magic" type handlers (the latter is obsoleted by mod_actions, and
  ! ## don't confuse it with the previous module).
    ## mod_negotiation allows content selection based on the Accept* headers.
    
  + # Module mime_magic_module   mod_mime_magic.o
    Module mime_module         mod_mime.o
    Module negotiation_module  mod_negotiation.o
    
  
  
  
  1.1                  apache/src/mod_mime_magic.c
  
  Index: mod_mime_magic.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission.
   *
   * 5. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group and was originally based
   * on public domain software written at the National Center for
   * Supercomputing Applications, University of Illinois, Urbana-Champaign.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  /*
   * mod_mime_magic: MIME type lookup via file magic numbers
   * Copyright (c) 1996-1997 Cisco Systems, Inc.
   * $Revision: 1.1 $
   * 
   * 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.
   * 
   * 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.
   * ---------------------------------------------------------------------------
   * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
   * 
   * This software is not subject to any license of the American Telephone and
   * Telegraph Company or of the Regents of the University of California.
   * 
   * 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:
   * 
   * 1. 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.
   * 
   * 2. 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.
   * 
   * 3. 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.
   * 
   * 4. This notice may not be removed or altered.
   * -------------------------------------------------------------------------
   * 
   * For complicance with Mr Darwin's terms: this has been very significantly
   * modified from the free "file" command.
   * - all-in-one file for compilation convenience when moving from one
   *   version of Apache to the next.
   * - Memory allocation is done through the Apache API's pool structure.
   * - All functions have had necessary Apache API request or server
   *   structures passed to them where necessary to call other Apache API
   *   routines.  (i.e. usually for logging, files, or memory allocation in
   *   itself or a called function.)
   * - 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 realloc().
   * - 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.)
   * - 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.
   * - Command-line flags have been removed since they will never be used here.
   * 
   * Ian Kluft <ikluft@cisco.com>
   * Engineering Information Framework
   * Central Engineering
   * Cisco Systems, Inc.
   * San Jose, CA, USA
   * 
   * Initial installation          July/August 1996
   * Misc bug fixes                May 1997
   * Submission to Apache Group    July 1997
   * 
   */
  
  #include <sys/wait.h>
  #include <sys/stat.h>
  #include <sys/sysmacros.h>
  #include <time.h>
  #include <utime.h>
  #include <stdarg.h>
  #include "httpd.h"
  #include "http_config.h"
  #include "http_request.h"
  #include "http_core.h"
  #include "http_log.h"
  #include "http_protocol.h"
  
  
  /*
   * data structures and related constants
   */
  
  #define MODNAME        "mod_mime_magic"
  #define DEBUG        0
  
  #ifndef MAGIC
  #define MAGIC "conf/magic"
  #endif
  #define MIME_BINARY_UNKNOWN    "application/octet-stream"
  #define MIME_TEXT_UNKNOWN    "text/plain"
  
  #define MAXMIMESTRING        256
  
  #define HOWMANY 1024      /* big enough to recognize most WWW files */
  #define MAXDESC    50     /* max leng of text description */
  #define MAXstring 64      /* max leng of "string" types */
  
  struct magic {
      struct magic *next;   /* link to next entry */
      int lineno;           /* line number from magic file */
  
      short flag;
  #define INDIR    1        /* if '>(...)' appears,  */
  #define    UNSIGNED 2        /* comparison is unsigned */
      short cont_level;     /* level of ">" */
      struct {
          char type;        /* byte short long */
          long offset;      /* offset from indirection */
      } in;
      long offset;          /* offset to magic number */
      unsigned char reln;   /* relation (0=eq, '>'=gt, etc) */
      char type;            /* int, short, long or string. */
      char vallen;          /* length of string value, if any */
  #define BYTE    1
  #define SHORT    2
  #define LONG    4
  #define STRING    5
  #define DATE    6
  #define BESHORT    7
  #define BELONG    8
  #define BEDATE    9
  #define LESHORT    10
  #define LELONG    11
  #define LEDATE    12
      union VALUETYPE {
          unsigned char b;
          unsigned short h;
          unsigned long l;
          char s[MAXstring];
          unsigned char hs[2];    /* 2 bytes of a fixed-endian "short" */
          unsigned char hl[4];    /* 2 bytes of a fixed-endian "long" */
      } value;    /* either number or string */
      unsigned long mask;    /* mask before comparison with value */
      char nospflag;        /* supress space character */
  
      /* NOTE: this string is suspected of overrunning - find it! */
      char desc[MAXDESC];    /* description */
  };
  
  /*
   * data structures for tar file recognition
   * --------------------------------------------------------------------------
   * Header file for public domain tar (tape archive) program.
   * 
   * @(#)tar.h 1.20 86/10/29    Public Domain. Created 25 August 1985 by John
   * Gilmore, ihnp4!hoptoad!gnu.
   * 
   * Header block on tape.
   * 
   * I'm going to use traditional DP naming conventions here. A "block" is a big
   * chunk of stuff that we do I/O on. A "record" is a piece of info that we
   * care about. Typically many "record"s fit into a "block".
   */
  #define RECORDSIZE    512
  #define NAMSIZ    100
  #define TUNMLEN    32
  #define TGNMLEN    32
  
  union record {
      char charptr[RECORDSIZE];
      struct header {
          char name[NAMSIZ];
          char mode[8];
          char uid[8];
          char gid[8];
          char size[12];
          char mtime[12];
          char chksum[8];
          char linkflag;
          char linkname[NAMSIZ];
          char magic[8];
          char uname[TUNMLEN];
          char gname[TGNMLEN];
          char devmajor[8];
          char devminor[8];
      } header;
  };
  
  /* The magic field is filled with this if uname and gname are valid. */
  #define    TMAGIC        "ustar  "    /* 7 chars and a null */
  
  /*
   * file-function prototypes
   */
  static int apprentice(server_rec *, pool *);
  static int ascmagic(request_rec *, unsigned char *, int);
  static int is_compress(const unsigned char *, int *);
  static int is_tar(unsigned char *, int);
  static void magic_process(request_rec *);
  static int softmagic(request_rec *, unsigned char *, int);
  static void tryit(request_rec *, unsigned char *, int);
  static int zmagic(request_rec *, unsigned char *, int);
  static unsigned long signextend(server_rec *, struct magic *, unsigned long);
  
  static int getvalue(server_rec *, struct magic *, char **);
  static int hextoint(int);
  static char *getstr(char *, char *, int, int *);
  static int parse(server_rec *, pool * p, char *, int);
  
  static int match(request_rec *, unsigned char *, int);
  static int 
  mget(request_rec *, union VALUETYPE *, unsigned char *,
       struct magic *, int);
  static int mcheck(request_rec *, union VALUETYPE *, struct magic *);
  static void mprint(request_rec *, union VALUETYPE *, struct magic *);
  static int mconvert(request_rec *, union VALUETYPE *, struct magic *);
  
  static int 
  uncompress(server_rec *, int, const unsigned char *,
         unsigned char **, int);
  static long from_oct(int, char *);
  static int fsmagic(request_rec * r, const char *fn, struct stat * sb);
  
  /*
   * includes for ASCII substring recognition formerly "names.h" in file
   * command
   * 
   * Original notes: names and types used by ascmagic in file(1). These tokens are
   * here because they can appear anywhere in the first HOWMANY bytes, while
   * tokens in /etc/magic must appear at fixed offsets into the file. Don't
   * make HOWMANY too high unless you have a very fast CPU.
   */
  
  /* these types are used to index the table 'types': keep em in sync! */
  /* HTML inserted in first because this is a web server module now */
  #define L_HTML    0        /* HTML */
  #define L_C       1        /* first and foremost on UNIX */
  #define L_FORT    2        /* the oldest one */
  #define L_MAKE    3        /* Makefiles */
  #define L_PLI     4        /* PL/1 */
  #define L_MACH    5        /* some kinda assembler */
  #define L_ENG     6        /* English */
  #define L_PAS     7        /* Pascal */
  #define L_MAIL    8        /* Electronic mail */
  #define L_NEWS    9        /* Usenet Netnews */
  
  static char *types[] = {
      "text/html",        /* HTML */
      "text/plain",       /* "c program text", */
      "text/plain",       /* "fortran program text", */
      "text/plain",       /* "make commands text", */
      "text/plain",       /* "pl/1 program text", */
      "text/plain",       /* "assembler program text", */
      "text/plain",       /* "English text", */
      "text/plain",       /* "pascal program text", */
      "message/rfc822",   /* "mail text", */
      "message/news",     /* "news text", */
      "application/binary", /* "can't happen error on names.h/types", */
  0};
  
  static struct names {
      char *name;
      short type;
  }     names[] = {
      /* These must be sorted by eye for optimal hit rate */
      /* Add to this list only after substantial meditation */
      {
          "<html>", L_HTML
      },
      {
          "<HTML>", L_HTML
      },
      {
          "<head>", L_HTML
      },
      {
          "<HEAD>", L_HTML
      },
      {
          "<title>", L_HTML
      },
      {
          "<TITLE>", L_HTML
      },
      {
          "<h1>", L_HTML
      },
      {
          "<H1>", L_HTML
      },
      {
          "<!--", L_HTML
      },
      {
          "<!DOCTYPE HTML", L_HTML
      },
      {
          "/*", L_C
      },            /* must preced "The", "the", etc. */
      {
          "#include", L_C
      },
      {
          "char", L_C
      },
      {
          "The", L_ENG
      },
      {
          "the", L_ENG
      },
      {
          "double", L_C
      },
      {
          "extern", L_C
      },
      {
          "float", L_C
      },
      {
          "real", L_C
      },
      {
          "struct", L_C
      },
      {
          "union", L_C
      },
      {
          "CFLAGS", L_MAKE
      },
      {
          "LDFLAGS", L_MAKE
      },
      {
          "all:", L_MAKE
      },
      {
          ".PRECIOUS", L_MAKE
      },
      /*
       * Too many files of text have these words in them.  Find another way
       * to recognize Fortrash.
       */
  #ifdef    NOTDEF
      {
          "subroutine", L_FORT
      },
      {
          "function", L_FORT
      },
      {
          "block", L_FORT
      },
      {
          "common", L_FORT
      },
      {
          "dimension", L_FORT
      },
      {
          "integer", L_FORT
      },
      {
          "data", L_FORT
      },
  #endif                /* NOTDEF */
      {
          ".ascii", L_MACH
      },
      {
          ".asciiz", L_MACH
      },
      {
          ".byte", L_MACH
      },
      {
          ".even", L_MACH
      },
      {
          ".globl", L_MACH
      },
      {
          "clr", L_MACH
      },
      {
          "(input,", L_PAS
      },
      {
          "dcl", L_PLI
      },
      {
          "Received:", L_MAIL
      },
      {
          ">From", L_MAIL
      },
      {
          "Return-Path:", L_MAIL
      },
      {
          "Cc:", L_MAIL
      },
      {
          "Newsgroups:", L_NEWS
      },
      {
          "Path:", L_NEWS
      },
      {
          "Organization:", L_NEWS
      },
      {
          NULL, 0
      }
  };
  
  #define NNAMES ((sizeof(names)/sizeof(struct names)) - 1)
  
  /*
   * Result String List (RSL)
   * 
   * The file(1) command prints its output.  Instead, we store the various
   * "printed" strings in a list (allocating memory as we go) and concatenate
   * them at the end when we finally know how much space they'll need.
   */
  
  typedef struct magic_rsl_s {
      char *str;        /* string, possibly a fragment */
      struct magic_rsl_s *next;    /* pointer to next fragment */
  } magic_rsl;
  
  /*
   * Apache module configuration structures
   */
  
  /* per-server info */
  typedef struct {
      char *magicfile;    /* where magic be found */
      struct magic *magic,    /* head of magic config list */
          *last;
  } magic_server_config_rec;
  
  /* per-request info */
  typedef struct {
      magic_rsl *head,    /* result string list */
          *tail;
      unsigned suf_recursion;    /* recursion depth in suffix check */
  } magic_req_rec;
  
  /*
   * configuration functions - called by Apache API routines
   */
  
  module mime_magic_module;
  extern module mime_module;
  
  static void *
  create_magic_server_config(pool * p, server_rec *d)
  {
      /* allocate the config - use pcalloc because it needs to be zeroed */
      return pcalloc(p, sizeof(magic_server_config_rec));
  }
  
  static void *
  merge_magic_server_config(pool * p, void *basev, void *addv)
  {
      magic_server_config_rec *base = (magic_server_config_rec *) basev;
      magic_server_config_rec *add = (magic_server_config_rec *) addv;
      magic_server_config_rec *new = (magic_server_config_rec *)
          palloc(p, sizeof(magic_server_config_rec));
  
      new->magicfile = add->magicfile ? add->magicfile : base->magicfile;
      if (add->magic && add->last) {
          new->magic = add->magic;
          new->last = add->last;
      }
      else if (base->magic && base->last) {
          new->magic = base->magic;
          new->last = base->last;
      }
      else {
          new->magic = new->last = NULL;
      }
      return new;
  }
  
  static const char *
  set_magicfile(cmd_parms * cmd, char *d, char *arg)
  {
      magic_server_config_rec *conf = (magic_server_config_rec *)
          get_module_config(cmd->server->module_config,
          &mime_magic_module);
  
      if (!conf) {
          return MODNAME ": server structure not allocated";
      }
      conf->magicfile = arg;
      return NULL;
  }
  
  /*
   * configuration file commands - exported to Apache API
   */
  
  static command_rec mime_magic_cmds[] = {
      {"MimeMagicFile", set_magicfile, NULL, RSRC_CONF, TAKE1,
      "Path to MIME Magic file (in file(1) format, default " MAGIC ")"},
      {NULL}
  };
  
  /*
   * RSL (result string list) processing routines
   * 
   * These collect strings that would have been printed in fragments by file(1)
   * into a list of magic_rsl structures with the strings. When complete,
   * they're concatenated together to become the MIME content and encoding
   * types.
   * 
   * return value conventions for these functions: functions which return int:
   * failure = -1, other = result functions which return pointers: failure = 0,
   * other = result
   */
  
  /* allocate a per-request structure and put it in the request record */
  static magic_req_rec *
  magic_set_config(request_rec * r)
  {
      magic_req_rec *req_dat = (magic_req_rec *)
          palloc(r->pool, sizeof(magic_req_rec));
  
      if (!req_dat) {
          fprintf(stderr, MODNAME ": memory allocation "
              "failure in magic_set_config()\n");
          fflush(stderr);
          return NULL;
      }
      req_dat->head = req_dat->tail = (magic_rsl *) NULL;
      set_module_config(r->request_config, &mime_magic_module, req_dat);
      return req_dat;
  }
  
  /* add a string to the result string list for this request */
  /* it is the responsibility of the caller to allocate "str" */
  static int 
  magic_rsl_add(request_rec * r, char *str)
  {
      magic_req_rec *req_dat = (magic_req_rec *)
          get_module_config(r->request_config, &mime_magic_module);
      magic_rsl *rsl;
  
      /* make sure we have a list to put it in */
      if (!req_dat) {
          fprintf(stderr, MODNAME ": request config "
              "should not be NULL\n");
          fflush(stderr);
          if (!(req_dat = magic_set_config(r))) {
              /* failure */
              return -1;
          }
      }
  
      /* allocate the list entry */
      if (!(rsl = (magic_rsl *) palloc(r->pool, sizeof(magic_rsl)))) {
          log_printf(r->server, MODNAME ": "
                 "memory allocation failure in magic_rsl_add()\n");
          fflush(stderr);
          /* failure */
          return -1;
      }
  
      /* fill it */
      rsl->str = str;
      rsl->next = (magic_rsl *) NULL;
  
      /* append to the list */
      if (req_dat->head && req_dat->tail) {
          req_dat->tail->next = rsl;
          req_dat->tail = rsl;
      }
      else {
          req_dat->head = req_dat->tail = rsl;
      }
  
      /* success */
      return 0;
  }
  
  /* RSL hook for puts-type functions */
  static int 
  magic_rsl_puts(request_rec * r, char *str)
  {
      return magic_rsl_add(r, str);
  }
  
  /* RSL hook for printf-type functions */
  static int 
  magic_rsl_printf(request_rec * r, char *str,...)
  {
      va_list ap;
  
      /* make sure this is long enough since most OS's have no vsnprintf() */
      char buf[MAXMIMESTRING];
  
      /* assemble the string into the buffer */
      va_start(ap, str);
      vsprintf(buf, str, ap);
      va_end(ap);
  
      /* add the buffer to the list */
      return magic_rsl_add(r, strdup(buf));
  }
  
  /* RSL hook for putchar-type functions */
  static int 
  magic_rsl_putchar(request_rec * r, char c)
  {
      char str[2];
  
      /* high overhead for 1 char - just hope they don't do this much */
      str[0] = c;
      str[1] = '\0';
      return magic_rsl_add(r, str);
  }
  
  /* allocate and copy a contiguous string from a result string list */
  static char *
  rsl_strdup(request_rec * r, int start_frag, int start_pos, int len)
  {
      char *result;        /* return value */
      int cur_frag,        /* current fragment number/counter */
          cur_pos,        /* current position within fragment */
          res_pos;        /* position in result string */
      magic_rsl *frag;    /* list-traversal pointer */
  
      magic_req_rec *req_dat = (magic_req_rec *)
          get_module_config(r->request_config, &mime_magic_module);
  
      /* allocate the result string */
      if (!(result = (char *) palloc(r->pool, len + 1))) {
          log_printf(r->server, MODNAME ": "
                 "memory allocation failure in rsl_strdup()\n");
          return NULL;
      }
  
      /* loop through and collect the string */
      res_pos = 0;
      for (frag = req_dat->head, cur_frag = 0;
           frag->next;
           frag = frag->next, cur_frag++) {
          /* loop to the first fragment */
          if (cur_frag < start_frag)
              continue;
  
          /* loop through and collect chars */
          for (cur_pos = (cur_frag == start_frag) ? start_pos : 0;
               frag->str[cur_pos];
               cur_pos++) {
              if (cur_frag >= start_frag
                  && cur_pos >= start_pos
                  && res_pos <= len) {
                  result[res_pos++] = frag->str[cur_pos];
                  if (res_pos > len) {
                      break;
                  }
              }
          }
      }
  
      /* clean up and return */
      result[res_pos] = 0;
  #if DEBUG
      fprintf(stderr, MODNAME ": rsl_strdup() %d chars: %s\n",
          res_pos - 1, result);
      fflush(stderr);
      fflush(stderr);
  #endif
      return result;
  }
  
  /* states for the state-machine algorithm in magic_rsl_to_request() */
  typedef enum {
      rsl_leading_space, rsl_type, rsl_subtype, rsl_separator, rsl_encoding
  } rsl_states;
  
  /* process the RSL and set the MIME info in the request record */
  static int 
  magic_rsl_to_request(request_rec * r)
  {
      int cur_frag,        /* current fragment number/counter */
          cur_pos,        /* current position within fragment */
          type_frag,        /* content type starting point: fragment */
          type_pos,        /* content type starting point: position */
          type_len,        /* content type length */
          encoding_frag,    /* content encoding starting point: fragment */
          encoding_pos,    /* content encoding starting point: position */
          encoding_len;    /* content encoding length */
  
      magic_rsl *frag;    /* list-traversal pointer */
      rsl_states state;
  
      magic_req_rec *req_dat = (magic_req_rec *)
          get_module_config(r->request_config, &mime_magic_module);
  
      /* check if we have a result */
      if (!req_dat || !req_dat->head) {
          /* empty - no match, we defer to other Apache modules */
          return DECLINED;
      }
  
      /* start searching for the type and encoding */
      state = rsl_leading_space;
      type_frag = type_pos = type_len = 0;
      encoding_frag = encoding_pos = encoding_len = 0;
      for (frag = req_dat->head, cur_frag = 0;
           frag && frag->next;
           frag = frag->next, cur_frag++) {
          /* loop through the characters in the fragment */
          for (cur_pos = 0; frag->str[cur_pos]; cur_pos++) {
              if (isspace(frag->str[cur_pos])) {
                  /* process whitespace actions for each state */
                  if (state == rsl_leading_space) {
                      /* eat whitespace in this state */
                      continue;
                  }
                  else if (state == rsl_type) {
                      /* whitespace: type has no slash! */
                      return DECLINED;
                  }
                  else if (state == rsl_subtype) {
                      /* whitespace: end of MIME type */
                      state++;
                      continue;
                  }
                  else if (state == rsl_separator) {
                      /* eat whitespace in this state */
                      continue;
                  }
                  else if (state == rsl_encoding) {
                      /* whitespace: end of MIME encoding */
                      /* we're done */
                      frag = req_dat->tail;
                      break;
                  }
                  else {
                      /* should not be possible */
                      /* abandon malfunctioning module */
                      log_printf(r->server,
                             "%s: bad state %d (ws)\n",
                             MODNAME);
                      return DECLINED;
                  }
                  /* NOTREACHED */
              }
              else if (state == rsl_type &&
                   frag->str[cur_pos] == '/') {
                  /* copy the char and go to rsl_subtype state */
                  type_len++;
                  state++;
              }
              else {
                  /* process non-space actions for each state */
                  if (state == rsl_leading_space) {
                      /* non-space: begin MIME type */
                      state++;
                      type_frag = cur_frag;
                      type_pos = cur_pos;
                      type_len = 1;
                      continue;
                  }
                  else if (state == rsl_type ||
                       state == rsl_subtype) {
                      /* non-space: adds to type */
                      type_len++;
                      continue;
                  }
                  else if (state == rsl_separator) {
                      /* non-space: begin MIME encoding */
                      state++;
                      encoding_frag = cur_frag;
                      encoding_pos = cur_pos;
                      encoding_len = 1;
                      continue;
                  }
                  else if (state == rsl_encoding) {
                      /* non-space: adds to encoding */
                      encoding_len++;
                      continue;
                  }
                  else {
                      /* should not be possible */
                      /* abandon malfunctioning module */
                      log_printf(r->server,
                             "%s: bad state %d (ns)\n",
                             MODNAME);
                      return DECLINED;
                  }
                  /* NOTREACHED */
              }
              /* NOTREACHED */
          }
      }
  
      /* if we ended prior to state rsl_subtype, we had incomplete info */
      if (state != rsl_subtype && state != rsl_separator &&
          state != rsl_encoding) {
          /* defer to other modules */
          return DECLINED;
      }
  
      /* save the info in the request record */
      if (state == rsl_subtype || state == rsl_encoding ||
          state == rsl_encoding) {
          r->content_type = rsl_strdup(r, type_frag, type_pos, type_len);
      }
      if (state == rsl_encoding) {
          r->content_encoding = rsl_strdup(r, encoding_frag,
                           encoding_pos, encoding_len);
      }
  
      /* detect memory allocation errors */
      if (!r->content_type ||
          (state == rsl_encoding && !r->content_encoding)) {
          return 500;
      }
  
      /* success! */
      return OK;
  }
  
  /*
   * magic_process - process input file r        Apache API request record
   * (formerly caled "process" in file command, prefix added for clarity) Opens
   * the file and reads a fixed-size buffer to begin processing the contents.
   */
  static void
  magic_process(request_rec * r)
  {
      int fd = 0;
      unsigned char buf[HOWMANY + 1];    /* one extra for terminating '\0' */
      struct utimbuf utbuf;
      struct stat sb;
      int nbytes = 0;        /* number of bytes read from a datafile */
  
      /*
       * first try judging the file based on its filesystem status
       */
      if (fsmagic(r, r->filename, &sb) != 0) {
          magic_rsl_putchar(r, '\n');
          return;
      }
  
      if ((fd = open(r->filename, O_RDONLY)) < 0) {
          /* We can't open it, but we were able to stat it. */
          /*
           * if (sb.st_mode & 0002) magic_rsl_puts(r,"writeable, "); if
           * (sb.st_mode & 0111) magic_rsl_puts(r,"executable, ");
           */
          log_printf(r->server, "can't read `%s' (%s).\n",
                 r->filename, strerror(errno));
          return;
      }
  
      /*
       * try looking at the first HOWMANY bytes
       */
      if ((nbytes = read(fd, (char *) buf, HOWMANY)) == -1) {
          log_printf(r->server, "read failed (%s).\n", strerror(errno));
          /* NOTREACHED */
      }
  
      if (nbytes == 0)
          magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
      else {
          buf[nbytes++] = '\0';    /* null-terminate it */
          tryit(r, buf, nbytes);
      }
  
      /*
       * Try to restore access, modification times if read it.
       */
      utbuf.actime = sb.st_atime;
      utbuf.modtime = sb.st_mtime;
      (void) utime(r->filename, &utbuf);    /* don't care if loses */
      (void) close(fd);
      (void) magic_rsl_putchar(r, '\n');
  }
  
  
  static void
  tryit(request_rec * r, unsigned char *buf, int nb)
  {
      /*
       * Try compression stuff
       */
      if (zmagic(r, buf, nb) == 1)
          return;
  
      /*
       * try tests in /etc/magic (or surrogate magic file)
       */
      if (softmagic(r, buf, nb) == 1)
          return;
  
      /*
       * try known keywords, check for ascii-ness too.
       */
      if (ascmagic(r, buf, nb) == 1)
          return;
  
      /*
       * abandon hope, all ye who remain here
       */
      magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
  }
  
  #define    EATAB {while (isascii((unsigned char) *l) && \
                isspace((unsigned char) *l))  ++l;}
  
  /*
   * apprentice - load configuration from the magic file r
   *  API request record
   */
  static int 
  apprentice(server_rec * s, pool * p)
  {
      FILE *f;
      char line[BUFSIZ + 1];
      int errs = 0;
      int lineno;
  #if DEBUG
      int rule;
      struct magic *m, *prevm;
  #endif
      char *fname;
  
      magic_server_config_rec *conf = (magic_server_config_rec *)
          get_module_config(s->module_config, &mime_magic_module);
  
      if (!conf->magicfile) {
          conf->magicfile = pstrdup(p, MAGIC);
      }
      fname = server_root_relative(p, conf->magicfile);
      f = pfopen(p, fname, "r");
      if (f == NULL) {
          (void) fprintf(stderr, "%s: can't read magic file %s\n",
                     MODNAME, fname);
          fflush(stderr);
          perror(MODNAME);
          fflush(stderr);
          return -1;
      }
  
      /* set up the magic list (empty) */
      conf->magic = conf->last = NULL;
  
      /* parse it */
      for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) {
          int ws_offset;
  
          /* delete newline */
          if (line[0]) {
              line[strlen(line) - 1] = '\0';
          }
  
          /* skip leading whitespace */
          ws_offset = 0;
          while (line[ws_offset] && isspace(line[ws_offset])) {
              ws_offset++;
          }
  
          /* skip blank lines */
          if (line[ws_offset] == 0) {
              continue;
          }
  
          /* comment, do not parse */
          if (line[ws_offset] == '#')
              continue;
  
  #if DEBUG
          /* if we get here, we're going to use it so count it */
          rule++;
  #endif
  
          /* parse it */
          if (parse(s, p, line + ws_offset, lineno) != 0)
              ++errs;
      }
  
      (void) pfclose(p, f);
  
  #if DEBUG
      fprintf(stderr,
          "%s: apprentice conf=%x file=%s m=%s m->next=%s last=%s\n",
          MODNAME, conf,
          conf->magicfile ? conf->magicfile : "NULL",
          conf->magic ? "set" : "NULL",
          (conf->magic && conf->magic->next) ? "set" : "NULL",
          conf->last ? "set" : "NULL");
      fprintf(stderr, "%s: apprentice read %d lines, %d rules, %d errors\n",
          MODNAME, lineno, rule, errs);
      fflush(stderr);
  #endif
  
  #if DEBUG
      prevm = 0;
      fprintf(stderr, "%s: apprentice test\n", MODNAME);
      for (m = conf->magic; m; m = m->next) {
          if (isprint((((unsigned long) m) >> 24) & 255) &&
              isprint((((unsigned long) m) >> 16) & 255) &&
              isprint((((unsigned long) m) >> 8) & 255) &&
              isprint(((unsigned long) m) & 255)) {
              fprintf(stderr, "%s: apprentice: "
                  "POINTER CLOBBERED! "
                  "m=\"%c%c%c%c\" line=%d\n", MODNAME,
                  (((unsigned long) m) >> 24) & 255,
                  (((unsigned long) m) >> 16) & 255,
                  (((unsigned long) m) >> 8) & 255,
                  ((unsigned long) m) & 255,
                  prevm ? prevm->lineno : -1);
              break;
          }
          prevm = m;
      }
      fflush(stderr);
  #endif
  
      return (errs ? -1 : 0);
  }
  
  /*
   * extend the sign bit if the comparison is to be signed
   */
  static unsigned long
  signextend(server_rec * s, struct magic * m, unsigned long v)
  {
      if (!(m->flag & UNSIGNED))
          switch (m->type) {
              /*
               * Do not remove the casts below.  They are vital.
               * When later compared with the data, the sign
               * extension must have happened.
               */
              case BYTE:
              v = (char) v;
              break;
          case SHORT:
          case BESHORT:
          case LESHORT:
              v = (short) v;
              break;
          case DATE:
          case BEDATE:
          case LEDATE:
          case LONG:
          case BELONG:
          case LELONG:
              v = (long) v;
              break;
          case STRING:
              break;
          default:
              fprintf(stderr, "%s: can't happen: m->type=%d\n",
                  MODNAME, m->type);
              fflush(stderr);
              fflush(stderr);
              return -1;
          }
      return v;
  }
  
  /*
   * parse one line from magic file, put into magic[index++] if valid
   */
  static int
  parse(server_rec * serv, pool * p, char *l, int lineno)
  {
      int i = 0;
      struct magic *m;
      char *t, *s;
      magic_server_config_rec *conf = (magic_server_config_rec *)
          get_module_config(serv->module_config, &mime_magic_module);
  
      /* allocate magic structure entry */
      if ((m = (struct magic *) pcalloc(p, sizeof(struct magic))) == NULL) {
          (void) fprintf(stderr, "%s: Out of memory.\n", MODNAME);
          fflush(stderr);
          return -1;
      }
  
      /* append to linked list */
      m->next = NULL;
      if (!conf->magic || !conf->last) {
          conf->magic = conf->last = m;
      }
      else {
          conf->last->next = m;
          conf->last = m;
      }
  
      /* set values in magic structure */
      m->flag = 0;
      m->cont_level = 0;
      m->lineno = lineno;
  
      while (*l == '>') {
          ++l;        /* step over */
          m->cont_level++;
      }
  
      if (m->cont_level != 0 && *l == '(') {
          ++l;        /* step over */
          m->flag |= INDIR;
      }
  
      /* get offset, then skip over it */
      m->offset = (int) strtol(l, &t, 0);
      if (l == t) {
          fprintf(stderr, "%s: offset %s invalid", MODNAME, l);
          fflush(stderr);
      }
      l = t;
  
      if (m->flag & INDIR) {
          m->in.type = LONG;
          m->in.offset = 0;
          /*
           * read [.lbs][+-]nnnnn)
           */
          if (*l == '.') {
              switch (*++l) {
              case 'l':
                  m->in.type = LONG;
                  break;
              case 's':
                  m->in.type = SHORT;
                  break;
              case 'b':
                  m->in.type = BYTE;
                  break;
              default:
                  fprintf(stderr, "%s: indirect offset type %c invalid", MODNAME, *l);
                  fflush(stderr);
                  break;
              }
              l++;
          }
          s = l;
          if (*l == '+' || *l == '-')
              l++;
          if (isdigit((unsigned char) *l)) {
              m->in.offset = strtol(l, &t, 0);
              if (*s == '-')
                  m->in.offset = -m->in.offset;
          }
          else
              t = l;
          if (*t++ != ')') {
              fprintf(stderr, "%s: missing ')' in indirect offset",
                  MODNAME);
              fflush(stderr);
          }
          l = t;
      }
  
  
      while (isascii((unsigned char) *l) && isdigit((unsigned char) *l))
          ++l;
      EATAB;
  
  #define NBYTE        4
  #define NSHORT        5
  #define NLONG        4
  #define NSTRING     6
  #define NDATE        4
  #define NBESHORT    7
  #define NBELONG        6
  #define NBEDATE        6
  #define NLESHORT    7
  #define NLELONG        6
  #define NLEDATE        6
  
      if (*l == 'u') {
          ++l;
          m->flag |= UNSIGNED;
      }
  
      /* get type, skip it */
      if (strncmp(l, "byte", NBYTE) == 0) {
          m->type = BYTE;
          l += NBYTE;
      }
      else if (strncmp(l, "short", NSHORT) == 0) {
          m->type = SHORT;
          l += NSHORT;
      }
      else if (strncmp(l, "long", NLONG) == 0) {
          m->type = LONG;
          l += NLONG;
      }
      else if (strncmp(l, "string", NSTRING) == 0) {
          m->type = STRING;
          l += NSTRING;
      }
      else if (strncmp(l, "date", NDATE) == 0) {
          m->type = DATE;
          l += NDATE;
      }
      else if (strncmp(l, "beshort", NBESHORT) == 0) {
          m->type = BESHORT;
          l += NBESHORT;
      }
      else if (strncmp(l, "belong", NBELONG) == 0) {
          m->type = BELONG;
          l += NBELONG;
      }
      else if (strncmp(l, "bedate", NBEDATE) == 0) {
          m->type = BEDATE;
          l += NBEDATE;
      }
      else if (strncmp(l, "leshort", NLESHORT) == 0) {
          m->type = LESHORT;
          l += NLESHORT;
      }
      else if (strncmp(l, "lelong", NLELONG) == 0) {
          m->type = LELONG;
          l += NLELONG;
      }
      else if (strncmp(l, "ledate", NLEDATE) == 0) {
          m->type = LEDATE;
          l += NLEDATE;
      }
      else {
          fprintf(stderr, "%s: type %s invalid", MODNAME, l);
          fflush(stderr);
          return -1;
      }
      /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
      if (*l == '&') {
          ++l;
          m->mask = signextend(serv, m, strtol(l, &l, 0));
      }
      else
          m->mask = ~0L;
      EATAB;
  
      switch (*l) {
      case '>':
      case '<':
          /* Old-style anding: "0 byte &0x80 dynamically linked" */
      case '&':
      case '^':
      case '=':
          m->reln = *l;
          ++l;
          break;
      case '!':
          if (m->type != STRING) {
              m->reln = *l;
              ++l;
              break;
          }
          /* FALL THROUGH */
      default:
          if (*l == 'x' && isascii((unsigned char) l[1]) &&
              isspace((unsigned char) l[1])) {
              m->reln = *l;
              ++l;
              goto GetDesc;    /* Bill The Cat */
          }
          m->reln = '=';
          break;
      }
      EATAB;
  
      if (getvalue(serv, m, &l))
          return -1;
      /*
       * now get last part - the description
       */
  GetDesc:
      EATAB;
      if (l[0] == '\b') {
          ++l;
          m->nospflag = 1;
      }
      else if ((l[0] == '\\') && (l[1] == 'b')) {
          ++l;
          ++l;
          m->nospflag = 1;
      }
      else
          m->nospflag = 0;
      while ((m->desc[i++] = *l++) != '\0' && i < MAXDESC)
           /* NULLBODY */ ;
  
  #if DEBUG
      fprintf(stderr, "%s: parse line=%d m=%x next=%x cont=%d desc=%s\n",
          MODNAME, lineno, m, m->next, m->cont_level,
          m->desc ? m->desc : "NULL");
      fflush(stderr);
  #endif                /* DEBUG */
  
      return 0;
  }
  
  /*
   * Read a numeric value from a pointer, into the value union of a magic
   * pointer, according to the magic type.  Update the string pointer to point
   * just after the number read.  Return 0 for success, non-zero for failure.
   */
  static int
  getvalue(server_rec * s, struct magic * m, char **p)
  {
      int slen;
  
      if (m->type == STRING) {
          *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
          m->vallen = slen;
      }
      else if (m->reln != 'x')
          m->value.l = signextend(s, m, strtol(*p, p, 0));
      return 0;
  }
  
  /*
   * Convert a string containing C character escapes.  Stop at an unescaped
   * space or tab. Copy the converted version to "p", returning its length in
   * *slen. Return updated scan pointer as function result.
   */
  static char *
  getstr(s, p, plen, slen)
      register char *s;
      register char *p;
      int plen, *slen;
  {
      char *origs = s, *origp = p;
      char *pmax = p + plen - 1;
      register int c;
      register int val;
  
      while ((c = *s++) != '\0') {
          if (isspace((unsigned char) c))
              break;
          if (p >= pmax) {
              fprintf(stderr, "String too long: %s\n", origs);
              fflush(stderr);
              break;
          }
          if (c == '\\') {
              switch (c = *s++) {
  
              case '\0':
                  goto out;
  
              default:
                  *p++ = (char) c;
                  break;
  
              case 'n':
                  *p++ = '\n';
                  break;
  
              case 'r':
                  *p++ = '\r';
                  break;
  
              case 'b':
                  *p++ = '\b';
                  break;
  
              case 't':
                  *p++ = '\t';
                  break;
  
              case 'f':
                  *p++ = '\f';
                  break;
  
              case 'v':
                  *p++ = '\v';
                  break;
  
                  /* \ and up to 3 octal digits */
              case '0':
              case '1':
              case '2':
              case '3':
              case '4':
              case '5':
              case '6':
              case '7':
                  val = c - '0';
                  c = *s++;    /* try for 2 */
                  if (c >= '0' && c <= '7') {
                      val = (val << 3) | (c - '0');
                      c = *s++;    /* try for 3 */
                      if (c >= '0' && c <= '7')
                          val = (val << 3) | (c - '0');
                      else
                          --s;
                  }
                  else
                      --s;
                  *p++ = (char) val;
                  break;
  
                  /* \x and up to 3 hex digits */
              case 'x':
                  val = 'x';    /* Default if no digits */
                  c = hextoint(*s++);    /* Get next char */
                  if (c >= 0) {
                      val = c;
                      c = hextoint(*s++);
                      if (c >= 0) {
                          val = (val << 4) + c;
                          c = hextoint(*s++);
                          if (c >= 0) {
                              val = (val << 4) + c;
                          }
                          else
                              --s;
                      }
                      else
                          --s;
                  }
                  else
                      --s;
                  *p++ = (char) val;
                  break;
              }
          }
          else
              *p++ = (char) c;
      }
  out:
      *p = '\0';
      *slen = p - origp;
      return s;
  }
  
  
  /* Single hex char to int; -1 if not a hex char. */
  static int
  hextoint(c)
      int c;
  {
      if (!isascii((unsigned char) c))
          return -1;
      if (isdigit((unsigned char) c))
          return c - '0';
      if ((c >= 'a') && (c <= 'f'))
          return c + 10 - 'a';
      if ((c >= 'A') && (c <= 'F'))
          return c + 10 - 'A';
      return -1;
  }
  
  
  static int
  fsmagic(request_rec * r, const char *fn, struct stat * sb)
  {
      int ret = 0;
  
      /*
       * Fstat is cheaper but fails for files you don't have read perms on.
       * On 4.2BSD and similar systems, use lstat() to identify symlinks.
       */
      ret = stat(fn, sb);    /* don't merge into if; see "ret =" above */
  
      if (ret) {
          log_printf(r->server,
          /* No \n, caller will provide. */
                 "can't stat `%s' (%s).", fn, strerror(errno));
          return 1;
      }
  
      /*
       * if (sb->st_mode & S_ISUID) magic_rsl_puts(r,"setuid "); if
       * (sb->st_mode & S_ISGID) magic_rsl_puts(r,"setgid "); if
       * (sb->st_mode & S_ISVTX) magic_rsl_puts(r,"sticky ");
       */
  
      switch (sb->st_mode & S_IFMT) {
      case S_IFDIR:
          magic_rsl_puts(r, DIR_MAGIC_TYPE);
          return 1;
      case S_IFCHR:
          /*
           * (void) magic_rsl_printf(r,"character special (%d/%d)",
           * major(sb->st_rdev), minor(sb->st_rdev));
           */
          (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
          return 1;
      case S_IFBLK:
          /*
           * (void) magic_rsl_printf(r,"block special (%d/%d)",
           * major(sb->st_rdev), minor(sb->st_rdev));
           */
          (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
          return 1;
          /* TODO add code to handle V7 MUX and Blit MUX files */
  #ifdef    S_IFIFO
      case S_IFIFO:
          /*
           * magic_rsl_puts(r,"fifo (named pipe)");
           */
          (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
          return 1;
  #endif
  #ifdef    S_IFLNK
      case S_IFLNK:
          {
              char buf[BUFSIZ + 4];
              register int nch;
              struct stat tstatbuf;
  
              if ((nch = readlink(fn, buf, BUFSIZ - 1)) <= 0) {
                  /*
                   * magic_rsl_printf(r, "unreadable symlink
                   * (%s).", strerror(errno));
                   */
                  magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
                  return 1;
              }
              buf[nch] = '\0';    /* readlink(2) forgets this */
  
              /* If broken symlink, say so and quit early. */
              if (*buf == '/') {
                  if (stat(buf, &tstatbuf) < 0) {
                      /*
                       * magic_rsl_printf(r, "broken
                       * symbolic link to %s", buf);
                       */
                      magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
                      return 1;
                  }
              }
              else {
                  char *tmp;
                  char buf2[BUFSIZ + BUFSIZ + 4];
  
                  if ((tmp = strrchr(fn, '/')) == NULL) {
                      tmp = buf;    /* in current directory
                               * anyway */
                  }
                  else {
                      strcpy(buf2, fn);    /* take directory part */
                      buf2[tmp - fn + 1] = '\0';
                      strcat(buf2, buf);    /* plus (relative)
                                   * symlink */
                      tmp = buf2;
                  }
                  if (stat(tmp, &tstatbuf) < 0) {
                      /*
                       * magic_rsl_printf(r, "broken
                       * symbolic link to %s", buf);
                       */
                      magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
                      return 1;
                  }
              }
  
              /* At MIME-typing time we want to follow symlinks */
              /* So just handle it. */
              magic_process(r);
              return 1;
          }
          return 1;
  #endif
  #ifdef    S_IFSOCK
  #ifndef __COHERENT__
      case S_IFSOCK:
          magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
          return 1;
  #endif
  #endif
      case S_IFREG:
          break;
      default:
          log_printf(r->server, "%s: invalid mode 0%o.\n", MODNAME,
                 sb->st_mode);
          /* NOTREACHED */
      }
  
      /*
       * regular file, check next possibility
       */
      if (sb->st_size == 0) {
          magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
          return 1;
      }
      return 0;
  }
  
  /*
   * softmagic - lookup one file in database (already read from /etc/magic by
   * apprentice.c). Passed the name and FILE * of one file to be typed.
   */
  /* ARGSUSED1 *//* nbytes passed for regularity, maybe need later */
  static int
  softmagic(request_rec * r, unsigned char *buf, int nbytes)
  {
      if (match(r, buf, nbytes))
          return 1;
  
      return 0;
  }
  
  /*
   * Go through the whole list, stopping if you find a match.  Process all the
   * continuations of that match before returning.
   * 
   * We support multi-level continuations:
   * 
   * At any time when processing a successful top-level match, there is a current
   * continuation level; it represents the level of the last successfully
   * matched continuation.
   * 
   * Continuations above that level are skipped as, if we see one, it means that
   * the continuation that controls them - i.e, the lower-level continuation
   * preceding them - failed to match.
   * 
   * Continuations below that level are processed as, if we see one, it means
   * we've finished processing or skipping higher-level continuations under the
   * control of a successful or unsuccessful lower-level continuation, and are
   * now seeing the next lower-level continuation and should process it.  The
   * current continuation level reverts to the level of the one we're seeing.
   * 
   * Continuations at the current level are processed as, if we see one, there's
   * no lower-level continuation that may have failed.
   * 
   * If a continuation matches, we bump the current continuation level so that
   * higher-level continuations are processed.
   */
  static int
  match(request_rec * r, unsigned char *s, int nbytes)
  {
  #if DEBUG
      int rule_counter;
  #endif
      int cont_level = 0;
      int need_separator = 0;
      union VALUETYPE p;
      magic_server_config_rec *conf = (magic_server_config_rec *)
          get_module_config(r->server->module_config, &mime_magic_module);
      struct magic *m;
  
  #if DEBUG
      fprintf(stderr,
          "%s: match conf=%x file=%s m=%s m->next=%s last=%s\n",
          MODNAME, conf,
          conf->magicfile ? conf->magicfile : "NULL",
          conf->magic ? "set" : "NULL",
          (conf->magic && conf->magic->next) ? "set" : "NULL",
          conf->last ? "set" : "NULL");
      fflush(stderr);
  #endif
  
  #if DEBUG
      for (m = conf->magic; m; m = m->next) {
          if (isprint((((unsigned long) m) >> 24) & 255) &&
              isprint((((unsigned long) m) >> 16) & 255) &&
              isprint((((unsigned long) m) >> 8) & 255) &&
              isprint(((unsigned long) m) & 255)) {
              fprintf(stderr, "%s: match: POINTER CLOBBERED! "
                  "m=\"%c%c%c%c\"\n", MODNAME,
                  (((unsigned long) m) >> 24) & 255,
                  (((unsigned long) m) >> 16) & 255,
                  (((unsigned long) m) >> 8) & 255,
                  ((unsigned long) m) & 255);
              fflush(stderr);
              break;
          }
      }
  #endif
  
      for (m = conf->magic; m; m = m->next) {
  #if DEBUG
          rule_counter++;
          fprintf(stderr, "%s: line=%d desc=%s\n", MODNAME,
              m->lineno, m->desc);
          fflush(stderr);
  #endif
  
          /* check if main entry matches */
          if (!mget(r, &p, s, m, nbytes) ||
              !mcheck(r, &p, m)) {
              struct magic *m_cont;
  
              /*
               * main entry didn't match, flush its continuations
               */
              if (!m->next || (m->next->cont_level == 0)) {
                  continue;
              }
  
              m_cont = m->next;
              while (m_cont && (m_cont->cont_level != 0)) {
  #if DEBUG
                  rule_counter++;
                  fprintf(stderr,
                      "%s: line=%d mc=%x mc->next=%x "
                      "cont=%d desc=%s\n",
                      MODNAME, m_cont->lineno, m_cont,
                      m_cont->next, m_cont->cont_level,
                        m_cont->desc ? m_cont->desc : "NULL");
                  fflush(stderr);
  #endif
                  /*
                   * this trick allows us to keep *m in sync
                   * when the continue advances the pointer
                   */
                  m = m_cont;
                  m_cont = m_cont->next;
              }
              continue;
          }
  
          /* if we get here, the main entry rule was a match */
          /* this will be the last run through the loop */
  #if DEBUG
          fprintf(stderr, "%s: rule matched, line=%d type=%d %s\n",
              MODNAME, m->lineno, m->type,
              (m->type == STRING) ? m->value.s : "");
          fflush(stderr);
  #endif
  
          /* print the match */
          mprint(r, &p, m);
  
          /*
           * If we printed something, we'll need to print a blank
           * before we print something else.
           */
          if (m->desc[0])
              need_separator = 1;
          /* and any continuations that match */
          cont_level++;
          /*
           * while (m && m->next && m->next->cont_level != 0 && ( m =
           * m->next ))
           */
          m = m->next;
          while (m && (m->cont_level != 0)) {
  #if DEBUG
              fprintf(stderr,
                  "%s: match line=%d cont=%d type=%d %s\n",
                  MODNAME, m->lineno, m->cont_level, m->type,
                  (m->type == STRING) ? m->value.s : "");
              fflush(stderr);
  #endif
              if (cont_level >= m->cont_level) {
                  if (cont_level > m->cont_level) {
                      /*
                       * We're at the end of the level
                       * "cont_level" continuations.
                       */
                      cont_level = m->cont_level;
                  }
                  if (mget(r, &p, s, m, nbytes) &&
                      mcheck(r, &p, m)) {
                      /*
                       * This continuation matched. Print
                       * its message, with a blank before
                       * it if the previous item printed
                       * and this item isn't empty.
                       */
                      /* space if previous printed */
                      if (need_separator
                          && (m->nospflag == 0)
                          && (m->desc[0] != '\0')
                          ) {
                          (void) magic_rsl_putchar(r, ' ');
                          need_separator = 0;
                      }
                      mprint(r, &p, m);
                      if (m->desc[0])
                          need_separator = 1;
  
                      /*
                       * If we see any continuations at a
                       * higher level, process them.
                       */
                      cont_level++;
                  }
              }
  
              /* move to next continuation record */
              m = m->next;
          }
  #if DEBUG
          fprintf(stderr, "%s: matched after %d rules\n",
              MODNAME, rule_counter);
          fflush(stderr);
  #endif
          return 1;    /* all through */
      }
  #if DEBUG
      fprintf(stderr, "%s: failed after %d rules\n", MODNAME, rule_counter);
      fflush(stderr);
  #endif
      return 0;        /* no match at all */
  }
  
  static void
  mprint(request_rec * r, union VALUETYPE * p, struct magic * m)
  {
      char *pp, *rt;
      unsigned long v;
  
  
      switch (m->type) {
      case BYTE:
          v = p->b;
          break;
  
      case SHORT:
      case BESHORT:
      case LESHORT:
          v = p->h;
          break;
  
      case LONG:
      case BELONG:
      case LELONG:
          v = p->l;
          break;
  
      case STRING:
          if (m->reln == '=') {
              (void) magic_rsl_printf(r, m->desc, m->value.s);
          }
          else {
              (void) magic_rsl_printf(r, m->desc, p->s);
          }
          return;
  
      case DATE:
      case BEDATE:
      case LEDATE:
          pp = ctime((time_t *) & p->l);
          if ((rt = strchr(pp, '\n')) != NULL)
              *rt = '\0';
          (void) magic_rsl_printf(r, m->desc, pp);
          return;
      default:
          log_printf(r->server, "%s: invalid m->type (%d) in mprint().\n",
                 MODNAME, m->type);
          return;
      }
  
      v = signextend(r->server, m, v) & m->mask;
      (void) magic_rsl_printf(r, m->desc, (unsigned long) v);
  }
  
  /*
   * Convert the byte order of the data we are looking at
   */
  static int
  mconvert(request_rec * r, union VALUETYPE * p, struct magic * m)
  {
      char *rt;
  
      switch (m->type) {
      case BYTE:
      case SHORT:
      case LONG:
      case DATE:
          return 1;
      case STRING:
          /* Null terminate and eat the return */
          p->s[sizeof(p->s) - 1] = '\0';
          if ((rt = strchr(p->s, '\n')) != NULL)
              *rt = '\0';
          return 1;
      case BESHORT:
          p->h = (short) ((p->hs[0] << 8) | (p->hs[1]));
          return 1;
      case BELONG:
      case BEDATE:
          p->l = (long)
              ((p->hl[0] << 24) | (p->hl[1] << 16) | (p->hl[2] << 8) | (p->hl[3]));
          return 1;
      case LESHORT:
          p->h = (short) ((p->hs[1] << 8) | (p->hs[0]));
          return 1;
      case LELONG:
      case LEDATE:
          p->l = (long)
              ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
          return 1;
      default:
          log_printf(r->server, "%s: invalid type %d in mconvert().\n",
                 MODNAME, m->type);
          return 0;
      }
  }
  
  
  static int
  mget(request_rec * r, union VALUETYPE * p, unsigned char *s, struct magic * m,
       int nbytes)
  {
      long offset = m->offset;
      if (offset + sizeof(union VALUETYPE) > nbytes)
          return 0;
  
  
      memcpy(p, s + offset, sizeof(union VALUETYPE));
  
      if (!mconvert(r, p, m))
          return 0;
  
      if (m->flag & INDIR) {
  
          switch (m->in.type) {
          case BYTE:
              offset = p->b + m->in.offset;
              break;
          case SHORT:
              offset = p->h + m->in.offset;
              break;
          case LONG:
              offset = p->l + m->in.offset;
              break;
          }
  
          if (offset + sizeof(union VALUETYPE) > nbytes)
              return 0;
  
          memcpy(p, s + offset, sizeof(union VALUETYPE));
  
          if (!mconvert(r, p, m))
              return 0;
      }
      return 1;
  }
  
  static int
  mcheck(request_rec * r, union VALUETYPE * p, struct magic * m)
  {
      register unsigned long l = m->value.l;
      register unsigned long v;
      int matched;
  
      if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
          fprintf(stderr, "BOINK");
          fflush(stderr);
          return 1;
      }
  
  
      switch (m->type) {
      case BYTE:
          v = p->b;
          break;
  
      case SHORT:
      case BESHORT:
      case LESHORT:
          v = p->h;
          break;
  
      case LONG:
      case BELONG:
      case LELONG:
      case DATE:
      case BEDATE:
      case LEDATE:
          v = p->l;
          break;
  
      case STRING:
          l = 0;
          /*
           * What we want here is: v = strncmp(m->value.s, p->s,
           * m->vallen); but ignoring any nulls.  bcmp doesn't give
           * -/+/0 and isn't universally available anyway.
           */
          v = 0;
          {
              register unsigned char *a = (unsigned char *) m->value.s;
              register unsigned char *b = (unsigned char *) p->s;
              register int len = m->vallen;
  
              while (--len >= 0)
                  if ((v = *b++ - *a++) != 0)
                      break;
          }
          break;
      default:
          log_printf(r->server, "%s: invalid type %d in mcheck().\n",
                 MODNAME, m->type);
          return 0;    /* NOTREACHED */
      }
  
      v = signextend(r->server, m, v) & m->mask;
  
      switch (m->reln) {
      case 'x':
  #if DEBUG
          (void) fprintf(stderr, "%lu == *any* = 1\n", v);
  #endif
          matched = 1;
          break;
  
      case '!':
          matched = v != l;
  #if DEBUG
          (void) fprintf(stderr, "%lu != %lu = %d\n",
                     v, l, matched);
  #endif
          break;
  
      case '=':
          matched = v == l;
  #if DEBUG
          (void) fprintf(stderr, "%lu == %lu = %d\n",
                     v, l, matched);
  #endif
          break;
  
      case '>':
          if (m->flag & UNSIGNED) {
              matched = v > l;
  #if DEBUG
              (void) fprintf(stderr, "%lu > %lu = %d\n",
                         v, l, matched);
  #endif
          }
          else {
              matched = (long) v > (long) l;
  #if DEBUG
              (void) fprintf(stderr, "%ld > %ld = %d\n",
                         v, l, matched);
  #endif
          }
          break;
  
      case '<':
          if (m->flag & UNSIGNED) {
              matched = v < l;
              if (DEBUG)
                  (void) fprintf(stderr, "%lu < %lu = %d\n",
                             v, l, matched);
          }
          else {
              matched = (long) v < (long) l;
  #if DEBUG
              (void) fprintf(stderr, "%ld < %ld = %d\n",
                         v, l, matched);
  #endif
          }
          break;
  
      case '&':
          matched = (v & l) == l;
  #if DEBUG
          (void) fprintf(stderr, "((%lx & %lx) == %lx) = %d\n",
                     v, l, l, matched);
  #endif
          break;
  
      case '^':
          matched = (v & l) != l;
  #if DEBUG
          (void) fprintf(stderr, "((%lx & %lx) != %lx) = %d\n",
                     v, l, l, matched);
  #endif
          break;
  
      default:
          matched = 0;
          log_printf(r->server, "%s: mcheck: can't happen: invalid relation %d.\n", MODNAME, m->reln);
          break;        /* NOTREACHED */
      }
  #if DEBUG
      fflush(stderr);
  #endif
  
      return matched;
  }
  
  /* an optimisation over plain strcmp() */
  #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  
  static int
  ascmagic(request_rec * r, unsigned char *buf, int nbytes)
  {
      int i, has_escapes = 0;
      unsigned char *s;
      char nbuf[HOWMANY + 1];    /* one extra for terminating '\0' */
      char *token;
      register struct names *p;
  
      /* these are easy, do them first */
  
      /*
       * for troff, look for . + letter + letter or .\"; this must be done
       * to disambiguate tar archives' ./file and other trash from real
       * troff input.
       */
      if (*buf == '.') {
          unsigned char *tp = buf + 1;
  
          while (isascii(*tp) && isspace(*tp))
              ++tp;    /* skip leading whitespace */
          if ((isascii(*tp) && (isalnum(*tp) || *tp == '\\') &&
          isascii(*(tp + 1)) && (isalnum(*(tp + 1)) || *tp == '"'))) {
              magic_rsl_puts(r, "application/x-troff");
              return 1;
          }
      }
      if ((*buf == 'c' || *buf == 'C') &&
          isascii(*(buf + 1)) && isspace(*(buf + 1))) {
          /* Fortran */
          magic_rsl_puts(r, "text/plain");
          return 1;
      }
  
      /* look for tokens from names.h - this is expensive! */
      /* make a copy of the buffer here because strtok() will destroy it */
      s = (unsigned char *) memcpy(nbuf, buf, nbytes);
      s[nbytes] = '\0';
      has_escapes = (memchr(s, '\033', nbytes) != NULL);
      while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
          s = NULL;    /* make strtok() keep on tokin' */
          for (p = names; p < names + NNAMES; p++) {
              if (STREQ(p->name, token)) {
                  magic_rsl_puts(r, types[p->type]);
                  if (has_escapes)
                      magic_rsl_puts(r, " (with escape sequences)");
                  return 1;
              }
          }
      }
  
      switch (is_tar(buf, nbytes)) {
      case 1:
          /* V7 tar archive */
          magic_rsl_puts(r, "application/x-tar");
          return 1;
      case 2:
          /* POSIX tar archive */
          magic_rsl_puts(r, "application/x-tar");
          return 1;
      }
  
      for (i = 0; i < nbytes; i++) {
          if (!isascii(*(buf + i)))
              return 0;    /* not all ascii */
      }
  
      /* all else fails, but it is ascii... */
      if (has_escapes) {
          /* text with escape sequences */
          /* we leave this open for further differentiation later */
          magic_rsl_puts(r, "text/plain");
      }
      else {
          /* plain text */
          magic_rsl_puts(r, "text/plain");
      }
      return 1;
  }
  
  
  /*
   * compress routines: zmagic() - returns 0 if not recognized, uncompresses
   * and prints information if recognized uncompress(s, method, old, n, newch)
   * - uncompress old into new, using method, return sizeof new
   */
  
  static struct {
      char *magic;
      int maglen;
      char *argv[3];
      int silent;
      char *encoding;
  }      compr[] = {
      {
          "\037\235", 2, {
              "uncompress", "-c", NULL
          }, 0, "x-compress"
      },
      {
          "\037\213", 2, {
              "gzip", "-dq", NULL
          }, 1, "x-gzip"
      },
      /*
       * XXX pcat does not work, cause I don't know how to make it read
       * stdin, so we use gzip
       */
      {
          "\037\036", 2, {
              "gzip", "-dq", NULL
          }, 0, "x-gzip"
      },
  };
  
  static int ncompr = sizeof(compr) / sizeof(compr[0]);
  
  static int
  zmagic(request_rec * r, unsigned char *buf, int nbytes)
  {
      unsigned char *newbuf;
      int newsize;
      int i;
  
      for (i = 0; i < ncompr; i++) {
          if (nbytes < compr[i].maglen)
              continue;
          if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
              break;
      }
  
      if (i == ncompr)
          return 0;
  
      if ((newsize = uncompress(r->server, i, buf, &newbuf, nbytes)) != 0) {
          tryit(r, newbuf, newsize);
          free(newbuf);
  
          /* set encoding type in the request record */
          r->content_encoding = pstrdup(r->pool, compr[i].encoding);
      }
      return 1;
  }
  
  
  static int
  uncompress(server_rec * s, int method, const unsigned char *old,
         unsigned char **newch, int n)
  {
      int fdin[2], fdout[2];
  
      if (pipe(fdin) == -1 || pipe(fdout) == -1) {
          log_printf(s, "%s: cannot create pipe (%s).\n",
                 MODNAME, strerror(errno));
          return -1;
      }
      switch (fork()) {
      case 0:        /* child */
          (void) close(0);
          (void) dup(fdin[0]);
          (void) close(fdin[0]);
          (void) close(fdin[1]);
  
          (void) close(1);
          (void) dup(fdout[1]);
          (void) close(fdout[0]);
          (void) close(fdout[1]);
          if (compr[method].silent)
              (void) close(2);
  
          execvp(compr[method].argv[0], compr[method].argv);
          log_printf(s, "%s: could not execute `%s' (%s).\n", MODNAME,
                 compr[method].argv[0], strerror(errno));
          return -1;
      case -1:
          log_printf(s, "%s: could not fork (%s).\n", MODNAME,
                 strerror(errno));
          return -1;
  
      default:        /* parent */
          (void) close(fdin[0]);
          (void) close(fdout[1]);
          if (write(fdin[1], old, n) != n) {
              log_printf(s, "%s: write failed (%s).\n", MODNAME,
                     strerror(errno));
              return -1;
          }
          (void) close(fdin[1]);
          if ((*newch = (unsigned char *) malloc(n)) == NULL) {
              log_printf(s, "%s: out of memory in uncompress()\n",
                     MODNAME);
              return -1;
          }
          if ((n = read(fdout[0], *newch, n)) <= 0) {
              free(*newch);
              log_printf(s, "%s: read failed (%s).\n", MODNAME,
                     strerror(errno));
              return -1;
          }
          (void) close(fdout[0]);
          (void) wait(NULL);
          return n;
      }
  }
  
  /*
   * is_tar() -- figure out whether file is a tar archive.
   * 
   * Stolen (by author of file utility) from the public domain tar program: Public
   * Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
   * 
   * @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
   * 1997/06/24 00:41:02 ikluft Exp ikluft $
   * 
   * Comments changed and some code/comments reformatted for file command by Ian
   * Darwin.
   */
  
  #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  
  /*
   * Return 0 if the checksum is bad (i.e., probably not a tar archive), 1 for
   * old UNIX tar file, 2 for Unix Std (POSIX) tar file.
   */
  
  static int
  is_tar(buf, nbytes)
      unsigned char *buf;
      int nbytes;
  {
      register union record *header = (union record *) buf;
      register int i;
      register long sum, recsum;
      register char *p;
  
      if (nbytes < sizeof(union record))
          return 0;
  
      recsum = from_oct(8, header->header.chksum);
  
      sum = 0;
      p = header->charptr;
      for (i = sizeof(union record); --i >= 0;) {
          /*
           * We can't use unsigned char here because of old compilers,
           * e.g. V7.
           */
          sum += 0xFF & *p++;
      }
  
      /* Adjust checksum to count the "chksum" field as blanks. */
      for (i = sizeof(header->header.chksum); --i >= 0;)
          sum -= 0xFF & header->header.chksum[i];
      sum += ' ' * sizeof header->header.chksum;
  
      if (sum != recsum)
          return 0;    /* Not a tar archive */
  
      if (0 == strcmp(header->header.magic, TMAGIC))
          return 2;    /* Unix Standard tar archive */
  
      return 1;        /* Old fashioned tar archive */
  }
  
  
  /*
   * Quick and dirty octal conversion.
   * 
   * Result is -1 if the field is invalid (all blank, or nonoctal).
   */
  static long
  from_oct(int digs, char *where)
  {
      register long value;
  
      while (isspace(*where)) {    /* Skip spaces */
          where++;
          if (--digs <= 0)
              return -1;    /* All blank field */
      }
      value = 0;
      while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
          value = (value << 3) | (*where++ - '0');
          --digs;
      }
  
      if (digs > 0 && *where && !isspace(*where))
          return -1;    /* Ended on non-space/nul */
  
      return value;
  }
  
  /*
   * Check for file-revision suffix
   *
   * This is for an obscure document control system used on an intranet.
   * The web respresentation of each file's revision has an @1, @2, etc
   * appended with the revision number.  This needs to be stripped off to
   * find the file suffix, which can be recognized by sending the name back
   * through a sub-request.  The base file name (without the @num suffix)
   * must exist because its type will be used as the result.
   */
  static int 
  revision_suffix(request_rec * r)
  {
      int suffix_pos, result;
      char *sub_filename;
      request_rec *sub;
  
  #if DEBUG
      fprintf(stderr, "%s: revision_suffix checking%s\n", MODNAME,
          r->filename);
  #endif                /* DEBUG */
  
      /* check for recognized revision suffix */
      suffix_pos = strlen(r->filename) - 1;
      if (!isdigit(r->filename[suffix_pos])) {
          return 0;
      }
      while (suffix_pos >= 0 && isdigit(r->filename[suffix_pos]))
          suffix_pos--;
      if (suffix_pos < 0 || r->filename[suffix_pos] != '@') {
          return 0;
      }
  
      /* perform sub-request for the file name without the suffix */
      result = 0;
      sub_filename = pstrndup(r->pool, r->filename, suffix_pos);
  #if DEBUG
      fprintf(stderr, "%s: subrequest lookup for %s\n", MODNAME, sub_filename);
  #endif                /* DEBUG */
      sub = sub_req_lookup_file(sub_filename, r);
  
      /* extract content type/encoding/language from sub-request */
      if (sub->content_type) {
          r->content_type = pstrdup(r->pool, sub->content_type);
  #if DEBUG
          fprintf(stderr, "%s: subrequest %s got %s\n", MODNAME,
              sub_filename, r->content_type);
  #endif                /* DEBUG */
          if (sub->content_encoding)
              r->content_encoding =
                  pstrdup(r->pool, sub->content_encoding);
          if (sub->content_language)
              r->content_language =
                  pstrdup(r->pool, sub->content_language);
          result = 1;
      }
  
      /* clean up */
      destroy_sub_req(sub);
  
      return result;
  }
  
  /*
   * initialize the module
   */
  
  static void 
  magic_init(server_rec * s, pool * p)
  {
      int result;
      magic_server_config_rec *conf = (magic_server_config_rec *)
          get_module_config(s->module_config, &mime_magic_module);
  #if DEBUG
      struct magic *m, *prevm;
  #endif                /* DEBUG */
  
      /* on the first time through we read the magic file */
      if (conf->magicfile && !conf->magic) {
          result = apprentice(s, p);
          if (result == -1)
              return;
  #if DEBUG
          prevm = 0;
          fprintf(stderr, "%s: magic_init 1 test\n", MODNAME);
          for (m = conf->magic; m; m = m->next) {
              if (isprint((((unsigned long) m) >> 24) & 255) &&
                  isprint((((unsigned long) m) >> 16) & 255) &&
                  isprint((((unsigned long) m) >> 8) & 255) &&
                  isprint(((unsigned long) m) & 255)) {
                  fprintf(stderr, "%s: magic_init 1: "
                      "POINTER CLOBBERED! "
                      "m=\"%c%c%c%c\" line=%d\n", MODNAME,
                      (((unsigned long) m) >> 24) & 255,
                      (((unsigned long) m) >> 16) & 255,
                      (((unsigned long) m) >> 8) & 255,
                      ((unsigned long) m) & 255,
                      prevm ? prevm->lineno : -1);
                  break;
              }
              prevm = m;
          }
          fflush(stderr);
  #endif
      }
  
      if (!conf->magic)
          return;
      return;
  }
  
  /*
   * Find the Content-Type from any resource this module has available
   */
  
  static int 
  magic_find_ct(request_rec * r)
  {
      int result;
  
      /* was someone else already here? */
      if (r->content_type) {
          return DECLINED;
      }
  
      /* make sure mime_module was called first - it's cheaper! */
      if (mime_module.type_checker) {
          result = (mime_module.type_checker) (r);
          if (result == OK) {
              return OK;
          }
      }
  
      /* initialize per-request info */
      if (!magic_set_config(r)) {
          return HTTP_INTERNAL_SERVER_ERROR;
      }
  
      /* try excluding file-revision suffixes */
      if (revision_suffix(r) != 1) {
          /* process it based on the file contents */
          magic_process(r);
      }
  
      /* if we have any results, put them in the request structure */
      return magic_rsl_to_request(r);
  }
  
  /*
   * Apache API module interface
   */
  
  module mime_magic_module = {
      STANDARD_MODULE_STUFF,
      magic_init,      /* initializer */
      NULL,            /* dir config creater */
      NULL,            /* dir merger --- default is to override */
      create_magic_server_config,    /* server config */
      merge_magic_server_config,    /* merge server config */
      mime_magic_cmds, /* command table */
      NULL,            /* handlers */
      NULL,            /* filename translation */
      NULL,            /* check_user_id */
      NULL,            /* check auth */
      NULL,            /* check access */
      magic_find_ct,   /* type_checker */
      NULL,            /* fixups */
      NULL             /* logger */
  };
  
  
  

Mime
View raw message