/* Part of SWI-Prolog Author: Jan Wielemaker E-mail: J.Wielemaker@vu.nl WWW: http://www.swi-prolog.org Copyright (c) 2002-2017, University of Amsterdam VU University Amsterdam All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ :- module(demo_body, [ reply/1 ]). :- use_module(library(http/http_client)). :- if(exists_source(library(pce))). :- use_module(library(pce)). :- use_module(library(http/http_image)). % make XPCE generate images :- endif. :- discontiguous reply/1. /** Demo implementation of some HTTP handlers This module implements some HTTP handlers using rather low-level simple primitives. It illustrates the process that HTTP handlers are similar to CGI handlers in the sense that they need to write a CGI document to the `current_output` stream. For high level primitives: - More advanced dispatching of requests over predicates that implement them is provided by http_handler/3 from library(http/http_dispatch). - High level HTML is generated with library(http/html_write). @see http://www.swi-prolog.org/howto/http/ provides a much better tutorial introduction to the SWI-Prolog HTTP services. */ reply(_) :- flag(request, N, N+1), fail. % / % % Show available locations reply(Request) :- memberchk(path('/'), Request), !, format('Content-type: text/html~n~n', []), format('~n', []), format('

Some simple demo queries

', []), format('~n', []), format('~n', []). % /quit % % Explicitely close the connection reply(Request) :- member(path('/quit'), Request), !, format('Connection: close~n', []), format('Content-type: text/html~n~n', []), format('Bye Bye~n'). % /env % % Reply with the output of printenv (Unix systems only). reply(Request) :- member(path('/env'), Request), !, expand_file_name(~, Home), format('Content-type: text/html~n~n', []), format('~n', []), flag(request, RN, RN), format('Request ~d~n', [RN]), format('
~n', []),
    format('HOME = ~w~n~n', [Home]),
    open(pipe(printenv), read, Fd),
    copy_stream_data(Fd, current_output),
    close(Fd),
    format('
~n', []), format('~n', []). % /upload % /upload_reply % % Provide a form for uploading a file, and deal with the resulting % upload. Contributed by Nicos Angelopoulos. reply(Request) :- member(path('/upload'), Request), !, format('Content-type: text/html~n~n', []), format('~n', []), format('
~n', []), format(''), format(''), format('~n', []), format('~n', []). reply(Request) :- member(path('/upload_reply'), Request), !, format('Content-type: text/html~n~n', []), format('~n', []), format('
~n', []),
    write( req(Request) ), nl,
    http_read_data(Request, Data, []),
    write( data(Data) ), nl,
    format('
'), format('~n', []), format('~n', []). % /xml % % Return a simple formatted XML message. reply(Request) :- member(path('/xml'), Request), !, format('Content-type: text/xml~n~n', []), format('\c Jan Wielemaker Prolog users The SWI-Prolog web-server

This is the first demo of the web-server serving an XML message

', []). % /foreign % % Test emitting text using UTF-8 encoding reply(Request) :- member(path('/foreign'), Request), !, format('Content-type: text/html~n~n', []), format('\c Foreign characters

Chinese for book is \u5b66\u4e60 '). % /work % % Do a lot of work and then say 'ok'. Can be used to test % concurrent access using the multi-threaded server. reply(Request) :- member(path('/work'), Request), format(user_error, 'Starting work ...', []), forall(between(1, 10000000, _), atom_codes(_, "hello")), format(user_error, 'done!~n', []), format('Content-type: text/plain~n~n', []), format('ok~n'). % /error % % Produce an error. Load http_error to see the effect. reply(Request) :- member(path('/error'), Request), A is 1/0, format('Content-type: text/plain~n~n', []), format('A = ~w~n', [A]). % /xpce?class=box % % Make XPCE reply with a graphics image. The demo-body pce_reply/1 % is called embedded in a message to XPCE to force the XPCE % incremental garbage collector to reclaim objects created while % serving the request. pce_reply/1 replies to ?class=box using a % blue box with rounded corners. :- if(current_predicate(send/3)). reply(Request) :- member(path('/xpce'), Request), !, send(@(prolog), call, demo_body:pce_reply(Request)). pce_reply(Request) :- memberchk(search(Search), Request), memberchk(class=box, Search), new(Box, box(200,200)), send(Box, radius, 20), send(Box, fill_pattern, colour(skyblue)), reply_image(Box, []). :- endif. % ... Otherwise % % Print the request itself. reply(Request) :- format('Content-type: text/html~n~n', []), format('~n', []), format('~n'), print_request(Request), format('~n
~n'), format('~n', []). print_request([]). print_request([H|T]) :- H =.. [Name, Value], format('~w~w~n', [Name, Value]), print_request(T).