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