|Did you know ...||Search Documentation:|
|http_cgi.pl -- Run CGI scripts from the SWI-Prolog web-server|
The Prolog HTTP server is primarily designed to be able to handle HTTP requests from a running Prolog process, which avoids the Prolog startup time and, at least as interesting, allows you to keep state in the Prolog database. It is not designed to run as a generic web server. There are tools that are much better for that job. Nevertheless, it is useful to host a complete server in one process, mainly to simplify deployment. For this reason, the SWI-Prolog HTTP server provides libraries to serve static files (http_reply_file/3, http_reply_from_files/3) and this library, which allows executing CGI scripts.
A sensible alternative setup for a mixed server is to use a normal server such as Apache as main server, serving files, CGI scripts, modules, etc., and use Apache's proxy facilities to host a subdirectory of the server using a Prolog server. That approach is most likely more efficient for production environments, but harder to setup for development purposes.
This module provides two interfaces:
myscript.phpfrom the location =/myscript/=. Note that this requires the commandline version of PHP to be installed as
phpin the current
:- http_handler(root(myscript), http_run_cgi(path(php), [argv('myscript.php')]), ).
cgi_binfor absolute_file_name/3. If this path is present, calls to /cgi-bin/... are translated into calling the script. For example, if programs in the directory
cgi-binmust be accessible as CGI services, add a rule
:- multifile user:file_search_path/2. user:file_search_path(cgi_bin, 'cgi-bin').
path_infoin Request and execute the script using http_run_cgi/3. This library installs one handler using:
:- http_handler(root('cgi-bin'), http_run_cgi(cgi_bin, ), [prefix, spawn()]).
:- multifile http_cgi:environment/2. http_cgi:environment('SERVER_ADMIN', 'email@example.com').