3 The HTTP server libraries
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog HTTP support
        • The HTTP server libraries
          • Creating an HTTP reply
          • library(http/http_dispatch): Dispatch requests in the HTTP server
          • library(http/http_dirindex): HTTP directory listings
          • library(http/http_files): Serve plain files from a hierarchy
            • http_reply_from_files/3
          • library(http/http_session): HTTP Session management
          • library(http/http_cors): Enable CORS: Cross-Origin Resource Sharing
          • library(http/http_authenticate): Authenticate HTTP connections using 401 headers
          • library(http/http_digest): HTTP Digest authentication
          • library(http/http_dyn_workers): Dynamically schedule HTTP workers.
          • Custom Error Pages
          • library(http/http_openid): OpenID consumer and server library
          • Get parameters from HTML forms
          • Request format
          • Running the server
          • The wrapper library
          • library(http/http_host): Obtain public server location
          • library(http/http_log): HTTP Logging module
          • Debugging HTTP servers
          • library(http/http_header): Handling HTTP headers
          • The library(http/html_write) library
          • library(http/js_write): Utilities for including JavaScript
          • library(http/http_path): Abstract specification of HTTP server locations
          • library(http/html_head): Automatic inclusion of CSS and scripts links
          • library(http/http_pwp): Serve PWP pages through the HTTP server

3.4 library(http/http_files): Serve plain files from a hierarchy

See also
pwp_handler/2 provides similar facilities, where .pwp files can be used to add dynamic behaviour.

Although the SWI-Prolog Web Server is intended to serve documents that are computed dynamically, serving plain files is sometimes necessary. This small module combines the functionality of http_reply_file/3 and http_reply_dirindex/3 to act as a simple web-server. Such a server can be created using the following code sample, which starts a server at port 8080 that serves files from the current directory ('.'). Note that the handler needs a prefix option to specify that it must handle all paths that begin with the registed location of the handler.

:- use_module(library(http/http_server)).
:- use_module(library(http/http_files)).

:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).

:- initialization(http_server([port(8080)]), main).
http_reply_from_files(+Dir, +Options, +Request)
HTTP handler that serves files from the directory Dir. This handler uses http_reply_file/3 to reply plain files. If the request resolves to a directory, it uses the option indexes to locate an index file (see below) or uses http_reply_dirindex/3 to create a listing of the directory.

Options:

indexes(+List)
List of files tried to find an index for a directory. The default is ['index.html'].

Note that this handler must be tagged as a prefix handler (see http_handler/3 and module introduction). This also implies that it is possible to override more specific locations in the hierarchy using http_handler/3 with a longer path-specifier.

Dir is either a directory or an path-specification as used by absolute_file_name/3. This option provides great flexibility in (re-)locating the physical files and allows merging the files of multiple physical locations into one web-hierarchy by using multiple user:file_search_path/2 clauses that define the same alias.
See also
The hookable predicate file_mime_type/2 is used to determine the Content-type from the file name.