This file is indexed.

/usr/share/Yap/http/xpce_httpd.pl is in yap 6.2.2-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        wielemak@science.uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2005, 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(xpce_httpd,
	  [ http_current_server/2,	% ?:Goal, ?Port
	    http_server/2		% :Goal, :Options
	  ]).
:- use_module(library(pce)).
:- use_module(http_header).
:- use_module(library(debug)).
:- use_module(http_wrapper).
:- use_module(library(lists)).

:- meta_predicate
	http_server(:, ?),
	http_server(:, ?, +).

%	@http_servers: keep track of them and avoid the servers being
%	garbage collected.

:- pce_global(@http_servers, new(chain)).

%:- debug(connection).

http_current_server(Goal, Port) :-
	object(@open_sockets),
	chain_list(@open_sockets, Sockets),
	member(Socket, Sockets),
	send(Socket, instance_of, interactive_httpd),
	get(Socket, goal, Goal),
	get(Socket, address, Port).

%%	http_server(:Goal, +Options) is det.
%
%	Start server at given or arbitrary port.

http_server(Goal, Options) :-
	select(port(Port), Options, Options1), !,
	http_server(Goal, Port, Options1).
http_server(_Goal, _Options) :-
	throw(error(existence_error(option, port), _)).

http_server(Goal, Port, _Options) :-
	strip_module(Goal, M, PlainGoal),
	(   var(Port)
	->  new(X, interactive_httpd(M:PlainGoal)),
	    get(X, address, Port)
	;   new(X, interactive_httpd(M:PlainGoal, Port))
	).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
XPCE based socket handling for   generic HTTP interface infra-structure.
This module acts as a replacement for inetd_httpd, which allows a Prolog
process to acts as an inet-driven HTTP server.

Using this module the user can easily  debug HTTP connections or provide
services while running the XPCE GUI.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- pce_begin_class(interactive_httpd, socket,
		   "Prolog HTTP debugger").

variable(allowed_hosts,	chain*,	 both, "Chain of regex with acceptable peers").
variable(goal,		prolog,	 get,  "Goal to use for processing").
variable(out_stream,	prolog,	 get,  "Stream for output").
variable(peer,		name,	 get,  "Peer connection (host only)").
variable(request,	string*, get,  "Data for first line").
variable(data,		string*, get,  "Data for POST request").
variable(chunk_data,	string*, get,  "Collect chunked input").
variable(mode,
	 {request,header,post_content_length,chunked} := request,
				 get,  "Mode of operation").

:- pce_global(@http_end_header_regex,
	      new(regex('\n\r?\n\r?'))).
:- pce_global(@http_end_line_regex,
	      new(regex('\n\r?'))).
:- pce_global(@http_has_header_regex,
	      new(regex('[^\n]*HTTP/'))).

initialise(S, Goal:prolog, Port:[int]) :->
	default(Port, 0, ThePort),	% anonymous
	send_super(S, initialise, ThePort),
	send(S, slot, goal, Goal),
	send(S, record_separator, @http_end_line_regex),
	send(S, input_message, message(@receiver, input, @arg1)),
	send(S, accept_message, message(@arg1, accepted)),
	send(S, listen, reuse := @on),
	send(@http_servers, append, S).

unlink(S) :->
	send(@http_servers, delete_all, S),
	send_super(S, unlink).

:- pce_group(connection).

accepted(S) :->
	"A new connection is established on this socket"::
	(   pce_catch_error(_, get(S, peer_name, tuple(Peer, _Port)))
	->  send(S, slot, peer, Peer),
	    send(S, authorise),
	    debug(connection, 'New connection from ~w', [Peer]),
	    pce_open(S, append, Fd),
	    send(S, slot, out_stream, Fd)
	;   debug(connection, 'Cannot get peer: closing.', []),
	    free(S)
	).

authorise(S) :->
	"See whether we will proceeed with this connection"::
	get(S, allowed_hosts, Allowed),
	(   Allowed == @nil
	->  true
	;   get(S, peer, Peer),
	    get(Allowed, find,
		message(@arg1, match, Peer),
		_)
	->  true
	;   debug(connection, 'Refused connection from ~w', [Peer]),
	    free(S),
	    fail
	).

unlink(S) :->
	(   debugging(connection)
	->  get(S, peer, Peer),
	    debug(connection, 'Closed connection from ~w', [Peer])
	;   true
	),
	(   get(S, slot, out_stream, Fd),
	    Fd \== @nil
	->  catch(close(Fd), _, true)
	;   true
	),
	send_super(S, unlink).

:- pce_group(request).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
->input collects input from  the  stream   until  an  entire  request is
complete. A request consists of one of the following:

	<Request>	::= <Action> <Path>\n
			  | <Action> <Path> HTTP/<Version>\n
			    <Header>
			    <Post Data>?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

input(S, Input:string) :->
	"Process input.  The argument is the header"::
	get(S, mode, Mode),
	(   debugging(input)
	->  send(@pce, format, 'GOT (mode %s): "%s"\n', Mode, Input)
	;   true
	),
	(   Mode == request		% got first line
	->  (   send(@http_has_header_regex, match, Input)
	    ->	send(S, slot, request, Input),
		send(S, slot, mode, header),
		send(S, record_separator, @http_end_header_regex)
	    ;	send(S, dispatch, Input)
	    )
	;   Mode == header
	->  send(Input, prepend, S?request),
	    send(S, slot, request, @nil),
	    (	send(S, collect_post_data, Input)
	    ->	true
	    ;	send(S, dispatch, Input)
	    )
	;   Mode == post_content_length
	->  send(S, slot, mode, request),
	    send(S, record_separator, @http_end_line_regex),
	    get(S, data, Header),
	    send(Header, append, Input),
	    send(Header, lock_object, @on),
	    send(S, slot, data, @nil),
	    send(S, dispatch, Header),
	    send(Header, lock_object, @off)
	;   Mode == chunked
	->  get(S, chunk_data, ChunkData),
	    (   get(S, record_separator, Bytes),
	        integer(Bytes)
	    ->  send(ChunkData, append, Input),
		send(S, record_separator, '\n')
	    ;	send(Input, prepend, '0x'),
		get(Input, value, HexAtom),
		term_to_atom(Bytes, HexAtom),
		(   Bytes == 0
		->  get(S, data, Header),
		    get(ChunkData, size, ContentLength),
		    send(@http_chunked_regex, search, Header),
		    send(@http_chunked_regex, register_value, 0, Header,
			 string('Content-Length: %d', ContentLength)),
		    send(Header, append, ChunkData),
		    send(S, slot, chunk_data, @nil),
		    send(S, slot, mode, request),
		    send(S, record_separator, @http_end_line_regex),
		    send(S, dispatch, Header)
		;   send(S, record_separator, Bytes)
		)
	    )
	).


dispatch(S, Input:string) :->
	"Hand complete input for dispatching"::
	(   debugging(dispatch)
	->  send(@pce, write_ln, Input)
	;   true
	),
	pce_open(Input, read, In),
	get(S, goal, Goal),
	get(S, out_stream, Out),
	(   catch(http_wrapper(Goal, In, Out, Close, []),
		  E, wrapper_error(E))
	->  close(In),
	    (   downcase_atom(Close, 'keep-alive')
	    ->  send(S, slot, mode, request), % prepare for next
		send(S, record_separator, @http_end_line_regex),
		send(S, slot, data, @nil)
	    ;   free(S)
	    )
	;   close(In),			% exception or failure
	    free(S)
	).

wrapper_error(Error) :-
	(   debugging(connection)
	->  print_message(error, Error)
	;   true
	),
	fail.

:- pce_group(post).


:- pce_global(@http_content_length_regex,
	      new(regex('^Content-Length:[[:blank:]]*([0-9]+)', @off))).
:- pce_global(@http_chunked_regex,
	      new(regex('^Transfer-encoding:[[:blank:]]*chunked', @off))).


collect_post_data(S, Header:string) :->
	(   send(@http_content_length_regex, search, Header)
	->  get(@http_content_length_regex, register_value, Header,
		1, int, Len),
	    debug(dispatch, '[POST] Content-length: ~w~n', [Len]),
	    send(S, slot, mode, post_content_length),
	    send(S, slot, data, Header),
	    send(S, record_separator, Len)
	;   send(@http_chunked_regex, search, Header)
	->  send(S, slot, mode, chunked),
	    send(S, slot, chunk_data, new(string)),
	    send(S, record_separator, '\n')
	).

:- pce_end_class(interactive_httpd).