Did you know ... | Search Documentation: |
CGI Support library |
This is currently a very simple library, providing support for obtaining the form-data for a CGI script:
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).
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.