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
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;
function MediaTypeOfExtension(const aExtension, aFallback: string): string;
function MediaTypeOf(const aFileName, aFallback: string): string;
procedure AddMapping(const aExtensions, aMediaType: string);
procedure RemoveExtension(const aExtension: string);
procedure Clear;
procedure ListExtensions(aList: TStrings);
function Count: Integer;
function Clone: iMimeTypeMap;