/* Part of ClioPatria SeRQL and SPARQL server Author: Jan Wielemaker E-mail: J.Wielemaker@cs.vu.nl WWW: http://www.swi-prolog.org Copyright (C): 2004-2010, University of Amsterdam, VU University Amsterdam This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ :- module(api_sparql, [ ]). :- use_module(user(user_db)). :- use_module(library(lists)). :- use_module(library(option)). :- use_module(library(uri)). :- use_module(library(rdf_write)). :- use_module(library(http/http_parameters)). :- use_module(library(http/http_client)). :- use_module(library(http/http_dispatch)). :- use_module(library(http/http_request_value)). :- use_module(library(http/http_cors)). :- use_module(library(http/html_write)). :- use_module(rdfql(sparql)). :- use_module(rdfql(sparql_xml_result)). :- use_module(rdfql(sparql_json_result)). :- use_module(rdfql(sparql_csv_result)). :- use_module(library(settings)). :- if(exists_source(applications(yasgui))). :- use_module(applications(yasgui)). :- endif. % We serve both `/sparql/` and `/sparql`. The first is merely for % historical reasons. Note that we cannot turn a path alias into a path % without a `/`, so we must use root(sparql) as a hack. :- http_handler(sparql(.), sparql_query, [spawn(sparql_query), id('sparql_query/')]). :- http_handler(root(sparql), sparql_query, [spawn(sparql_query), id(sparql_query)]). :- http_handler(sparql(update), sparql_update, [spawn(sparql_query), id(sparql_update)]). %% sparql_query(+Request) % % HTTP handler for SPARQL requests. Mounted the http-path % sparql(.) (by default =|/sparql/|=, see % library(http/http_path)). % % As part of a SPARQL request the user may specify the following things: % 1. `query` % The contents of the SPARQL query. % Exactly one must occur in every SPARQL query request. % 2. `default-graph` % The default graph as specified by the SPARQL dataset structure, % against which the query is evaluated. % Zero or more default graphs may be specified. % 3. `named-graph' % The named graphs as specified by the SPARQL dataset structure, % against which the query is evaluated. % Zero or more named graphs may be specified. % % There are three ways of posing a SPARQL query: % 1. An HTTP GET request where `query`, `default-graph` and `named-graph` % appear in the IRI's search string and are all subject to % IRI-encoding. Example: % `curl http://localhost:3020/sparql/?query=select%20*%20where%20%7B%20%3Fs%20%3Fp%20%3Fo%20%7D` % No Content-Type needs to be specified. % 2. An HTTP POST request where `query`, `default-graph` % and `named-graph` appear in the POST body using IRI search string % syntax and subject to IRI-encoding. Example: % `curl --data "query=select * where { ?s ?p ?o }" http://localhost:3020/sparql/` % The Content-Type must be `application/x-www-form-urlencoded`. % 3. An HTTP POST request where `default-graph` and `named-graph` appear % in the IRI's search string and are subject to IRI-encoding % and where the query appears as-is in the POST body. Example: % `curl -X POST -H "Content-Type: application/sparql-query" -d @query.sparql http://localhost:3020/sparql/` % The Content-Type must be `application/sparql-query`. sparql_query(Request) :- empty_get_request(Request), !, redirect_human_form(Request). % Perform a SPARQL query via GET. % @compat SPARQL 1.1 Protocol recommendation, section 2.1.1. sparql_query(Request) :- memberchk(method(get), Request), !, sparql_query_parameters(Request). % Perform a SPARQL query via POST with encoded parameters in body. % @compat SPARQL 1.1 Protocol recommendation, section 2.1.2. sparql_query(Request) :- memberchk(method(post), Request), memberchk(content_type(ContentType), Request), sub_atom(ContentType, 0, _, _, 'application/x-www-form-urlencoded'), !, catch(sparql_query_parameters(Request), E, sparql_query_exception(E)). % Perform a SPARQL query via POST with unencoded body. % @compat SPARQL 1.1 Protocol recommendation, section 2.1.3. sparql_query(Request) :- memberchk(method(post), Request), memberchk(content_type(ContentType), Request), sub_atom(ContentType, 0, _, _, 'application/sparql-query'), !, http_parameters(Request, [ 'default-graph-uri'(DefaultGraphs), 'named-graph-uri'(NamedGraphs), format(ReqFormat), entailment(Entailment) ], [ attribute_declarations(sparql_decl) ]), append(DefaultGraphs, NamedGraphs, Graphs), http_read_data(Request, Query, []), authorized(read(Graphs, sparql)), sparql_reply(Request, Query, Graphs, ReqFormat, Entailment). sparql_query(_) :- throw(http_reply(bad_request(format('Unrecognized SPARQL query request.', [])))). sparql_query_parameters(Request) :- http_parameters(Request, [ query(Query), 'default-graph-uri'(DefaultGraphs), 'named-graph-uri'(NamedGraphs), format(ReqFormat), entailment(Entailment) ], [ attribute_declarations(sparql_decl) ]), append(DefaultGraphs, NamedGraphs, Graphs), authorized(read(Graphs, sparql)), sparql_reply(Request, Query, Graphs, ReqFormat, Entailment). sparql_query_exception(E) :- E = error(syntax_error(illegal_uri_query),_), !, throw(http_reply(bad_request(format('Malformed search parameters.', [])))). sparql_query_exception(E) :- throw(E). %% empty_get_request(+Request) is semidet. % % True if Request is an HTTP GET request without any parameters. empty_get_request(Request) :- option(request_uri(URI), Request), uri_components(URI, Components), uri_data(search, Components, Search), var(Search), option(method(get), Request). :- if(current_predicate(has_yasgui/0)). human_form_location(HREF) :- has_yasgui, !, http_link_to_id(yasgui, [], HREF). :- endif. human_form_location(HREF) :- http_link_to_id(sparql_query_form, [], HREF). redirect_human_form(Request) :- human_form_location(HREF), reply_html_page(cliopatria(default), [ title('Redirect to SPARQL editor'), meta([ 'http-equiv'(refresh), content('5; url='+HREF) ]) ], \sparql_redirect_explanation(Request, HREF)). sparql_redirect_explanation(Request, EditorHREF) --> { option(request_uri(URI), Request) }, html({|html(URI, EditorHREF)||