% This LaTeX document was generated using the LaTeX backend of PlDoc,
% The SWI-Prolog documentation system



\subsection{library(http/http_pwp): Serve PWP pages through the HTTP server}

\label{sec:httppwp}

\begin{tags}
\mtag{To be done}- Support elements in the HTML header that allow controlling the
page, such as setting the CGI-header, authorization, etc. \\- Allow external styling. Pass through \predref{reply_html_page}{2}? Allow
filtering the DOM before/after PWP?
\end{tags}

This module provides convience predicates to include PWP (Prolog
Well-formed Pages) in a Prolog web-server. It provides the following
predicates:

\begin{description}
    \infixtermitem{\Sdiv}{\term{pwp_handler}{}}{2}
This is a complete web-server aimed at serving static pages, some
of which include PWP. This API is intended to allow for programming
the web-server from a hierarchy of pwp files, prolog files and static
web-pages.
    \infixtermitem{\Sdiv}{\term{reply_pwp_page}{}}{3}
Return a single PWP page that is executed in the context of the calling
module. This API is intended for individual pages that include so much
text that generating from Prolog is undesirable.
\end{description}

\vspace{0.7cm}

\begin{description}
    \predicate{pwp_handler}{2}{+Options, +Request}
Handle PWP files. This predicate is defined to create a simple
HTTP server from a hierarchy of PWP, HTML and other files. The
interface is kept compatible with the
\verb$library(http/http_dispatch)$. In the typical usage scenario, one
needs to define an http location and a file-search path that is
used as the root of the server. E.g., the following declarations
create a self-contained web-server for files in \verb$/web/pwp/$.

\begin{code}
user:file_search_path(pwp, '/web/pwp').

:- http_handler(root(.), pwp_handler([path_alias(pwp)]), [prefix]).
\end{code}

\arg{Options} include:

\begin{description}
    \termitem{path_alias}{+Alias}
Search for PWP files as \arg{Alias}(Path). See \predref{absolute_file_name}{3}.
    \termitem{index}{+Index}
Name of the directory index (pwp) file. This option may
appear multiple times. If no such option is provided,
\predref{pwp_handler}{2} looks for \verb$index.pwp$.
    \termitem{view}{+Boolean}
If \const{true} (default is \const{false}), allow for ?view=source to serve
PWP file as source.
    \termitem{index_hook}{:Hook}
If a directory has no index-file, \predref{pwp_handler}{2} calls
\arg{Hook}(PhysicalDir, \arg{Options}, \arg{Request}). If this semidet
predicate succeeds, the request is considered handled.
    \termitem{hide_extensions}{+List}
Hide files of the given extensions. The default is to
hide .pl files.
    \termitem{dtd}{?DTD}
\arg{DTD} to parse the input file with. If unbound, the generated
\arg{DTD} is returned
\end{description}

\begin{tags}
    \tag{Errors}
\verb$permission_error(index, http_location, Location)$ is
raised if the handler resolves to a directory that has no
index.
    \tag{See also}
\predref{reply_pwp_page}{3}
\end{tags}

    \predicate{reply_pwp_page}{3}{:File, +Options, +Request}
Reply a PWP file. This interface is provided to server
individual locations from PWP files. Using a PWP file rather
than generating the page from Prolog may be desirable because
the page contains a lot of text (which is cumbersome to generate
from Prolog) or because the maintainer is not familiar with
Prolog.

\arg{Options} supported are:

\begin{description}
    \termitem{mime_type}{+Type}
Serve the file using the given mime-type. Default is
text/html.
    \termitem{unsafe}{+Boolean}
Passed to \predref{http_safe_file}{2} to check for unsafe paths.
    \termitem{pwp_module}{+Boolean}
If \const{true}, (default \const{false}), process the PWP file in
a module constructed from its canonical absolute path.
Otherwise, the PWP file is processed in the calling
module.
\end{description}

Initial context:

\begin{description}
    \termitem{\arg{SCRIPT_NAME}}{}
Virtual path of the script.
    \termitem{\arg{SCRIPT_DIRECTORY}}{}
Physical directory where the script lives
    \termitem{\arg{QUERY}}{}
Var=Value list representing the query-parameters
    \termitem{\arg{REMOTE_USER}}{}
If access has been authenticated, this is the authenticated
user.
    \termitem{\arg{REQUEST_METHOD}}{}
One of \const{get}, \const{post}, \const{put} or \const{head}
    \termitem{\arg{CONTENT_TYPE}}{}
Content-type provided with HTTP POST and PUT requests
    \termitem{\arg{CONTENT_LENGTH}}{}
Content-length provided with HTTP POST and PUT requests
\end{description}

While processing the script, the file-search-path pwp includes
the current location of the script. I.e., the following will
find myprolog in the same directory as where the PWP file
resides.

\begin{code}
pwp:ask="ensure_loaded(pwp(myprolog))"
\end{code}

\begin{tags}
    \tag{See also}
\predref{pwp_handler}{2}.
    \tag{To be done}
complete the initial context, as far as possible from CGI
variables. See \url{http://hoohoo.ncsa.illinois.edu/docs/cgi/env.html}
\end{tags}
\end{description}

