File: http_log.pl

package info (click to toggle)
swi-prolog 7.2.3%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,180 kB
  • ctags: 45,684
  • sloc: ansic: 330,358; perl: 268,104; sh: 6,795; java: 4,904; makefile: 4,561; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (280 lines) | stat: -rw-r--r-- 8,409 bytes parent folder | download | duplicates (3)
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2009, 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 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(http_log,
	  [ http_log_stream/1,		% -Stream
	    http_log/2,			% +Format, +Args
	    http_log_close/1		% +Reason
	  ]).
:- use_module(library(settings)).
:- use_module(library(broadcast)).

:- setting(http:logfile, callable, 'httpd.log',
	   'File in which to log HTTP requests').

/** <module> HTTP Logging module

Simple module for logging HTTP requests to a file. Logging is enabled by
loading this file and ensure the setting   http:logfile is not the empty
atom. The default  file  for  writing   the  log  is  =|httpd.log|=. See
library(settings) for details.

The  level  of  logging  can  modified  using  the  multifile  predicate
http_log:nolog/1 to hide HTTP  request  fields   from  the  logfile  and
http_log:password_field/1   to   hide   passwords   from   HTTP   search
specifications (e.g. =|/topsecret?password=secret|=).
*/

:- multifile
	nolog/1,
	password_field/1.

% If the log settings change,  simply  close   the  log  and  it will be
% reopened with the new settings.

:- listen(settings(changed(http:logfile, _, New)),
	  http_log_close(changed(New))).
:- listen(http(Message),
	  http_message(Message)).


http_message(request_start(Id, Request)) :- !,
	http_log_stream(Stream),
	log_started(Request, Id, Stream).
http_message(request_finished(Id, Code, Status, CPU, Bytes)) :- !,
	http_log_stream(Stream),
	log_completed(Code, Status, Bytes, Id, CPU, Stream).


		 /*******************************
		 *	   LOG ACTIVITY		*
		 *******************************/

:- dynamic
	log_stream/2.			% Stream, TimeTried

%%	http_log_stream(-Stream) is semidet.
%
%	True when Stream is a stream to  the opened HTTP log file. Opens
%	the log file in =append= mode if the   file is not yet open. The
%	log file is determined  from   the  setting =|http:logfile|=. If
%	this setting is set  to  the   empty  atom  (''), this predicate
%	fails.
%
%	If  a  file  error  is  encountered,   this  is  reported  using
%	print_message/2, after which this predicate silently fails.

http_log_stream(Stream) :-
	log_stream(Stream, _Opened), !,
	Stream \== [].
http_log_stream([]) :-
	setting(http:logfile, ''), !,
	get_time(Now),
	assert(log_stream([], Now)).
http_log_stream(Stream) :-
	setting(http:logfile, Term),
	catch(absolute_file_name(Term, File,
				 [ access(append)
				 ]), E, open_error(E)),
	with_mutex(http_log,
		   (   catch(open(File, append, Stream,
				  [ close_on_abort(false),
				    encoding(utf8),
				    buffer(line)
				  ]), E, open_error(E)),
		       get_time(Time),
		       format(Stream,
			      'server(started, ~0f).~n',
			      [ Time ]),
		       assert(log_stream(Stream, Time)),
		       at_halt(close_log(stopped))
		   )).

open_error(E) :-
	print_message(error, E),
	get_time(Now),
	assert(log_stream([], Now)),
	fail.


%%	http_log_close(+Reason) is det.
%
%	If there is a currently open HTTP logfile, close it after adding
%	a term server(Reason, Time).  to  the   logfile.  This  call  is
%	intended for cooperation with the Unix logrotate facility
%	using the following schema:
%
%	    * Move logfile (the HTTP server keeps writing to the moved
%	    file)
%	    * Inform the server using an HTTP request that calls
%	    http_log_close/1
%	    * Compress the moved logfile
%
%	@author Suggested by Jacco van Ossenbruggen

http_log_close(Reason) :-
	with_mutex(http_log, close_log(Reason)).

close_log(Reason) :-
	retract(log_stream(Stream, _Opened)), !,
	(   is_stream(Stream)
	->  get_time(Time),
	    format(Stream, 'server(~q, ~0f).~n', [ Reason, Time ]),
	    close(Stream)
	;   true
	).
close_log(_).

%%	http_log(+Format, +Args) is det.
%
%	Write message from Format and Args   to log-stream. See format/2
%	for details. Succeed without side  effects   if  logging  is not
%	enabled.

http_log(Format, Args) :-
	(   http_log_stream(Stream)
	->  format(Stream, Format, Args)
	;   true
	).


%%	log_started(+Request, +Id, +Stream) is det.
%
%	Write log message that Request was started to Stream.
%
%	@param	Filled with sequence identifier for the request

log_started(Request, Id, Stream) :-
	get_time(Now),
	log_request(Request, LogRequest),
	format_time(string(HDate), '%+', Now),
	format(Stream,
	       '/*~s*/ request(~q, ~3f, ~q).~n',
	       [HDate, Id, Now, LogRequest]).

%%	log_request(+Request, -Log)
%
%	Remove passwords from the request to avoid sending them to the
%	logfiles.

log_request([], []).
log_request([search(Search0)|T0], [search(Search)|T]) :- !,
	mask_passwords(Search0, Search),
	log_request(T0, T).
log_request([H|T0], T) :-
	nolog(H), !,
	log_request(T0, T).
log_request([H|T0], [H|T]) :-
	log_request(T0, T).

mask_passwords([], []).
mask_passwords([Name=_|T0], [Name=xxx|T]) :-
	password_field(Name), !,
	mask_passwords(T0, T).
mask_passwords([H|T0], [H|T]) :-
	mask_passwords(T0, T).

%%	password_field(+Field) is semidet.
%
%	Multifile predicate that can be defined to hide passwords from
%	the logfile.

password_field(password).
password_field(pwd0).
password_field(pwd1).
password_field(pwd2).


%%	nolog(+HTTPField)
%
%	Multifile  predicate  that  can  be   defined  to  hide  request
%	parameters from the request logfile.

nolog(input(_)).
nolog(accept(_)).
nolog(accept_language(_)).
nolog(accept_encoding(_)).
nolog(accept_charset(_)).
nolog(pool(_)).
nolog(protocol(_)).
nolog(referer(R)) :-
	sub_atom(R, _, _, _, password), !.

%%	log_completed(+Code, +Status, +Bytes, +Id, +CPU, +Stream) is det.
%
%	Write log message to Stream from a call_cleanup/3 call.
%
%	@param Status	2nd argument of call_cleanup/3
%	@param Id	Term identifying the completed request
%	@param CPU0	CPU time at time of entrance
%	@param Stream	Stream to write to (normally from http_log_stream/1).

log_completed(Code, Status, Bytes, Id, CPU, Stream) :-
	is_stream(Stream),
	log_check_deleted(Stream), !,
	log(Code, Status, Bytes, Id, CPU, Stream).
log_completed(Code, Status, Bytes, Id, CPU0, _) :-
	http_log_stream(Stream), !,	% Logfile has changed!
	log_completed(Code, Status, Bytes, Id, CPU0, Stream).
log_completed(_,_,_,_,_,_).


%%	log_check_deleted(+Stream) is semidet.
%
%	If the link-count of the stream has   dropped  to zero, the file
%	has been deleted/moved. In this case the  log file is closed and
%	log_check_deleted/6 will open a  new   one.  This  provides some
%	support for cleaning up the logfile   without  shutting down the
%	server.
%
%	@see logrotate(1) to manage logfiles on Unix systems.

log_check_deleted(Stream) :-
	stream_property(Stream, nlink(Links)),
	Links == 0, !,
	http_log_close(log_file_deleted),
	fail.
log_check_deleted(_).


log(Code, ok, Bytes, Id, CPU, Stream) :- !,
	format(Stream, 'completed(~q, ~q, ~q, ~q, ok).~n',
	       [ Id, CPU, Bytes, Code ]).
log(Code, Status, Bytes, Id, CPU, Stream) :-
	(   map_exception(Status, Term)
	->  true
	;   message_to_string(Status, String),
	    Term = error(String)
	),
	format(Stream, 'completed(~q, ~q, ~q, ~q, ~q).~n',
	       [ Id, CPU, Bytes, Code, Term ]).

map_exception(http_reply(Reply), Reply).
map_exception(error(existence_error(http_location, Location), _Stack),
	      error(404, Location)).