annotate peel/urispace/urispace.pl @ 27:d95e683fbd35 tip

Enable CORS on urispace redirects as well
author Chris Cannam
date Tue, 20 Feb 2018 14:52:02 +0000
parents d100023520b4
children
rev   line source
Chris@7 1 :- module(urispace,[init/0]).
Chris@7 2
Chris@7 3 :- use_module(library('http/thread_httpd')).
Chris@27 4 :- use_module(library('http/http_cors')).
Chris@7 5 :- use_module(library('semweb/rdf_db')).
Chris@7 6 :- use_module(log).
Chris@7 7 :- use_module(config).
Chris@7 8 :- use_module(mapping).
Chris@7 9
Chris@27 10 :- set_setting_default(http:cors, [*]).
Chris@27 11
Chris@27 12
Chris@7 13 server(Port, Options) :-
Chris@7 14 http_server(reply,[ port(Port),timeout(20)| Options]).
Chris@7 15
Chris@7 16
Chris@7 17 /**
Chris@7 18 * Closes the servlet
Chris@7 19 */
Chris@7 20 reply(Request) :-
Chris@7 21 log:log(Request),
Chris@7 22 member(path('/quit'), Request), !,
Chris@7 23 format('Connection: close~n', []),
Chris@7 24 format('Content-type: text/html~n~n', []),
Chris@7 25 format('Bye Bye~n').
Chris@7 26
Chris@7 27
Chris@7 28 /**
Chris@7 29 * Sends back 303 to RDF document describing the resource
Chris@7 30 */
Chris@7 31 reply(Request) :-
Chris@7 32 member(path(Path),Request),
Chris@7 33 member(accept(AcceptHeader),Request),
Chris@7 34 format(user_error,'Accept header: ~w ',[AcceptHeader]),
Chris@7 35 %accept_rdf(AcceptHeader),
Chris@7 36 concat_atom(PatternX,'/',Path),
Chris@7 37 delete(PatternX,'',Pattern),
Chris@7 38 mapping:see_other_rdf(requested_pattern(Pattern),redirect_pattern(RedirectP)),
Chris@7 39 !,
Chris@7 40 concat_atom(RedirectP,Redirect),
Chris@7 41 log:log('Sending a 303 towards ~w',Redirect),
Chris@27 42 cors_enable,
Chris@7 43 throw(http_reply(see_other(Redirect),[])).
Chris@7 44
Chris@7 45 accept_rdf('application/rdf+xml').
Chris@7 46 %accept_rdf('text/xml').
Chris@7 47 accept_rdf(AcceptHeader) :-
Chris@7 48 sub_atom(AcceptHeader,_,_,_,'application/rdf+xml').
Chris@7 49 %accept_rdf(AcceptHeader) :-
Chris@7 50 % sub_atom(AcceptHeader,_,_,_,'text/xml').
Chris@7 51
Chris@7 52 /**
Chris@7 53 * Sends back towards the default representation of the resource
Chris@7 54 * (usually html)
Chris@7 55 */
Chris@7 56 reply(Request) :-
Chris@7 57 member(path(Path),Request),
Chris@7 58 concat_atom(PatternX,'/',Path),
Chris@7 59 delete(PatternX,'',Pattern),
Chris@7 60 mapping:see_other(requested_pattern(Pattern),redirect_pattern(RedirectP)),
Chris@7 61 !,
Chris@7 62 concat_atom(RedirectP,Redirect),
Chris@7 63 log:log('Sending a 303 towards ~w',Redirect),
Chris@27 64 cors_enable,
Chris@7 65 throw(http_reply(see_other(Redirect),[])).
Chris@7 66
Chris@7 67 init :-
Chris@7 68 config:port(P),
Chris@7 69 server(P,[]),
Chris@7 70 nl,
Chris@7 71 writeln(' - Server launched'),nl.
Chris@7 72
Chris@7 73 :-
Chris@7 74 nl,writeln('----------------'),nl,
Chris@7 75 writeln(' - Launch the server by running ?-init.'),
Chris@7 76 writeln(' - To change the port, change config.pl'),nl,writeln('----------------'),nl.
Chris@7 77
Chris@7 78