-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlf.c
More file actions
677 lines (545 loc) · 14.6 KB
/
Copy pathlf.c
File metadata and controls
677 lines (545 loc) · 14.6 KB
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* Copyright 2017 Katherine Flavel
*
* See LICENCE for the full copyright terms.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <lf/lf.h>
#define MAX_STATUS 0xffffU /* minimum UINT_MAX */
#define ERR(code) \
*e = LF_ERR_ ## code; \
return 0;
struct errstuff {
const char *toomanyredirect;
const char *percent;
const char *endofstatuslist;
const char *openingbrace;
};
struct txt {
const char *p;
size_t n;
};
static int
uintcmp(const void *a, const void *b)
{
assert(a != NULL);
assert(b != NULL);
if (* (unsigned *) a < * (unsigned *) b) {
return -1;
}
if (* (unsigned *) a > * (unsigned *) b) {
return +1;
}
return 0;
}
static int
nameeq(const char *p, size_t n, const char *s)
{
assert(p != NULL);
assert(s != NULL);
if (strlen(s) != n) {
return 0;
}
return 0 == memcmp(p, s, n);
}
static unsigned
parse_status(const char *p, const char **e)
{
unsigned u;
assert(p != NULL);
u = 0;
while (*p != '\0' && isdigit((unsigned char) *p)) {
if (u >= MAX_STATUS / 10U) {
u = 0;
break;
}
u *= 10;
u += *p - '0';
p++;
}
*e = p;
return u;
}
static void
uniq(unsigned a[], size_t *count)
{
size_t i, j;
assert(count != NULL);
if (*count <= 1) {
return;
}
j = 0;
for (i = 1; i < *count; i++) {
if (a[j] == a[i]) {
continue;
}
j++;
a[j] = a[i];
}
*count = j + 1;
}
static int
notcustom(struct lf_config *conf, void *opaque,
const char **p, const struct lf_pred *pred, enum lf_redirect redirect,
const struct txt *name, enum lf_errno *e)
{
const char *fmt;
char buf[128]; /* arbitrary limit */
assert(conf != NULL);
assert(p != NULL && *p != NULL);
assert(pred != NULL);
assert(name != NULL);
assert(e != NULL);
switch (**p) {
case 't':
/*
* LogFormat:
* "Time the request was received, in the format
* [18/Sep/2011:19:18:28 -0400]"
*/
if (name->p == NULL) {
fmt = "[%d/%b/%Y:%T %z]";
break;
}
fmt = buf;
/* fallthrough */
case 'C':
case 'e':
case 'i':
case 'n':
case 'o':
case '^':
if (name->p == NULL) {
ERR(MISSING_NAME);
}
/* XXX: pass pointer and length instead */
if (name->n > (sizeof buf) - 1) {
ERR(NAME_OVERFLOW);
}
memcpy(buf, name->p, name->n);
buf[name->n] = '\0';
break;
case 'a':
case 'p':
case 'P':
case 'T':
break;
default:
if (!isalpha((unsigned char) **p)) {
break;
}
if (name->p != NULL) {
ERR(UNWANTED_NAME);
}
break;
}
switch (**p) {
case '%':
/* TODO: is a status list permitted here? i don't see why not */
return conf->literal(opaque, '%');
case 'A': return conf->ip(opaque, pred, redirect, LF_IP_LOCAL);
case 'B': return conf->resp_size(opaque, pred, redirect);
case 'b': return conf->resp_size_clf(opaque, pred, redirect);
case 'C': return conf->req_cookie(opaque, pred, redirect, buf);
case 'D': return conf->time_taken(opaque, pred, redirect, LF_RTIME_US);
case 'e': return conf->env_var(opaque, pred, redirect, buf);
case 'f': return conf->filename(opaque, pred, redirect);
case 'h': return conf->remote_hostname(opaque, pred, redirect, conf->hostname_lookups);
case 'H': return conf->req_protocol(opaque, pred, redirect);
case 'i': return conf->req_header(opaque, pred, redirect, buf);
case 'k': return conf->keepalive_reqs(opaque, pred, redirect);
case 'l': return conf->remote_logname(opaque, pred, redirect);
case 'L': return conf->req_logid(opaque, pred, redirect);
case 'm': return conf->req_method(opaque, pred, redirect);
case 'n': return conf->note(opaque, pred, redirect, buf);
case 'o': return conf->reply_header(opaque, pred, redirect, buf);
case 'a':
if (name->p == NULL) {
return conf->ip(opaque, pred, redirect, LF_IP_CLIENT);
} else if (nameeq(name->p, name->n, "c")) {
return conf->ip(opaque, pred, redirect, LF_IP_PEER);
} else {
ERR(UNRECOGNISED_IP_TYPE);
}
case 'p':
if (name->p == NULL) {
return conf->server_port(opaque, pred, redirect, LF_PORT_CANONICAL);
} else if (nameeq(name->p, name->n, "canonical")) {
return conf->server_port(opaque, pred, redirect, LF_PORT_CANONICAL);
} else if (nameeq(name->p, name->n, "local")) {
return conf->server_port(opaque, pred, redirect, LF_PORT_LOCAL);
} else if (nameeq(name->p, name->n, "remote")) {
return conf->server_port(opaque, pred, redirect, LF_PORT_REMOTE);
} else {
ERR(UNRECOGNISED_PORT_TYPE);
}
case 'P':
if (name->p == NULL) {
return conf->id(opaque, pred, redirect, LF_ID_PID);
} else if (nameeq(name->p, name->n, "pid")) {
return conf->id(opaque, pred, redirect, LF_ID_PID);
} else if (nameeq(name->p, name->n, "tid")) {
return conf->id(opaque, pred, redirect, LF_ID_TID);
} else if (nameeq(name->p, name->n, "hextid")) {
return conf->id(opaque, pred, redirect, LF_ID_HEXTID);
} else {
ERR(UNRECOGNISED_ID_TYPE);
}
case 't': {
enum lf_when when;
/*
* LogFormat:
* "If the format starts with begin: (default) the time
* is taken at the beginning of the req processing.
* If it starts with end: it is the time when the log entry
* gets written, close to the end of the req processing."
*/
if (0 == strncmp(fmt, "begin:", strlen("begin:"))) {
when = LF_WHEN_BEGIN;
fmt += strlen("begin:");
} else if (0 == strncmp(fmt, "end:", strlen("end:"))) {
when = LF_WHEN_END;
fmt += strlen("end:");
} else {
when = LF_WHEN_BEGIN;
}
/*
* LogFormat:
* "In addition to the formats supported by strftime(3),
* the following format tokens are supported [...]
* These tokens can not be combined with each other
* or strftime(3) formatting in the same format string.
*/
if (0 == strcmp(fmt, "sec")) {
return conf->time_frac(opaque, pred, redirect, when, LF_RTIME_S);
} else if (0 == strcmp(fmt, "msec")) {
return conf->time_frac(opaque, pred, redirect, when, LF_RTIME_MS);
} else if (0 == strcmp(fmt, "usec")) {
return conf->time_frac(opaque, pred, redirect, when, LF_RTIME_US);
} else if (0 == strcmp(fmt, "msec_frac")) {
return conf->time_frac(opaque, pred, redirect, when, LF_RTIME_MS_FRAC);
} else if (0 == strcmp(fmt, "usec_frac")) {
return conf->time_frac(opaque, pred, redirect, when, LF_RTIME_US_FRAC);
} else {
return conf->time(opaque, pred, redirect, when, fmt);
}
}
case 'T':
if (name->p == NULL) {
return conf->time_taken(opaque, pred, redirect, LF_RTIME_S);
} else if (nameeq(name->p, name->n, "ms")) {
return conf->time_taken(opaque, pred, redirect, LF_RTIME_MS);
} else if (nameeq(name->p, name->n, "us")) {
return conf->time_taken(opaque, pred, redirect, LF_RTIME_US);
} else if (nameeq(name->p, name->n, "s")) {
return conf->time_taken(opaque, pred, redirect, LF_RTIME_S);
} else {
ERR(UNRECOGNISED_RTIME_UNIT);
}
case 'u': return conf->remote_user(opaque, pred, redirect);
case 'U': return conf->url_path(opaque, pred, redirect);
case 'v': return conf->server_name(opaque, pred, redirect, 1);
case 'V': return conf->server_name(opaque, pred, redirect, conf->use_canonical_name);
case 'X': return conf->conn_status(opaque, pred, redirect);
case 'I': return conf->bytes_recv(opaque, pred, redirect);
case 'O': return conf->bytes_sent(opaque, pred, redirect);
case 'q': return conf->query_string(opaque, pred, redirect);
case 'r': return conf->req_first_line(opaque, pred, redirect);
case 'R': return conf->resp_handler(opaque, pred, redirect);
case 's': return conf->status(opaque, pred, redirect);
case 'S': return conf->bytes_xfer(opaque, pred, redirect);
case '^':
(*p)++;
if (**p != 't') {
ERR(UNRECOGNISED_DIRECTIVE);
}
(*p)++;
switch (**p) {
case 'i': return conf->req_trailer (opaque, pred, redirect, buf);
case 'o': return conf->resp_trailer(opaque, pred, redirect, buf);
default:
ERR(UNRECOGNISED_DIRECTIVE);
}
case '\0':
ERR(MISSING_DIRECTIVE);
default:
ERR(UNRECOGNISED_DIRECTIVE);
}
}
static int
parse_escape(struct lf_config *conf, void *opaque, const char **p,
enum lf_errno *e)
{
assert(conf != NULL);
assert(p != NULL && *p != NULL);
assert(e != NULL);
(*p)++;
/*
* LogFormat:
* "... and the C-style control characters "\n" and "\t"
* to represent new-lines and tabs. Literal quotes and backslashes
* should be escaped with backslashes."
*/
assert(conf->literal != NULL);
switch (**p) {
case 't': return conf->literal(opaque, '\t');
case 'n': return conf->literal(opaque, '\n');
case '\'': return conf->literal(opaque, '\'');
case '\"': return conf->literal(opaque, '\"');
case '\\': return conf->literal(opaque, '\\');
case '\0':
ERR(MISSING_ESCAPE);
default:
ERR(UNRECOGNISED_ESCAPE);
}
}
static int
parse_directive(struct lf_config *conf, void *opaque, const char **p,
enum lf_errno *e, struct errstuff *errstuff)
{
const char *redirectp;
unsigned status[128]; /* arbitrary limit */
struct txt name;
struct lf_pred pred;
enum lf_redirect redirect;
int r;
assert(conf != NULL);
assert(p != NULL && *p != NULL);
assert(e != NULL);
assert(errstuff != NULL);
name.p = NULL;
pred.neg = 0;
pred.count = 0;
pred.status = status;
errstuff->percent = *p;
(*p)++;
/*
* LogFormat:
* "Particular items can be restricted to print only for resps
* with specific HTTP status codes by placing a comma-separated list
* of status codes immediately following the "%". The status code
* list may be preceded by a "!" to indicate negation.
*/
if (**p == '!') {
pred.neg = 1;
(*p)++;
}
do {
const char *end;
unsigned u;
/* skip comma-separated list of digits */
u = parse_status(*p, &end);
if (u == 0 && end != *p) {
ERR(STATUS_OVERFLOW);
}
if (u == 0) {
break;
}
*p = end;
errstuff->endofstatuslist = end;
if (pred.count == sizeof status / sizeof *status) {
ERR(TOO_MANY_STATUSES);
}
status[pred.count] = u;
pred.count++;
} while (**p == ',' && (*p)++);
if (pred.count > 0) {
qsort(pred.status, pred.count, sizeof *pred.status, uintcmp);
}
uniq(pred.status, &pred.count);
if (**p == '<' || **p == '>') {
redirectp = *p;
(*p)++;
} else {
redirectp = NULL;
}
if (**p == '{') {
size_t n;
errstuff->openingbrace = *p;
(*p)++;
n = strcspn(*p, "}");
if ((*p)[n] != '}') {
ERR(MISSING_CLOSING_BRACE);
}
if (n == 0) {
ERR(EMPTY_NAME);
}
assert(name.p == NULL);
name.p = *p;
name.n = n;
(*p) += n;
(*p)++;
}
/*
* LogFormat:
* "By default, the % directives %s, %U, %T, %D, and %r
* look at the original request while all others look at
* the final request."
*
* There is currently no way to specify equivalent defaults
* for custom directives.
*/
switch (**p) {
case 's':
case 'U':
case 'T':
case 'D':
case 'r': redirect = LF_REDIRECT_ORIG; break;
default: redirect = LF_REDIRECT_FINAL; break;
}
if (redirectp != NULL) {
switch (*redirectp) {
case '<': redirect = LF_REDIRECT_ORIG; break;
case '>': redirect = LF_REDIRECT_FINAL; break;
default:
assert(!"unreached");
}
errstuff->toomanyredirect = redirectp;
redirectp++;
if (*redirectp == '<' || *redirectp == '>') {
ERR(TOO_MANY_REDIRECT_FLAGS);
}
}
/*
* The custom callback decides if the character is relevant.
* Note handling for non-alpha characters falls through to
* the non-custom directives below, which will then error out.
*/
if (conf->override != NULL && isalpha((unsigned char) **p) && strchr(conf->override, **p)) {
assert(conf->custom != NULL);
r = conf->custom(conf, opaque, **p, &pred, redirect, name.p, name.n, e);
} else {
r = notcustom(conf, opaque, p, &pred, redirect, &name, e);
}
return r;
}
int
lf_parse(struct lf_config *conf, void *opaque, const char *fmt,
struct lf_err *ep)
{
struct errstuff errstuff;
enum lf_errno e;
const char *p;
assert(conf != NULL);
assert(ep != NULL);
for (p = fmt; *p != '\0'; p++) {
int r;
errstuff.toomanyredirect = NULL;
errstuff.percent = NULL;
errstuff.endofstatuslist = NULL;
errstuff.openingbrace = NULL;
e = LF_ERR_ERRNO;
switch (*p) {
case '\\':
r = parse_escape(conf, opaque, &p, &e);
break;
case '%':
r = parse_directive(conf, opaque, &p, &e, &errstuff);
break;
default:
r = conf->literal(opaque, *p);
break;
}
if (!r) {
goto error;
}
}
return 1;
error:
if (ep != NULL) {
ep->errnum = e;
/*
* Errors from errno are expected to typically come from hooks,
* (e.g. some error on printing output). These don't pertain to
* any particular input in the fmt string, but we point to where
* the cursor is anyway.
*
* LF_ERR_ERRNO can also be produced by a custom directive,
* where the error may or may not be directly caused by the input.
* We don't know the difference here.
*/
if (e == LF_ERR_ERRNO) {
ep->p = p;
ep->n = 0;
return 0;
}
/*
* Henceforth we know .percent is set for both errors produced by
* custom directives (because .custom is only called after '%'
* is parsed), and errors about other things (e.g. pointing at
* escapes) do not expect .percent to be set.
*/
switch (e) {
case LF_ERR_MISSING_CLOSING_BRACE:
if (errstuff.openingbrace == NULL) {
goto percent;
}
ep->p = errstuff.openingbrace;
ep->n = 1;
break;
case LF_ERR_MISSING_ESCAPE:
case LF_ERR_UNRECOGNISED_ESCAPE:
ep->p = p - 1;
ep->n = 1 + (*p != '\0');
break;
case LF_ERR_TOO_MANY_STATUSES:
if (errstuff.endofstatuslist == NULL) {
goto percent;
}
ep->p = p;
ep->n = errstuff.endofstatuslist - p;
break;
case LF_ERR_STATUS_OVERFLOW:
ep->p = p;
ep->n = strspn(p, "0123456789");
break;
case LF_ERR_TOO_MANY_REDIRECT_FLAGS:
if (errstuff.toomanyredirect == NULL) {
goto percent;
}
ep->p = errstuff.toomanyredirect;
ep->n = strspn(errstuff.toomanyredirect, "<>");
break;
case LF_ERR_NAME_OVERFLOW:
case LF_ERR_UNRECOGNISED_ID_TYPE:
case LF_ERR_UNRECOGNISED_IP_TYPE:
case LF_ERR_UNRECOGNISED_PORT_TYPE:
case LF_ERR_UNRECOGNISED_RTIME_UNIT:
if (errstuff.openingbrace == NULL) {
goto percent;
}
ep->p = errstuff.openingbrace + 1;
ep->n = strcspn(ep->p, "}");
/* TODO: cut down xn to (sizeof buf) - 1 here */
break;
case LF_ERR_EMPTY_NAME:
case LF_ERR_UNWANTED_NAME:
if (errstuff.openingbrace == NULL) {
goto percent;
}
ep->p = errstuff.openingbrace;
ep->n = strcspn(ep->p, "}") + 1;
break;
percent:
case LF_ERR_UNSUPPORTED:
case LF_ERR_MISSING_NAME:
case LF_ERR_UNRECOGNISED_DIRECTIVE:
assert(errstuff.percent != NULL);
ep->p = errstuff.percent;
ep->n = p - errstuff.percent + (*p != '\0');
break;
case LF_ERR_MISSING_DIRECTIVE:
assert(errstuff.percent != NULL);
ep->p = errstuff.percent;
ep->n = p - errstuff.percent;
break;
default:
assert(!"unreached");
}
}
return 0;
}