aboutsummaryrefslogtreecommitdiff
path: root/www/publicfile/files/publicfile-0.52-commonlog-2.patch
blob: ed14642b22e706620213fe7f2a0971497061a92b (plain) (blame)
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
diff -rNC3 publicfile-0.52.orig/Makefile publicfile-0.52/Makefile
*** publicfile-0.52.orig/Makefile	Tue Nov  9 08:23:46 1999
--- publicfile-0.52/Makefile	Fri Nov  9 10:09:45 2001
***************
*** 450,460 ****
  stralloc.a: \
  makelib stralloc_cat.o stralloc_catb.o stralloc_cats.o \
  stralloc_copy.o stralloc_eady.o stralloc_opyb.o stralloc_opys.o \
! stralloc_pend.o stralloc_num.o
  	./makelib stralloc.a stralloc_cat.o stralloc_catb.o \
  	stralloc_cats.o stralloc_copy.o stralloc_eady.o \
  	stralloc_opyb.o stralloc_opys.o stralloc_pend.o \
! 	stralloc_num.o
  
  stralloc_cat.o: \
  compile stralloc_cat.c byte.h stralloc.h gen_alloc.h
--- 450,460 ----
  stralloc.a: \
  makelib stralloc_cat.o stralloc_catb.o stralloc_cats.o \
  stralloc_copy.o stralloc_eady.o stralloc_opyb.o stralloc_opys.o \
! stralloc_pend.o stralloc_num.o stralloc_opytrim.o
  	./makelib stralloc.a stralloc_cat.o stralloc_catb.o \
  	stralloc_cats.o stralloc_copy.o stralloc_eady.o \
  	stralloc_opyb.o stralloc_opys.o stralloc_pend.o \
! 	stralloc_num.o stralloc_opytrim.o
  
  stralloc_cat.o: \
  compile stralloc_cat.c byte.h stralloc.h gen_alloc.h
***************
*** 484,489 ****
--- 484,493 ----
  stralloc_opyb.o: \
  compile stralloc_opyb.c stralloc.h gen_alloc.h byte.h
  	./compile stralloc_opyb.c
+ 
+ stralloc_opytrim.o: \
+ compile stralloc_opytrim.c stralloc.h
+ 	./compile stralloc_opytrim.c
  
  stralloc_opys.o: \
  compile stralloc_opys.c byte.h str.h stralloc.h gen_alloc.h
diff -rNC3 publicfile-0.52.orig/README.log_combined publicfile-0.52/README.log_combined
*** publicfile-0.52.orig/README.log_combined	Thu Jan  1 01:00:00 1970
--- publicfile-0.52/README.log_combined	Fri Nov  9 10:09:45 2001
***************
*** 0 ****
--- 1,36 ----
+ This version of publicfile has been patched to write *additional* log 
+ information to STDERR. This additional information conforms to Apache's
+ and Netscape's "common" or "combined" log formats, i. e. a line may look
+ like this (common log format):
+ 
+ 127.0.0.1 - - [23/Oct/2001:14:04:56 +0200] "HEAD /~conrad/hello_world_gen.swf HTTP/1.0" 200 0
+ 
+ or like this (combined log format):
+ 
+ 127.0.0.1 - - [23/Oct/2001:14:04:56 +0200] "HEAD /~conrad/hello_world_gen.swf HTTP/1.0" 200 0 "http://localhost/~conrad/hello_world.html" "Mozilla 4"
+ 
+ The meaning of the (space-separated) fields is as follows:
+ 
+ 1. Client IP-address
+ 2. Remote logname (from ident (RFC-...) lookup - always "-" in publicfile)
+ 3. Authenticated username (from Basic-Auth - always "-" in publicfile)
+ 4. Human-readable timestamp enclosed in []
+ 5. The request line enclosed in ""
+ 6. HTTP status code
+ 7. Number of bytes sent excluding HTTP headers
+ 8. Referrer information sent by the browser
+ 9. User-Agent information sent by the browser
+ 
+ These log lines can easily be distinguished from other log lines using the
+ second field: this will always be "-" for common and combined log lines,
+ and "read" or "dir" for publicfile standard log lines (see
+ http://cr.yp.to/publicfile/log.html). The script "splitPublicfileLog.pl"
+ may serve as an example how to split the log into interesting parts.
+ 
+ The logformat to be used can be selected via environment variables:
+ 
+  - HTTPD_LOG_COMMON selects common log format
+  - HTTPD_LOG_COMBINED selects combined log format
+ 
+ The latter takes precedence over the former.
+ 
diff -rNC3 publicfile-0.52.orig/httpd.c publicfile-0.52/httpd.c
*** publicfile-0.52.orig/httpd.c	Tue Nov  9 08:23:46 1999
--- publicfile-0.52/httpd.c	Fri Nov  9 10:10:51 2001
***************
*** 15,20 ****
--- 15,102 ----
  #include "substdio.h"
  #include "error.h"
  #include "getln.h"
+ #include "caltime.h"
+ #include "subfd.h"
+ #include "env.h"
+ 
+ char logformat = 0;
+ char *remoteip;
+ char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ 		  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+ stralloc referer = {0};
+ stralloc agent = {0};
+ 
+ char strnum[FMT_ULONG];
+ stralloc method = {0};
+ stralloc url = {0};
+ stralloc protocol = {0};
+ stralloc host = {0};
+ 
+ void log(char *code, unsigned long length) {
+   struct caltime ct;
+   int dummy1, dummy2;
+   struct tai now;
+ 
+   if (!logformat) return;
+ 
+   substdio_puts(subfderr,remoteip);
+   substdio_puts(subfderr," - - [");
+   tai_now(&now);
+   caltime_utc(&ct, &now, &dummy1, &dummy2);
+   if (ct.date.day < 10)
+     substdio_puts(subfderr,"0");
+   dummy1 = fmt_ulong(strnum,ct.date.day);
+   substdio_put(subfderr,strnum,dummy1);
+   substdio_puts(subfderr,"/");
+   if (ct.date.month > 0 && ct.date.month < 13)
+     substdio_puts(subfderr,months[ct.date.month-1]);
+   substdio_puts(subfderr,"/");
+   dummy1 = fmt_ulong(strnum,ct.date.year);
+   substdio_put(subfderr,strnum,dummy1);
+   substdio_puts(subfderr,":");
+   if (ct.hour < 10)
+     substdio_puts(subfderr,"0");
+   dummy1 = fmt_ulong(strnum,ct.hour);
+   substdio_put(subfderr,strnum,dummy1);
+   substdio_puts(subfderr,":");
+   if (ct.minute < 10)
+     substdio_puts(subfderr,"0");
+   dummy1 = fmt_ulong(strnum,ct.minute);
+   substdio_put(subfderr,strnum,dummy1);
+   substdio_puts(subfderr,":");
+   if (ct.second < 10)
+     substdio_puts(subfderr,"0");
+   dummy1 = fmt_ulong(strnum,ct.second);
+   substdio_put(subfderr,strnum,dummy1);
+   substdio_puts(subfderr," +0000] \"");
+   substdio_put(subfderr,method.s,method.len - 1); /* len includes \0 */
+   substdio_puts(subfderr," ");
+   substdio_put(subfderr,url.s,url.len);
+   substdio_puts(subfderr," ");
+   substdio_put(subfderr,protocol.s,protocol.len > 0 ? protocol.len - 1 : 0); /* len includes \0 */
+   substdio_puts(subfderr,"\" ");
+   substdio_put(subfderr,code,str_len(code) < 3 ? str_len(code) : 3);
+   substdio_puts(subfderr," ");
+   if (length > 0) {
+     dummy1 = fmt_ulong(strnum,length);
+     substdio_put(subfderr,strnum,dummy1);
+   } else {
+     substdio_puts(subfderr,"-");
+   }
+ 
+   if (logformat > 1) {
+     substdio_puts(subfderr," \"");
+     substdio_put(subfderr,referer.s,referer.len);
+     substdio_puts(subfderr,"\" \"");
+     substdio_put(subfderr,agent.s,agent.len);
+     substdio_puts(subfderr,"\"");
+   }
+ 
+   substdio_puts(subfderr," \"");
+   substdio_put(subfderr,host.s,host.len);
+   substdio_puts(subfderr,"\"\n");
+   substdio_flush(subfderr);
+ }
  
  int safewrite(int fd,char *buf,int len)
  {
***************
*** 42,54 ****
    substdio_flush(&out);
  }
  
- char strnum[FMT_ULONG];
- 
- stralloc protocol = {0};
  int protocolnum = 0;
- stralloc method = {0};
- stralloc url = {0};
- stralloc host = {0};
  stralloc path = {0};
  stralloc ims = {0};
  int flagbody = 1;
--- 124,130 ----
***************
*** 93,98 ****
--- 169,175 ----
      out_puts("</body></html>\r\n");
    }
    out_flush();
+   log(code, str_len(message) + 28);
    if (protocolnum >= 2) {
      shutdown(1,1);
      sleep(1); /* XXX */
***************
*** 105,108 ****
--- 182,186 ----
  
  void get(void)
  {
+   char *code;
    unsigned long length;
***************
*** 133,143 ****
      tai_now(&now);
      if (!httpdate(&mtimestr,&mtime)) _exit(21);
      if ((ims.len < mtimestr.len) || byte_diff(mtimestr.s,mtimestr.len,ims.s))
!       header("200 ","OK");
      else {
!       header("304 ","OK");
        flagbody = 0;
      }
      if (tai_less(&mtime,&now)) {
        tai_sub(&mtimeage,&now,&mtime);
        if (tai_approx(&mtimeage) >= 60.0) {
--- 211,222 ----
      tai_now(&now);
      if (!httpdate(&mtimestr,&mtime)) _exit(21);
      if ((ims.len < mtimestr.len) || byte_diff(mtimestr.s,mtimestr.len,ims.s))
!       code = "200 ";
      else {
!       code = "304 ";
        flagbody = 0;
      }
+     header(code,"OK");
      if (tai_less(&mtime,&now)) {
        tai_sub(&mtimeage,&now,&mtime);
        if (tai_approx(&mtimeage) >= 60.0) {
***************
*** 166,171 ****
--- 245,253 ----
          if (r == 0) break;
          out_put(filebuf,r);
        }
+     else
+       length = 0;
+     log(code,length);
      out_flush();
      _exit(0);
    }
***************
*** 180,185 ****
--- 262,270 ----
        out_put(filebuf,r);
        out_puts("\r\n");
      }
+   else
+     length = 0;
+   log(code,length);
  
    close(fd);
  }
***************
*** 216,221 ****
--- 301,314 ----
  
    sig_pipeignore();
  
+   if (env_get("HTTPD_LOG_COMMON"))
+     logformat = 1;
+   if (env_get("HTTPD_LOG_COMBINED"))
+     logformat = 2;
+   remoteip = env_get("TCPREMOTEIP");
+   if (!remoteip)
+     remoteip = "0";
+ 
    for (;;) {
      readline();
  
***************
*** 227,232 ****
--- 320,327 ----
      if (!stralloc_copys(&path,"")) _exit(21);
      if (!stralloc_copys(&protocol,"")) _exit(21);
      if (!stralloc_copys(&ims,"")) _exit(21);
+     if (!stralloc_copys(&referer,"")) _exit(21);
+     if (!stralloc_copys(&agent,"")) _exit(21);
      protocolnum = 2;
  
      spaces = 0;
***************
*** 294,299 ****
--- 389,398 ----
              barf("412 ","I do not accept If-None-Match");
            if (case_startb(field.s,field.len,"if-unmodified-since:"))
              barf("412 ","I do not accept If-Unmodified-Since");
+           if (case_startb(field.s,field.len,"referer:"))
+ 	    if (!stralloc_copytrim(&referer,field.s + 8,field.len - 8)) _exit(21);
+           if (case_startb(field.s,field.len,"user-agent:"))
+ 	    if (!stralloc_copytrim(&agent,field.s + 11,field.len - 11)) _exit(21);
            if (case_startb(field.s,field.len,"host:"))
              if (!host.len)
                for (i = 5;i < field.len;++i)
diff -rNC3 publicfile-0.52.orig/splitPublicfileLog.pl publicfile-0.52/splitPublicfileLog.pl
*** publicfile-0.52.orig/splitPublicfileLog.pl	Thu Jan  1 01:00:00 1970
--- publicfile-0.52/splitPublicfileLog.pl	Fri Nov  9 10:09:45 2001
***************
*** 0 ****
--- 1,119 ----
+ #!/usr/bin/perl -w
+ 
+ # This script will read lines from stdin. All lines starting with
+ # "a.b.c.d - " (for an IP-number a.b.c.d) will be written to the
+ # "access logfile" given as the first argument. Anything else will be
+ # prepended with a timestamp and
+ # written to the "error logfile" given as the second argument.
+ # Note that the messages usually are not really *error* messages, but
+ # simply the standard publicfile output.
+ 
+ use strict;
+ use Symbol;
+ 
+ if ($#ARGV < 0 || $#ARGV > 3) {
+     &usage();
+ }
+ 
+ local $main::logdir;
+ local $main::accessfile;
+ local $main::errorfile;
+ local $main::multilog;
+ local %main::handles = ();
+ 
+ if ($ARGV[0] eq "-m") {
+     $main::multilog = 1;
+     if ($#ARGV != 3) { &usage(); }
+     $main::logdir = $ARGV[1]."/";
+     $main::accessfile = $main::logdir.$ARGV[2];
+     $main::errorfile = $main::logdir.$ARGV[3];
+ } else {
+     $main::multilog = 0;
+     if ($#ARGV != 1) { &usage(); }
+     $main::accessfile = $ARGV[0];
+     $main::errorfile = $ARGV[1];
+ }
+ 
+ #open(ACCESS, ">>".$ARGV[0]) || die("Can't open access logfile!");
+ #open(ERROR, ">>".$ARGV[1]) || die("Can't open error logfile!");
+ $SIG{PIPE} = \&closeAndExit;
+ $SIG{TERM} = \&closeAndExit;
+ $SIG{QUIT} = \&closeAndExit;
+ $SIG{INT} = \&closeAndExit;
+ $SIG{HUP} = \&closeAll;
+ 
+ while($_ = <STDIN>) {
+     if (/^\d+\.\d+\.\d+\.\d+ - /) {
+ 	my $host = "";
+ 	if ($main::multilog && / \"([^\"\/]*)\"[\r\n]*$/) {
+ 	    $_ = $`."\n";
+ 	    $host = $1;
+ 	}
+ 	my $fh = &getHandle($host);
+ 	print $fh $_;
+     } else {
+ 	my $fh = &getHandle($main::errorfile);
+ 	print $fh time()." $_";
+     }
+ }
+ 
+ closeAndExit("End of input");
+ 
+ sub closeAll {
+     foreach my $key (keys %main::handles) {
+ 	my $fh = $main::handles{$key};
+ 	close $fh;
+ 	delete $main::handles{$key};
+     }
+ }
+ 
+ sub closeAndExit {
+     my $reason = shift;
+     my $now = time();
+ 
+     my $fh = &getHandle($main::errorfile);
+     print $fh "$now $reason received\n";
+     print STDERR "$now $reason received\n";
+     &closeAll();
+     exit 0;
+ }
+ 
+ sub getHandle {
+     my $key = shift;
+     my $res = 0;
+ 
+     if (defined($main::handles{$key})) {
+ 	return $main::handles{$key};
+     }
+ 
+     my $fh = Symbol::gensym();
+     if ($key eq $main::errorfile) {
+ 	open($fh, ">>".$main::errorfile) || die("Can't open error logfile!");
+     } else {
+ 	if ($main::multilog) {
+             my $fn = $main::logdir.$key;
+             if (-f $fn && -w $fn) {
+ 	        $res = open($fh, ">>$fn");
+ 	    }
+         }
+ 	if (!$res) {
+ 	    open($fh, ">>".$main::accessfile) || die("Can't open error logfile!");
+ 	}
+     }
+ 
+     $main::handles{$key} = $fh;
+     return $fh;
+ }
+ 
+ sub usage {
+     print STDERR "Usage: $0 <access-path> <error-path>\n";
+     print STDERR "   or: $0 -m <logdir> <access-file> <error-file>\n";
+     print STDERR "The first form will separate publicfile standard messages from \"common\" log\n";
+     print STDERR "lines. The second form will write \"common\" lines to <logdir>/<hostname>\n";
+     print STDERR "where <hostname> is the host given in the request. The file <logdir>/<hostname>\n";
+     print STDERR "must exist before it will be used. Anything for which no suitable logfile can\n";
+     print STDERR "be found will be logged to <logdir>/<access-path>.\n";
+ 
+     exit 1;
+ }
+ 
diff -rNC3 publicfile-0.52.orig/stralloc.h publicfile-0.52/stralloc.h
*** publicfile-0.52.orig/stralloc.h	Tue Nov  9 08:23:46 1999
--- publicfile-0.52/stralloc.h	Fri Nov  9 10:09:45 2001
***************
*** 12,17 ****
--- 12,18 ----
  extern int stralloc_copys();
  extern int stralloc_cats();
  extern int stralloc_copyb();
+ extern int stralloc_copytrim();
  extern int stralloc_catb();
  extern int stralloc_append(); /* beware: this takes a pointer to 1 char */
  extern int stralloc_starts();
diff -rNC3 publicfile-0.52.orig/stralloc_opytrim.c publicfile-0.52/stralloc_opytrim.c
*** publicfile-0.52.orig/stralloc_opytrim.c	Thu Jan  1 01:00:00 1970
--- publicfile-0.52/stralloc_opytrim.c	Fri Nov  9 10:09:45 2001
***************
*** 0 ****
--- 1,17 ----
+ #include "stralloc.h"
+ 
+ int stralloc_copytrim(sa,s,n)
+ stralloc *sa;
+ char *s;
+ unsigned int n;
+ {
+   while (n > 0 &&
+ 	 (s[n] == ' ' || s[n] == '\t' || s[n] == '\n' || s[n] == '\r')) n--;
+   while (n > 0 &&
+ 	 (s[0] == ' ' || s[0] == '\t' || s[0] == '\n' || s[0] == '\r')) {
+     n--;
+     s++;
+   }
+ 
+   return stralloc_copyb(sa,s,n);
+ }