/* Part of SWI-Prolog Author: Jan Wielemaker E-mail: J.Wielemaker@vu.nl WWW: http://www.swi-prolog.org Copyright (c) 2002-2023, University of Amsterdam VU University Amsterdam CWI, Amsterdam SWI-Prolog Solutions b.v. 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(http_open, [ http_open/3, % +URL, -Stream, +Options http_set_authorization/2, % +URL, +Authorization http_close_keep_alive/1 % +Address ]). :- autoload(library(aggregate),[aggregate_all/3]). :- autoload(library(apply),[foldl/4,include/3]). :- autoload(library(base64),[base64/3]). :- autoload(library(debug),[debug/3,debugging/1]). :- autoload(library(error), [ domain_error/2, must_be/2, existence_error/2, instantiation_error/1 ]). :- autoload(library(lists),[last/2,member/2]). :- autoload(library(option), [ meta_options/3, option/2, select_option/4, merge_options/3, option/3, select_option/3 ]). :- autoload(library(readutil),[read_line_to_codes/2]). :- autoload(library(uri), [ uri_resolve/3, uri_components/2, uri_data/3, uri_authority_components/2, uri_authority_data/3, uri_encoded/3, uri_query_components/2, uri_is_global/1 ]). :- autoload(library(http/http_header), [ http_parse_header/2, http_post_data/3 ]). :- autoload(library(http/http_stream),[stream_range_open/3]). :- if(exists_source(library(ssl))). :- autoload(library(ssl), [ssl_upgrade_legacy_options/2]). :- endif. :- use_module(library(socket)). /** HTTP client library This library defines http_open/3, which opens an URL as a Prolog stream. The functionality of the library can be extended by loading two additional modules that act as plugins: * library(http/http_ssl_plugin) Loading this library causes http_open/3 to handle HTTPS connections. Relevant options for SSL certificate handling are handed to ssl_context/3. This plugin is loaded automatically if the scheme `https` is requested using a default SSL context. See the plugin for additional information regarding security. * library(zlib) Loading this library supports the `gzip` transfer encoding. This plugin is lazily loaded if a connection is opened that claims this transfer encoding. * library(http/http_cookie) Loading this library adds tracking cookies to http_open/3. Returned cookies are collected in the Prolog database and supplied for subsequent requests. * library(http/http_stream) This library adds support for _chunked_ encoding. It is lazily loaded if the server sends a ``Transfer-encoding: chunked`` header. Here is a simple example to fetch a web-page: ``` ?- http_open('http://www.google.com/search?q=prolog', In, []), copy_stream_data(In, user_output), close(In). prolog - Google Search