comparison chord/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 7902f428bc60
children
comparison
equal deleted inserted replaced
26:a93fe6c02d20 27:d95e683fbd35
1 :- module(urispace,[init/0]). 1 :- module(urispace,[init/0]).
2 2
3
4 :- use_module(library('http/thread_httpd')). 3 :- use_module(library('http/thread_httpd')).
4 :- use_module(library('http/http_cors')).
5 :- use_module(library('semweb/rdf_db')). 5 :- use_module(library('semweb/rdf_db')).
6 :- use_module(log). 6 :- use_module(log).
7 :- use_module(chord_parser). 7 :- use_module(chord_parser).
8 8
9 :- style_check(-discontiguous). 9 :- style_check(-discontiguous).
10 10
11 :- set_setting_default(http:cors, [*]).
12
13
11 server(Port, Options) :- 14 server(Port, Options) :-
12 http_server(reply,[ port(Port),timeout(20)| Options]). 15 http_server(reply,[ port(Port),timeout(20)| Options]).
13
14
15 16
16 17
17 namespace('http://dbtune.org/chord'). 18 namespace('http://dbtune.org/chord').
18 19
19 /** 20 /**
21 */ 22 */
22 reply(Request) :- 23 reply(Request) :-
23 member(path(Path),Request), 24 member(path(Path),Request),
24 atom_concat(SymbolT,'.rdf',Path), 25 atom_concat(SymbolT,'.rdf',Path),
25 atom_concat('/',Symbol,SymbolT), 26 atom_concat('/',Symbol,SymbolT),
27 cors_enable,
26 !, 28 !,
27 (parse(Symbol,RDF) -> 29 (parse(Symbol,RDF) ->
28 ( 30 (
29 current_output(S), 31 current_output(S),
30 set_stream(S,encoding(utf8)), 32 set_stream(S,encoding(utf8)),
42 member(accept(AcceptHeader),Request), 44 member(accept(AcceptHeader),Request),
43 log:log('Accept header: ~w ',[AcceptHeader]), 45 log:log('Accept header: ~w ',[AcceptHeader]),
44 accept_png(AcceptHeader), 46 accept_png(AcceptHeader),
45 !, 47 !,
46 atom_concat('/',Symbol,Path), 48 atom_concat('/',Symbol,Path),
49 cors_enable,
47 (parse(Symbol,RDF) -> 50 (parse(Symbol,RDF) ->
48 ( 51 (
49 member(rdf(_,'http://xmlns.com/foaf/0.1/depiction',Pic),RDF), 52 member(rdf(_,'http://xmlns.com/foaf/0.1/depiction',Pic),RDF),
50 throw(http_reply(see_other(Pic),[]))); 53 throw(http_reply(see_other(Pic),[])));
51 ( 54 (
64 accept_rdf(AcceptHeader), 67 accept_rdf(AcceptHeader),
65 !, 68 !,
66 namespace(NS), 69 namespace(NS),
67 format(atom(Redirect),'~w~w.rdf',[NS,Path]), 70 format(atom(Redirect),'~w~w.rdf',[NS,Path]),
68 log:log('Sending a 303 towards ~w',Redirect), 71 log:log('Sending a 303 towards ~w',Redirect),
72 cors_enable,
69 throw(http_reply(see_other(Redirect),[])). 73 throw(http_reply(see_other(Redirect),[])).
70 74
71 accept_rdf('application/rdf+xml'). 75 accept_rdf('application/rdf+xml').
72 accept_rdf('text/xml'). 76 accept_rdf('text/xml').
73 accept_rdf(AcceptHeader) :- 77 accept_rdf(AcceptHeader) :-
88 member(path(Path),Request), 92 member(path(Path),Request),
89 !, 93 !,
90 html(Html),namespace(Namespace), 94 html(Html),namespace(Namespace),
91 format(atom(Redirect),'~w~w~w',[Html,Namespace,Path]), 95 format(atom(Redirect),'~w~w~w',[Html,Namespace,Path]),
92 log:log('Sending a 303 towards ~w',Redirect), 96 log:log('Sending a 303 towards ~w',Redirect),
97 cors_enable,
93 throw(http_reply(see_other(Redirect),[])). 98 throw(http_reply(see_other(Redirect),[])).
94 99
95 port(1111). 100 port(1111).
96 init :- 101 init :-
97 port(P), 102 port(P),