AgentBase   AgentBase for SICStus
Components for Internet Programming
Version 2001-0.2-alpha

AgentBase Home > HTTP

  OVERVIEW
  Introduction
Download
Installation
FAQ

  EXAMPLES
  Web Server + CGI
Prolog + Java
Prolog + Java Applets
Applet Logic Monitor
Query-Eval via Telnet

  COMPONENTS
  Generic Server
HTTP Support
HTML Support
JEval Java Interface
Utilities

  RELATED
  SICStus Prolog Home
AgentBase 0.1-alpha


Page Formatted for Printing

HTTP

HTTP is a package that enable SICStus developers to use the web as interface to their programs.

The package consist of the following modules:

  • http_server - a simple web server where pages can be registered
  • http - interface to access HTTP requests and respond to them
  • webfile - a simple file server

Using the HTTP server

Initialization

  • set_default_port(?Port) - sets the port to use for the web server. This must be set before the server is started. An uninstantiated variable means any free port.
  • register_page(+Page,+Handler) - registers a page and its HTTP handler. The handler will be called for each HTTP request accessing a path starting with the specified path.
  • unregister_page(+Page,+Handler) - unregisters the specified page

HTTP handler

  • http_request(+HttpRequest,+HttpResponse) - called whenever a HTTP request is made to the page registered with the handler and is responsible to respond to the request.
The handler uses the module http to access information in the request and respond to it.

The module http

  • method(+HttpRequest,-Method) - retrieves the method of the request (GET,POST,HEAD,etc)
  • page(+HttpRequest,-Page) - retrieves the page of the request i.e. the first part of the request path
  • path(+HttpRequest,-Path) - retrieves the extra path of the request i.e. the path following the page in the request path
  • header(+HttpRequest,+Name,-Value) - retrieves the value of the specified header. Fails if no such header was specified in the request.
  • query(+HttpRequest,-Query) - retrieves the query of the request. Fails if no query was specified in the request.
  • parameter(+HttpRequest,+Name,-Value) - retrieves the value of the specified parameter. Fails if no such parameter was specified in the request.
  • authorization(+HttpRequest,-AuthScheme,-Authorization) - retrieves the authorization credentials. Fails if no authorization was specified in the request.

  • send_header(+HttpResponse,+Code) - sends a HTTP header with the specified HTTP code
  • send_header(+HttpResponse,+Code,+Fields) - sends a HTTP header with the specified HTTP code and response fields
  • send_error(+HttpResponse,+ErrorCode) - sends a HTTP header with the specified HTTP error code and a suitable error message
  • send_error(+HttpResponse,+ErrorCode,+Fields) - sends a HTTP header with the specified HTTP error code, fields, and a suitable error message

    These two methods are used to send data back to the HTTP requester. A HTTP header is sent automatically with content type set to text/html if no header has yet been sent.

  • send(+HttpResponse,+Text) - sends the specified text
  • send(+HttpResponse,+Format,+Args) - formats and sends the specified text using same formatting as user:format/2

HTTP Authentication

With the HTTP server it is simple to use basic authentication to authenticate users.

  1. Extract authorization information from the request:
    http:authorization(HttpRequest,basic,BasicCookie)
  2. Decode the base64 encoded cookie:
    base64:b64decode(BasicCookie,UserInfo)
  3. Extract the user and password from the user information:
    lists:append(User,[0':|Password],UserInfo)
  4. Check that the user name and password are correct

If any of the above steps fails, the user is not authenticated and a challenge must be sent:
AuthField = field('WWW-Authenticate',"Basic realm=\"Test Authentication\""),
http:send_error(HttpResponse,unauthorized,[AuthField])

Note that basic authentication transmitts any passwords over Internet in plain text.

Webfile

Webfile uses the HTTP server to return static files from any specified file directory.

  • add_page(+Page,+Directory) - adds a directory from which files are served whenever a HTTP request is made with a path starting with the specified page.
  • remove_page(+Page) - removes the specified page and its directory

Example

The code of a simple web server example can be found here: webserver.pl


Copyright © 2001 SICS AB, All Rights Reserved.
For more information about AgentBase please email agentbase@sics.se.