Module http

This module provides a HTTP server based on Pythonesk dispatch of a server object. The class of the server object need only implement a predicate dispatch/4 with the Pythonesk convention that the receiver appears in the first argument. The server can be started by providing the server object that will be responsible for handling HTTP requests:

?- run_http(<object>, <port>), fail; true.

The server currently implements a minimal subset of the HTTP/1.1 protocol restricted to GET method. The server will read the request line and the header lines. The server is able to generate error messages through the predicate dispatch_error/2. The server will generate a 404 error when dispatch/4 failed. The following HTTP/1.1 errors have been realized:

The predicate http_parameter/3 can be used by the server object to access URI query parame-ters. The predicates response_text/3, response_binary/3 and html_escape/2 can be used to generate dynamic content by the server object. The predicates dispatch_text/3 and dispatch_binary/3 can be used by the server object to deliver static content.

The web server also supports the above HTTP/1.1 codes, which might have additional response headers. The predicate dispatch_upgrade/2 will automatically generate a web socket accept key and can be used to implement upgrade/4. The predicate dispatch_redirect/2 requires a location, whereas the predicate dispatch_head/2 requires meta-data.

The following HTTP server predicates are provided:

run_http(O, P):
The predicate runs a web server with object O at port P.
http_parameter(R, N, V):
The predicate succeeds in V with the value of the parameter named N from the request R.
http_header(R, N, V):
The predicate succeeds in V with the value of the header named N from the request R.
dispatch_error(E, O):
The predicate sends the error code E to the socket O. The error codes from 4xx and 5xx are supported.
response_text(C, H, O):
Send an OK response C with meta-data headers H to the text output stream O.
dispatch_text(F, R, O):
The predicate sends the text resource F for request R to the socket O. Mime type determined from resource. Meta data determined from resource and validated with request conditions.
response_binary(C, H, O):
Send an OK response C with meta-data headers H to the binary output stream O. The OK responses from 2xx and 3xx are supported.
dispatch_binary(F, R, O):
The predicate sends the binary resource F for request R to the socket O. Mime type determined from resource. Meta data determined from resource and validated with request conditions.
dispatch_upgrade(R, O):
Send an upgrade response from request R to the socket O.
dispatch_redirect(L, O):
Send a redirect response to location L to the socket O.
validate_meta(R, H):
The predicate succeeds when the resource meta-data in the headers H satisfies the conditions in the request R.
dispatch_head(C, H, O):
Send an OK response C with meta-data headers H to the socket O. The OK responses from 2xx and 3xx are supported.

The following abstract HTTP server predicates are provided:

initialized(O, S):
The predicate is called when the server S is initialized for object O.
destroyed(O, S):
The predicate is called when the server S is destroyed for object O.
dispatch(O, P, R, S):
The predicate succeeds in dispatching the request for object O, with path P, with request R and the session S.
upgrade(O, P, R, S):
The predicate succeeds in upgrading the request for object O, with path P, with request R and the session S.

Kommentare