1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2013, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(yadis,
[ xrds_dom/2, % +URI, -XRDS_DOM
xrds_location/2 % +Xid, -URL
]).
:- use_module(library(http/http_open)).
:- use_module(library(xpath)).
:- use_module(library(uri)).
:- use_module(library(sgml)).
/** <module> Yadis discovery
@see http://en.wikipedia.org/wiki/Yadis
*/
:- multifile
xrds_specified_location/2.
%! xrds_dom(+Id, -XRDS_DOM) is det.
%
% True when XRDS_DOM is a parsed XML document for the given
% resource.
xrds_dom(Xid, XRDS_DOM) :-
xrds_location(Xid, XRDSLocation),
xrds_load(XRDSLocation, XRDS_DOM).
%! xid_normalize(+OpenID, -URL) is det.
%
% Translate the user-specified OpenID agent into a URL. This
% follows appendix A.1. (Normalization), RFC3986).
%
% @tbd This does not implement XRI identifiers.
xid_normalize(Xid, URL) :-
add_component(scheme, Xid, URL0, http),
add_component(path, URL0, URL, /).
add_component(Field, URL0, URL, Default) :-
uri_components(URL0, Comp),
uri_data(Field, Comp, Value),
( var(Value)
-> ( Field == scheme
-> atomic_list_concat([Default, '://', URL0], URL)
; Value = Default,
uri_components(URL, Comp)
)
; Field == path,
Value = ''
-> uri_data(path, Comp, Default, Comp2),
uri_components(URL, Comp2)
; URL = URL0
).
%! xrds_location(+Id, -XRDSLocation) is semidet.
%
% Discover the location of the XRDS document from the given Id.
xrds_location(Xid, XRDSLocation) :-
xid_normalize(Xid, URL),
( xrds_specified_location(URL, XRDSLocation)
-> XRDSLocation \== (-)
; catch(xrds_location_direct(URL, XRDSLocation),
E, yadis_failed(E))
-> true
; catch(xrds_location_html(URL, XRDSLocation),
E, yadis_failed(E))
).
yadis_failed(E) :-
( debugging(yadis)
-> print_message(warning, E)
; true
),
fail.
xrds_location_direct(URL, XRDSLocation) :-
setup_call_cleanup(
http_open(URL, In,
[ method(head),
request_header(accept='application/xrds+xml'),
header(x_xrds_location, Reply),
cert_verify_hook(ssl_verify)
]),
true,
close(In)),
Reply \== '',
!,
XRDSLocation = Reply.
xrds_location_html(URL, XRDSLocation) :-
setup_call_cleanup(
http_open(URL, In,
[ cert_verify_hook(ssl_verify)
]),
html_head_dom(In, DOM),
close(In)),
xpath(DOM, meta(@'http-equiv'=Equiv, @content), Content),
downcase_atom(Equiv, 'x-xrds-location'),
!,
XRDSLocation = Content.
%! xrds_load(+XRDSLocation, -XRDS_DOM) is det.
%
% Parse the XRDS document at XRDSLocation.
xrds_load(XRDSLocation, XRDS_DOM) :-
setup_call_cleanup(
http_open(XRDSLocation, In,
[ request_header(accept='application/xrds+xml'),
cert_verify_hook(ssl_verify)
]),
load_structure(In, XRDS_DOM,
[ dialect(xmlns),
space(remove)
]),
close(In)).
:- public ssl_verify/5.
%! ssl_verify(+SSL, +ProblemCert, +AllCerts, +FirstCert, +Error)
%
% Accept all certificates.
ssl_verify(_SSL,
_ProblemCertificate, _AllCertificates, _FirstCertificate,
_Error).
%! html_head_dom(+Stream, -HeadDOM) is semidet.
%
% Extract the HTML head content from the given stream. Does not
% parse the remainder of the document.
:- thread_local
html_head_dom/1.
html_head_dom(Stream, HeadDOM) :-
dtd(html, DTD),
new_sgml_parser(Parser, [dtd(DTD)]),
call_cleanup(
sgml_parse(Parser,
[ source(Stream),
syntax_errors(quiet),
call(begin, on_begin)
]),
free_sgml_parser(Parser)),
retract(html_head_dom(HeadDOM)).
on_begin(head, Attrs, Parser) :-
sgml_parse(Parser,
[ document(DOM),
parse(content)
]),
asserta(html_head_dom(element(head, Attrs, DOM))).
%! xrds_specified_location(+URL, -XRDSLocation) is nondet.
%
% Hook that allows for specifying locations of XRDS documents. For
% example, Google does not reply to Yadis discovery messages. We
% can fake it does using:
%
% ==
% 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.
|