Chris@21: :- module(urispace,[init/0]). Chris@21: Chris@21: :- use_module(library('http/thread_httpd')). Chris@27: :- use_module(library('http/http_cors')). Chris@21: :- use_module(library('semweb/rdf_db')). Chris@21: :- use_module(log). Chris@21: :- use_module(config). Chris@21: :- use_module(mapping). Chris@21: Chris@27: :- set_setting_default(http:cors, [*]). Chris@27: Chris@27: Chris@21: server(Port, Options) :- Chris@21: http_server(reply,[ port(Port),timeout(20)| Options]). Chris@21: Chris@21: Chris@21: Chris@21: /** Chris@21: * Closes the servlet Chris@21: */ Chris@21: reply(Request) :- Chris@21: log:log(Request), Chris@21: member(path('/quit'), Request), !, Chris@21: format('Connection: close~n', []), Chris@21: format('Content-type: text/html~n~n', []), Chris@21: format('Bye Bye~n'). Chris@21: Chris@21: Chris@21: /** Chris@21: * Sends back 303 to RDF document describing the resource Chris@21: */ Chris@21: reply(Request) :- Chris@21: member(path(Path),Request), Chris@21: member(accept(AcceptHeader),Request), Chris@21: format(user_error,'Accept header: ~w ',[AcceptHeader]), Chris@21: %accept_rdf(AcceptHeader), Chris@21: concat_atom(PatternX,'/',Path), Chris@21: delete(PatternX,'',Pattern), Chris@21: mapping:see_other_rdf(requested_pattern(Pattern),redirect_pattern(RedirectP)), Chris@21: !, Chris@21: concat_atom(RedirectP,Redirect), Chris@21: log:log('Sending a 303 towards ~w',Redirect), Chris@27: cors_enable, Chris@21: throw(http_reply(see_other(Redirect),[])). Chris@21: Chris@21: accept_rdf('application/rdf+xml'). Chris@21: %accept_rdf('text/xml'). Chris@21: accept_rdf(AcceptHeader) :- Chris@21: sub_atom(AcceptHeader,_,_,_,'application/rdf+xml'). Chris@21: %accept_rdf(AcceptHeader) :- Chris@21: % sub_atom(AcceptHeader,_,_,_,'text/xml'). Chris@21: Chris@21: /** Chris@21: * Sends back towards the default representation of the resource Chris@21: * (usually html) Chris@21: */ Chris@21: reply(Request) :- Chris@21: member(path(Path),Request), Chris@21: concat_atom(PatternX,'/',Path), Chris@21: delete(PatternX,'',Pattern), Chris@21: mapping:see_other(requested_pattern(Pattern),redirect_pattern(RedirectP)), Chris@21: !, Chris@21: concat_atom(RedirectP,Redirect), Chris@21: log:log('Sending a 303 towards ~w',Redirect), Chris@27: cors_enable, Chris@21: throw(http_reply(see_other(Redirect),[])). Chris@21: Chris@21: init :- Chris@21: config:port(P), Chris@21: server(P,[]), Chris@21: nl, Chris@21: writeln(' - Server launched'),nl. Chris@21: Chris@21: :- Chris@21: nl,writeln('----------------'),nl, Chris@21: writeln(' - Launch the server by running ?-init.'), Chris@21: writeln(' - To change the port, change config.pl'),nl,writeln('----------------'),nl. Chris@21: Chris@21: