HTTP Server

(Refer to this link for Z-Script version 1.3)

Web applications with embedded HTTP server are becoming popular because writing user interface in HTML is much easier than in many other languages, and the same applications may be transferred to a dedicated server with only small changes. The simple web server by Sergey Lyubka has been added to Z-Script library for using Z-Script as embedded CGI. The library has been used with the embedded web browser to deliver demo CDs of a world air CO2 database.

Function Parameter Type Remark
httpd(root[, ext]) strings Returns a HTTP server object with specified document root directory. The optional parameter should be a file extension (e.g. ".htmz"). If it is used, Z-Script code may be embedded in HTML document, just as PHP. In embedded Z-Script, HTTP functions may called through object HTTP_USER.
:version()   Returns version as string.
:register(url, func[, url, func...]) strings Associates URLs with script functions, which will be called with one argument whenever a client request the URLs. A registered URL does not have to exist and URLs with certain extension may be registered simply as, for example, ".ext"
:listen([port]) integer Starts HTTP server with the port number, which will be 80 if not given.
:get(name) string Used by a callback function to get the value of the named variable of POST/GET method.
:env(name) string Used by a callback function to get the value of the named environment variable.
:header(name) string Used by a callback function to get the value of the named HTML header variable.
:print(s[,...]) strings Used by a callback function to send contents to client.

Example

load("httpd.dll");

w3 = httpd(".", ".htmz");

w3:register("/debug.zs", "test");

w3:listen();

function test(arg)
{
    arg:print(arg:url());
}

If the above code is used to start the server, Z-Script script code may be embedded in a HTML document that has ".htmz" extension, e.g.,

...
<?zs
s = "<p>embedded zs</p>";
HTTP_USER:print(s);
?>
...