This directive can be used to declare that an HTML rendering
rule takes HTML content as argument. It has two effects. It
emits the appropriate meta_predicate/1 and instructs the
built-in editor (PceEmacs) to provide proper colouring for the
arguments. The arguments in Head are the same as for
meta_predicate or can be constant html
. For example:
:- html_meta
page(html,html,?,?).
Get HTTP GET or POST form-data, applying type validation,
default values, etc. Provided options are:
- attribute_declarations(:Goal)
- Causes the declarations for an attributed named A to be
fetched using
call(Goal, A, Declarations)
.
- form_data(-Data)
- Return the data read from the GET or POST request as a
list Name = Value. All data, including name/value pairs
used for Parms, is unified with Data.
The attribute_declarations hook allows sharing the declaration
of attribute-properties between many http_parameters/3 calls. In
this form, the requested attribute takes only one argument and
the options are acquired by calling the hook. For example:
...,
http_parameters(Request,
[ sex(Sex)
],
[ attribute_declarations(http_param)
]),
...
http_param(sex, [ oneof(male, female),
description('Sex of the person')
]).
- bug
- - If both request parameters (?name=value&...) and a POST are
present the parameters are extracted from the request parameters.
Still, as it is valid to have request parameters in a POST request
this predicate should not process POST requests. We will keep the
current behaviour as the it is not common for a request to have both
request parameters and a POST data of the type
application/x-www-form-urlencoded
.
In the unlikely event this poses a problem the request may be
specified as [method(get)
|Request].
Get HTTP GET or POST form-data, applying type validation,
default values, etc. Provided options are:
- attribute_declarations(:Goal)
- Causes the declarations for an attributed named A to be
fetched using
call(Goal, A, Declarations)
.
- form_data(-Data)
- Return the data read from the GET or POST request as a
list Name = Value. All data, including name/value pairs
used for Parms, is unified with Data.
The attribute_declarations hook allows sharing the declaration
of attribute-properties between many http_parameters/3 calls. In
this form, the requested attribute takes only one argument and
the options are acquired by calling the hook. For example:
...,
http_parameters(Request,
[ sex(Sex)
],
[ attribute_declarations(http_param)
]),
...
http_param(sex, [ oneof(male, female),
description('Sex of the person')
]).
- bug
- - If both request parameters (?name=value&...) and a POST are
present the parameters are extracted from the request parameters.
Still, as it is valid to have request parameters in a POST request
this predicate should not process POST requests. We will keep the
current behaviour as the it is not common for a request to have both
request parameters and a POST data of the type
application/x-www-form-urlencoded
.
In the unlikely event this poses a problem the request may be
specified as [method(get)
|Request].
Create a server at Port that calls Goal for each parsed request.
Options provide a list of options. Defined options are
- port(?Address)
- Port to bind to. Address is either a port or a term
Host:Port. The port may be a variable, causing the system
to select a free port. See tcp_bind/2.
- unix_socket(+Path)
- Instead of binding to a TCP port, bind to a Unix Domain
Socket at Path.
- entry_page(+URI)
- Affects the message printed while the server is started.
Interpreted as a URI relative to the server root.
- tcp_socket(+Socket)
- If provided, use this socket instead of the creating one and
binding it to an address. The socket must be bound to an
address. Note that this also allows binding an HTTP server to
a Unix domain socket (
AF_UNIX
). See socket_create/2.
- workers(+Count)
- Determine the number of worker threads. Default is 5. This
is fine for small scale usage. Public servers typically need
a higher number.
- timeout(+Seconds)
- Maximum time of inactivity trying to read the request after a
connection has been opened. Default is 60 seconds. See
set_stream/1 using the timeout option.
- keep_alive_timeout(+Seconds)
- Time to keep `Keep alive' connections alive. Default is
2 seconds.
- stack_limit(+Bytes)
- Stack limit to use for the workers. The default is inherited
from the
main
thread.
If you need to control resource usage you may consider the
spawn
option of http_handler/3 and library(thread_pool).
- silent(Bool)
- If
true
(default false
), do not print an informational
message that the server was started.
A typical initialization for an HTTP server that uses
http_dispatch/1 to relay requests to predicates is:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
start_server(Port) :-
http_server(http_dispatch, [port(Port)]).
Note that multiple servers can coexist in the same Prolog
process. A notable application of this is to have both an HTTP
and HTTPS server, where the HTTP server redirects to the HTTPS
server for handling sensitive requests.
Continue this connection on a new thread. A handler may call
http_spawn/2 to start a new thread that continues processing the
current request using Goal. The original thread returns to the
worker pool for processing new requests. Options are passed to
thread_create/3, except for:
- pool(+Pool)
- Interfaces to library(thread_pool), starting the thread
on the given pool.
If a pool does not exist, this predicate calls the multifile
hook create_pool/1 to create it. If this predicate succeeds
the operation is retried.