annotate jamendo/sparql-archived/SeRQL/http_sparql.pl @ 27:d95e683fbd35 tip

Enable CORS on urispace redirects as well
author Chris Cannam
date Tue, 20 Feb 2018 14:52:02 +0000
parents df9685986338
children
rev   line source
Chris@0 1 /* $Id$
Chris@0 2
Chris@0 3 Part of SWI-Prolog
Chris@0 4
Chris@0 5 Author: Jan Wielemaker
Chris@0 6 E-mail: wielemak@science.uva.nl
Chris@0 7 WWW: http://www.swi-prolog.org
Chris@0 8 Copyright (C): 2004-2006, University of Amsterdam
Chris@0 9
Chris@0 10 This program is free software; you can redistribute it and/or
Chris@0 11 modify it under the terms of the GNU General Public License
Chris@0 12 as published by the Free Software Foundation; either version 2
Chris@0 13 of the License, or (at your option) any later version.
Chris@0 14
Chris@0 15 This program is distributed in the hope that it will be useful,
Chris@0 16 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 18 GNU General Public License for more details.
Chris@0 19
Chris@0 20 You should have received a copy of the GNU Lesser General Public
Chris@0 21 License along with this library; if not, write to the Free Software
Chris@0 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Chris@0 23
Chris@0 24 As a special exception, if you link this library with other files,
Chris@0 25 compiled with a Free Software compiler, to produce an executable, this
Chris@0 26 library does not by itself cause the resulting executable to be covered
Chris@0 27 by the GNU General Public License. This exception does not however
Chris@0 28 invalidate any other reasons why the executable file might be covered by
Chris@0 29 the GNU General Public License.
Chris@0 30 */
Chris@0 31
Chris@0 32 :- module(http_sparql,
Chris@0 33 [ sparql_reply/1
Chris@0 34 ]).
Chris@0 35 :- use_module(user_db).
Chris@0 36 :- use_module(library(lists)).
Chris@0 37 :- use_module(library(rdf_write)).
Chris@0 38 :- use_module(library('http/http_parameters')).
Chris@0 39 :- use_module(library('http/http_dispatch')).
Chris@0 40 :- use_module(sparql).
Chris@0 41 :- use_module(sparql_xml_result).
Chris@0 42
Chris@0 43 :- http_handler('/sparql/', sparql_reply, []).
Chris@0 44
Chris@0 45 %% sparql_reply(+Request)
Chris@0 46 %
Chris@0 47 % HTTP handler for SPARQL requests. Typically mounted on
Chris@0 48 % =|/sparql/|=
Chris@0 49
Chris@0 50 sparql_reply(Request) :-
Chris@0 51 http_parameters(Request,
Chris@0 52 [ query(Query),
Chris@0 53 'default-graph-uri'(DefaultGraphs),
Chris@0 54 'named-graph-uri'(NamedGraphs)
Chris@0 55 ],
Chris@0 56 [ attribute_declarations(sparql_decl)
Chris@0 57 ]),
Chris@0 58 append(DefaultGraphs, NamedGraphs, AllGraphs),
Chris@0 59 authorized(read(AllGraphs, query)),
Chris@0 60 statistics(cputime, CPU0),
Chris@0 61 sparql_compile(Query, Compiled,
Chris@0 62 [ type(Type),
Chris@0 63 ordered(Ordered),
Chris@0 64 distinct(Distinct)
Chris@0 65 %,entailment(lit)
Chris@0 66 ]),
Chris@0 67 findall(R, sparql_run(Compiled, R), Rows),
Chris@0 68 statistics(cputime, CPU1),
Chris@0 69 CPU is CPU1 - CPU0,
Chris@0 70 write_result(Type, Rows,
Chris@0 71 [ cputime(CPU),
Chris@0 72 ordered(Ordered),
Chris@0 73 distinct(Distinct)
Chris@0 74 ]).
Chris@0 75
Chris@0 76 write_result(ask, [True], Options) :- !,
Chris@0 77 format('Transfer-encoding: chunked~n'),
Chris@0 78 format('Content-type: application/sparql-results+xml; charset=UTF-8~n~n'),
Chris@0 79 sparql_write_xml_result(current_output, ask(True), Options).
Chris@0 80 write_result(select(VarNames), Rows, Options) :- !,
Chris@0 81 format('Transfer-encoding: chunked~n'),
Chris@0 82 format('Content-type: application/sparql-results+xml; charset=UTF-8~n~n'),
Chris@0 83 sparql_write_xml_result(current_output, select(VarNames, Rows), Options).
Chris@0 84 write_result(_, RDF, _Options) :-
Chris@0 85 format('Content-type: application/rdf+xml; charset=UTF-8~n~n'),
Chris@0 86 rdf_write_xml(current_output, RDF).
Chris@0 87
Chris@0 88
Chris@0 89 %% sparql_decl(+OptionName, -Options)
Chris@0 90 %
Chris@0 91 % Default options for specified attribute names. See
Chris@0 92 % http_parameters/3.
Chris@0 93
Chris@0 94 sparql_decl(query,
Chris@0 95 []).
Chris@0 96 sparql_decl('default-graph-uri',
Chris@0 97 [ zero_or_more
Chris@0 98 ]).
Chris@0 99 sparql_decl('named-graph-uri',
Chris@0 100 [ zero_or_more
Chris@0 101 ]).