Register Closure as a handler for HTTP requests. Path
is either an absolute path such as '/home.html'
or a term
Alias(Relative). Where Alias is associated with a concrete path using http:location/3
and resolved using http_absolute_location/3. Relative
can be a single atom or a term‘Segment1/Segment2/...`, where each
element is either an atom or a variable. If a segment is a variable it
matches any segment and the binding may be passed to the closure. If the
last segment is a variable it may match multiple segments. This allows
registering REST paths, for example:
:- http_handler(root(user/User), user(Method, User),
[ method(Method),
methods([get,post,put])
]).
user(get, User, Request) :-
...
user(post, User, Request) :-
...
If an HTTP request arrives at the server that matches Path, Closure
is called as below, where Request is the parsed HTTP request.
call(Closure, Request)
Options is a list containing the following options:
- authentication(+Type)
- Demand authentication. Authentication methods are pluggable. The library
http_authenticate.pl
provides a plugin for user/password based Basic
HTTP
authentication.
- chunked
- Use
Transfer-encoding: chunked
if the client allows for it.
- condition(:Goal)
- If present, the handler is ignored if Goal does not succeed.
- content_type(+Term)
- Specifies the content-type of the reply. This value is currently not
used by this library. It enhances the reflexive capabilities of this
library through http_current_handler/3.
- id(+Atom)
- Identifier of the handler. The default identifier is the predicate name.
Used by http_location_by_id/2
and
http_link_to_id/3.
- hide_children(+Bool)
- If
true
on a prefix-handler (see prefix), possible children
are masked. This can be used to (temporary) overrule part of the tree.
- method(+Method)
- Declare that the handler processes Method. This is equivalent
to
methods([Method])
. Using method(*)
allows for
all methods.
- methods(+ListOfMethods)
- Declare that the handler processes all of the given methods. If this
option appears multiple times, the methods are combined.
- prefix
- Call Pred on any location that is a specialisation of Path.
If multiple handlers match, the one with the longest path is used.
Options defined with a prefix handler are the default options
for paths that start with this prefix. Note that the handler acts as a
fallback handler for the tree below it:
:- http_handler(/, http_404([index('index.html')]),
[spawn(my_pool),prefix]).
- priority(+Integer)
- If two handlers handle the same path, the one with the highest priority
is used. If equal, the last registered is used. Please be aware that the
order of clauses in multifile predicates can change due to reloading
files. The default priority is 0 (zero).
- spawn(+SpawnOptions)
- Run the handler in a separate thread. If SpawnOptions is an
atom, it is interpreted as a thread pool name (see
create_thread_pool/3). Otherwise the
options are passed to
http_spawn/2 and from there to thread_create/3.
These options are typically used to set the stack limits.
- time_limit(+Spec)
- One of
infinite
, default
or a positive number
(seconds). If
default
, the value from the setting http:time_limit
is taken. The default of this setting is 300 (5 minutes). See
setting/2.
Note that http_handler/3
is normally invoked as a directive and processed using term-expansion.
Using term-expansion ensures proper update through make/0
when the specification is modified.
- Errors
- -
existence_error(http_location, Location)
- permission_error(http_method, Method, Location)
- See also
- http_reply_file/3 and http_redirect/3
are generic handlers to serve files and achieve redirects.