1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2013, VU University Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(yadis, 36 [ xrds_dom/2, % +URI, -XRDS_DOM 37 xrds_location/2 % +Xid, -URL 38 ]). 39:- use_module(library(http/http_open)). 40:- use_module(library(xpath)). 41:- use_module(library(uri)). 42:- use_module(library(sgml)). 43:- use_module(library(debug)).
50:- multifile
51 xrds_specified_location/2.
58xrds_dom(Xid, XRDS_DOM) :-
59 xrds_location(Xid, XRDSLocation),
60 xrds_load(XRDSLocation, XRDS_DOM).
69xid_normalize(Xid, URL) :- 70 add_component(scheme, Xid, URL0, http), 71 add_component(path, URL0, URL, /). 72 73add_component(Field, URL0, URL, Default) :- 74 uri_components(URL0, Comp), 75 uri_data(Field, Comp, Value), 76 ( var(Value) 77 -> ( Field == scheme 78 -> atomic_list_concat([Default, '://', URL0], URL) 79 ; Value = Default, 80 uri_components(URL, Comp) 81 ) 82 ; Field == path, 83 Value = '' 84 -> uri_data(path, Comp, Default, Comp2), 85 uri_components(URL, Comp2) 86 ; URL = URL0 87 ).
94xrds_location(Xid, XRDSLocation) :- 95 xid_normalize(Xid, URL), 96 ( xrds_specified_location(URL, XRDSLocation) 97 -> XRDSLocation \== (-) 98 ; catch(xrds_location_direct(URL, XRDSLocation), 99 E, yadis_failed(E)) 100 -> true 101 ; catch(xrds_location_html(URL, XRDSLocation), 102 E, yadis_failed(E)) 103 ). 104 105yadis_failed(E) :- 106 ( debugging(yadis) 107 -> print_message(warning, E) 108 ; true 109 ), 110 fail. 111 112xrds_location_direct(URL, XRDSLocation) :- 113 setup_call_cleanup( 114 http_open(URL, In, 115 [ method(head), 116 request_header(accept='application/xrds+xml'), 117 header(x_xrds_location, Reply), 118 cert_verify_hook(ssl_verify) 119 ]), 120 true, 121 close(In)), 122 Reply \== '', 123 !, 124 XRDSLocation = Reply. 125 126xrds_location_html(URL, XRDSLocation) :- 127 setup_call_cleanup( 128 http_open(URL, In, 129 [ cert_verify_hook(ssl_verify) 130 ]), 131 html_head_dom(In, DOM), 132 close(In)), 133 xpath(DOM, meta(@'http-equiv'=Equiv, @content), Content), 134 downcase_atom(Equiv, 'x-xrds-location'), 135 !, 136 XRDSLocation = Content.
142xrds_load(XRDSLocation, XRDS_DOM) :- 143 setup_call_cleanup( 144 http_open(XRDSLocation, In, 145 [ request_header(accept='application/xrds+xml'), 146 cert_verify_hook(ssl_verify) 147 ]), 148 load_structure(In, XRDS_DOM, 149 [ dialect(xmlns), 150 space(remove) 151 ]), 152 close(In)). 153 154:- public ssl_verify/5.
160ssl_verify(_SSL,
161 _ProblemCertificate, _AllCertificates, _FirstCertificate,
162 _Error).
170:- thread_local 171 html_head_dom/1. 172 173html_head_dom(Stream, HeadDOM) :- 174 dtd(html, DTD), 175 new_sgml_parser(Parser, [dtd(DTD)]), 176 call_cleanup( 177 sgml_parse(Parser, 178 [ source(Stream), 179 syntax_errors(quiet), 180 call(begin, on_begin) 181 ]), 182 free_sgml_parser(Parser)), 183 retract(html_head_dom(HeadDOM)). 184 185on_begin(head, Attrs, Parser) :- 186 sgml_parse(Parser, 187 [ document(DOM), 188 parse(content) 189 ]), 190 asserta(html_head_dom(element(head, Attrs, DOM))).
yadis:xrds_specified_location('http://google.com/', 'https://www.google.com/accounts/o8/id').
If this hook succeeds with XRDSLocation bound to -
(minus), we
assume there is no XRDS document associated to URL. This can be
used to avoid retrieving misleading or broken XRDS documents.
Yadis discovery