<commit_before>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2007, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/


:- module(http_open,
	  [ http_open/3			% +URL, -Stream, +Options
	  ]).
:- use_module(library(url)).
:- use_module(library(readutil)).
:- use_module(library(socket)).
:- use_module(library(lists)).
:- use_module(library(option)).
:- use_module(library(error)).

user_agent('SWI-Prolog (http://www.swi-prolog.org)').

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This library provides a simple-minded   light-weight HTTP client library
to get the data from an  URL   using  the GET-method. More advanced HTTP
client support is provided by http_client.pl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

%%	http_open(+Url, -Stream, +Options) is det.
%	
%	Open a HTTP url as a (binary) stream. Uses HTTP 1.0 protocol
%	revision to deal with virtual hosts and to be able to interpret
%	the header.  Supported options:
%	
%		* size(-Size)
%		  Return size of the resource
%		* header(Name, -Atom)
%		  Return headerfield as atom
%		* timeout(+Timeout)
%		  Raise exception on timeout
%		* proxy(+Host, +Port)
%		  Use an HTTP proxy server
%		* user_agent(+Agent)
%		  User agent for identifying
%		* request_header(Name=Value)
%		  Extra header field
%		* method(+Method)
%		  One of =get= (default) or =head=
%		* final_url(-URL)
%		  Unified with the final URL to deal with
%		  redirections.
%	
%	@error	existence_error(url, Id)

http_open(URL, Stream, Options) :-
	atom(URL), !,
	parse_url(URL, Parts),
	http_open(Parts, Stream, Options).
http_open(Parts, Stream, Options0) :-
	Options = [visited(Parts)|Options0],
	memberchk(proxy(Host, ProxyPort), Options), !,
	parse_url(Location, Parts),
	open_socket(Host:ProxyPort, In, Out, Options),
	option(port(Port), Parts, 80),
	host_and_port(Host, Port, HostPort),
	send_rec_header(Out, In, Stream, HostPort, Location, Parts, Options),
	return_final_url(Options).
http_open(Parts, Stream, Options0) :-
	Options = [visited(Parts)|Options0],
	memberchk(host(Host), Parts),
	option(port(Port), Parts, 80),
	http_location(Parts, Location),
	open_socket(Host:Port, In, Out, Options),
	host_and_port(Host, Port, HostPort),
	send_rec_header(Out, In, Stream, HostPort, Location, Parts, Options),
	return_final_url(Options).

host_and_port(Host, 80, Host) :- !.
host_and_port(Host, Port, Host:Port).

%%	send_rec_header(+Out, +In, -InStream,
%%			+Host, +Location, +Parts, +Options) is det.
%
%	Send header to Out and process reply.  If there is an error or
%	failure, close In and Out and return the error or failure.

send_rec_header(Out, In, Stream, Host, Location, Parts, Options) :-
	(   catch(guarded_send_rec_header(Out, In, Stream,
					  Host, Location, Parts, Options),
		  E, true)
	->  (   var(E)
	    ->	close(Out)
	    ;	force_close(In, Out),
		throw(E)
	    )
	;   force_close(In, Out),
	    fail
	).

guarded_send_rec_header(Out, In, Stream, Host, Location, Parts, Options) :-
	user_agent(Agent, Options),
	method(Options, MNAME),
	format(Out,
	       '~w ~w HTTP/1.1\r\n\
	       Host: ~w\r\n\
	       User-Agent: ~w\r\n\
	       Connection: close\r\n',
	       [MNAME, Location, Host, Agent]),
	x_headers(Options, Out),
	format(Out, '\r\n', []),
	flush_output(Out),
					% read the reply header
	read_header(In, Code, Comment, Lines),
	do_open(Code, Comment, Lines, Options, Parts, In, Stream).

force_close(S1, S2) :-
	close(S1, [force(true)]),
	close(S2, [force(true)]).

method(Options, MNAME) :-
	option(method(M), Options, get),
	must_be(oneof([get,head]), M),
	(   M == get
	->  MNAME = 'GET'
	;   MNAME = 'HEAD'
	).
<commit_msg>* 1.1 --> 1.0 (no chunked support)<commit_after>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2007, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/


:- module(http_open,
	  [ http_open/3			% +URL, -Stream, +Options
	  ]).
:- use_module(library(url)).
:- use_module(library(readutil)).
:- use_module(library(socket)).
:- use_module(library(lists)).
:- use_module(library(option)).
:- use_module(library(error)).

user_agent('SWI-Prolog (http://www.swi-prolog.org)').

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This library provides a simple-minded   light-weight HTTP client library
to get the data from an  URL   using  the GET-method. More advanced HTTP
client support is provided by http_client.pl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

%%	http_open(+Url, -Stream, +Options) is det.
%	
%	Open a HTTP url as a (binary) stream. Uses HTTP 1.0 protocol
%	revision to deal with virtual hosts and to be able to interpret
%	the header.  Supported options:
%	
%		* size(-Size)
%		  Return size of the resource
%		* header(Name, -Atom)
%		  Return headerfield as atom
%		* timeout(+Timeout)
%		  Raise exception on timeout
%		* proxy(+Host, +Port)
%		  Use an HTTP proxy server
%		* user_agent(+Agent)
%		  User agent for identifying
%		* request_header(Name=Value)
%		  Extra header field
%		* method(+Method)
%		  One of =get= (default) or =head=
%		* final_url(-URL)
%		  Unified with the final URL to deal with
%		  redirections.
%	
%	@error	existence_error(url, Id)

http_open(URL, Stream, Options) :-
	atom(URL), !,
	parse_url(URL, Parts),
	http_open(Parts, Stream, Options).
http_open(Parts, Stream, Options0) :-
	Options = [visited(Parts)|Options0],
	memberchk(proxy(Host, ProxyPort), Options), !,
	parse_url(Location, Parts),
	open_socket(Host:ProxyPort, In, Out, Options),
	option(port(Port), Parts, 80),
	host_and_port(Host, Port, HostPort),
	send_rec_header(Out, In, Stream, HostPort, Location, Parts, Options),
	return_final_url(Options).
http_open(Parts, Stream, Options0) :-
	Options = [visited(Parts)|Options0],
	memberchk(host(Host), Parts),
	option(port(Port), Parts, 80),
	http_location(Parts, Location),
	open_socket(Host:Port, In, Out, Options),
	host_and_port(Host, Port, HostPort),
	send_rec_header(Out, In, Stream, HostPort, Location, Parts, Options),
	return_final_url(Options).

host_and_port(Host, 80, Host) :- !.
host_and_port(Host, Port, Host:Port).

%%	send_rec_header(+Out, +In, -InStream,
%%			+Host, +Location, +Parts, +Options) is det.
%
%	Send header to Out and process reply.  If there is an error or
%	failure, close In and Out and return the error or failure.

send_rec_header(Out, In, Stream, Host, Location, Parts, Options) :-
	(   catch(guarded_send_rec_header(Out, In, Stream,
					  Host, Location, Parts, Options),
		  E, true)
	->  (   var(E)
	    ->	close(Out)
	    ;	force_close(In, Out),
		throw(E)
	    )
	;   force_close(In, Out),
	    fail
	).

guarded_send_rec_header(Out, In, Stream, Host, Location, Parts, Options) :-
	user_agent(Agent, Options),
	method(Options, MNAME),
	format(Out,
	       '~w ~w HTTP/1.0\r\n\
	       Host: ~w\r\n\
	       User-Agent: ~w\r\n\
	       Connection: close\r\n',
	       [MNAME, Location, Host, Agent]),
	x_headers(Options, Out),
	format(Out, '\r\n', []),
	flush_output(Out),
					% read the reply header
	read_header(In, Code, Comment, Lines),
	do_open(Code, Comment, Lines, Options, Parts, In, Stream).

force_close(S1, S2) :-
	close(S1, [force(true)]),
	close(S2, [force(true)]).

method(Options, MNAME) :-
	option(method(M), Options, get),
	must_be(oneof([get,head]), M),
	(   M == get
	->  MNAME = 'GET'
	;   MNAME = 'HEAD'
	).

{'fraction_non_alphanumeric': 0.13417747370570815, 'fraction_numerical': 0.008169100377820893, 'mean_word_length': 3.216099870856651, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1330403', 'n_tokens_mistral': 3474, 'n_tokens_neox': 3284, 'n_words': 1029}