forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_wrapper.pl
472 lines (405 loc) · 14 KB
/
http_wrapper.pl
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2013, University of Amsterdam
VU University 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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(httpd_wrapper,
[ http_wrapper/5, % :Goal, +In, +Out, -Conn, +Options
http_current_request/1, % -Request
http_peer/2, % +Request, -PeerIP
http_send_header/1, % +Term
http_relative_path/2, % +AbsPath, -RelPath
% Internal API
http_wrap_spawned/3, % :Goal, -Request, -Connection
http_spawned/1 % +ThreadId
]).
:- use_module(http_header).
:- use_module(http_stream).
:- use_module(http_exception).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- use_module(library(broadcast)).
:- use_module(library(dcg/basics)).
:- meta_predicate
http_wrapper(0, +, +, -, +).
:- multifile
http:request_expansion/2.
%% http_wrapper(:Goal, +In, +Out, -Close, +Options) is det.
%
% Simple wrapper to read and decode an HTTP header from `In', call
% :Goal while watching for exceptions and send the result to the
% stream `Out'.
%
% The goal is assumed to write the reply to =current_output=
% preceeded by an HTTP header, closed by a blank line. The header
% *must* contain a Content-type: <type> line. It may optionally
% contain a line =|Transfer-encoding: chunked|= to request chunked
% encoding.
%
% Options:
%
% * request(-Request)
% Return the full request to the caller
% * peer(+Peer)
% IP address of client
%
% @param Close Unified to one of =close=, =|Keep-Alife|= or
% spawned(ThreadId).
http_wrapper(Goal, In, Out, Close, Options) :-
status(Id, State0),
catch(http_read_request(In, Request0), ReqError, true),
( Request0 == end_of_file
-> Close = close,
extend_request(Options, [], _) % return request
; var(ReqError)
-> extend_request(Options, Request0, Request1),
memberchk(method(Method), Request1),
memberchk(path(Location), Request1),
cgi_open(Out, CGI, cgi_hook, [request(Request1)]),
cgi_property(CGI, id(Id)),
debug(http(request), '[~D] ~w ~w ...', [Id, Method, Location]),
handler_with_output_to(Goal, Id, Request1, CGI, Error),
cgi_close(CGI, State0, Error, Close)
; Id = 0,
( debugging(http(request))
-> print_message(warning, ReqError)
; true
),
send_error(Out, State0, ReqError, Close),
extend_request(Options, [], _)
).
status(Id, state0(Thread, CPU, Id)) :-
thread_self(Thread),
thread_cputime(CPU).
%% http_wrap_spawned(:Goal, -Request, -Close) is det.
%
% Internal use only. Helper for wrapping the handler for
% http_spawn/2.
%
% @see http_spawned/1, http_spawn/2.
http_wrap_spawned(Goal, Request, Close) :-
current_output(CGI),
cgi_property(CGI, id(Id)),
handler_with_output_to(Goal, Id, -, current_output, Error),
( retract(spawned(ThreadId))
-> Close = spawned(ThreadId),
Request = []
; cgi_property(CGI, request(Request)),
status(Id, State0),
catch(cgi_close(CGI, State0, Error, Close),
_,
Close = close)
).
:- thread_local
spawned/1.
%% http_spawned(+ThreadId)
%
% Internal use only. Indicate that the request is handed to thread
% ThreadId.
http_spawned(ThreadId) :-
assert(spawned(ThreadId)).
%% cgi_close(+CGI, +State0, +Error, -Close) is det.
%
% The wrapper has completed. Finish the CGI output. We have three
% cases:
%
% * The wrapper delegated the request to a new thread
% * The wrapper succeeded
% * The wrapper threw an error, non-200 status reply
% (e.g., =not_modified=, =moved=) or a request to reply with
% the content of a file.
%
% @error socket I/O errors.
cgi_close(_, _, _, Close) :-
retract(spawned(ThreadId)), !,
Close = spawned(ThreadId).
cgi_close(CGI, State0, ok, Close) :- !,
catch(cgi_finish(CGI, Close, Bytes), E, true),
( var(E)
-> http_done(200, ok, Bytes, State0)
; http_done(500, E, 0, State0), % TBD: amount written?
throw(E)
).
cgi_close(CGI, Id, http_reply(Status), Close) :-
cgi_property(CGI, header_codes(Text)),
Text \== [], !,
http_parse_header(Text, ExtraHdr),
cgi_property(CGI, client(Out)),
cgi_discard(CGI),
close(CGI),
send_error(Out, Id, http_reply(Status, ExtraHdr), Close).
cgi_close(CGI, Id, Error, Close) :-
cgi_property(CGI, client(Out)),
cgi_discard(CGI),
close(CGI),
send_error(Out, Id, Error, Close).
cgi_finish(CGI, Close, Bytes) :-
flush_output(CGI), % update the content-length
cgi_property(CGI, connection(Close)),
cgi_property(CGI, content_length(Bytes)),
close(CGI).
%% send_error(+Out, +State0, +Error, -Close)
%
% Send status replies and reply files. The =current_output= no
% longer points to the CGI stream, but simply to the socket that
% connects us to the client.
%
% @param State0 is start-status as returned by status/1. Used to
% find CPU usage, etc.
send_error(Out, State0, Error, Close) :-
map_exception_to_http_status(Error, Reply, HdrExtra, Context),
catch(http_reply(Reply,
Out,
[ content_length(CLen)
| HdrExtra
],
Context,
Code),
E, true),
( var(E)
-> http_done(Code, Error, CLen, State0)
; http_done(500, E, 0, State0),
throw(E) % is that wise?
),
( Error = http_reply(switching_protocols(Goal, SwitchOptions))
-> Close = switch_protocol(Goal, SwitchOptions)
; memberchk(connection(Close), HdrExtra)
-> true
; Close = close
).
%% http_done(+Code, +Status, +BytesSent, +State0) is det.
%
% Provide feedback for logging and debugging on how the request
% has been completed.
http_done(Code, Status, Bytes, state0(_Thread, CPU0, Id)) :-
thread_cputime(CPU1),
CPU is CPU1 - CPU0,
( debugging(http(request))
-> debug_request(Code, Status, Id, CPU, Bytes)
; true
),
broadcast(http(request_finished(Id, Code, Status, CPU, Bytes))).
%% handler_with_output_to(:Goal, +Id, +Request, +Output, -Status) is det.
%
% Run Goal with output redirected to Output. Unifies Status with
% =ok=, the error from catch/3 or a term error(goal_failed(Goal),
% _).
%
% @param Request The HTTP request read or '-' for a continuation
% using http_spawn/2.
handler_with_output_to(Goal, Id, Request, current_output, Status) :- !,
( catch(call_handler(Goal, Id, Request), Status, true)
-> ( var(Status)
-> Status = ok
; true
)
; Status = error(goal_failed(Goal),_)
).
handler_with_output_to(Goal, Id, Request, Output, Error) :-
current_output(OldOut),
set_output(Output),
handler_with_output_to(Goal, Id, Request, current_output, Error),
set_output(OldOut).
call_handler(Goal, _, -) :- !, % continuation through http_spawn/2
call(Goal).
call_handler(Goal, Id, Request0) :-
expand_request(Request0, Request),
current_output(CGI),
cgi_set(CGI, request(Request)),
broadcast(http(request_start(Id, Request))),
call(Goal, Request).
%% thread_cputime(-CPU) is det.
%
% CPU is the CPU time used by the calling thread.
%
% @tbd This does not work on MacOS X!
:- if(current_prolog_flag(threads, true)).
thread_cputime(CPU) :-
thread_self(Me),
thread_statistics(Me, cputime, CPU).
:- else.
thread_cputime(CPU) :-
statistics(cputime, CPU._).
:- endif.
%% cgi_hook(+Event, +CGI) is det.
%
% Hook called from the CGI processing stream. See http_stream.pl
% for details.
:- public cgi_hook/2.
cgi_hook(What, _CGI) :-
debug(http(hook), 'Running hook: ~q', [What]),
fail.
cgi_hook(header, CGI) :-
cgi_property(CGI, header_codes(HeadText)),
cgi_property(CGI, header(Header0)), % see http_send_header/1
http_parse_header(HeadText, CgiHeader0),
append(Header0, CgiHeader0, CgiHeader),
cgi_property(CGI, request(Request)),
http_update_connection(CgiHeader, Request, Connection, Header1),
http_update_transfer(Request, Header1, Transfer, Header2),
http_update_encoding(Header2, Encoding, Header),
set_stream(CGI, encoding(Encoding)),
cgi_set(CGI, connection(Connection)),
cgi_set(CGI, header(Header)),
debug(http(transfer_encoding), 'Transfer-encoding: ~w', [Transfer]),
cgi_set(CGI, transfer_encoding(Transfer)). % must be LAST
cgi_hook(send_header, CGI) :-
cgi_property(CGI, header(Header)),
debug(http(cgi), 'Header: ~q', [Header]),
cgi_property(CGI, client(Out)),
( redirect(Header, Action, RedirectHeader)
-> http_status_reply(Action, Out, RedirectHeader, _),
cgi_discard(CGI)
; cgi_property(CGI, transfer_encoding(chunked))
-> http_reply_header(Out, chunked_data, Header)
; cgi_property(CGI, content_length(Len))
-> http_reply_header(Out, cgi_data(Len), Header)
).
cgi_hook(close, _).
%% redirect(+Header, -Action, -RestHeader) is semidet.
%
% Detect the CGI =Location= and =Status= headers for formulating a
% HTTP redirect.
redirect(Header, Action, RestHeader) :-
memberchk(location(To), Header),
delete(Header, location(_), Header1),
( memberchk(status(Line), Header1)
-> delete(Header1, status(_), RestHeader),
( atom_codes(Line, Codes),
phrase(integer(Status), Codes, _)
-> true
; Status = 302
)
; RestHeader = Header1,
Status = 302
),
redirect_action(Status, To, Action).
redirect_action(301, To, moved(To)).
redirect_action(302, To, moved_temporary(To)).
redirect_action(303, To, see_other(To)).
%% http_send_header(+Header)
%
% This API provides an alternative for writing the header field as
% a CGI header. Header has the format Name(Value), as produced by
% http_read_header/2.
%
% @deprecated Use CGI lines instead
http_send_header(Header) :-
current_output(CGI),
cgi_property(CGI, header(Header0)),
cgi_set(CGI, header([Header|Header0])).
%% expand_request(+Request0, -Request)
%
% Allow for general rewrites of a request by calling
% http:request_expansion/2.
expand_request(R0, R) :-
http:request_expansion(R0, R1), % Hook
R1 \== R0, !,
expand_request(R1, R).
expand_request(R, R).
%% extend_request(+Options, +RequestIn, -Request)
%
% Merge options in the request.
extend_request([], R, R).
extend_request([request(R)|T], R0, R) :- !,
extend_request(T, R0, R).
extend_request([H|T], R0, R) :-
request_option(H), !,
extend_request(T, [H|R0], R).
extend_request([_|T], R0, R) :-
extend_request(T, R0, R).
request_option(peer(_)).
request_option(protocol(_)).
request_option(pool(_)).
%% http_current_request(-Request) is semidet.
%
% Returns the HTTP request currently being processed. Fails
% silently if there is no current request. This typically happens
% if a goal is run outside the HTTP server context.
http_current_request(Request) :-
current_output(CGI),
is_cgi_stream(CGI),
cgi_property(CGI, request(Request)).
%% http_peer(+Request, -PeerIP:atom) is semidet.
%
% True when PeerIP is the IP address of the connection peer. If
% the connection is established via a proxy that supports the
% =X-Forwarded-For= HTTP header, PeerIP is the IP address of the
% original initiater.
http_peer(Request, IP) :-
memberchk(x_forwarded_for(IP0), Request), !,
atomic_list_concat(Parts, ', ', IP0),
last(Parts, IP).
http_peer(Request, IP) :-
memberchk(peer(Peer), Request), !,
peer_to_ip(Peer, IP).
peer_to_ip(ip(A,B,C,D), IP) :-
atomic_list_concat([A,B,C,D], '.', IP).
%% http_relative_path(+AbsPath, -RelPath) is det.
%
% Convert an absolute path (without host, fragment or search) into
% a path relative to the current page. This call is intended to
% create reusable components returning relative paths for easier
% support of reverse proxies.
http_relative_path(Path, RelPath) :-
http_current_request(Request),
memberchk(path(RelTo), Request),
http_relative_path(Path, RelTo, RelPath), !.
http_relative_path(Path, Path).
http_relative_path(Path, RelTo, RelPath) :-
atomic_list_concat(PL, /, Path),
atomic_list_concat(RL, /, RelTo),
delete_common_prefix(PL, RL, PL1, PL2),
to_dot_dot(PL2, DotDot, PL1),
atomic_list_concat(DotDot, /, RelPath).
delete_common_prefix([H|T01], [H|T02], T1, T2) :- !,
delete_common_prefix(T01, T02, T1, T2).
delete_common_prefix(T1, T2, T1, T2).
to_dot_dot([], Tail, Tail).
to_dot_dot([_], Tail, Tail) :- !.
to_dot_dot([_|T0], ['..'|T], Tail) :-
to_dot_dot(T0, T, Tail).
/*******************************
* DEBUG SUPPORT *
*******************************/
%% debug_request(+Code, +Status, +Id, +CPU0, Bytes)
%
% Emit debugging info after a request completed with Status.
debug_request(Code, ok, Id, CPU, Bytes) :- !,
debug(http(request), '[~D] ~w OK (~3f seconds; ~D bytes)',
[Id, Code, CPU, Bytes]).
debug_request(Code, Status, Id, _, Bytes) :-
map_exception(Status, Reply), !,
debug(http(request), '[~D] ~w ~w; ~D bytes',
[Id, Code, Reply, Bytes]).
debug_request(Code, Except, Id, _, _) :-
Except = error(_,_), !,
message_to_string(Except, Message),
debug(http(request), '[~D] ~w ERROR: ~w',
[Id, Code, Message]).
debug_request(Code, Status, Id, _, Bytes) :-
debug(http(request), '[~D] ~w ~w; ~D bytes',
[Id, Code, Status, Bytes]).
map_exception(http_reply(Reply), Reply).
map_exception(http_reply(Reply, _), Reply).
map_exception(error(existence_error(http_location, Location), _Stack),
error(404, Location)).