% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \subsection{library(http/http_files): Serve plain files from a hierarchy} \label{sec:httpfiles} \begin{tags} \tag{See also} \predref{pwp_handler}{2} provides similar facilities, where .pwp files can be used to add dynamic behaviour. \end{tags} 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 \predref{http_reply_file}{3} and \predref{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 \const{prefix} option to specify that it must handle all paths that begin with the registed location of the handler. \begin{code} :- 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). \end{code} \vspace{0.7cm} \begin{description} \predicate{http_reply_from_files}{3}{+Dir, +Options, +Request} HTTP handler that serves files from the directory \arg{Dir}. This handler uses \predref{http_reply_file}{3} to reply plain files. If the request resolves to a directory, it uses the option \const{indexes} to locate an index file (see below) or uses \predref{http_reply_dirindex}{3} to create a listing of the directory. \arg{Options}: \begin{description} \termitem{indexes}{+List} \arg{List} of files tried to find an index for a directory. The default is \verb$['index.html']$. \end{description} Note that this handler must be tagged as a \const{prefix} handler (see \predref{http_handler}{3} and module introduction). This also implies that it is possible to override more specific locations in the hierarchy using \predref{http_handler}{3} with a longer path-specifier. When using \predref{http_handler}{3} to bind this predicate to an HTTP location, make sure it is bound to a location that ends in a \verb$/$. When using \qpredref{http}{location}{3} to define symbolic names to HTTP locations this is written as \begin{center} \Sneck{} \verb$http_handler(aliasname(.), http_reply_from_files(srcdir, []), [prefix])$. \end{center} \begin{arguments} \arg{Dir} & is either a directory or an path-specification as used by \predref{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 \qpredref{user}{file_search_path}{2} clauses that define the same alias. \\ \end{arguments} \begin{tags} \tag{See also} The hookable predicate \predref{file_mime_type}{2} is used to determine the \verb$Content-type$ from the file name. \end{tags} \end{description}