Module WebServer

The WebServer module exports an interface to a simple multi-threaded web server. The Start function allows the programmer to start the web server on a specific port on the host machine where WebL is running. After starting the web server, HTML and other files will be served from the fileroot directory indicated when the server was started. The programmer may publish WebL functions to be executed when specific URL paths are requested.

For example, the following program starts the web server on port 90, and publishes a function called Echo that returns an HTML page. Afterwards, its goes to sleep while requests are serviced. If a request for the URL /bin/echo is received by the server, the Echo function is invoked.

import

WebServer;

 

WebServer_Start("c:\\InetPub\\wwwroot", 90);

 

var Echo = fun(req, res)

res.result = "<html><body>Hello!</body></html>";

end;

 

WebServer_Publish("/bin/echo", Echo);

 

while true do

Sleep(10000)

end

 

Functions may be published under any (case-sensitive) name. The web server will first consult the list of exported functions when a request is received, by comparing the path of the URL requested to each of the given names of the published functions. Should no published name match the URL path requested, the web server attempts to serve a file in the directory rooted by fileroot.

The formal arguments req and res represent respectively the request the web server received and the response the web server has to return. The idea is that the invoked function looks at the object req to figure out what to do, and modifies the object res to tell the server what to do (i.e. what data to return, etc.). For example, given the following request to the web server (running on a machine called ck.pa.dec.com):

http://ck.pa.dec.com:90/bin/echo?x=3&y=abc+def

 

the Echo function could be invoked with the following req object:

[.

contents = "",

protocol = "HTTP/1.0",

method = "GET",

uri = "/bin/echo?x=3&y=abc+def",

path = "/bin/echo",

query = "x=3&y=abc+def",

param = [. y ="abc def", x = "3" .],

header = [.

"Accept-Charset" = "iso-8859-1,*,utf-8",

Connection = "Keep-Alive",

"User-Agent" = "Mozilla/4.04[en] (WinNT; I)",

Accept = "image/gif, image/x-xbitmap,

image/jpeg, image/pjpeg, image/png, */*",

"Accept-Language" = "en",

Host = "ck.pa.dec.com:90"

.]

.]

 

and the following res object:

 

[.

result = nil,

header = [.

Server = "WebL",

Date = "Thu May 14 15:59:01 PDT 1998",

Content-Type" = "text/html"

.],

statuscode = 200,

statusmsg = "OK"

.]

 

The invoked function can now look at the fields of req to determine how to handle the request, and modify the fields of res to indicate the result to be returned (note that many of the fields are filled in to sensible values when the function is invoked). The meaning of the individual fields of the request and response object are listed in See Fields of the Request Object and See Fields of the Response Object respectively (note that all fields except statuscode are of value type string or object). The most commonly used field is param, which indicates the request parameters received.

 

Module WebServer

Function

Description

Start(fileroot: string, port: int): nil

Starts the web server on the indicated port, and prepares to server up files located at fileroot in the file system.

Stop(): nil

Stops the web server.

Publish(name: string, f: fun): nil

Publishes the function f under a name on the server. The name indicates the URL that will invoke function f. Function f has to be a function with two formal arguments (see discussion above).

 

Fields of the Request Object

Field

Description

method

HTTP method GET, POST, etc.

protocol

The HTTP protocol version.

uri

The URL of the complete request.

query

The query part of the request.

path

The path of the script requested.

contents

The contents of the request message (typically only has a value for POST methods).

param

Object with fields submitted in either a GET or POST method. In case a particular parameter is repeated in the request, the appropriate field of the the param object will be set to a list of strings, corresponding to the individual parameter values.

header

Header fields the browser sent with the HTTP request. In case a particular header field is repeated in the request, the appropriate field of the the header object will be set to a list of strings, corresponding to the individual header field values.

 

Fields of the Response Object

Field

Description

statuscode

Integer status code to be returned. The semantics of the codes are listed in the HTTP specification.

statusmsg

Status message that matches this status code.

result

The page that is to be returned to the client.

header

The header fields the server will return to the client.


Up Previous Next