next up previous index
Next: ISO Standard Prolog Compatibility Up: Libraries Previous: Crossreference Checking   Index

Subsections


HTTP Library

The HTTP library contains an extensible server and a client for the Hyper Text Transfer Protocol. The library is entirely written in ECLiPSe.

Typical use of the client is for building WWW "Worms", WWW "Robots" or customized WWW browsers. Typical use of the server is for building customized servers, e.g. dynamic generation of HTML pages. The server and the client can typically be used together to build proxy servers.

Limitations and Bugs:

The library provides two predicates: http_client/7, and http_server/1.

Client

http_client(+Method, +Uri, +ObjectBody, +HttpParams, 
                    -RespError, -RespParam, -RespObjectBody)
        Metod and Uri and ObjectBody and String are strings
        HttpParams is a list of terms defined in the DCG
        Error is a term error(ErrorCode, ErrorPhrase)
                ErrorCode: the error code contained in the response
                ErrorCode: the error phrase contained in the response
        RespParam is a list of terms defined in the DCG
        RespObjectBody is the object body of the response

The client does:

Example

The following example illustrates the use of a client.

The predicate http_client/7 is used to access the HTML pages, given their URI (the method GET is applied).

/********************************************************************
 *  Web client example
 *******************************************************************/

> eclipse
ECRC Common Logic Programming System [sepia opium megalog parallel]
Version 3.5.2, Copyright ECRC GmbH, Wed Jan  3 12:54 1996
[eclipse 1]: use_module(http).
http_grammar.pl compiled traceable 25048 bytes in 0.38 seconds
http_client.pl compiled traceable 5916 bytes in 0.47 seconds
http_server.pl compiled traceable 5304 bytes in 0.07 seconds
http.pl    compiled traceable 0 bytes in 0.57 seconds

yes.
[eclipse 2]:  http_client("GET", "http://www.ecrc.de/staff/", "", [],
	Status, Param, Resp).

Status = error(200, "Document follows ")
Param = [date, server, contentType(mt(text, html))]

Resp = "<HTML>...</HTML>"

yes.

Server

http_server(+Port)
        Port is an integer (usually superior to the last protected TCP port)

The server does:

NOTE: The predicate http_server/1 requires that a module http_method is defined that contains a predicate http_method/6. This predicate is used by the programmer to customize the server. For instance the method GET can be simply implemented. The programmer can define its own methods.

Example

A simple example of server is the implementation of the method GET. A module is created that contains the predicate http_method/6 that implements the method GET: a read on the file identified by its URL. The file is returned if it is found, otherwise an error parameter is returned.

[eclipse 1]: [user].
 
/********************************************************************
 *  test (server)
 *******************************************************************/

:- module(http_method).

:- set_error_handler(170, fail/0).
:- set_error_handler(171, fail/0).

/* 
http_method(+Method, +Url, +ObjectBody, -Output, -StatusCode, -Parameter)
executes the method on the object and returns:
- the output of the method (possibly empty)
- a status code for the response status line
- a list of http parameters (in particular the length of the object body).

*/


http_method("GET", Url, _, Contents, 200, [contentLength(CL)]):-
        append_strings("/", FileName, Url),
        getContents(FileName, Contents), !,
        string_length(Contents, CL).
http_method("GET", _, _, "", 404, []).
        

getContents(Url, Contents):-
        open(Url, read, s),
        read_string(s, "", _, Contents),
        close(s).

^D

yes.

[eclipse 2]: use_module(http).
http_grammar.pl compiled traceable 25048 bytes in 0.27 seconds
http_client.pl compiled traceable 6052 bytes in 0.28 seconds
http_server.pl compiled traceable 5564 bytes in 0.03 seconds
http.pl    compiled traceable 0 bytes in 0.35 seconds

yes.
[eclipse 3]: use_module(http_method).

yes.
[eclipse 4]: http_server(8000).

This simple program can be used to test HTML pages. Viewers such as Netscape provide a view code option that signalizes syntax errors in the HTML code. This simple program can be used as a light weight testing tool, possibly launched from the directory where the HTML page resides.

HTTP Grammar

The structure of the HTTP messages is precisely described in the specification document (http://www.w3.org/pub/WWW/Protocols/). An augmented BNF is provided for each component of the header. We have used the DCG (Definite Clause Grammar) mechanism of ECLiPSe to encode the grammar, that we use for both parsing (from HTTP messages into Prolog terms) and pretty printing (from prolog terms into HTTP messages).

This DCG grammar may have to be modified with the evolutions of the HTTP protocol (standard modification and available client or server implementations).

File Structure

http.pl
exports the predicates that the user can manipulate: http_client/6, and http_server/1.

http_grammar.pl
contains the grammar that allows to parse and pretty print the headers of the HTTP messages.

http_client.pl
implements the HTTP client.

http_server.pl
implements the HTTP server.

Authors

This library was designed and implemented by Ph. Bonnet, S. Bressan and M. Meier.


next up previous index
Next: ISO Standard Prolog Compatibility Up: Libraries Previous: Crossreference Checking   Index

1999-08-06