- Documentation
- Reference manual
- Packages
- SWI-Prolog C-library
- Introduction
- library(process): Create processes and redirect I/O
- library(filesex): Extended operations on files
- library(uid): User and group management on Unix systems
- library(syslog): Unix syslog interface
- library(socket): Network socket (TCP and UDP) library
- The stream_pool library
- library(uri): Process URIs
- CGI Support library
- Password encryption library
- library(uuid): Universally Unique Identifier (UUID) Library
- SHA* Secure Hash Algorithms
- library(md5): MD5 hashes
- library(hash_stream): Maintain a hash on a stream
- Memory files
- Time and alarm library
- library(unix): Unix specific operations
- Limiting process resources
- library(udp_broadcast): A UDP Broadcast Bridge
- library(prolog_stream): A stream with Prolog callbacks
- SWI-Prolog C-library
9 CGI Support library
This is currently a very simple library, providing support for obtaining the form-data for a CGI script:
- cgi_get_form(-Form)
- Decodes standard input and the environment variables to obtain a list of
arguments passed to the CGI script. This predicate both deals with the
CGI GET method as well as the POST method. If the data
cannot be obtained, an
existence_error
exception is raised.
Below is a very simple CGI script that prints the passed parameters.
To test it, compile this program using the command below, copy it to
your cgi-bin directory (or make it otherwise known as a CGI-script) and
make the query http://myhost.mydomain/cgi-bin/cgidemo?hello=world
% pl -o cgidemo --goal=main --toplevel=halt -c cgidemo.pl
:- use_module(library(cgi)). main :- set_stream(current_output, encoding(utf8)), cgi_get_form(Arguments), format('Content-type: text/html; charset=UTF-8~n~n', []), format('<html>~n', []), format('<head>~n', []), format('<title>Simple SWI-Prolog CGI script</title>~n', []), format('</head>~n~n', []), format('<body>~n', []), format('<p>', []), print_args(Arguments), format('</body>~n</html>~n', []). print_args([]). print_args([A0|T]) :- A0 =.. [Name, Value], format('<b>~w</b>=<em>~w</em><br>~n', [Name, Value]), print_args(T).
9.1 Some considerations
Printing an HTML document using format/2
is not a neat way of producing HTML because it is vulnerable to required
escape sequences. A high-level alternative is provided by library(http/html_write)
from the HTTP library.
The startup-time of Prolog is relatively long, in particular if the program is large. In many cases it is much better to use the SWI-Prolog HTTP server library and make the main web-server relay requests to the SWI-Prolog webserver. See the SWI-Prolog HTTP package for details.
The CGI standard is unclear about handling Unicode data. The above two declarations ensure the CGI script will send all data in UTF-8 and thus provide full support of Unicode. It is assumed that browsers generally send form-data using the same encoding as the page in which the form appears, UTF-8 or ISO Latin-1. The current version of cgi_get_form/1 assumes the CGI data is in UTF-8.