1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2000-2022, University of Amsterdam 7 CWI, Amsterdam 8 SWI-Prolog Solutions b.v. 9 All rights reserved. 10 11 Redistribution and use in source and binary forms, with or without 12 modification, are permitted provided that the following conditions 13 are met: 14 15 1. Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 18 2. Redistributions in binary form must reproduce the above copyright 19 notice, this list of conditions and the following disclaimer in 20 the documentation and/or other materials provided with the 21 distribution. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35*/ 36 37:- module(cgi, 38 [ cgi_get_form/1 % -ListOf Name(Value) 39 ]). 40 41:- use_foreign_library(foreign(cgi)). 42 43/** <module> Read CGI parameters 44 45Below is a very simple CGI script that prints the passed parameters. To 46test it, compile this program using the command below, copy it to your 47cgi-bin directory (or make it otherwise known as a CGI-script) and try 48the query =|http://myhost.mydomain/cgi-bin/cgidemo?hello=world|= 49 50 == 51 % swipl -o cgidemo --goal=main --toplevel=halt -c cgidemo.pl 52 == 53 54 == 55 :- use_module(library(cgi)). 56 57 main :- 58 set_stream(current_output, encoding(utf8)), 59 cgi_get_form(Arguments), 60 format('Content-type: text/html; charset=UTF-8~n~n', []), 61 format('<html>~n', []), 62 format('<head>~n', []), 63 format('<title>Simple SWI-Prolog CGI script</title>~n', []), 64 format('</head>~n~n', []), 65 format('<body>~n', []), 66 format('<p>', []), 67 print_args(Arguments), 68 format('</body>~n</html>~n', []). 69 70 print_args([]). 71 print_args([A0|T]) :- 72 A0 =.. [Name, Value], 73 format('<b>~w</b>=<em>~w</em><br>~n', [Name, Value]), 74 print_args(T). 75 == 76*/ 77 78%! cgi_get_form(-Form) 79% 80% Decodes standard input and the environment variables to obtain a 81% list of arguments passed to the CGI script. This predicate both 82% deals with the CGI *GET* method as well as the *POST* method. If 83% the data cannot be obtained, an existence_error exception is 84% raised. 85% 86% @param Form is a list of Name(Value) terms.