Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

stMimeTypes

The one extension table in the library, read for TWO DIFFERENT PURPOSES that
  must not be confused:

    - as a LABEL, to answer "what Content-Type do I stamp on this file?"; and
    - as a PERMIT, to answer "may this file be served at all?".

  THE DISTINCTION IS THE WHOLE POINT OF THIS UNIT. A label lookup wants a
  fallback - an unrecognised extension is still a file, and
  application/octet-stream is a perfectly good answer. A permit lookup must
  NEVER fall back: an extension nobody enumerated is exactly the case a
  deny-by-default content server has to refuse, and a table that answers
  "application/octet-stream" for .pfx has permitted the private key.

  So the two questions get two methods. HasExtension is the permit predicate
  and is true only for an extension explicitly in the table. MediaTypeOf is
  the label and takes the fallback from the CALLER, who is the only one who
  knows whether a fallback is safe in its context. There is deliberately no
  single method that does both.

  WHAT IS CODE AND WHAT IS CONFIGURATION. The seed table below is small on
  purpose - the extensions a static web server cannot sensibly run without.
  A deployment's real table (the ninety-odd rows a general-purpose server
  ends up carrying) is CONFIGURATION: load it with AddMapping at startup, in
  the same syntax an ini file already uses, e.g.

      Map.AddMapping('jpeg,jpg,jpe','image/jpeg');

  Growing the seed list instead is how a library ends up shipping one
  deployment's policy to every other deployment.

  THREAD SAFETY. A map is NOT internally locked. DefaultMimeTypeMap is built
  once during unit initialisation - not lazily - so there is no first-call
  race, but it is a mutable shared object: configure it at startup, before
  any server is listening, and treat it as read-only thereafter. A consumer
  that needs to vary the table per mount should hold its own map from
  NewMimeTypeMap, or take a Clone. Clone is what stFileJail uses so that a
  jail's permit set cannot change under it after the jail is minted.

Feature notes: File jail (filesystem confinement) notes

Classes, interfaces and records

interfaceiMimeTypeMap

Extension -> media-type registry. Extensions are stored and compared normalised (leading dot, lower case); every method below accepts either spelling from the caller and normalises on the way in.

function HasExtension(const aExtension: string): Boolean;
THE PERMIT PREDICATE. True only when aExtension is explicitly present. Never infers, never falls back. An empty extension is never present - a file with no extension is decided by jail policy, not by this table.
function MediaTypeOfExtension(const aExtension, aFallback: string): string;
THE LABEL, by extension. Returns aFallback when the extension is absent; pass '' when the caller would rather emit no Content-Type at all than emit a wrong one.
function MediaTypeOf(const aFileName, aFallback: string): string;
THE LABEL, by file name - ExtractFileExt then MediaTypeOfExtension.
procedure AddMapping(const aExtensions, aMediaType: string);
Adds one media type under one or more extensions. aExtensions is a comma-separated list in either spelling ('jpeg,jpg,jpe' or '.jpeg,.jpg'), which is the syntax deployments already keep in ini files. Re-adding an extension replaces its media type. An empty media type is rejected: a row that maps an extension to nothing reads as "permitted, unlabelled", which is a footgun in a permit table.
procedure RemoveExtension(const aExtension: string);
Removes one extension. Absent is not an error.
procedure Clear;
Empties the table. A cleared map permits nothing - which is the correct starting point for a caller that intends to enumerate its own set.
procedure ListExtensions(aList: TStrings);
Appends the normalised extensions, one per line, in no defined order.
function Count: Integer;
Number of extensions currently mapped.
function Clone: iMimeTypeMap;
An independent copy. Mutating either afterwards does not affect the other - this is how a consumer takes a snapshot of a shared table.

Routines