http_static_files
The http_static_files library provides a router-agnostic helper for
serving existing files from a configured document root using normalized
http_core request and response terms.
This library resolves a relative request path against a document root,
canonicalizes the joined target path using os::absolute_file_name/2,
rejects canonicalized escapes with the same outward 404 Not Found
response used for missing files, falls back to configurable index files
for directory targets, guesses the response media type using
mime_types, emits ETag, Last-Modified, and Accept-Ranges
metadata, and returns file-backed response bodies that http_server
can stream efficiently.
API documentation
Open the ../../apis/library_index.html#http_static_files link in a web browser.
Loading
To load the library, load the loader.lgt file:
| ?- logtalk_load(http_static_files(loader)).
Testing
To test this library, load the tester.lgt file:
| ?- logtalk_load(http_static_files(tester)).
Current scope
The current library provides one public object with two public predicates:
serve/4serve/5
Supported options are:
index_files(IndexFiles)mime_types_strict(Boolean)cache_control(Directives)expires(Expires)
Default options are:
index_files(['index.html', 'index.htm'])mime_types_strict(false)cache_control([])expires(none)
Supported features:
GETandHEAD200 OKfile responsescanonical docroot-prefix checks after path normalization
weak
ETagandLast-Modifiedvalidators derived from file size and modification time304 Not Modifiedresponses for matchingIf-None-MatchandIf-Modified-Sincerequests, withIf-None-Matchtaking precedencesingle
bytes=ranges with206 Partial Content416 Range Not Satisfiablefor malformed, unsupported multi-range, and unsatisfiable range requestsIf-Rangefallback to full200 OKresponses when the validator does not match or is only weakly equal406 Not AcceptablewhenAccept-Encodingrejects the identity representation and no acceptable precompressed variant is available404 Not Foundfor missing or unsafe paths405 Method Not Allowedfor other methodsindex-file lookup for directory targets
precompressed
.brand.gzasset negotiation driven byAccept-EncodingVary: Accept-Encodingresponses when negotiated precompressed variants existMIME type and content-encoding guessing
configurable
Cache-ControlandExpiresresponse headers
For directory listing, see the http_directory_listing library.
Cache-policy configuration uses these option values:
cache_control(Directives)whereDirectivesis a list containing any ofpublic,private,no_cache,no_store,no_transform,must_revalidate,proxy_revalidate,immutable,max_age(Seconds),s_maxage(Seconds),stale_while_revalidate(Seconds),stale_if_error(Seconds), orextension(Directive)expires(none)to omit the header,expires(Seconds)for a relative expiry from the current system time, orexpires(date_time(Year,Month,Day,Hour,Minute,Second))for an absolute expiry time
Current validator and date handling is intentionally conservative:
ETagvalues are weak validators of the formW/"Size-ModifiedTime"HTTP-date formatting, date validation, and Unix-time conversion delegate to the
dateslibrary; request parsing remains limited to the IMF-fixdate form generated forLast-ModifiedIf-Rangeentity-tag evaluation uses strong comparison semantics, so the generated weakETagvalues never authorize partial responses; matchingLast-Modifieddates can still do soIf-Rangemismatches fall back to the full200 OKresponse before range validation, so malformed or unsupported range syntax is ignored in that caseprecompressed-asset negotiation currently recognizes
.brand.gzsibling files and selects between them usingAccept-Encodingquality values, otherwise falling back to the identity representation when it remains acceptablepath sandboxing is based on canonicalized absolute-path prefix checks; no separate cross-backend symlink-policy layer has been added yet