0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10 #include <nginx.h>
|
|
11
|
|
12
|
|
13 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
|
|
14 uintptr_t data);
|
|
15 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
16 uintptr_t data);
|
|
17 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
|
|
18 uintptr_t data);
|
|
19 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
|
|
20 uintptr_t data);
|
|
21 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
|
|
22 uintptr_t data);
|
|
23 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
|
|
24 uintptr_t data);
|
|
25 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
|
|
26 uintptr_t data);
|
|
27 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
|
|
28 uintptr_t data);
|
|
29 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
|
|
30 uintptr_t data);
|
|
31 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
|
|
32 uintptr_t data);
|
|
33 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
|
|
34 u_char *buf, uintptr_t data);
|
|
35 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
|
|
36 u_char *buf,
|
|
37 uintptr_t data);
|
|
38 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
|
|
39 u_char *buf, uintptr_t data);
|
|
40 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
|
|
41 uintptr_t data);
|
|
42 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
|
|
43 uintptr_t data);
|
|
44
|
|
45 static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf);
|
|
46 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
|
|
47 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
|
|
48 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
49 void *child);
|
|
50 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
51 void *conf);
|
|
52 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
53 void *conf);
|
|
54 static ngx_int_t ngx_http_log_parse_format(ngx_conf_t *cf, ngx_array_t *ops,
|
|
55 ngx_str_t *line);
|
|
56
|
|
57
|
|
58 static ngx_command_t ngx_http_log_commands[] = {
|
|
59
|
|
60 {ngx_string("log_format"),
|
|
61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
|
|
62 ngx_http_log_set_format,
|
|
63 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
64 0,
|
|
65 NULL},
|
|
66
|
|
67 {ngx_string("access_log"),
|
|
68 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
|
69 ngx_http_log_set_log,
|
|
70 NGX_HTTP_LOC_CONF_OFFSET,
|
|
71 0,
|
|
72 NULL},
|
|
73
|
|
74 ngx_null_command
|
|
75 };
|
|
76
|
|
77
|
|
78 ngx_http_module_t ngx_http_log_module_ctx = {
|
|
79 ngx_http_log_pre_conf, /* pre conf */
|
|
80
|
|
81 ngx_http_log_create_main_conf, /* create main configuration */
|
|
82 NULL, /* init main configuration */
|
|
83
|
|
84 NULL, /* create server configuration */
|
|
85 NULL, /* merge server configuration */
|
|
86
|
|
87 ngx_http_log_create_loc_conf, /* create location configration */
|
|
88 ngx_http_log_merge_loc_conf /* merge location configration */
|
|
89 };
|
|
90
|
|
91
|
|
92 ngx_module_t ngx_http_log_module = {
|
|
93 NGX_MODULE,
|
|
94 &ngx_http_log_module_ctx, /* module context */
|
|
95 ngx_http_log_commands, /* module directives */
|
|
96 NGX_HTTP_MODULE, /* module type */
|
|
97 NULL, /* init module */
|
|
98 NULL /* init child */
|
|
99 };
|
|
100
|
|
101
|
|
102 static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
|
|
103
|
|
104
|
|
105 static ngx_str_t ngx_http_combined_fmt =
|
|
106 ngx_string("%addr - - [%time] \"%request\" %status %apache_length "
|
|
107 "\"%{Referer}i\" \"%{User-Agent}i\"");
|
|
108
|
|
109
|
|
110 ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
|
|
111 { ngx_string("addr"), INET_ADDRSTRLEN - 1, ngx_http_log_addr },
|
|
112 { ngx_string("conn"), NGX_INT32_LEN, ngx_http_log_connection },
|
|
113 { ngx_string("pipe"), 1, ngx_http_log_pipe },
|
|
114 { ngx_string("time"), sizeof("28/Sep/1970:12:00:00") - 1,
|
|
115 ngx_http_log_time },
|
|
116 { ngx_string("msec"), TIME_T_LEN + 4, ngx_http_log_msec },
|
|
117 { ngx_string("request"), 0, ngx_http_log_request },
|
|
118 { ngx_string("status"), 3, ngx_http_log_status },
|
|
119 { ngx_string("length"), NGX_OFF_T_LEN, ngx_http_log_length },
|
|
120 { ngx_string("apache_length"), NGX_OFF_T_LEN, ngx_http_log_apache_length },
|
|
121 { ngx_string("i"), NGX_HTTP_LOG_ARG, ngx_http_log_header_in },
|
|
122 { ngx_string("o"), NGX_HTTP_LOG_ARG, ngx_http_log_header_out },
|
|
123 { ngx_null_string, 0, NULL }
|
|
124 };
|
|
125
|
|
126
|
|
127 ngx_int_t ngx_http_log_handler(ngx_http_request_t *r)
|
|
128 {
|
|
129 ngx_uint_t i, l;
|
|
130 uintptr_t data;
|
|
131 u_char *line, *p;
|
|
132 size_t len;
|
|
133 ngx_http_log_t *log;
|
|
134 ngx_http_log_op_t *op;
|
|
135 ngx_http_log_loc_conf_t *lcf;
|
10
|
136 #if (NGX_WIN32)
|
0
|
137 u_long written;
|
|
138 #endif
|
|
139
|
|
140 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
141 "http log handler");
|
|
142
|
|
143 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
144
|
|
145 if (lcf->off) {
|
|
146 return NGX_OK;
|
|
147 }
|
|
148
|
|
149 log = lcf->logs->elts;
|
|
150 for (l = 0; l < lcf->logs->nelts; l++) {
|
|
151
|
|
152 len = 0;
|
|
153 op = log[l].ops->elts;
|
|
154 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
155 if (op[i].len == 0) {
|
|
156 len += (size_t) op[i].op(r, NULL, op[i].data);
|
|
157
|
|
158 } else {
|
|
159 len += op[i].len;
|
|
160 }
|
|
161 }
|
|
162
|
10
|
163 #if (NGX_WIN32)
|
0
|
164 len += 2;
|
|
165 #else
|
|
166 len++;
|
|
167 #endif
|
|
168
|
22
|
169 if (!(line = ngx_palloc(r->pool, len))) {
|
|
170 return NGX_ERROR;
|
|
171 }
|
|
172
|
0
|
173 p = line;
|
|
174
|
|
175 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
176 if (op[i].op == NGX_HTTP_LOG_COPY_SHORT) {
|
|
177 len = op[i].len;
|
|
178 data = op[i].data;
|
|
179 while (len--) {
|
|
180 *p++ = (char) (data & 0xff);
|
|
181 data >>= 8;
|
|
182 }
|
|
183
|
|
184 } else if (op[i].op == NGX_HTTP_LOG_COPY_LONG) {
|
|
185 p = ngx_cpymem(p, (void *) op[i].data, op[i].len);
|
|
186
|
|
187 } else {
|
|
188 p = op[i].op(r, p, op[i].data);
|
|
189 }
|
|
190 }
|
|
191
|
10
|
192 #if (NGX_WIN32)
|
0
|
193 *p++ = CR; *p++ = LF;
|
|
194 WriteFile(log[l].file->fd, line, p - line, &written, NULL);
|
|
195 #else
|
|
196 *p++ = LF;
|
|
197 write(log[l].file->fd, line, p - line);
|
|
198 #endif
|
|
199 }
|
|
200
|
|
201 return NGX_OK;
|
|
202 }
|
|
203
|
|
204
|
|
205 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
|
|
206 uintptr_t data)
|
|
207 {
|
|
208 return ngx_cpymem(buf, r->connection->addr_text.data,
|
|
209 r->connection->addr_text.len);
|
|
210 }
|
|
211
|
|
212
|
|
213 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
214 uintptr_t data)
|
|
215 {
|
4
|
216 return ngx_sprintf(buf, "%ui", r->connection->number);
|
0
|
217 }
|
|
218
|
|
219
|
|
220 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
|
|
221 uintptr_t data)
|
|
222 {
|
|
223 if (r->pipeline) {
|
|
224 *buf = 'p';
|
|
225 } else {
|
|
226 *buf = '.';
|
|
227 }
|
|
228
|
|
229 return buf + 1;
|
|
230 }
|
|
231
|
|
232
|
|
233 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
|
|
234 uintptr_t data)
|
|
235 {
|
|
236 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
|
|
237 ngx_cached_http_log_time.len);
|
|
238 }
|
|
239
|
|
240
|
|
241 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
|
|
242 uintptr_t data)
|
|
243 {
|
|
244 struct timeval tv;
|
|
245
|
|
246 ngx_gettimeofday(&tv);
|
|
247
|
4
|
248 return ngx_sprintf(buf, "%l.%03l", tv.tv_sec, tv.tv_usec / 1000);
|
0
|
249 }
|
|
250
|
|
251
|
|
252 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
|
|
253 uintptr_t data)
|
|
254 {
|
|
255 if (buf == NULL) {
|
|
256 /* find the request line length */
|
|
257 return (u_char *) r->request_line.len;
|
|
258 }
|
|
259
|
|
260 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
|
|
261 }
|
|
262
|
|
263
|
|
264 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
|
|
265 uintptr_t data)
|
|
266 {
|
4
|
267 return ngx_sprintf(buf, "%ui",
|
|
268 r->err_status ? r->err_status : r->headers_out.status);
|
0
|
269 }
|
|
270
|
|
271
|
|
272 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
|
|
273 uintptr_t data)
|
|
274 {
|
4
|
275 return ngx_sprintf(buf, "%O", r->connection->sent);
|
0
|
276 }
|
|
277
|
|
278
|
|
279 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
|
|
280 uintptr_t data)
|
|
281 {
|
4
|
282 return ngx_sprintf(buf, "%O", r->connection->sent - r->header_size);
|
0
|
283 }
|
|
284
|
|
285
|
|
286 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
|
|
287 uintptr_t data)
|
|
288 {
|
|
289 ngx_uint_t i;
|
|
290 ngx_str_t *s;
|
|
291 ngx_table_elt_t *h;
|
|
292 ngx_http_log_op_t *op;
|
|
293
|
|
294 if (r) {
|
|
295 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
|
|
296
|
|
297 if (h == NULL) {
|
|
298
|
|
299 /* no header */
|
|
300
|
|
301 if (buf) {
|
|
302 *buf = '-';
|
|
303 }
|
|
304
|
|
305 return buf + 1;
|
|
306 }
|
|
307
|
|
308 if (buf == NULL) {
|
|
309 /* find the header length */
|
|
310 return (u_char *) h->value.len;
|
|
311 }
|
|
312
|
|
313 return ngx_cpymem(buf, h->value.data, h->value.len);
|
|
314 }
|
|
315
|
|
316 /* find an offset while a format string compilation */
|
|
317
|
|
318 op = (ngx_http_log_op_t *) buf;
|
|
319 s = (ngx_str_t *) data;
|
|
320
|
|
321 op->len = 0;
|
|
322
|
|
323 for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
|
|
324 if (ngx_http_headers_in[i].name.len != s->len) {
|
|
325 continue;
|
|
326 }
|
|
327
|
|
328 if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, s->data, s->len)
|
|
329 == 0)
|
|
330 {
|
|
331 op->op = ngx_http_log_header_in;
|
|
332 op->data = ngx_http_headers_in[i].offset;
|
|
333 return NULL;
|
|
334 }
|
|
335 }
|
|
336
|
|
337 op->op = ngx_http_log_unknown_header_in;
|
|
338 op->data = (uintptr_t) s;
|
|
339
|
|
340 return NULL;
|
|
341 }
|
|
342
|
|
343
|
|
344 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
|
|
345 u_char *buf, uintptr_t data)
|
|
346 {
|
|
347 ngx_uint_t i;
|
|
348 ngx_str_t *s;
|
|
349 ngx_list_part_t *part;
|
|
350 ngx_table_elt_t *h;
|
|
351
|
|
352 s = (ngx_str_t *) data;
|
|
353
|
|
354 part = &r->headers_in.headers.part;
|
|
355 h = part->elts;
|
|
356
|
|
357 for (i = 0; /* void */; i++) {
|
|
358
|
|
359 if (i >= part->nelts) {
|
|
360 if (part->next == NULL) {
|
|
361 break;
|
|
362 }
|
|
363
|
|
364 part = part->next;
|
|
365 h = part->elts;
|
|
366 i = 0;
|
|
367 }
|
|
368
|
|
369 if (h[i].key.len != s->len) {
|
|
370 continue;
|
|
371 }
|
|
372
|
|
373 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) {
|
|
374 if (buf == NULL) {
|
|
375 /* find the header length */
|
|
376 return (u_char *) h[i].value.len;
|
|
377 }
|
|
378
|
|
379 return ngx_cpymem(buf, h[i].value.data, h[i].value.len);
|
|
380 }
|
|
381 }
|
|
382
|
|
383 /* no header */
|
|
384
|
|
385 if (buf) {
|
|
386 *buf = '-';
|
|
387 }
|
|
388
|
|
389 return buf + 1;
|
|
390 }
|
|
391
|
|
392
|
|
393 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
|
|
394 uintptr_t data)
|
|
395 {
|
|
396 ngx_uint_t i;
|
|
397 ngx_str_t *s;
|
|
398 ngx_table_elt_t *h;
|
|
399 ngx_http_log_op_t *op;
|
|
400
|
|
401 if (r) {
|
|
402
|
|
403 /* run-time execution */
|
|
404
|
|
405 if (r->http_version < NGX_HTTP_VERSION_10) {
|
|
406 if (buf) {
|
|
407 *buf = '-';
|
|
408 }
|
|
409
|
|
410 return buf + 1;
|
|
411 }
|
|
412
|
|
413 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
|
|
414
|
|
415 if (h == NULL) {
|
|
416
|
|
417 /*
|
|
418 * No header pointer was found.
|
|
419 * However, some headers: "Date", "Server", "Content-Length",
|
|
420 * and "Last-Modified" have a special handling in the header filter
|
|
421 * but we do not set up their pointers in the filter because
|
|
422 * they are too seldom needed to be logged.
|
|
423 */
|
|
424
|
|
425 if (data == offsetof(ngx_http_headers_out_t, date)) {
|
|
426 if (buf == NULL) {
|
|
427 return (u_char *) ngx_cached_http_time.len;
|
|
428 }
|
|
429 return ngx_cpymem(buf, ngx_cached_http_time.data,
|
|
430 ngx_cached_http_time.len);
|
|
431 }
|
|
432
|
|
433 if (data == offsetof(ngx_http_headers_out_t, server)) {
|
|
434 if (buf == NULL) {
|
|
435 return (u_char *) (sizeof(NGINX_VER) - 1);
|
|
436 }
|
|
437 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
|
|
438 }
|
|
439
|
|
440 if (data == offsetof(ngx_http_headers_out_t, content_length)) {
|
|
441 if (r->headers_out.content_length_n == -1) {
|
|
442 if (buf) {
|
|
443 *buf = '-';
|
|
444 }
|
|
445 return buf + 1;
|
|
446 }
|
|
447
|
|
448 if (buf == NULL) {
|
|
449 return (u_char *) NGX_OFF_T_LEN;
|
|
450 }
|
10
|
451 return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
|
0
|
452 }
|
|
453
|
|
454 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
|
|
455 if (r->headers_out.last_modified_time == -1) {
|
|
456 if (buf) {
|
|
457 *buf = '-';
|
|
458 }
|
|
459 return buf + 1;
|
|
460 }
|
|
461
|
|
462 if (buf == NULL) {
|
|
463 return (u_char *)
|
|
464 sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
|
|
465 }
|
4
|
466 return ngx_http_time(buf, r->headers_out.last_modified_time);
|
0
|
467 }
|
|
468
|
|
469 if (buf) {
|
|
470 *buf = '-';
|
|
471 }
|
|
472
|
|
473 return buf + 1;
|
|
474 }
|
|
475
|
|
476 if (buf == NULL) {
|
|
477 /* find the header length */
|
|
478 return (u_char *) h->value.len;
|
|
479 }
|
|
480
|
|
481 return ngx_cpymem(buf, h->value.data, h->value.len);
|
|
482 }
|
|
483
|
|
484 /* find an offset while a format string compilation */
|
|
485
|
|
486 op = (ngx_http_log_op_t *) buf;
|
|
487 s = (ngx_str_t *) data;
|
|
488
|
|
489 op->len = 0;
|
|
490
|
|
491 for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
|
|
492 if (ngx_http_headers_out[i].name.len != s->len) {
|
|
493 continue;
|
|
494 }
|
|
495
|
|
496 if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, s->data, s->len)
|
|
497 == 0)
|
|
498 {
|
|
499 op->op = ngx_http_log_header_out;
|
|
500 op->data = ngx_http_headers_out[i].offset;
|
|
501 return NULL;
|
|
502 }
|
|
503 }
|
|
504
|
|
505 if (s->len == sizeof("Connection") - 1
|
|
506 && ngx_strncasecmp(s->data, "Connection", s->len) == 0)
|
|
507 {
|
|
508 op->op = ngx_http_log_connection_header_out;
|
|
509 op->data = (uintptr_t) NULL;
|
|
510 return NULL;
|
|
511 }
|
|
512
|
|
513 if (s->len == sizeof("Transfer-Encoding") - 1
|
|
514 && ngx_strncasecmp(s->data, "Transfer-Encoding", s->len) == 0) {
|
|
515 op->op = ngx_http_log_transfer_encoding_header_out;
|
|
516 op->data = (uintptr_t) NULL;
|
|
517 return NULL;
|
|
518 }
|
|
519
|
|
520 op->op = ngx_http_log_unknown_header_out;
|
|
521 op->data = (uintptr_t) s;
|
|
522
|
|
523 return NULL;
|
|
524 }
|
|
525
|
|
526
|
|
527 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
|
|
528 u_char *buf, uintptr_t data)
|
|
529 {
|
|
530 if (buf == NULL) {
|
|
531 return (u_char *) ((r->keepalive) ? sizeof("keep-alive") - 1:
|
|
532 sizeof("close") - 1);
|
|
533 }
|
|
534
|
|
535 if (r->keepalive) {
|
|
536 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
|
|
537
|
|
538 } else {
|
|
539 return ngx_cpymem(buf, "close", sizeof("close") - 1);
|
|
540 }
|
|
541 }
|
|
542
|
|
543
|
|
544 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
|
|
545 u_char *buf,
|
|
546 uintptr_t data)
|
|
547 {
|
|
548 if (buf == NULL) {
|
|
549 return (u_char *) ((r->chunked) ? sizeof("chunked") - 1 : 1);
|
|
550 }
|
|
551
|
|
552 if (r->chunked) {
|
|
553 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
|
|
554 }
|
|
555
|
|
556 *buf = '-';
|
|
557
|
|
558 return buf + 1;
|
|
559 }
|
|
560
|
|
561
|
|
562 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
|
|
563 u_char *buf,
|
|
564 uintptr_t data)
|
|
565 {
|
|
566 ngx_uint_t i;
|
|
567 ngx_str_t *s;
|
|
568 ngx_list_part_t *part;
|
|
569 ngx_table_elt_t *h;
|
|
570
|
|
571 s = (ngx_str_t *) data;
|
|
572
|
|
573 part = &r->headers_out.headers.part;
|
|
574 h = part->elts;
|
|
575
|
|
576 for (i = 0; /* void */; i++) {
|
|
577
|
|
578 if (i >= part->nelts) {
|
|
579 if (part->next == NULL) {
|
|
580 break;
|
|
581 }
|
|
582
|
|
583 part = part->next;
|
|
584 h = part->elts;
|
|
585 i = 0;
|
|
586 }
|
|
587
|
|
588 if (h[i].key.len != s->len) {
|
|
589 continue;
|
|
590 }
|
|
591
|
|
592 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) {
|
|
593 if (buf == NULL) {
|
|
594 /* find the header length */
|
|
595 return (u_char *) h[i].value.len;
|
|
596 }
|
|
597
|
|
598 return ngx_cpymem(buf, h[i].value.data, h[i].value.len);
|
|
599 }
|
|
600 }
|
|
601
|
|
602 /* no header */
|
|
603
|
|
604 if (buf) {
|
|
605 *buf = '-';
|
|
606 }
|
|
607
|
|
608 return buf + 1;
|
|
609 }
|
|
610
|
|
611
|
|
612 static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf)
|
|
613 {
|
|
614 ngx_http_log_op_name_t *op;
|
|
615
|
|
616 for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
|
|
617 op->op = NULL;
|
|
618
|
|
619 return NGX_OK;
|
|
620 }
|
|
621
|
|
622
|
|
623 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
624 {
|
|
625 ngx_http_log_main_conf_t *conf;
|
|
626
|
|
627 char *rc;
|
|
628 ngx_str_t *value;
|
|
629
|
|
630 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t)))) {
|
|
631 return NGX_CONF_ERROR;
|
|
632 }
|
|
633
|
|
634 ngx_init_array(conf->formats, cf->pool, 5, sizeof(ngx_http_log_fmt_t),
|
|
635 NGX_CONF_ERROR);
|
|
636
|
|
637 cf->args->nelts = 0;
|
|
638
|
|
639 if (!(value = ngx_push_array(cf->args))) {
|
|
640 return NGX_CONF_ERROR;
|
|
641 }
|
|
642
|
|
643 if (!(value = ngx_push_array(cf->args))) {
|
|
644 return NGX_CONF_ERROR;
|
|
645 }
|
|
646
|
|
647 value->len = sizeof("combined") - 1;
|
|
648 value->data = (u_char *) "combined";
|
|
649
|
|
650 if (!(value = ngx_push_array(cf->args))) {
|
|
651 return NGX_CONF_ERROR;
|
|
652 }
|
|
653
|
|
654 *value = ngx_http_combined_fmt;
|
|
655
|
|
656 rc = ngx_http_log_set_format(cf, NULL, conf);
|
|
657 if (rc != NGX_CONF_OK) {
|
|
658 return NULL;
|
|
659 }
|
|
660
|
|
661 return conf;
|
|
662 }
|
|
663
|
|
664
|
|
665 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
666 {
|
|
667 ngx_http_log_loc_conf_t *conf;
|
|
668
|
|
669 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t)))) {
|
|
670 return NGX_CONF_ERROR;
|
|
671 }
|
|
672
|
|
673 return conf;
|
|
674 }
|
|
675
|
|
676
|
|
677 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
678 void *child)
|
|
679 {
|
|
680 ngx_http_log_loc_conf_t *prev = parent;
|
|
681 ngx_http_log_loc_conf_t *conf = child;
|
|
682
|
|
683 ngx_http_log_t *log;
|
|
684 ngx_http_log_fmt_t *fmt;
|
|
685 ngx_http_log_main_conf_t *lmcf;
|
|
686
|
|
687 if (conf->logs == NULL) {
|
|
688
|
|
689 if (conf->off) {
|
|
690 return NGX_CONF_OK;
|
|
691 }
|
|
692
|
|
693 if (prev->logs) {
|
|
694 conf->logs = prev->logs;
|
|
695
|
|
696 } else {
|
|
697
|
|
698 if (prev->off) {
|
|
699 conf->off = prev->off;
|
|
700 return NGX_CONF_OK;
|
|
701 }
|
|
702
|
|
703 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
704 if (conf->logs == NULL) {
|
|
705 return NGX_CONF_ERROR;
|
|
706 }
|
|
707
|
|
708 if (!(log = ngx_array_push(conf->logs))) {
|
|
709 return NGX_CONF_ERROR;
|
|
710 }
|
|
711
|
|
712 log->file = ngx_conf_open_file(cf->cycle, &http_access_log);
|
|
713 if (log->file == NULL) {
|
|
714 return NGX_CONF_ERROR;
|
|
715 }
|
|
716
|
|
717 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
718 fmt = lmcf->formats.elts;
|
|
719
|
|
720 /* the default "combined" format */
|
|
721 log->ops = fmt[0].ops;
|
|
722 }
|
|
723 }
|
|
724
|
|
725 return NGX_CONF_OK;
|
|
726 }
|
|
727
|
|
728
|
|
729 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
730 void *conf)
|
|
731 {
|
|
732 ngx_http_log_loc_conf_t *llcf = conf;
|
|
733
|
|
734 ngx_uint_t i;
|
|
735 ngx_str_t *value, name;
|
|
736 ngx_http_log_t *log;
|
|
737 ngx_http_log_fmt_t *fmt;
|
|
738 ngx_http_log_main_conf_t *lmcf;
|
|
739
|
|
740 value = cf->args->elts;
|
|
741
|
|
742 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
743 llcf->off = 1;
|
|
744 return NGX_CONF_OK;
|
|
745 }
|
|
746
|
|
747 if (llcf->logs == NULL) {
|
|
748 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
749 if (llcf->logs == NULL) {
|
|
750 return NGX_CONF_ERROR;
|
|
751 }
|
|
752 }
|
|
753
|
|
754 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
755
|
|
756 if (!(log = ngx_array_push(llcf->logs))) {
|
|
757 return NGX_CONF_ERROR;
|
|
758 }
|
|
759
|
|
760 if (!(log->file = ngx_conf_open_file(cf->cycle, &value[1]))) {
|
|
761 return NGX_CONF_ERROR;
|
|
762 }
|
|
763
|
|
764 if (cf->args->nelts == 3) {
|
|
765 name = value[2];
|
|
766 } else {
|
|
767 name.len = sizeof("combined") - 1;
|
|
768 name.data = (u_char *) "combined";
|
|
769 }
|
|
770
|
|
771 fmt = lmcf->formats.elts;
|
|
772 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
773 if (fmt[i].name.len == name.len
|
|
774 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
775 {
|
|
776 log->ops = fmt[i].ops;
|
|
777 return NGX_CONF_OK;
|
|
778 }
|
|
779 }
|
|
780
|
|
781 return NGX_CONF_OK;
|
|
782 }
|
|
783
|
|
784
|
|
785 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
786 void *conf)
|
|
787 {
|
|
788 ngx_http_log_main_conf_t *lmcf = conf;
|
|
789
|
|
790 ngx_uint_t s, f, invalid;
|
|
791 u_char *data, *p, *fname;
|
|
792 size_t i, len, fname_len;
|
|
793 ngx_str_t *value, arg, *a;
|
|
794 ngx_http_log_op_t *op;
|
|
795 ngx_http_log_fmt_t *fmt;
|
|
796 ngx_http_log_op_name_t *name;
|
|
797
|
|
798 value = cf->args->elts;
|
|
799
|
|
800 fmt = lmcf->formats.elts;
|
|
801 for (f = 0; f < lmcf->formats.nelts; f++) {
|
|
802 if (fmt[f].name.len == value[1].len
|
|
803 && ngx_strcmp(fmt->name.data, value[1].data) == 0)
|
|
804 {
|
|
805 return "duplicate \"log_format\" name";
|
|
806 }
|
|
807 }
|
|
808
|
|
809 if (!(fmt = ngx_push_array(&lmcf->formats))) {
|
|
810 return NGX_CONF_ERROR;
|
|
811 }
|
|
812
|
|
813 fmt->name = value[1];
|
|
814
|
|
815 if (!(fmt->ops = ngx_create_array(cf->pool, 20,
|
|
816 sizeof(ngx_http_log_op_t)))) {
|
|
817 return NGX_CONF_ERROR;
|
|
818 }
|
|
819
|
|
820 invalid = 0;
|
|
821 data = NULL;
|
|
822
|
|
823 for (s = 2; s < cf->args->nelts && !invalid; s++) {
|
|
824
|
|
825 i = 0;
|
|
826
|
|
827 while (i < value[s].len) {
|
|
828
|
|
829 if (!(op = ngx_push_array(fmt->ops))) {
|
|
830 return NGX_CONF_ERROR;
|
|
831 }
|
|
832
|
|
833 data = &value[s].data[i];
|
|
834
|
|
835 if (value[s].data[i] == '%') {
|
|
836 i++;
|
|
837
|
|
838 if (i == value[s].len) {
|
|
839 invalid = 1;
|
|
840 break;
|
|
841 }
|
|
842
|
|
843 if (value[s].data[i] == '{') {
|
|
844 i++;
|
|
845
|
|
846 arg.data = &value[s].data[i];
|
|
847
|
|
848 while (i < value[s].len && value[s].data[i] != '}') {
|
|
849 i++;
|
|
850 }
|
|
851
|
|
852 arg.len = &value[s].data[i] - arg.data;
|
|
853
|
|
854 if (i == value[s].len || arg.len == 0) {
|
|
855 invalid = 1;
|
|
856 break;
|
|
857 }
|
|
858
|
|
859 i++;
|
|
860
|
|
861 } else {
|
|
862 arg.len = 0;
|
|
863 }
|
|
864
|
|
865 fname = &value[s].data[i];
|
|
866
|
|
867 while (i < value[s].len
|
|
868 && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
|
|
869 || value[s].data[i] == '_'))
|
|
870 {
|
|
871 i++;
|
|
872 }
|
|
873
|
|
874 fname_len = &value[s].data[i] - fname;
|
|
875
|
|
876 if (fname_len == 0) {
|
|
877 invalid = 1;
|
|
878 break;
|
|
879 }
|
|
880
|
|
881 for (name = ngx_http_log_fmt_ops; name->op; name++) {
|
|
882 if (name->name.len == 0) {
|
|
883 name = (ngx_http_log_op_name_t *) name->op;
|
|
884 }
|
|
885
|
|
886 if (name->name.len == fname_len
|
|
887 && ngx_strncmp(name->name.data, fname, fname_len) == 0)
|
|
888 {
|
|
889 if (name->len != NGX_HTTP_LOG_ARG) {
|
|
890 if (arg.len) {
|
|
891 fname[fname_len] = '\0';
|
|
892 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
893 "\"%s\" must not have argument",
|
|
894 data);
|
|
895 return NGX_CONF_ERROR;
|
|
896 }
|
|
897
|
|
898 op->len = name->len;
|
|
899 op->op = name->op;
|
|
900 op->data = 0;
|
|
901
|
|
902 break;
|
|
903 }
|
|
904
|
|
905 if (arg.len == 0) {
|
|
906 fname[fname_len] = '\0';
|
|
907 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
908 "\"%s\" requires argument",
|
|
909 data);
|
|
910 return NGX_CONF_ERROR;
|
|
911 }
|
|
912
|
|
913 if (!(a = ngx_palloc(cf->pool, sizeof(ngx_str_t)))) {
|
|
914 return NGX_CONF_ERROR;
|
|
915 }
|
|
916
|
|
917 *a = arg;
|
|
918 name->op(NULL, (u_char *) op, (uintptr_t) a);
|
|
919
|
|
920 break;
|
|
921 }
|
|
922 }
|
|
923
|
|
924 if (name->name.len == 0) {
|
|
925 invalid = 1;
|
|
926 break;
|
|
927 }
|
|
928
|
|
929 } else {
|
|
930 i++;
|
|
931
|
|
932 while (i < value[s].len && value[s].data[i] != '%') {
|
|
933 i++;
|
|
934 }
|
|
935
|
|
936 len = &value[s].data[i] - data;
|
|
937
|
|
938 if (len) {
|
|
939
|
|
940 op->len = len;
|
|
941
|
|
942 if (len <= sizeof(uintptr_t)) {
|
|
943 op->op = NGX_HTTP_LOG_COPY_SHORT;
|
|
944 op->data = 0;
|
|
945
|
|
946 while (len--) {
|
|
947 op->data <<= 8;
|
|
948 op->data |= data[len];
|
|
949 }
|
|
950
|
|
951 } else {
|
|
952 op->op = NGX_HTTP_LOG_COPY_LONG;
|
|
953
|
|
954 if (!(p = ngx_palloc(cf->pool, len))) {
|
|
955 return NGX_CONF_ERROR;
|
|
956 }
|
|
957
|
|
958 ngx_memcpy(p, data, len);
|
|
959 op->data = (uintptr_t) p;
|
|
960 }
|
|
961 }
|
|
962 }
|
|
963 }
|
|
964 }
|
|
965
|
|
966 if (invalid) {
|
|
967 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
968 "invalid parameter \"%s\"", data);
|
|
969 return NGX_CONF_ERROR;
|
|
970 }
|
|
971
|
|
972 return NGX_CONF_OK;
|
|
973 }
|