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(prolog_http_load, []). 36:- use_module(library(http/http_open)). 37:- use_module(library(uri)). 38 39:- multifile 40 user:prolog_load_file/2. 41 42/** <module> Load Prolog code from a web server 43 44This module provides a hook into load_files/2 that allows loading Prolog 45code from HTTP and HTTPS servers. Below is an example session: 46 47 == 48 ?- [library(http/http_load)]. 49 ... 50 true. 51 ?- ['http://www.swi-prolog.org/download/demo/likes']. 52 % http://www.swi-prolog.org/download/demo/likes.pl compiled 0.00 sec, 17 clauses 53 true. 54 == 55 56*Warning* Loading code from untrusted HTTP resources may compromise your 57security. 58*/ 59 60%! user:prolog_load_file(+URL, +Options) 61% 62% Hook into load_files/2 that loads =|http://|= and =|https://|= 63% resources directly from the web. Options are passed to load_files/2 64% and http_open/2. 65 66userprolog_load_file(Spec, Options) :- 67 strip_module(Spec, Module, URL), 68 atom(URL), 69 ( is_http_url(URL) 70 -> GlobalURL = URL 71 ; prolog_load_context(file, Parent), 72 is_http_url(Parent), 73 uri_resolve(URL, Parent, GlobalURL) 74 ), 75 ensure_extension(GlobalURL, pl, FinalURL), 76 setup_call_cleanup( 77 http_open(FinalURL, In, Options), 78 load_files(Module:FinalURL, [stream(In)|Options]), 79 close(In)). 80 81is_http_url(URL) :- 82 uri_is_global(URL), 83 uri_components(URL, Components), 84 uri_data(scheme, Components, Scheme), 85 nonvar(Scheme), 86 http_scheme(Scheme). 87 88http_scheme(http). 89http_scheme(https) :- 90 exists_source(library(http/http_ssl_plugin)). 91 92%! ensure_extension(+URL, +Ext, -PlParts) 93% 94% If the HTTP location is a plain path without extension, add the 95% .pl extension. This ensures extension-less files appearing in 96% file-loading directives are processed correctly. 97 98ensure_extension(URL0, Ext, URL) :- 99 uri_components(URL0, Components0), 100 uri_data(path, Components0, Path0), 101 ensure_path_extension(Path0, Ext, Path), 102 ( Path0 == Path 103 -> URL = URL0 104 ; uri_data(path, Components0, Path, Components), 105 uri_components(URL, Components) 106 ). 107 108ensure_path_extension(Path0, Ext, Path) :- 109 file_name_extension(Base, '', Path0), 110 !, 111 file_name_extension(Base, Ext, Path). 112ensure_path_extension(Path, _, Path)