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