| | varnish-cache/bin/varnishncsa/varnishncsa.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2016 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Anders Berg <andersb@vgnett.no> |
6 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
7 |
|
* Author: Tollef Fog Heen <tfheen@varnish-software.com> |
8 |
|
* Author: Martin Blix Grydeland <mbgrydeland@varnish-software.com> |
9 |
|
* |
10 |
|
* SPDX-License-Identifier: BSD-2-Clause |
11 |
|
* |
12 |
|
* Redistribution and use in source and binary forms, with or without |
13 |
|
* modification, are permitted provided that the following conditions |
14 |
|
* are met: |
15 |
|
* 1. Redistributions of source code must retain the above copyright |
16 |
|
* notice, this list of conditions and the following disclaimer. |
17 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
18 |
|
* notice, this list of conditions and the following disclaimer in the |
19 |
|
* documentation and/or other materials provided with the distribution. |
20 |
|
* |
21 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
22 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
25 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 |
|
* SUCH DAMAGE. |
32 |
|
* |
33 |
|
* Obtain log data from the shared memory log, order it by session ID, and |
34 |
|
* display it in Apache / NCSA combined log format. |
35 |
|
* |
36 |
|
* See doc/sphinx/reference/varnishncsa.rst for the supported format |
37 |
|
* specifiers. |
38 |
|
* |
39 |
|
*/ |
40 |
|
|
41 |
|
#include "config.h" |
42 |
|
|
43 |
|
#include <stdlib.h> |
44 |
|
#include <stdio.h> |
45 |
|
#include <unistd.h> |
46 |
|
#include <string.h> |
47 |
|
#include <signal.h> |
48 |
|
#include <stdarg.h> |
49 |
|
#include <inttypes.h> |
50 |
|
#include <limits.h> |
51 |
|
#include <ctype.h> |
52 |
|
#include <time.h> |
53 |
|
#include <math.h> |
54 |
|
|
55 |
|
#define VOPT_DEFINITION |
56 |
|
#define VOPT_INC "varnishncsa_options.h" |
57 |
|
|
58 |
|
#include "vdef.h" |
59 |
|
|
60 |
|
#include "vapi/vsl.h" |
61 |
|
#include "vapi/voptget.h" |
62 |
|
#include "vas.h" |
63 |
|
#include "venc.h" |
64 |
|
#include "vsb.h" |
65 |
|
#include "vut.h" |
66 |
|
#include "vqueue.h" |
67 |
|
#include "miniobj.h" |
68 |
|
|
69 |
|
#define TIME_FMT "[%d/%b/%Y:%T %z]" |
70 |
|
#define FORMAT "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" |
71 |
|
|
72 |
|
static struct VUT *vut; |
73 |
|
|
74 |
|
struct format; |
75 |
|
|
76 |
|
enum e_frag { |
77 |
|
F_H, /* %H Proto */ |
78 |
|
F_U, /* %U URL path */ |
79 |
|
F_q, /* %q Query string */ |
80 |
|
F_b, /* %b Body bytes sent */ |
81 |
|
F_h, /* %h Host name / IP Address */ |
82 |
|
F_m, /* %m Method */ |
83 |
|
F_s, /* %s Status */ |
84 |
|
F_I, /* %I Bytes received */ |
85 |
|
F_O, /* %O Bytes sent */ |
86 |
|
F_tstart, /* Time start */ |
87 |
|
F_tend, /* Time end */ |
88 |
|
F_ttfb, /* %{Varnish:time_firstbyte}x */ |
89 |
|
F_host, /* Host header */ |
90 |
|
F_auth, /* Authorization header */ |
91 |
|
F__MAX, |
92 |
|
}; |
93 |
|
|
94 |
|
struct fragment { |
95 |
|
uint64_t gen; |
96 |
|
const char *b, *e; |
97 |
|
}; |
98 |
|
|
99 |
|
typedef int format_f(const struct format *format); |
100 |
|
|
101 |
|
struct format { |
102 |
|
unsigned magic; |
103 |
|
#define FORMAT_MAGIC 0xC3119CDA |
104 |
|
|
105 |
|
char time_type; |
106 |
|
VTAILQ_ENTRY(format) list; |
107 |
|
format_f *func; |
108 |
|
struct fragment *frag; |
109 |
|
char *string; |
110 |
|
const char *const *strptr; |
111 |
|
char *time_fmt; |
112 |
|
int64_t *int64; |
113 |
|
}; |
114 |
|
|
115 |
|
struct watch { |
116 |
|
unsigned magic; |
117 |
|
#define WATCH_MAGIC 0xA7D4005C |
118 |
|
|
119 |
|
VTAILQ_ENTRY(watch) list; |
120 |
|
char *key; |
121 |
|
int keylen; |
122 |
|
struct fragment frag; |
123 |
|
}; |
124 |
|
VTAILQ_HEAD(watch_head, watch); |
125 |
|
|
126 |
|
struct vsl_watch { |
127 |
|
unsigned magic; |
128 |
|
#define VSL_WATCH_MAGIC 0xE3E27D23 |
129 |
|
|
130 |
|
VTAILQ_ENTRY(vsl_watch) list; |
131 |
|
enum VSL_tag_e tag; |
132 |
|
int idx; |
133 |
|
char *prefix; |
134 |
|
int prefixlen; |
135 |
|
struct fragment frag; |
136 |
|
}; |
137 |
|
VTAILQ_HEAD(vsl_watch_head, vsl_watch); |
138 |
|
|
139 |
|
static struct ctx { |
140 |
|
/* Options */ |
141 |
|
int a_opt; |
142 |
|
char *w_arg; |
143 |
|
|
144 |
|
FILE *fo; |
145 |
|
struct vsb *vsb; |
146 |
|
uint64_t gen; |
147 |
|
VTAILQ_HEAD(,format) format; |
148 |
|
int quote_how; |
149 |
|
char *missing_string; |
150 |
|
char *missing_int; |
151 |
|
|
152 |
|
/* State */ |
153 |
|
struct watch_head watch_vcl_log; |
154 |
|
struct watch_head watch_reqhdr; /* also bereqhdr */ |
155 |
|
struct watch_head watch_resphdr; /* also beresphdr */ |
156 |
|
struct vsl_watch_head watch_vsl; |
157 |
|
struct fragment frag[F__MAX]; |
158 |
|
const char *hitmiss; |
159 |
|
const char *handling; |
160 |
|
const char *side; |
161 |
|
int64_t vxid; |
162 |
|
int recv_compl; |
163 |
|
} CTX; |
164 |
|
|
165 |
|
enum format_policy { |
166 |
|
FMTPOL_INTERNAL, |
167 |
|
FMTPOL_REQ, |
168 |
|
FMTPOL_RESP, |
169 |
|
}; |
170 |
|
|
171 |
|
static void parse_format(const char *format); |
172 |
|
|
173 |
|
static unsigned |
174 |
3768 |
frag_needed(const struct fragment *frag, enum format_policy fp) |
175 |
|
{ |
176 |
|
unsigned is_first, want_first, want_frag; |
177 |
|
|
178 |
3768 |
is_first = CTX.gen != frag->gen; |
179 |
|
|
180 |
3768 |
switch (fp) { |
181 |
|
case FMTPOL_INTERNAL: |
182 |
1917 |
want_first = 1; |
183 |
1917 |
want_frag = 1; |
184 |
1917 |
break; |
185 |
|
case FMTPOL_REQ: |
186 |
1575 |
want_first = *CTX.side == 'c'; |
187 |
1575 |
want_frag = !CTX.recv_compl; |
188 |
1575 |
break; |
189 |
|
case FMTPOL_RESP: |
190 |
276 |
want_first = *CTX.side == 'b'; |
191 |
276 |
want_frag = (*CTX.side == 'c') || !CTX.recv_compl; |
192 |
276 |
break; |
193 |
|
default: |
194 |
0 |
WRONG("Invalid format policy"); |
195 |
0 |
} |
196 |
|
|
197 |
3768 |
if (!want_frag) |
198 |
132 |
return (0); |
199 |
3636 |
if (want_first && !is_first) |
200 |
108 |
return (0); |
201 |
3528 |
return (1); |
202 |
3768 |
} |
203 |
|
|
204 |
|
static void |
205 |
15 |
openout(int append) |
206 |
|
{ |
207 |
|
|
208 |
15 |
AN(CTX.w_arg); |
209 |
15 |
if (!strcmp(CTX.w_arg, "-")) |
210 |
0 |
CTX.fo = stdout; |
211 |
|
else |
212 |
15 |
CTX.fo = fopen(CTX.w_arg, append ? "a" : "w"); |
213 |
15 |
if (CTX.fo == NULL) |
214 |
6 |
VUT_Error(vut, 1, "Can't open output file (%s)", |
215 |
3 |
strerror(errno)); |
216 |
12 |
} |
217 |
|
|
218 |
|
static int v_matchproto_(VUT_cb_f) |
219 |
3 |
rotateout(struct VUT *v) |
220 |
|
{ |
221 |
|
|
222 |
3 |
assert(v == vut); |
223 |
3 |
AN(CTX.w_arg); |
224 |
3 |
AN(CTX.fo); |
225 |
3 |
(void)fclose(CTX.fo); |
226 |
3 |
openout(1); |
227 |
3 |
AN(CTX.fo); |
228 |
3 |
return (0); |
229 |
|
} |
230 |
|
|
231 |
|
static int v_matchproto_(VUT_cb_f) |
232 |
2547 |
flushout(struct VUT *v) |
233 |
|
{ |
234 |
|
|
235 |
2547 |
assert(v == vut); |
236 |
2547 |
AN(CTX.fo); |
237 |
2547 |
if (fflush(CTX.fo)) |
238 |
0 |
return (-5); |
239 |
2547 |
return (0); |
240 |
2547 |
} |
241 |
|
|
242 |
|
static inline int |
243 |
684 |
vsb_fcat(struct vsb *vsb, const struct fragment *f, const char *dflt) |
244 |
|
{ |
245 |
684 |
if (f->gen == CTX.gen) { |
246 |
672 |
assert(f->b <= f->e); |
247 |
672 |
VSB_quote(vsb, f->b, f->e - f->b, CTX.quote_how); |
248 |
684 |
} else if (dflt) |
249 |
12 |
VSB_quote(vsb, dflt, -1, CTX.quote_how); |
250 |
|
else |
251 |
0 |
return (-1); |
252 |
684 |
return (0); |
253 |
684 |
} |
254 |
|
|
255 |
|
static int v_matchproto_(format_f) |
256 |
885 |
format_string(const struct format *format) |
257 |
|
{ |
258 |
|
|
259 |
885 |
CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC); |
260 |
885 |
AN(format->string); |
261 |
885 |
AZ(VSB_cat(CTX.vsb, format->string)); |
262 |
885 |
return (1); |
263 |
|
} |
264 |
|
|
265 |
|
static int v_matchproto_(format_f) |
266 |
81 |
format_strptr(const struct format *format) |
267 |
|
{ |
268 |
|
|
269 |
81 |
CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC); |
270 |
81 |
AN(format->strptr); |
271 |
81 |
AN(*format->strptr); |
272 |
81 |
AZ(VSB_cat(CTX.vsb, *format->strptr)); |
273 |
81 |
return (1); |
274 |
|
} |
275 |
|
|
276 |
|
static int v_matchproto_(format_f) |
277 |
63 |
format_int64(const struct format *format) |
278 |
|
{ |
279 |
|
|
280 |
63 |
CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC); |
281 |
63 |
VSB_printf(CTX.vsb, "%jd", (intmax_t)*format->int64); |
282 |
63 |
return (1); |
283 |
|
} |
284 |
|
|
285 |
|
static int v_matchproto_(format_f) |
286 |
609 |
format_fragment(const struct format *format) |
287 |
|
{ |
288 |
|
|
289 |
609 |
CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC); |
290 |
609 |
AN(format->frag); |
291 |
|
|
292 |
609 |
if (format->frag->gen != CTX.gen) { |
293 |
144 |
if (format->string == NULL) |
294 |
0 |
return (-1); |
295 |
144 |
VSB_quote(CTX.vsb, format->string, -1, CTX.quote_how); |
296 |
144 |
return (0); |
297 |
|
} |
298 |
465 |
AZ(vsb_fcat(CTX.vsb, format->frag, NULL)); |
299 |
465 |
return (1); |
300 |
609 |
} |
301 |
|
|
302 |
|
static int v_matchproto_(format_f) |
303 |
180 |
format_time(const struct format *format) |
304 |
|
{ |
305 |
|
double t_start, t_end, d; |
306 |
|
char *p; |
307 |
|
char buf[64]; |
308 |
|
time_t t; |
309 |
|
intmax_t l; |
310 |
|
struct tm tm; |
311 |
|
|
312 |
180 |
CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC); |
313 |
180 |
if (CTX.frag[F_tstart].gen == CTX.gen) { |
314 |
180 |
t_start = strtod(CTX.frag[F_tstart].b, &p); |
315 |
180 |
if (p != CTX.frag[F_tstart].e) |
316 |
0 |
t_start = NAN; |
317 |
180 |
} else |
318 |
0 |
t_start = NAN; |
319 |
180 |
if (isnan(t_start)) { |
320 |
|
/* Missing t_start is a no go */ |
321 |
0 |
if (format->string == NULL) |
322 |
0 |
return (-1); |
323 |
0 |
AZ(VSB_cat(CTX.vsb, format->string)); |
324 |
0 |
return (0); |
325 |
|
} |
326 |
|
|
327 |
|
/* Missing t_end defaults to t_start */ |
328 |
180 |
if (CTX.frag[F_tend].gen == CTX.gen) { |
329 |
177 |
t_end = strtod(CTX.frag[F_tend].b, &p); |
330 |
177 |
if (p != CTX.frag[F_tend].e) |
331 |
0 |
t_end = t_start; |
332 |
177 |
} else |
333 |
3 |
t_end = t_start; |
334 |
|
|
335 |
180 |
AN(format->time_fmt); |
336 |
|
|
337 |
180 |
switch (format->time_type) { |
338 |
|
case 't': |
339 |
90 |
t = (intmax_t)floor(t_start); |
340 |
90 |
(void)localtime_r(&t, &tm); |
341 |
90 |
AN(strftime(buf, sizeof buf, format->time_fmt, &tm)); |
342 |
90 |
AZ(VSB_cat(CTX.vsb, buf)); |
343 |
90 |
return (1); |
344 |
|
case '3': |
345 |
9 |
l = (intmax_t)(modf(t_start, &d) * 1e3); |
346 |
9 |
break; |
347 |
|
case '6': |
348 |
9 |
l = (intmax_t)(modf(t_start, &d) * 1e6); |
349 |
9 |
break; |
350 |
|
case 'S': |
351 |
9 |
l = (intmax_t)t_start; |
352 |
9 |
break; |
353 |
|
case 'M': |
354 |
9 |
l = (intmax_t)(t_start * 1e3); |
355 |
9 |
break; |
356 |
|
case 'U': |
357 |
9 |
l = (intmax_t)(t_start * 1e6); |
358 |
9 |
break; |
359 |
|
case 's': |
360 |
18 |
l = (intmax_t)(t_end - t_start); |
361 |
18 |
break; |
362 |
|
case 'm': |
363 |
9 |
l = (intmax_t)((t_end - t_start) * 1e3); |
364 |
9 |
break; |
365 |
|
case 'u': |
366 |
18 |
l = (intmax_t)((t_end - t_start) * 1e6); |
367 |
18 |
break; |
368 |
|
default: |
369 |
0 |
WRONG("Time format specifier"); |
370 |
0 |
} |
371 |
|
|
372 |
|
#ifdef __FreeBSD__ |
373 |
90 |
assert(fmtcheck(format->time_fmt, "%jd") == format->time_fmt); |
374 |
|
#endif |
375 |
90 |
AZ(VSB_printf(CTX.vsb, format->time_fmt, l)); |
376 |
|
|
377 |
90 |
return (1); |
378 |
180 |
} |
379 |
|
|
380 |
|
static int v_matchproto_(format_f) |
381 |
45 |
format_requestline(const struct format *format) |
382 |
|
{ |
383 |
|
|
384 |
45 |
(void)format; |
385 |
45 |
AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_m], "-")); |
386 |
45 |
AZ(VSB_putc(CTX.vsb, ' ')); |
387 |
45 |
if (CTX.frag[F_host].gen == CTX.gen) { |
388 |
39 |
if (strncmp(CTX.frag[F_host].b, "http://", 7)) |
389 |
39 |
AZ(VSB_cat(CTX.vsb, "http://")); |
390 |
39 |
AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_host], NULL)); |
391 |
39 |
} else |
392 |
6 |
AZ(VSB_cat(CTX.vsb, "http://localhost")); |
393 |
45 |
AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_U], "")); |
394 |
45 |
AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_q], "")); |
395 |
45 |
AZ(VSB_putc(CTX.vsb, ' ')); |
396 |
45 |
AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_H], "HTTP/1.0")); |
397 |
45 |
return (1); |
398 |
|
} |
399 |
|
|
400 |
|
static int v_matchproto_(format_f) |
401 |
54 |
format_auth(const struct format *format) |
402 |
|
{ |
403 |
54 |
struct vsb *vsb = VSB_new_auto(); |
404 |
54 |
AN(vsb); |
405 |
|
char *q; |
406 |
|
|
407 |
54 |
if (CTX.frag[F_auth].gen != CTX.gen || |
408 |
24 |
VENC_Decode_Base64(vsb, CTX.frag[F_auth].b, CTX.frag[F_auth].e)) { |
409 |
30 |
VSB_destroy(&vsb); |
410 |
30 |
if (format->string == NULL) |
411 |
0 |
return (-1); |
412 |
30 |
VSB_quote(CTX.vsb, format->string, -1, CTX.quote_how); |
413 |
30 |
return (0); |
414 |
|
} |
415 |
24 |
AZ(VSB_finish(vsb)); |
416 |
24 |
q = strchr(VSB_data(vsb), ':'); |
417 |
24 |
if (q != NULL) |
418 |
24 |
*q = '\0'; |
419 |
24 |
VSB_quote(CTX.vsb, VSB_data(vsb), -1, CTX.quote_how); |
420 |
24 |
VSB_destroy(&vsb); |
421 |
24 |
return (1); |
422 |
54 |
} |
423 |
|
|
424 |
|
static int |
425 |
258 |
print(void) |
426 |
|
{ |
427 |
|
const struct format *f; |
428 |
258 |
int i, r = 1; |
429 |
|
|
430 |
258 |
VSB_clear(CTX.vsb); |
431 |
2175 |
VTAILQ_FOREACH(f, &CTX.format, list) { |
432 |
1917 |
CHECK_OBJ_NOTNULL(f, FORMAT_MAGIC); |
433 |
1917 |
i = (f->func)(f); |
434 |
1917 |
AZ(VSB_error(CTX.vsb)); |
435 |
1917 |
if (r > i) |
436 |
78 |
r = i; |
437 |
1917 |
} |
438 |
258 |
AZ(VSB_putc(CTX.vsb, '\n')); |
439 |
258 |
AZ(VSB_finish(CTX.vsb)); |
440 |
258 |
if (r >= 0) { |
441 |
258 |
i = fwrite(VSB_data(CTX.vsb), 1, VSB_len(CTX.vsb), CTX.fo); |
442 |
258 |
if (i != VSB_len(CTX.vsb)) |
443 |
0 |
return (-5); |
444 |
258 |
} |
445 |
258 |
return (0); |
446 |
258 |
} |
447 |
|
|
448 |
|
static void |
449 |
675 |
addf_string(const char *str) |
450 |
|
{ |
451 |
|
struct format *f; |
452 |
|
|
453 |
675 |
AN(str); |
454 |
675 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
455 |
675 |
AN(f); |
456 |
675 |
f->func = format_string; |
457 |
675 |
f->string = strdup(str); |
458 |
675 |
AN(f->string); |
459 |
675 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
460 |
675 |
} |
461 |
|
|
462 |
|
static void |
463 |
21 |
addf_strptr(const char *const *strptr) |
464 |
|
{ |
465 |
|
struct format *f; |
466 |
|
|
467 |
21 |
AN(strptr); |
468 |
21 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
469 |
21 |
AN(f); |
470 |
21 |
f->func = format_strptr; |
471 |
21 |
f->strptr = strptr; |
472 |
21 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
473 |
21 |
} |
474 |
|
|
475 |
|
static void |
476 |
294 |
addf_fragment(struct fragment *frag, const char *str) |
477 |
|
{ |
478 |
|
struct format *f; |
479 |
|
|
480 |
294 |
AN(frag); |
481 |
294 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
482 |
294 |
AN(f); |
483 |
294 |
f->func = format_fragment; |
484 |
294 |
f->frag = frag; |
485 |
294 |
if (str != NULL) { |
486 |
294 |
f->string = strdup(str); |
487 |
294 |
AN(f->string); |
488 |
294 |
} |
489 |
294 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
490 |
294 |
} |
491 |
|
|
492 |
|
static void |
493 |
15 |
addf_int64(int64_t *i) |
494 |
|
{ |
495 |
|
struct format *f; |
496 |
|
|
497 |
15 |
AN(i); |
498 |
15 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
499 |
15 |
AN(f); |
500 |
15 |
f->func = format_int64; |
501 |
15 |
f->int64 = i; |
502 |
15 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
503 |
15 |
} |
504 |
|
|
505 |
|
static void |
506 |
96 |
addf_time(char type, const char *fmt) |
507 |
|
{ |
508 |
|
struct format *f; |
509 |
|
|
510 |
96 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
511 |
96 |
AN(f); |
512 |
96 |
AN(fmt); |
513 |
96 |
f->func = format_time; |
514 |
96 |
f->time_type = type; |
515 |
96 |
f->time_fmt = strdup(fmt); |
516 |
|
|
517 |
96 |
if (f->time_type == 'T') { |
518 |
15 |
if (!strcmp(fmt, "s")) |
519 |
6 |
f->time_type = 's'; |
520 |
9 |
else if (!strcmp(fmt, "ms")) |
521 |
3 |
f->time_type = 'm'; |
522 |
6 |
else if (!strcmp(fmt, "us")) |
523 |
6 |
f->time_type = 'u'; |
524 |
|
else |
525 |
0 |
VUT_Error(vut, 1, "Unknown specifier: %%{%s}T", |
526 |
0 |
fmt); |
527 |
15 |
REPLACE(f->time_fmt, "%jd"); |
528 |
96 |
} else if (f->time_type == 't') { |
529 |
81 |
if (!strcmp(fmt, "sec")) { |
530 |
3 |
f->time_type = 'S'; |
531 |
3 |
REPLACE(f->time_fmt, "%jd"); |
532 |
81 |
} else if (!strncmp(fmt, "msec", 4)) { |
533 |
9 |
fmt += 4; |
534 |
9 |
if (!strcmp(fmt, "_frac")) { |
535 |
3 |
f->time_type = '3'; |
536 |
3 |
REPLACE(f->time_fmt, "%03jd"); |
537 |
9 |
} else if (*fmt == '\0') { |
538 |
3 |
f->time_type = 'M'; |
539 |
3 |
REPLACE(f->time_fmt, "%jd"); |
540 |
3 |
} |
541 |
78 |
} else if (!strncmp(fmt, "usec", 4)) { |
542 |
9 |
fmt += 4; |
543 |
9 |
if (!strcmp(fmt, "_frac")) { |
544 |
3 |
f->time_type = '6'; |
545 |
3 |
REPLACE(f->time_fmt, "%06jd"); |
546 |
9 |
} else if (*fmt == '\0') { |
547 |
3 |
f->time_type = 'U'; |
548 |
3 |
REPLACE(f->time_fmt, "%jd"); |
549 |
3 |
} |
550 |
9 |
} |
551 |
81 |
} |
552 |
|
|
553 |
96 |
AN(f->time_fmt); |
554 |
96 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
555 |
96 |
} |
556 |
|
|
557 |
|
static void |
558 |
57 |
addf_requestline(void) |
559 |
|
{ |
560 |
|
struct format *f; |
561 |
|
|
562 |
57 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
563 |
57 |
AN(f); |
564 |
57 |
f->func = format_requestline; |
565 |
57 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
566 |
57 |
} |
567 |
|
|
568 |
|
static void |
569 |
6 |
addf_vcl_log(const char *key) |
570 |
|
{ |
571 |
|
struct watch *w; |
572 |
|
struct format *f; |
573 |
|
|
574 |
6 |
AN(key); |
575 |
6 |
ALLOC_OBJ(w, WATCH_MAGIC); |
576 |
6 |
AN(w); |
577 |
6 |
w->keylen = asprintf(&w->key, "%s:", key); |
578 |
6 |
assert(w->keylen > 0); |
579 |
6 |
VTAILQ_INSERT_TAIL(&CTX.watch_vcl_log, w, list); |
580 |
|
|
581 |
6 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
582 |
6 |
AN(f); |
583 |
6 |
f->func = format_fragment; |
584 |
6 |
f->frag = &w->frag; |
585 |
6 |
f->string = strdup(""); |
586 |
6 |
AN(f->string); |
587 |
6 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
588 |
6 |
} |
589 |
|
|
590 |
|
static void |
591 |
165 |
addf_hdr(struct watch_head *head, const char *key) |
592 |
|
{ |
593 |
|
struct watch *w; |
594 |
|
struct format *f; |
595 |
|
|
596 |
165 |
AN(head); |
597 |
165 |
AN(key); |
598 |
165 |
ALLOC_OBJ(w, WATCH_MAGIC); |
599 |
165 |
AN(w); |
600 |
165 |
w->keylen = asprintf(&w->key, "%s:", key); |
601 |
165 |
assert(w->keylen > 0); |
602 |
165 |
VTAILQ_INSERT_TAIL(head, w, list); |
603 |
|
|
604 |
165 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
605 |
165 |
AN(f); |
606 |
165 |
f->func = format_fragment; |
607 |
165 |
f->frag = &w->frag; |
608 |
165 |
f->string = strdup(CTX.missing_string); |
609 |
165 |
AN(f->string); |
610 |
165 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
611 |
165 |
} |
612 |
|
|
613 |
|
static void |
614 |
39 |
addf_vsl(enum VSL_tag_e tag, long i, const char *prefix) |
615 |
|
{ |
616 |
|
struct vsl_watch *w; |
617 |
|
|
618 |
39 |
ALLOC_OBJ(w, VSL_WATCH_MAGIC); |
619 |
39 |
AN(w); |
620 |
39 |
if (VSL_tagflags[tag] && CTX.quote_how != VSB_QUOTE_JSON) |
621 |
6 |
VUT_Error(vut, 1, "Tag %s can contain control characters", |
622 |
3 |
VSL_tags[tag]); |
623 |
36 |
w->tag = tag; |
624 |
36 |
assert(i <= INT_MAX); |
625 |
36 |
w->idx = i; |
626 |
36 |
if (prefix != NULL) { |
627 |
9 |
w->prefixlen = asprintf(&w->prefix, "%s:", prefix); |
628 |
9 |
assert(w->prefixlen > 0); |
629 |
9 |
} |
630 |
36 |
VTAILQ_INSERT_TAIL(&CTX.watch_vsl, w, list); |
631 |
36 |
addf_fragment(&w->frag, CTX.missing_string); |
632 |
36 |
} |
633 |
|
|
634 |
|
static void |
635 |
60 |
addf_auth(void) |
636 |
|
{ |
637 |
|
struct format *f; |
638 |
|
|
639 |
60 |
ALLOC_OBJ(f, FORMAT_MAGIC); |
640 |
60 |
AN(f); |
641 |
60 |
f->func = format_auth; |
642 |
60 |
f->string = strdup("-"); |
643 |
60 |
AN(f->string); |
644 |
60 |
VTAILQ_INSERT_TAIL(&CTX.format, f, list); |
645 |
60 |
} |
646 |
|
|
647 |
|
static void |
648 |
117 |
parse_x_format(char *buf) |
649 |
|
{ |
650 |
|
char *e, *r, *s; |
651 |
|
long lval; |
652 |
|
int slt; |
653 |
|
|
654 |
117 |
if (!strcmp(buf, "Varnish:time_firstbyte")) { |
655 |
3 |
addf_fragment(&CTX.frag[F_ttfb], CTX.missing_int); |
656 |
3 |
return; |
657 |
|
} |
658 |
114 |
if (!strcmp(buf, "Varnish:hitmiss")) { |
659 |
3 |
addf_strptr(&CTX.hitmiss); |
660 |
3 |
return; |
661 |
|
} |
662 |
111 |
if (!strcmp(buf, "Varnish:handling")) { |
663 |
3 |
addf_strptr(&CTX.handling); |
664 |
3 |
return; |
665 |
|
} |
666 |
108 |
if (!strcmp(buf, "Varnish:side")) { |
667 |
15 |
addf_strptr(&CTX.side); |
668 |
15 |
return; |
669 |
|
} |
670 |
93 |
if (!strcmp(buf, "Varnish:vxid")) { |
671 |
15 |
addf_int64(&CTX.vxid); |
672 |
15 |
return; |
673 |
|
} |
674 |
78 |
if (!strncmp(buf, "VCL_Log:", 8)) { |
675 |
6 |
addf_vcl_log(buf + 8); |
676 |
6 |
return; |
677 |
|
} |
678 |
72 |
if (!strncmp(buf, "VSL:", 4)) { |
679 |
66 |
buf += 4; |
680 |
66 |
e = buf; |
681 |
618 |
while (*e != '\0') |
682 |
552 |
e++; |
683 |
66 |
if (e == buf) |
684 |
3 |
VUT_Error(vut, 1, "Missing tag in VSL:"); |
685 |
63 |
if (e[-1] == ']') { |
686 |
36 |
r = e - 1; |
687 |
150 |
while (r > buf && *r != '[') |
688 |
114 |
r--; |
689 |
36 |
if (r == buf || r[1] == ']') |
690 |
3 |
VUT_Error(vut, 1, "Syntax error: VSL:%s", buf); |
691 |
33 |
e[-1] = '\0'; |
692 |
33 |
lval = strtol(r + 1, &s, 10); |
693 |
33 |
if (s != e - 1) |
694 |
3 |
VUT_Error(vut, 1, "Syntax error: VSL:%s]", buf); |
695 |
30 |
if (lval <= 0 || lval > 255) { |
696 |
12 |
VUT_Error(vut, 1, |
697 |
|
"Syntax error. Field specifier must be" |
698 |
|
" between 1 and 255: %s]", |
699 |
6 |
buf); |
700 |
|
} |
701 |
24 |
*r = '\0'; |
702 |
24 |
} else |
703 |
27 |
lval = 0; |
704 |
51 |
r = buf; |
705 |
429 |
while (r < e && *r != ':') |
706 |
378 |
r++; |
707 |
51 |
if (r != e) { |
708 |
9 |
slt = VSL_Name2Tag(buf, r - buf); |
709 |
9 |
r++; |
710 |
9 |
} else { |
711 |
42 |
slt = VSL_Name2Tag(buf, -1); |
712 |
42 |
r = NULL; |
713 |
|
} |
714 |
51 |
if (slt == -2) |
715 |
3 |
VUT_Error(vut, 1, "Tag not unique: %s", buf); |
716 |
48 |
if (slt == -1) |
717 |
9 |
VUT_Error(vut, 1, "Unknown log tag: %s", buf); |
718 |
39 |
assert(slt >= 0); |
719 |
|
|
720 |
39 |
addf_vsl((enum VSL_tag_e)slt, lval, r); |
721 |
39 |
return; |
722 |
|
} |
723 |
6 |
if (!strcmp(buf, "Varnish:default_format")) { |
724 |
3 |
parse_format(FORMAT); |
725 |
3 |
return; |
726 |
|
} |
727 |
3 |
VUT_Error(vut, 1, "Unknown formatting extension: %s", buf); |
728 |
87 |
} |
729 |
|
|
730 |
|
static void |
731 |
213 |
parse_format(const char *format) |
732 |
|
{ |
733 |
|
const char *p, *q; |
734 |
|
struct vsb *vsb; |
735 |
|
char buf[256]; |
736 |
|
int b; |
737 |
|
|
738 |
213 |
if (format == NULL) |
739 |
45 |
format = FORMAT; |
740 |
|
|
741 |
213 |
vsb = VSB_new_auto(); |
742 |
213 |
AN(vsb); |
743 |
|
|
744 |
2079 |
for (p = format; *p != '\0'; p++) { |
745 |
|
|
746 |
|
/* Allow the most essential escape sequences in format */ |
747 |
1875 |
if (*p == '\\' && p[1] != '\0') { |
748 |
3 |
if (*++p == 't') |
749 |
0 |
AZ(VSB_putc(vsb, '\t')); |
750 |
3 |
else if (*p == 'n') |
751 |
3 |
AZ(VSB_putc(vsb, '\n')); |
752 |
|
else |
753 |
0 |
AZ(VSB_putc(vsb, *p)); |
754 |
3 |
continue; |
755 |
|
} |
756 |
|
|
757 |
1872 |
if (*p != '%') { |
758 |
1062 |
AZ(VSB_putc(vsb, *p)); |
759 |
1062 |
continue; |
760 |
|
} |
761 |
|
|
762 |
810 |
if (VSB_len(vsb) > 0) { |
763 |
612 |
AZ(VSB_finish(vsb)); |
764 |
612 |
addf_string(VSB_data(vsb)); |
765 |
612 |
VSB_clear(vsb); |
766 |
612 |
} |
767 |
|
|
768 |
810 |
p++; |
769 |
810 |
switch (*p) { |
770 |
|
case 'b': /* Body bytes sent */ |
771 |
60 |
addf_fragment(&CTX.frag[F_b], CTX.missing_int); |
772 |
60 |
break; |
773 |
|
case 'D': /* Float request time */ |
774 |
3 |
addf_time('T', "us"); |
775 |
3 |
break; |
776 |
|
case 'h': /* Client host name / IP Address */ |
777 |
60 |
addf_fragment(&CTX.frag[F_h], CTX.missing_string); |
778 |
60 |
break; |
779 |
|
case 'H': /* Protocol */ |
780 |
9 |
addf_fragment(&CTX.frag[F_H], "HTTP/1.0"); |
781 |
9 |
break; |
782 |
|
case 'I': /* Bytes received */ |
783 |
3 |
addf_fragment(&CTX.frag[F_I], CTX.missing_int); |
784 |
3 |
break; |
785 |
|
case 'l': /* Client user ID (identd) always '-' */ |
786 |
51 |
AZ(VSB_putc(vsb, '-')); |
787 |
51 |
break; |
788 |
|
case 'm': /* Method */ |
789 |
9 |
addf_fragment(&CTX.frag[F_m], CTX.missing_string); |
790 |
9 |
break; |
791 |
|
case 'O': /* Bytes sent */ |
792 |
3 |
addf_fragment(&CTX.frag[F_O], CTX.missing_int); |
793 |
3 |
break; |
794 |
|
case 'q': /* Query string */ |
795 |
9 |
addf_fragment(&CTX.frag[F_q], ""); |
796 |
9 |
break; |
797 |
|
case 'r': /* Request line */ |
798 |
57 |
addf_requestline(); |
799 |
57 |
break; |
800 |
|
case 's': /* Status code */ |
801 |
93 |
addf_fragment(&CTX.frag[F_s], CTX.missing_int); |
802 |
93 |
break; |
803 |
|
case 't': /* strftime */ |
804 |
51 |
addf_time(*p, TIME_FMT); |
805 |
51 |
break; |
806 |
|
case 'T': /* Int request time */ |
807 |
3 |
addf_time(*p, "s"); |
808 |
3 |
break; |
809 |
|
case 'u': /* Remote user from auth */ |
810 |
60 |
addf_auth(); |
811 |
60 |
break; |
812 |
|
case 'U': /* URL */ |
813 |
9 |
addf_fragment(&CTX.frag[F_U], CTX.missing_string); |
814 |
9 |
break; |
815 |
|
case '{': |
816 |
327 |
p++; |
817 |
327 |
q = p; |
818 |
327 |
b = 1; |
819 |
3327 |
while (*q) { |
820 |
3324 |
if (*q == '{') |
821 |
12 |
b++; |
822 |
3312 |
else if (*q == '}') |
823 |
336 |
if (--b == 0) |
824 |
324 |
break; |
825 |
3000 |
q++; |
826 |
|
} |
827 |
327 |
if (b > 0) |
828 |
6 |
VUT_Error(vut, 1, "Unmatched bracket at: %s", |
829 |
3 |
p - 2); |
830 |
324 |
assert((unsigned)(q - p) < sizeof buf - 1); |
831 |
324 |
strncpy(buf, p, q - p); |
832 |
324 |
buf[q - p] = '\0'; |
833 |
324 |
q++; |
834 |
324 |
switch (*q) { |
835 |
|
case 'i': |
836 |
147 |
addf_hdr(&CTX.watch_reqhdr, buf); |
837 |
147 |
break; |
838 |
|
case 'o': |
839 |
18 |
addf_hdr(&CTX.watch_resphdr, buf); |
840 |
18 |
break; |
841 |
|
case 't': |
842 |
30 |
addf_time(*q, buf); |
843 |
30 |
break; |
844 |
|
case 'T': |
845 |
9 |
addf_time(*q, buf); |
846 |
9 |
break; |
847 |
|
case 'x': |
848 |
117 |
parse_x_format(buf); |
849 |
117 |
break; |
850 |
|
default: |
851 |
6 |
VUT_Error(vut, 1, |
852 |
|
"Unknown format specifier at: %s", |
853 |
3 |
p - 2); |
854 |
|
} |
855 |
321 |
p = q; |
856 |
321 |
break; |
857 |
|
default: |
858 |
6 |
VUT_Error(vut, 1, "Unknown format specifier at: %s", |
859 |
3 |
p - 1); |
860 |
|
} |
861 |
801 |
} |
862 |
|
|
863 |
204 |
if (VSB_len(vsb) > 0) { |
864 |
|
/* Add any remaining static */ |
865 |
63 |
AZ(VSB_finish(vsb)); |
866 |
63 |
addf_string(VSB_data(vsb)); |
867 |
63 |
VSB_clear(vsb); |
868 |
63 |
} |
869 |
|
|
870 |
204 |
VSB_destroy(&vsb); |
871 |
204 |
} |
872 |
|
|
873 |
|
static int |
874 |
9039 |
isprefix(const char *prefix, size_t len, const char *b, |
875 |
|
const char *e, const char **next) |
876 |
|
{ |
877 |
9039 |
assert(len > 0); |
878 |
9039 |
if (e < b + len || strncasecmp(b, prefix, len)) |
879 |
7710 |
return (0); |
880 |
1329 |
b += len; |
881 |
1329 |
if (next) { |
882 |
2577 |
while (b < e && *b == ' ') |
883 |
1248 |
b++; |
884 |
1329 |
*next = b; |
885 |
1329 |
} |
886 |
1329 |
return (1); |
887 |
9039 |
} |
888 |
|
|
889 |
|
static void |
890 |
1410 |
frag_fields(enum format_policy fp, const char *b, const char *e, ...) |
891 |
|
{ |
892 |
|
va_list ap; |
893 |
|
const char *p, *q; |
894 |
|
int n, field; |
895 |
|
struct fragment *frag; |
896 |
|
|
897 |
1410 |
AN(b); |
898 |
1410 |
AN(e); |
899 |
1410 |
va_start(ap, e); |
900 |
|
|
901 |
1410 |
n = 0; |
902 |
3306 |
while (1) { |
903 |
3315 |
field = va_arg(ap, int); |
904 |
3315 |
frag = va_arg(ap, struct fragment *); |
905 |
3315 |
if (field == 0) { |
906 |
1410 |
AZ(frag); |
907 |
1410 |
break; |
908 |
|
} |
909 |
1905 |
p = q = NULL; |
910 |
5076 |
while (n < field) { |
911 |
4923 |
while (b < e && isspace(*b)) |
912 |
1752 |
b++; |
913 |
3171 |
p = b; |
914 |
27075 |
while (b < e && !isspace(*b)) |
915 |
23904 |
b++; |
916 |
3171 |
q = b; |
917 |
3171 |
n++; |
918 |
|
} |
919 |
1905 |
assert(p != NULL && q != NULL); |
920 |
1905 |
if (p >= e || q <= p) |
921 |
9 |
continue; |
922 |
1896 |
if (frag_needed(frag, fp)) { |
923 |
|
/* We only grab the same matching field once */ |
924 |
1845 |
frag->gen = CTX.gen; |
925 |
1845 |
frag->b = p; |
926 |
1845 |
frag->e = q; |
927 |
1845 |
} |
928 |
|
} |
929 |
1410 |
va_end(ap); |
930 |
1410 |
} |
931 |
|
|
932 |
|
static void |
933 |
1872 |
frag_line(enum format_policy fp, const char *b, const char *e, |
934 |
|
struct fragment *f) |
935 |
|
{ |
936 |
|
|
937 |
1872 |
if (!frag_needed(f, fp)) |
938 |
|
/* We only grab the same matching record once */ |
939 |
189 |
return; |
940 |
|
|
941 |
1683 |
if (e == NULL) |
942 |
12 |
e = b + strlen(b); |
943 |
|
|
944 |
|
/* Skip leading space */ |
945 |
1683 |
while (b < e && isspace(*b)) |
946 |
0 |
++b; |
947 |
|
|
948 |
|
/* Skip trailing space */ |
949 |
1683 |
while (e > b && isspace(e[-1])) |
950 |
0 |
--e; |
951 |
|
|
952 |
1683 |
f->gen = CTX.gen; |
953 |
1683 |
f->b = b; |
954 |
1683 |
f->e = e; |
955 |
1872 |
} |
956 |
|
|
957 |
|
static void |
958 |
2841 |
process_hdr(enum format_policy fp, const struct watch_head *head, const char *b, |
959 |
|
const char *e, int unset) |
960 |
|
{ |
961 |
|
struct watch *w; |
962 |
|
const char *p; |
963 |
|
|
964 |
3906 |
VTAILQ_FOREACH(w, head, list) { |
965 |
1065 |
CHECK_OBJ_NOTNULL(w, WATCH_MAGIC); |
966 |
1065 |
if (!isprefix(w->key, w->keylen, b, e, &p)) |
967 |
927 |
continue; |
968 |
138 |
if (unset) { |
969 |
66 |
frag_line(fp, CTX.missing_string, |
970 |
|
NULL, |
971 |
33 |
&w->frag); |
972 |
33 |
} else |
973 |
105 |
frag_line(fp, p, e, &w->frag); |
974 |
138 |
} |
975 |
2841 |
} |
976 |
|
|
977 |
|
static void |
978 |
9420 |
process_vsl(const struct vsl_watch_head *head, enum VSL_tag_e tag, |
979 |
|
const char *b, const char *e) |
980 |
|
{ |
981 |
|
struct vsl_watch *w; |
982 |
|
const char *p; |
983 |
13488 |
VTAILQ_FOREACH(w, head, list) { |
984 |
4068 |
CHECK_OBJ_NOTNULL(w, VSL_WATCH_MAGIC); |
985 |
4068 |
if (tag != w->tag) |
986 |
3855 |
continue; |
987 |
213 |
p = b; |
988 |
213 |
if (w->prefixlen > 0 && |
989 |
126 |
!isprefix(w->prefix, w->prefixlen, b, e, &p)) |
990 |
114 |
continue; |
991 |
99 |
if (w->idx == 0) |
992 |
18 |
frag_line(FMTPOL_INTERNAL, p, e, &w->frag); |
993 |
|
else |
994 |
81 |
frag_fields(FMTPOL_INTERNAL, p, e, w->idx, &w->frag, 0, NULL); |
995 |
99 |
} |
996 |
9420 |
} |
997 |
|
|
998 |
|
static int v_matchproto_(VSLQ_dispatch_f) |
999 |
333 |
dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[], |
1000 |
|
void *priv) |
1001 |
|
{ |
1002 |
|
struct VSL_transaction *t; |
1003 |
|
enum VSL_tag_e tag; |
1004 |
|
const char *b, *e, *p; |
1005 |
|
struct watch *w; |
1006 |
|
int i, skip; |
1007 |
|
|
1008 |
333 |
(void)vsl; |
1009 |
333 |
(void)priv; |
1010 |
|
|
1011 |
666 |
for (t = pt[0]; t != NULL; t = *++pt) { |
1012 |
333 |
CTX.gen++; |
1013 |
|
|
1014 |
333 |
if (t->type == VSL_t_req) { |
1015 |
222 |
CTX.side = "c"; |
1016 |
333 |
} else if (t->type == VSL_t_bereq) { |
1017 |
36 |
CTX.side = "b"; |
1018 |
36 |
} else |
1019 |
75 |
continue; |
1020 |
|
|
1021 |
258 |
CTX.recv_compl = 0; |
1022 |
258 |
CTX.hitmiss = "-"; |
1023 |
258 |
CTX.handling = "-"; |
1024 |
258 |
CTX.vxid = t->vxid; |
1025 |
258 |
skip = 0; |
1026 |
9687 |
while (skip == 0 && 1 == VSL_Next(t->c)) { |
1027 |
9429 |
tag = VSL_TAG(t->c->rec.ptr); |
1028 |
9429 |
if (VSL_tagflags[tag] && |
1029 |
12 |
CTX.quote_how != VSB_QUOTE_JSON) |
1030 |
9 |
continue; |
1031 |
|
|
1032 |
9420 |
b = VSL_CDATA(t->c->rec.ptr); |
1033 |
9420 |
e = b + VSL_LEN(t->c->rec.ptr); |
1034 |
9420 |
if (!VSL_tagflags[tag]) { |
1035 |
18834 |
while (e > b && e[-1] == '\0') |
1036 |
9417 |
e--; |
1037 |
9417 |
} |
1038 |
|
|
1039 |
9420 |
switch (tag) { |
1040 |
|
case SLT_HttpGarbage: |
1041 |
0 |
skip = 1; |
1042 |
0 |
break; |
1043 |
|
case SLT_PipeAcct: |
1044 |
21 |
frag_fields(FMTPOL_INTERNAL, b, e, |
1045 |
|
3, &CTX.frag[F_I], |
1046 |
|
4, &CTX.frag[F_O], |
1047 |
|
0, NULL); |
1048 |
21 |
break; |
1049 |
|
case SLT_BackendOpen: |
1050 |
36 |
frag_fields(FMTPOL_INTERNAL, b, e, |
1051 |
|
3, &CTX.frag[F_h], |
1052 |
|
0, NULL); |
1053 |
36 |
break; |
1054 |
|
case SLT_ReqStart: |
1055 |
222 |
frag_fields(FMTPOL_INTERNAL, b, e, |
1056 |
|
1, &CTX.frag[F_h], |
1057 |
|
0, NULL); |
1058 |
222 |
break; |
1059 |
|
case SLT_BereqMethod: |
1060 |
|
case SLT_ReqMethod: |
1061 |
267 |
frag_line(FMTPOL_REQ, b, e, &CTX.frag[F_m]); |
1062 |
267 |
break; |
1063 |
|
case SLT_BereqURL: |
1064 |
|
case SLT_ReqURL: |
1065 |
318 |
p = memchr(b, '?', e - b); |
1066 |
318 |
if (p == NULL) |
1067 |
243 |
p = e; |
1068 |
318 |
frag_line(FMTPOL_REQ, b, p, &CTX.frag[F_U]); |
1069 |
318 |
frag_line(FMTPOL_REQ, p, e, &CTX.frag[F_q]); |
1070 |
318 |
break; |
1071 |
|
case SLT_BereqProtocol: |
1072 |
|
case SLT_ReqProtocol: |
1073 |
264 |
frag_line(FMTPOL_REQ, b, e, &CTX.frag[F_H]); |
1074 |
264 |
break; |
1075 |
|
case SLT_BerespStatus: |
1076 |
|
case SLT_RespStatus: |
1077 |
240 |
frag_line(FMTPOL_RESP, b, e, &CTX.frag[F_s]); |
1078 |
240 |
break; |
1079 |
|
case SLT_BereqAcct: |
1080 |
|
case SLT_ReqAcct: |
1081 |
237 |
frag_fields(FMTPOL_INTERNAL, b, e, |
1082 |
|
3, &CTX.frag[F_I], |
1083 |
|
5, &CTX.frag[F_b], |
1084 |
|
6, &CTX.frag[F_O], |
1085 |
|
0, NULL); |
1086 |
237 |
break; |
1087 |
|
case SLT_Timestamp: |
1088 |
|
#define ISPREFIX(a, b, c, d) isprefix(a, strlen(a), b, c, d) |
1089 |
1251 |
if (ISPREFIX("Start:", b, e, &p)) { |
1090 |
258 |
frag_fields(FMTPOL_INTERNAL, p, e, 1, |
1091 |
|
&CTX.frag[F_tstart], 0, NULL); |
1092 |
|
|
1093 |
2025 |
} else if (ISPREFIX("Resp:", b, e, &p) || |
1094 |
795 |
ISPREFIX("PipeSess:", b, e, &p) || |
1095 |
774 |
ISPREFIX("BerespBody:", b, e, &p)) { |
1096 |
249 |
frag_fields(FMTPOL_INTERNAL, p, e, 1, |
1097 |
|
&CTX.frag[F_tend], 0, NULL); |
1098 |
|
|
1099 |
1461 |
} else if (ISPREFIX("Process:", b, e, &p) || |
1100 |
489 |
ISPREFIX("Pipe:", b, e, &p) || |
1101 |
468 |
ISPREFIX("Beresp:", b, e, &p)) { |
1102 |
306 |
frag_fields(FMTPOL_INTERNAL, p, e, 2, |
1103 |
|
&CTX.frag[F_ttfb], 0, NULL); |
1104 |
306 |
} |
1105 |
1251 |
break; |
1106 |
|
case SLT_BereqHeader: |
1107 |
|
case SLT_ReqHeader: |
1108 |
1167 |
process_hdr(FMTPOL_REQ, &CTX.watch_reqhdr, b, e, 0); |
1109 |
1167 |
if (ISPREFIX("Authorization:", b, e, &p) && |
1110 |
60 |
ISPREFIX("basic ", p, e, &p)) |
1111 |
60 |
frag_line(FMTPOL_REQ, p, e, |
1112 |
|
&CTX.frag[F_auth]); |
1113 |
1107 |
else if (ISPREFIX("Host:", b, e, &p)) |
1114 |
246 |
frag_line(FMTPOL_REQ, p, e, |
1115 |
|
&CTX.frag[F_host]); |
1116 |
|
#undef ISPREFIX |
1117 |
1167 |
break; |
1118 |
|
case SLT_BerespHeader: |
1119 |
|
case SLT_RespHeader: |
1120 |
1581 |
process_hdr(FMTPOL_RESP, &CTX.watch_resphdr, b, e, 0); |
1121 |
1581 |
break; |
1122 |
|
case SLT_BereqUnset: |
1123 |
|
case SLT_ReqUnset: |
1124 |
33 |
process_hdr(FMTPOL_REQ, &CTX.watch_reqhdr, b, e, 1); |
1125 |
33 |
break; |
1126 |
|
case SLT_BerespUnset: |
1127 |
|
case SLT_RespUnset: |
1128 |
60 |
process_hdr(FMTPOL_RESP, &CTX.watch_resphdr, b, e, 1); |
1129 |
60 |
break; |
1130 |
|
case SLT_VCL_call: |
1131 |
894 |
if (!strcasecmp(b, "recv")) { |
1132 |
219 |
CTX.recv_compl = 1; |
1133 |
219 |
CTX.hitmiss = "-"; |
1134 |
219 |
CTX.handling = "-"; |
1135 |
894 |
} else if (!strcasecmp(b, "hit")) { |
1136 |
48 |
CTX.hitmiss = "hit"; |
1137 |
48 |
CTX.handling = "hit"; |
1138 |
675 |
} else if (!strcasecmp(b, "miss")) { |
1139 |
123 |
CTX.hitmiss = "miss"; |
1140 |
123 |
CTX.handling = "miss"; |
1141 |
627 |
} else if (!strcasecmp(b, "pass")) { |
1142 |
12 |
CTX.hitmiss = "miss"; |
1143 |
12 |
CTX.handling = "pass"; |
1144 |
504 |
} else if (!strcasecmp(b, "synth")) { |
1145 |
|
/* Arguably, synth isn't a hit or |
1146 |
|
a miss, but miss is less |
1147 |
|
wrong */ |
1148 |
33 |
CTX.hitmiss = "miss"; |
1149 |
33 |
CTX.handling = "synth"; |
1150 |
492 |
} else if (!strcasecmp(b, "backend_response")) { |
1151 |
30 |
CTX.recv_compl = 1; |
1152 |
30 |
} |
1153 |
894 |
break; |
1154 |
|
case SLT_VCL_return: |
1155 |
894 |
if (!strcasecmp(b, "pipe")) { |
1156 |
42 |
CTX.hitmiss = "miss"; |
1157 |
42 |
CTX.handling = "pipe"; |
1158 |
894 |
} else if (!strcasecmp(b, "restart")) |
1159 |
0 |
skip = 1; |
1160 |
894 |
break; |
1161 |
|
case SLT_VCL_Log: |
1162 |
33 |
VTAILQ_FOREACH(w, &CTX.watch_vcl_log, list) { |
1163 |
3 |
CHECK_OBJ_NOTNULL(w, WATCH_MAGIC); |
1164 |
3 |
if (e - b < w->keylen || |
1165 |
3 |
strncmp(b, w->key, w->keylen)) |
1166 |
0 |
continue; |
1167 |
3 |
p = b + w->keylen; |
1168 |
3 |
frag_line(FMTPOL_INTERNAL, p, e, &w->frag); |
1169 |
3 |
} |
1170 |
30 |
break; |
1171 |
|
default: |
1172 |
1905 |
break; |
1173 |
|
} |
1174 |
|
|
1175 |
9420 |
process_vsl(&CTX.watch_vsl, tag, b, e); |
1176 |
|
} |
1177 |
258 |
if (skip) |
1178 |
0 |
continue; |
1179 |
258 |
i = print(); |
1180 |
258 |
if (i) |
1181 |
0 |
return (i); |
1182 |
258 |
} |
1183 |
|
|
1184 |
333 |
return (0); |
1185 |
333 |
} |
1186 |
|
|
1187 |
|
static char * |
1188 |
9 |
read_format(const char *formatfile) |
1189 |
|
{ |
1190 |
|
FILE *fmtfile; |
1191 |
9 |
size_t len = 0; |
1192 |
|
int fmtlen; |
1193 |
9 |
char *fmt = NULL; |
1194 |
|
|
1195 |
9 |
fmtfile = fopen(formatfile, "r"); |
1196 |
9 |
if (fmtfile == NULL) |
1197 |
6 |
VUT_Error(vut, 1, "Can't open format file (%s)", |
1198 |
3 |
strerror(errno)); |
1199 |
6 |
AN(fmtfile); |
1200 |
6 |
fmtlen = getline(&fmt, &len, fmtfile); |
1201 |
6 |
if (fmtlen == -1) { |
1202 |
3 |
free(fmt); |
1203 |
3 |
if (feof(fmtfile)) |
1204 |
3 |
VUT_Error(vut, 1, "Empty format file"); |
1205 |
0 |
VUT_Error(vut, 1, "Can't read format from file (%s)", |
1206 |
0 |
strerror(errno)); |
1207 |
|
} |
1208 |
3 |
AZ(fclose(fmtfile)); |
1209 |
3 |
if (fmt[fmtlen - 1] == '\n') |
1210 |
3 |
fmt[fmtlen - 1] = '\0'; |
1211 |
3 |
return (fmt); |
1212 |
|
} |
1213 |
|
|
1214 |
|
int |
1215 |
291 |
main(int argc, char * const *argv) |
1216 |
|
{ |
1217 |
|
signed char opt; |
1218 |
291 |
char *format = NULL; |
1219 |
291 |
int mode_opt = 0; |
1220 |
|
|
1221 |
291 |
vut = VUT_InitProg(argc, argv, &vopt_spec); |
1222 |
291 |
AN(vut); |
1223 |
291 |
memset(&CTX, 0, sizeof CTX); |
1224 |
291 |
VTAILQ_INIT(&CTX.format); |
1225 |
291 |
VTAILQ_INIT(&CTX.watch_vcl_log); |
1226 |
291 |
VTAILQ_INIT(&CTX.watch_reqhdr); |
1227 |
291 |
VTAILQ_INIT(&CTX.watch_resphdr); |
1228 |
291 |
VTAILQ_INIT(&CTX.watch_vsl); |
1229 |
291 |
CTX.vsb = VSB_new_auto(); |
1230 |
291 |
AN(CTX.vsb); |
1231 |
291 |
CTX.quote_how = VSB_QUOTE_ESCHEX; |
1232 |
291 |
REPLACE(CTX.missing_string, "-"); |
1233 |
291 |
REPLACE(CTX.missing_int, "-"); |
1234 |
|
|
1235 |
177 |
tzset(); // We use localtime_r(3) |
1236 |
|
|
1237 |
855 |
while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) { |
1238 |
681 |
switch (opt) { |
1239 |
|
case 'a': |
1240 |
|
/* Append to file */ |
1241 |
0 |
CTX.a_opt = 1; |
1242 |
0 |
break; |
1243 |
|
case 'b': /* backend mode */ |
1244 |
|
case 'c': /* client mode */ |
1245 |
|
case 'E': /* show ESI */ |
1246 |
48 |
AN(VUT_Arg(vut, opt, NULL)); |
1247 |
48 |
mode_opt = 1; |
1248 |
48 |
break; |
1249 |
|
case 'F': |
1250 |
162 |
if (format != NULL) |
1251 |
0 |
VUT_Error(vut, 1, "Format already set"); |
1252 |
162 |
REPLACE(format, optarg); |
1253 |
162 |
break; |
1254 |
|
case 'f': |
1255 |
3 |
if (format != NULL) |
1256 |
0 |
VUT_Error(vut, 1, "Format already set"); |
1257 |
|
/* Format string from file */ |
1258 |
3 |
format = read_format(optarg); |
1259 |
3 |
AN(format); |
1260 |
3 |
break; |
1261 |
|
case 'h': |
1262 |
|
/* Usage help */ |
1263 |
3 |
VUT_Usage(vut, &vopt_spec, 0); |
1264 |
|
break; |
1265 |
|
case 'j': |
1266 |
15 |
REPLACE(CTX.missing_string, ""); |
1267 |
15 |
REPLACE(CTX.missing_int, "0"); |
1268 |
15 |
CTX.quote_how = VSB_QUOTE_JSON; |
1269 |
15 |
break; |
1270 |
|
case 'w': |
1271 |
|
/* Write to file */ |
1272 |
12 |
REPLACE(CTX.w_arg, optarg); |
1273 |
12 |
break; |
1274 |
|
default: |
1275 |
438 |
if (!VUT_Arg(vut, opt, optarg)) |
1276 |
0 |
VUT_Usage(vut, &vopt_spec, 1); |
1277 |
438 |
} |
1278 |
|
} |
1279 |
|
|
1280 |
|
/* default is client mode: */ |
1281 |
174 |
if (!mode_opt) |
1282 |
174 |
AN(VUT_Arg(vut, 'c', NULL)); |
1283 |
|
|
1284 |
174 |
if (optind != argc) |
1285 |
3 |
VUT_Usage(vut, &vopt_spec, 1); |
1286 |
|
|
1287 |
171 |
if (vut->D_opt && !CTX.w_arg) |
1288 |
3 |
VUT_Error(vut, 1, "Missing -w option"); |
1289 |
|
|
1290 |
168 |
if (vut->D_opt && !strcmp(CTX.w_arg, "-")) |
1291 |
0 |
VUT_Error(vut, 1, "Daemon cannot write to stdout"); |
1292 |
|
|
1293 |
|
/* Check for valid grouping mode */ |
1294 |
168 |
assert(vut->g_arg < VSL_g__MAX); |
1295 |
168 |
if (vut->g_arg != VSL_g_vxid && vut->g_arg != VSL_g_request) |
1296 |
6 |
VUT_Error(vut, 1, "Invalid grouping mode: %s", |
1297 |
3 |
VSLQ_grouping[vut->g_arg]); |
1298 |
|
|
1299 |
|
/* Prepare output format */ |
1300 |
165 |
parse_format(format); |
1301 |
165 |
REPLACE(format, NULL); |
1302 |
|
|
1303 |
|
/* Setup output */ |
1304 |
165 |
vut->dispatch_f = dispatch_f; |
1305 |
165 |
vut->dispatch_priv = NULL; |
1306 |
165 |
if (CTX.w_arg) { |
1307 |
9 |
openout(CTX.a_opt); |
1308 |
9 |
AN(CTX.fo); |
1309 |
9 |
if (vut->D_opt) |
1310 |
3 |
vut->sighup_f = rotateout; |
1311 |
9 |
} else |
1312 |
156 |
CTX.fo = stdout; |
1313 |
165 |
vut->idle_f = flushout; |
1314 |
|
|
1315 |
165 |
VUT_Setup(vut); |
1316 |
165 |
(void)VUT_Main(vut); |
1317 |
165 |
VUT_Fini(&vut); |
1318 |
|
|
1319 |
165 |
exit(0); |
1320 |
|
} |