From: Adrián Arroyo Calle Date: Sat, 28 Jan 2023 15:00:27 +0000 (+0100) Subject: Fix library(http/http_server) docs. Other minor fixes X-Git-Tag: v0.9.2~203^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=4110cfcfcb41147ec0fed8011cb565b33f04f315;p=scryer-prolog.git Fix library(http/http_server) docs. Other minor fixes --- diff --git a/INDEX.md b/INDEX.md index 7653cfc4..b17a7190 100644 --- a/INDEX.md +++ b/INDEX.md @@ -15,7 +15,7 @@ Some of the Scryer Prolog features are: * Integrated constraint programming libraries: [clp(B)](/clpb.html), [clp(Z)](/clpz.html). * [Definite Clause Grammars](/dcgs.html) * Coroutining support ([`dif/2`](/dif.html), [`freeze/2`](/freeze.html), ...) -* Tabling and SLG resolution +* [Tabling and SLG resolution](/tabling.html) * Compact string representation * Network libraries ([TCP sockets](/sockets.html), [HTTP server](/http/http_server.html), [HTTP client](/http/http_open.html), ...) * [Cryptographical predicates](/crypto.html) diff --git a/src/lib/http/http_server.pl b/src/lib/http/http_server.pl index bbe17565..7f2de002 100644 --- a/src/lib/http/http_server.pl +++ b/src/lib/http/http_server.pl @@ -6,45 +6,49 @@ */ /** This library provides an starting point to build HTTP server based applications. -It is based on Hyper, which allows for HTTP/1.0, HTTP/1.1 and HTTP/2. However, +It is based on [Hyper](https://hyper.rs/), which allows for HTTP/1.0, HTTP/1.1 and HTTP/2. However, some advanced features that Hyper provides are still not accesible. ## Usage -The main predicate of the library is http\_listen/2, which needs a port number +The main predicate of the library is `http_listen/2`, which needs a port number (usually 80) and a list of handlers. A handler is a compound term with the functor as one HTTP method (in lowercase) and followed by a Route Match and a predicate which will handle the call. - text_handler(Request, Response) :- - http_status_code(Response, 200), - http_body(Response, text("Welcome to Scryer Prolog!")). - - parameter_handler(User, Request, Response) :- - http_body(Response, text(User)). - - http_listen(7890, [ - get(echo, text_handler), % GET /echo - post(user/User, parameter_handler(User)) % POST /user/ - ]). +``` +text_handler(Request, Response) :- + http_status_code(Response, 200), + http_body(Response, text("Welcome to Scryer Prolog!")). + +parameter_handler(User, Request, Response) :- + http_body(Response, text(User)). + +http_listen(7890, [ + get(echo, text_handler), % GET /echo + post(user/User, parameter_handler(User)) % POST /user/ +]). +``` Every handler predicate will have at least 2-arity, with Request and Response. -Although you can work directly with http\_request and http\_response terms, it is +Although you can work directly with `http_request` and `http_response` terms, it is recommeded to use the helper predicates, which are easier to understand and cleaner: - - `http\_headers(Response/Request, Headers)` - - `http\_status\_code(Responde, StatusCode)` - - `http\_body(Response/Request, text(Body))` - - `http\_body(Response/Request, binary(Body))` - - `http\_body(Request, form(Form))` - - `http\_body(Response, file(Filename))` - - `http\_redirect(Response, Url)` - - `http\_query(Request, QueryName, QueryValue)` - - Some things that are still missing: + + - `http_headers(Response/Request, Headers)` + - `http_status_code(Responde, StatusCode)` + - `http_body(Response/Request, text(Body))` + - `http_body(Response/Request, binary(Body))` + - `http_body(Request, form(Form))` + - `http_body(Response, file(Filename))` + - `http_redirect(Response, Url)` + - `http_query(Request, QueryName, QueryValue)` + +Some things that are still missing: + - Read forms in multipart format - HTTP Basic Auth - Session handling via cookies - - HTML Templating (but you can use [Teruel](https://github.com/aarroyoc/teruel/) and/or [Marquete](https://github.com/aarroyoc/marquete/) for that) + - HTML Templating (but you can use [Teruel](https://github.com/aarroyoc/teruel/), [Marquete](https://github.com/aarroyoc/marquete/) or [Djota](https://github.com/aarroyoc/djota) for that) */ @@ -71,8 +75,8 @@ recommeded to use the helper predicates, which are easier to understand and clea %% http_listen(+Port, +Handlers). % % Listens for HTTP connections on port Port. Each handler on the list Handlers should be of the form: `HttpVerb(PathUnification, Predicate)`. -% For example: `get(user/User, get\_info(User))` will match an HTTP request that is a GET, the path unifies with /user/User (where User is a variable) -% and it will call get_info with three arguments: an http\_request term, an http\_response term and User. +% For example: `get(user/User, get_info(User))` will match an HTTP request that is a GET, the path unifies with /user/User (where User is a variable) +% and it will call `get_info` with three arguments: an `http_request` term, an `http_response` term and User. http_listen(Port, Module:Handlers0) :- must_be(integer, Port), must_be(list, Handlers0), @@ -213,7 +217,7 @@ string_without(_, []) --> %% http_headers(?Request_Response, ?Headers). % -% True iff Request_Response is a request or response with headers Headers. Can be used both to get headers (usually in from a request) +% True iff `Request_Response` is a request or response with headers Headers. Can be used both to get headers (usually in from a request) % and to add headers (usually in a response). http_headers(http_request(Headers, _, _), Headers). http_headers(http_response(_, _, Headers), Headers). @@ -221,6 +225,7 @@ http_headers(http_response(_, _, Headers), Headers). %% http_body(?Request_Response, ?Body). % % True iff Body is the body of the request or response. A body can be of the following types: +% % * `bytes(Bytes)` for both requests and responses, interprets the body as bytes % * `text(Bytes)` for both requests and responses, interprets the body as text % * `form(Form)` only for requests, interprets the body as an `application/x-www-form-urlencoded` form. diff --git a/src/lib/lists.pl b/src/lib/lists.pl index cb33e8fe..1bdaf61e 100644 --- a/src/lib/lists.pl +++ b/src/lib/lists.pl @@ -73,6 +73,8 @@ resource_error(Resource, Context) :- % N = 3. % ?- length(Xs, 3). % Xs = [_A, _B, _C]. +% ?- length("chars", N). +% N = 5. % ``` length(Xs0, N) :-