View source with raw comments or as raw
    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.

Load Prolog code from a web server

This module provides a hook into load_files/2 that allows loading Prolog code from HTTP and HTTPS servers. Below is an example session:

?- [library(http/http_load)].
...
true.
?- ['http://www.swi-prolog.org/download/demo/likes'].
% http://www.swi-prolog.org/download/demo/likes.pl compiled 0.00 sec, 17 clauses
true.

Warning Loading code from untrusted HTTP resources may compromise your security. */

 user:prolog_load_file(+URL, +Options)
Hook into load_files/2 that loads http:// and https:// resources directly from the web.
bug
- Loading https does not validate the certificate.
   67user:prolog_load_file(Spec, Options) :-
   68    strip_module(Spec, Module, URL),
   69    atom(URL),
   70    (   is_http_url(URL)
   71    ->  GlobalURL = URL
   72    ;   prolog_load_context(file, Parent),
   73        is_http_url(Parent),
   74        uri_resolve(URL, Parent, GlobalURL)
   75    ),
   76    ensure_extension(GlobalURL, pl, FinalURL),
   77    setup_call_cleanup(
   78        http_open(FinalURL, In,
   79                  [ cert_verify_hook(ssl_verify)
   80                  ]),
   81        load_files(Module:FinalURL, [stream(In)|Options]),
   82        close(In)).
   83
   84:- public ssl_verify/5.
 ssl_verify(+SSL, +ProblemCert, +AllCerts, +FirstCert, +Error)
Currently we accept all certificates.
   90ssl_verify(_SSL,
   91           _ProblemCertificate, _AllCertificates, _FirstCertificate,
   92           _Error).
   93
   94is_http_url(URL) :-
   95    uri_is_global(URL),
   96    uri_components(URL, Components),
   97    uri_data(scheme, Components, Scheme),
   98    nonvar(Scheme),
   99    http_scheme(Scheme).
  100
  101http_scheme(http).
  102http_scheme(https) :-
  103    catch(use_module(library(http/http_ssl_plugin)),
  104          E, (print_message(warning, E), fail)).
 ensure_extension(+URL, +Ext, -PlParts)
If the HTTP location is a plain path without extension, add the .pl extension. This ensures extension-less files appearing in file-loading directives are processed correctly.
  113ensure_extension(URL0, Ext, URL) :-
  114    uri_components(URL0, Components0),
  115    uri_data(path, Components0, Path0),
  116    ensure_path_extension(Path0, Ext, Path),
  117    (   Path0 == Path
  118    ->  URL = URL0
  119    ;   uri_data(path, Components0, Path, Components),
  120        uri_components(URL, Components)
  121    ).
  122
  123ensure_path_extension(Path0, Ext, Path) :-
  124    file_name_extension(Base, '', Path0),
  125    !,
  126    file_name_extension(Base, Ext, Path).
  127ensure_path_extension(Path, _, Path)