comparison src/http/modules/ngx_http_log_module.c @ 118:644a7935144b NGINX_0_3_6

nginx 0.3.6 *) Change: now the IMAP/POP3 proxy do not send the empty login to authorization server. *) Feature: the "log_format" supports the variables in the $name form. *) Bugfix: if at least in one server was no the "listen" directive, then nginx did not listen on the 80 port; bug appeared in 0.3.3. *) Bugfix: if the URI part is omitted in "proxy_pass" directive, the the 80 port was always used.
author Igor Sysoev <http://sysoev.ru>
date Mon, 24 Oct 2005 00:00:00 +0400
parents
children e85dca77c46a
comparison
equal deleted inserted replaced
117:0821ea4ccfc5 118:644a7935144b
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 ngx_http_log_op_t *op);
15 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
16 ngx_http_log_op_t *op);
17 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
18 ngx_http_log_op_t *op);
19 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
20 ngx_http_log_op_t *op);
21 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
22 ngx_http_log_op_t *op);
23 static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
24 ngx_http_log_op_t *op);
25 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
26 ngx_http_log_op_t *op);
27 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
28 ngx_http_log_op_t *op);
29 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
30 ngx_http_log_op_t *op);
31 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
32 ngx_http_log_op_t *op);
33
34 static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
35 uintptr_t data);
36 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
37 ngx_http_log_op_t *op);
38
39 static ngx_int_t ngx_http_log_header_in_compile(ngx_conf_t *cf,
40 ngx_http_log_op_t *op, ngx_str_t *value);
41 static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
42 uintptr_t data);
43 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
44 ngx_http_log_op_t *op);
45 static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
46 uintptr_t data);
47 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
48 u_char *buf, ngx_http_log_op_t *op);
49
50 static ngx_int_t ngx_http_log_header_out_compile(ngx_conf_t *cf,
51 ngx_http_log_op_t *op, ngx_str_t *value);
52 static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
53 uintptr_t data);
54 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
55 ngx_http_log_op_t *op);
56 static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
57 uintptr_t data);
58 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
59 u_char *buf, ngx_http_log_op_t *op);
60
61 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
62 u_char *buf, ngx_http_log_op_t *op);
63 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
64 u_char *buf, ngx_http_log_op_t *op);
65
66 static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
67 ngx_str_t *value);
68
69 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
70 ngx_http_log_op_t *op, ngx_str_t *value);
71 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
72 uintptr_t data);
73 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
74 ngx_http_log_op_t *op);
75
76
77 static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf);
78 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
79 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
80 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
81 void *child);
82 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
83 void *conf);
84 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
85 void *conf);
86 static ngx_int_t ngx_http_log_init(ngx_cycle_t *cycle);
87
88
89 static ngx_command_t ngx_http_log_commands[] = {
90
91 { ngx_string("log_format"),
92 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
93 ngx_http_log_set_format,
94 NGX_HTTP_MAIN_CONF_OFFSET,
95 0,
96 NULL },
97
98 { ngx_string("access_log"),
99 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
100 ngx_http_log_set_log,
101 NGX_HTTP_LOC_CONF_OFFSET,
102 0,
103 NULL },
104
105 ngx_null_command
106 };
107
108
109 ngx_http_module_t ngx_http_log_module_ctx = {
110 ngx_http_log_set_formats, /* preconfiguration */
111 NULL, /* postconfiguration */
112
113 ngx_http_log_create_main_conf, /* create main configuration */
114 NULL, /* init main configuration */
115
116 NULL, /* create server configuration */
117 NULL, /* merge server configuration */
118
119 ngx_http_log_create_loc_conf, /* create location configration */
120 ngx_http_log_merge_loc_conf /* merge location configration */
121 };
122
123
124 ngx_module_t ngx_http_log_module = {
125 NGX_MODULE_V1,
126 &ngx_http_log_module_ctx, /* module context */
127 ngx_http_log_commands, /* module directives */
128 NGX_HTTP_MODULE, /* module type */
129 NULL, /* init master */
130 ngx_http_log_init, /* init module */
131 NULL, /* init process */
132 NULL, /* init thread */
133 NULL, /* exit thread */
134 NULL, /* exit process */
135 NULL, /* exit master */
136 NGX_MODULE_V1_PADDING
137 };
138
139
140 static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
141
142
143 static ngx_str_t ngx_http_combined_fmt =
144 #if 0
145 ngx_string("$remote_addr - $remote_user [%time] "
146 "\"$request\" %status %apache_length "
147 "\"$http_referer\" \"$http_user_agent\"");
148 #endif
149 ngx_string("%addr - - [%time] "
150 "\"%request\" %status %apache_length "
151 "\"%{referer}i\" \"%{user-agent}i\"");
152
153
154 ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
155 { ngx_string("addr"), INET_ADDRSTRLEN - 1, NULL, NULL, ngx_http_log_addr },
156 { ngx_string("conn"), NGX_ATOMIC_T_LEN, NULL, NULL,
157 ngx_http_log_connection },
158 { ngx_string("pipe"), 1, NULL, NULL, ngx_http_log_pipe },
159 { ngx_string("time"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
160 NULL, NULL, ngx_http_log_time },
161 { ngx_string("msec"), NGX_TIME_T_LEN + 4, NULL, NULL, ngx_http_log_msec },
162 { ngx_string("request_time"), NGX_TIME_T_LEN, NULL, NULL,
163 ngx_http_log_request_time },
164 { ngx_string("status"), 3, NULL, NULL, ngx_http_log_status },
165 { ngx_string("length"), NGX_OFF_T_LEN, NULL, NULL, ngx_http_log_length },
166 { ngx_string("apache_length"), NGX_OFF_T_LEN,
167 NULL, NULL, ngx_http_log_apache_length },
168 { ngx_string("request_length"), NGX_SIZE_T_LEN,
169 NULL, NULL, ngx_http_log_request_length },
170
171 { ngx_string("request"), 0, NULL,
172 ngx_http_log_request_getlen,
173 ngx_http_log_request },
174
175 { ngx_string("i"), 0, ngx_http_log_header_in_compile, NULL,
176 ngx_http_log_header_in },
177 { ngx_string("o"), 0, ngx_http_log_header_out_compile, NULL,
178 ngx_http_log_header_out },
179 { ngx_string("v"), 0, ngx_http_log_variable_compile, NULL,
180 ngx_http_log_variable },
181
182 { ngx_null_string, 0, NULL, NULL, NULL }
183 };
184
185
186 ngx_int_t
187 ngx_http_log_handler(ngx_http_request_t *r)
188 {
189 ngx_uint_t i, l;
190 u_char *line, *p;
191 size_t len;
192 ngx_http_log_t *log;
193 ngx_http_log_op_t *op;
194 ngx_http_log_loc_conf_t *lcf;
195 #if (NGX_WIN32)
196 u_long written;
197 #endif
198
199 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
200 "http log handler");
201
202 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
203
204 if (lcf->off) {
205 return NGX_OK;
206 }
207
208 log = lcf->logs->elts;
209 for (l = 0; l < lcf->logs->nelts; l++) {
210
211 len = 0;
212 op = log[l].ops->elts;
213 for (i = 0; i < log[l].ops->nelts; i++) {
214 if (op[i].len == 0) {
215 len += op[i].getlen(r, op[i].data);
216
217 } else {
218 len += op[i].len;
219 }
220 }
221
222 #if (NGX_WIN32)
223 len += 2;
224 #else
225 len++;
226 #endif
227
228 line = ngx_palloc(r->pool, len);
229 if (line == NULL) {
230 return NGX_ERROR;
231 }
232
233 p = line;
234
235 for (i = 0; i < log[l].ops->nelts; i++) {
236 p = op[i].run(r, p, &op[i]);
237 }
238
239 #if (NGX_WIN32)
240 *p++ = CR; *p++ = LF;
241 WriteFile(log[l].file->fd, line, p - line, &written, NULL);
242 #else
243 *p++ = LF;
244 write(log[l].file->fd, line, p - line);
245 #endif
246 }
247
248 return NGX_OK;
249 }
250
251
252 static u_char *
253 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
254 ngx_http_log_op_t *op)
255 {
256 size_t len;
257 uintptr_t data;
258
259 len = op->len;
260 data = op->data;
261
262 while (len--) {
263 *buf++ = (u_char) (data & 0xff);
264 data >>= 8;
265 }
266
267 return buf;
268 }
269
270
271 static u_char *
272 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
273 ngx_http_log_op_t *op)
274 {
275 return ngx_cpymem(buf, (u_char *) op->data, op->len);
276 }
277
278
279 static u_char *
280 ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
281 {
282 return ngx_cpymem(buf, r->connection->addr_text.data,
283 r->connection->addr_text.len);
284 }
285
286
287 static u_char *
288 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
289 ngx_http_log_op_t *op)
290 {
291 return ngx_sprintf(buf, "%ui", r->connection->number);
292 }
293
294
295 static u_char *
296 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
297 {
298 if (r->pipeline) {
299 *buf = 'p';
300 } else {
301 *buf = '.';
302 }
303
304 return buf + 1;
305 }
306
307
308 static u_char *
309 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
310 {
311 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
312 ngx_cached_http_log_time.len);
313 }
314
315
316 static u_char *
317 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
318 {
319 ngx_time_t *tp;
320
321 tp = ngx_timeofday();
322
323 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
324 }
325
326
327 static u_char *
328 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
329 ngx_http_log_op_t *op)
330 {
331 time_t elapsed;
332
333 elapsed = ngx_time() - r->start_time;
334
335 return ngx_sprintf(buf, "%T", elapsed);
336 }
337
338
339 static size_t
340 ngx_http_log_request_getlen(ngx_http_request_t *r, uintptr_t data)
341 {
342 return r->request_line.len;
343 }
344
345
346 static u_char *
347 ngx_http_log_request(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
348 {
349 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
350 }
351
352
353 static u_char *
354 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
355 {
356 return ngx_sprintf(buf, "%ui",
357 r->err_status ? r->err_status : r->headers_out.status);
358 }
359
360
361 static u_char *
362 ngx_http_log_length(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
363 {
364 return ngx_sprintf(buf, "%O", r->connection->sent);
365 }
366
367
368 static u_char *
369 ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
370 ngx_http_log_op_t *op)
371 {
372 off_t length;
373
374 length = r->connection->sent - r->header_size;
375
376 if (length > 0) {
377 return ngx_sprintf(buf, "%O", length);
378 }
379
380 *buf = '0';
381
382 return buf + 1;
383 }
384
385
386 static u_char *
387 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
388 ngx_http_log_op_t *op)
389 {
390 return ngx_sprintf(buf, "%z", r->request_length);
391 }
392
393
394 static ngx_int_t
395 ngx_http_log_header_in_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
396 ngx_str_t *value)
397 {
398 ngx_uint_t i;
399
400 op->len = 0;
401
402 for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
403
404 if (ngx_http_headers_in[i].name.len != value->len) {
405 continue;
406 }
407
408 /* STUB: "Cookie" speacial handling */
409 if (ngx_http_headers_in[i].offset == 0) {
410 continue;
411 }
412
413 if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, value->data,
414 value->len) == 0)
415 {
416 op->getlen = ngx_http_log_header_in_getlen;
417 op->run = ngx_http_log_header_in;
418 op->data = ngx_http_headers_in[i].offset;
419
420 return NGX_OK;
421 }
422 }
423
424 op->getlen = ngx_http_log_unknown_header_in_getlen;
425 op->run = ngx_http_log_unknown_header_in;
426 op->data = (uintptr_t) value;
427
428 return NGX_OK;
429 }
430
431
432 static size_t
433 ngx_http_log_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
434 {
435 ngx_table_elt_t *h;
436
437 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
438
439 if (h) {
440 return h->value.len;
441 }
442
443 return 1;
444 }
445
446
447 static u_char *
448 ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
449 ngx_http_log_op_t *op)
450 {
451 ngx_table_elt_t *h;
452
453 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + op->data);
454
455 if (h) {
456 return ngx_cpymem(buf, h->value.data, h->value.len);
457 }
458
459 *buf = '-';
460
461 return buf + 1;
462 }
463
464
465 static size_t
466 ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
467 {
468 ngx_table_elt_t *h;
469
470 h = ngx_http_log_unknown_header(&r->headers_in.headers, (ngx_str_t *) data);
471
472 if (h) {
473 return h->value.len;
474 }
475
476 return 1;
477 }
478
479
480 static u_char *
481 ngx_http_log_unknown_header_in(ngx_http_request_t *r, u_char *buf,
482 ngx_http_log_op_t *op)
483 {
484 ngx_table_elt_t *h;
485
486 h = ngx_http_log_unknown_header(&r->headers_in.headers,
487 (ngx_str_t *) op->data);
488
489 if (h) {
490 return ngx_cpymem(buf, h->value.data, h->value.len);
491 }
492
493 *buf = '-';
494
495 return buf + 1;
496 }
497
498
499 static ngx_int_t
500 ngx_http_log_header_out_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
501 ngx_str_t *value)
502 {
503 ngx_uint_t i;
504
505 op->len = 0;
506
507 for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
508
509 if (ngx_http_headers_out[i].name.len != value->len) {
510 continue;
511 }
512
513 if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, value->data,
514 value->len) == 0)
515 {
516 op->getlen = ngx_http_log_header_out_getlen;
517 op->run = ngx_http_log_header_out;
518 op->data = ngx_http_headers_out[i].offset;
519
520 return NGX_OK;
521 }
522 }
523
524 if (value->len == sizeof("Connection") - 1
525 && ngx_strncasecmp(value->data, "Connection", value->len) == 0)
526 {
527 op->len = sizeof("keep-alive") - 1;
528 op->getlen = NULL;
529 op->run = ngx_http_log_connection_header_out;
530 op->data = 0;
531 return NGX_OK;
532 }
533
534 if (value->len == sizeof("Transfer-Encoding") - 1
535 && ngx_strncasecmp(value->data, "Transfer-Encoding", value->len) == 0)
536 {
537 op->len = sizeof("chunked") - 1;
538 op->getlen = NULL;
539 op->run = ngx_http_log_transfer_encoding_header_out;
540 op->data = 0;
541 return NGX_OK;
542 }
543
544 op->getlen = ngx_http_log_unknown_header_out_getlen;
545 op->run = ngx_http_log_unknown_header_out;
546 op->data = (uintptr_t) value;
547
548 return NGX_OK;
549 }
550
551
552 static size_t
553 ngx_http_log_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
554 {
555 ngx_table_elt_t *h;
556
557 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
558
559 if (h) {
560 return h->value.len;
561 }
562
563 /*
564 * No header pointer was found.
565 * However, some headers: "Date", "Server", "Content-Length",
566 * and "Last-Modified" have a special handling in the header filter,
567 * but we do not set up their pointers in the filter,
568 * because they are too seldom needed to be logged.
569 */
570
571 if (data == offsetof(ngx_http_headers_out_t, date)) {
572 return ngx_cached_http_time.len;
573 }
574
575 if (data == offsetof(ngx_http_headers_out_t, server)) {
576 return (sizeof(NGINX_VER) - 1);
577 }
578
579 if (data == offsetof(ngx_http_headers_out_t, content_length)) {
580 if (r->headers_out.content_length_n == -1) {
581 return 1;
582 }
583
584 return NGX_OFF_T_LEN;
585 }
586
587 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
588 if (r->headers_out.last_modified_time == -1) {
589 return 1;
590 }
591
592 return sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
593 }
594
595 return 1;
596 }
597
598
599 static u_char *
600 ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
601 ngx_http_log_op_t *op)
602 {
603 ngx_table_elt_t *h;
604
605 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + op->data);
606
607 if (h) {
608 return ngx_cpymem(buf, h->value.data, h->value.len);
609 }
610
611 /*
612 * No header pointer was found.
613 * However, some headers: "Date", "Server", "Content-Length",
614 * and "Last-Modified" have a special handling in the header filter,
615 * but we do not set up their pointers in the filter,
616 * because they are too seldom needed to be logged.
617 */
618
619 if (op->data == offsetof(ngx_http_headers_out_t, date)) {
620 return ngx_cpymem(buf, ngx_cached_http_time.data,
621 ngx_cached_http_time.len);
622 }
623
624 if (op->data == offsetof(ngx_http_headers_out_t, server)) {
625 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
626 }
627
628 if (op->data == offsetof(ngx_http_headers_out_t, content_length)) {
629 if (r->headers_out.content_length_n == -1) {
630 *buf = '-';
631
632 return buf + 1;
633 }
634
635 return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
636 }
637
638 if (op->data == offsetof(ngx_http_headers_out_t, last_modified)) {
639 if (r->headers_out.last_modified_time == -1) {
640 *buf = '-';
641
642 return buf + 1;
643 }
644
645 return ngx_http_time(buf, r->headers_out.last_modified_time);
646 }
647
648 *buf = '-';
649
650 return buf + 1;
651 }
652
653
654 static size_t
655 ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
656 {
657 ngx_table_elt_t *h;
658
659 h = ngx_http_log_unknown_header(&r->headers_out.headers,
660 (ngx_str_t *) data);
661
662 if (h) {
663 return h->value.len;
664 }
665
666 return 1;
667 }
668
669
670 static u_char *
671 ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
672 ngx_http_log_op_t *op)
673 {
674 ngx_table_elt_t *h;
675
676 h = ngx_http_log_unknown_header(&r->headers_out.headers,
677 (ngx_str_t *) op->data);
678
679 if (h) {
680 return ngx_cpymem(buf, h->value.data, h->value.len);
681 }
682
683 *buf = '-';
684
685 return buf + 1;
686 }
687
688
689 static ngx_table_elt_t *
690 ngx_http_log_unknown_header(ngx_list_t *headers, ngx_str_t *value)
691 {
692 ngx_uint_t i;
693 ngx_list_part_t *part;
694 ngx_table_elt_t *h;
695
696 part = &headers->part;
697 h = part->elts;
698
699 for (i = 0; /* void */; i++) {
700
701 if (i >= part->nelts) {
702 if (part->next == NULL) {
703 break;
704 }
705
706 part = part->next;
707 h = part->elts;
708 i = 0;
709 }
710
711 if (h[i].key.len != value->len) {
712 continue;
713 }
714
715 if (ngx_strncasecmp(h[i].key.data, value->data, value->len) == 0) {
716 return &h[i];
717 }
718 }
719
720 return NULL;
721 }
722
723
724 static u_char *
725 ngx_http_log_connection_header_out(ngx_http_request_t *r, u_char *buf,
726 ngx_http_log_op_t *op)
727 {
728 if (r->keepalive) {
729 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
730
731 } else {
732 return ngx_cpymem(buf, "close", sizeof("close") - 1);
733 }
734 }
735
736
737 static u_char *
738 ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, u_char *buf,
739 ngx_http_log_op_t *op)
740 {
741 if (r->chunked) {
742 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
743 }
744
745 *buf = '-';
746
747 return buf + 1;
748 }
749
750
751 static ngx_int_t
752 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
753 ngx_str_t *value)
754 {
755 ngx_int_t index;
756
757 index = ngx_http_get_variable_index(cf, value);
758 if (index == NGX_ERROR) {
759 return NGX_ERROR;
760 }
761
762 op->len = 0;
763 op->getlen = ngx_http_log_variable_getlen;
764 op->run = ngx_http_log_variable;
765 op->data = index;
766
767 return NGX_OK;
768 }
769
770
771 static size_t
772 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
773 {
774 ngx_http_variable_value_t *value;
775
776 value = ngx_http_get_indexed_variable(r, data);
777
778 if (value == NULL
779 || value == NGX_HTTP_VAR_NOT_FOUND
780 || value->text.len == 0)
781 {
782 return 1;
783 }
784
785 return value->text.len;
786 }
787
788
789 static u_char *
790 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
791 {
792 ngx_http_variable_value_t *value;
793
794 value = ngx_http_get_indexed_variable(r, op->data);
795
796 if (value == NULL
797 || value == NGX_HTTP_VAR_NOT_FOUND
798 || value->text.len == 0)
799 {
800 *buf = '-';
801 return buf + 1;
802 }
803
804 return ngx_cpymem(buf, value->text.data, value->text.len);
805 }
806
807
808 static ngx_int_t
809 ngx_http_log_set_formats(ngx_conf_t *cf)
810 {
811 ngx_http_log_op_name_t *op;
812
813 for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
814 op->run = NULL;
815
816 return NGX_OK;
817 }
818
819
820 static void *
821 ngx_http_log_create_main_conf(ngx_conf_t *cf)
822 {
823 ngx_http_log_main_conf_t *conf;
824
825 char *rc;
826 ngx_str_t *value;
827
828 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
829 if (conf == NULL) {
830 return NGX_CONF_ERROR;
831 }
832
833 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
834 == NGX_ERROR)
835 {
836 return NGX_CONF_ERROR;
837 }
838
839 cf->args->nelts = 0;
840
841 value = ngx_array_push(cf->args);
842 if (value == NULL) {
843 return NGX_CONF_ERROR;
844 }
845
846 value->len = 0;
847 value->data = NULL;
848
849 value = ngx_array_push(cf->args);
850 if (value == NULL) {
851 return NGX_CONF_ERROR;
852 }
853
854 value->len = sizeof("combined") - 1;
855 value->data = (u_char *) "combined";
856
857 value = ngx_array_push(cf->args);
858 if (value == NULL) {
859 return NGX_CONF_ERROR;
860 }
861
862 *value = ngx_http_combined_fmt;
863
864 rc = ngx_http_log_set_format(cf, NULL, conf);
865 if (rc != NGX_CONF_OK) {
866 return NGX_CONF_ERROR;
867 }
868
869 return conf;
870 }
871
872
873 static void *
874 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
875 {
876 ngx_http_log_loc_conf_t *conf;
877
878 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
879 if (conf == NULL) {
880 return NGX_CONF_ERROR;
881 }
882
883 return conf;
884 }
885
886
887 static char *
888 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
889 {
890 ngx_http_log_loc_conf_t *prev = parent;
891 ngx_http_log_loc_conf_t *conf = child;
892
893 ngx_http_log_t *log;
894 ngx_http_log_fmt_t *fmt;
895 ngx_http_log_main_conf_t *lmcf;
896
897 if (conf->logs == NULL) {
898
899 if (conf->off) {
900 return NGX_CONF_OK;
901 }
902
903 if (prev->logs) {
904 conf->logs = prev->logs;
905
906 } else {
907
908 if (prev->off) {
909 conf->off = prev->off;
910 return NGX_CONF_OK;
911 }
912
913 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
914 if (conf->logs == NULL) {
915 return NGX_CONF_ERROR;
916 }
917
918 log = ngx_array_push(conf->logs);
919 if (log == NULL) {
920 return NGX_CONF_ERROR;
921 }
922
923 log->file = ngx_conf_open_file(cf->cycle, &http_access_log);
924 if (log->file == NULL) {
925 return NGX_CONF_ERROR;
926 }
927
928 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
929 fmt = lmcf->formats.elts;
930
931 /* the default "combined" format */
932 log->ops = fmt[0].ops;
933 }
934 }
935
936 return NGX_CONF_OK;
937 }
938
939
940 static char *
941 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
942 {
943 ngx_http_log_loc_conf_t *llcf = conf;
944
945 ngx_uint_t i;
946 ngx_str_t *value, name;
947 ngx_http_log_t *log;
948 ngx_http_log_fmt_t *fmt;
949 ngx_http_log_main_conf_t *lmcf;
950
951 value = cf->args->elts;
952
953 if (ngx_strcmp(value[1].data, "off") == 0) {
954 llcf->off = 1;
955 return NGX_CONF_OK;
956 }
957
958 if (llcf->logs == NULL) {
959 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
960 if (llcf->logs == NULL) {
961 return NGX_CONF_ERROR;
962 }
963 }
964
965 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
966
967 log = ngx_array_push(llcf->logs);
968 if (log == NULL) {
969 return NGX_CONF_ERROR;
970 }
971
972 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
973 if (log->file == NULL) {
974 return NGX_CONF_ERROR;
975 }
976
977 if (cf->args->nelts == 3) {
978 name = value[2];
979 } else {
980 name.len = sizeof("combined") - 1;
981 name.data = (u_char *) "combined";
982 }
983
984 fmt = lmcf->formats.elts;
985 for (i = 0; i < lmcf->formats.nelts; i++) {
986 if (fmt[i].name.len == name.len
987 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
988 {
989 log->ops = fmt[i].ops;
990 return NGX_CONF_OK;
991 }
992 }
993
994 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
995 "unknown log format \"%V\"", &name);
996
997 return NGX_CONF_ERROR;
998 }
999
1000
1001 static char *
1002 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1003 {
1004 ngx_http_log_main_conf_t *lmcf = conf;
1005
1006 u_char *data, *p, *fname, ch;
1007 size_t i, len, fname_len;
1008 ngx_str_t *value, var, arg, *a;
1009 ngx_uint_t s, f, bracket;
1010 ngx_http_log_op_t *op;
1011 ngx_http_log_fmt_t *fmt;
1012 ngx_http_log_op_name_t *name;
1013
1014 value = cf->args->elts;
1015
1016 fmt = lmcf->formats.elts;
1017 for (f = 0; f < lmcf->formats.nelts; f++) {
1018 if (fmt[f].name.len == value[1].len
1019 && ngx_strcmp(fmt->name.data, value[1].data) == 0)
1020 {
1021 return "duplicate \"log_format\" name";
1022 }
1023 }
1024
1025 fmt = ngx_array_push(&lmcf->formats);
1026 if (fmt == NULL) {
1027 return NGX_CONF_ERROR;
1028 }
1029
1030 fmt->name = value[1];
1031
1032 fmt->ops = ngx_array_create(cf->pool, 20, sizeof(ngx_http_log_op_t));
1033 if (fmt->ops == NULL) {
1034 return NGX_CONF_ERROR;
1035 }
1036
1037 arg.data = NULL;
1038
1039 for (s = 2; s < cf->args->nelts; s++) {
1040
1041 i = 0;
1042
1043 while (i < value[s].len) {
1044
1045 op = ngx_array_push(fmt->ops);
1046 if (op == NULL) {
1047 return NGX_CONF_ERROR;
1048 }
1049
1050 data = &value[s].data[i];
1051
1052 if (value[s].data[i] == '%') {
1053 i++;
1054
1055 if (i == value[s].len) {
1056 goto invalid;
1057 }
1058
1059 if (value[s].data[i] == '{') {
1060 i++;
1061
1062 arg.data = &value[s].data[i];
1063
1064 while (i < value[s].len && value[s].data[i] != '}') {
1065 i++;
1066 }
1067
1068 arg.len = &value[s].data[i] - arg.data;
1069
1070 if (i == value[s].len || arg.len == 0) {
1071 goto invalid;
1072 }
1073
1074 i++;
1075
1076 } else {
1077 arg.len = 0;
1078 }
1079
1080 fname = &value[s].data[i];
1081
1082 while (i < value[s].len
1083 && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
1084 || value[s].data[i] == '_'))
1085 {
1086 i++;
1087 }
1088
1089 fname_len = &value[s].data[i] - fname;
1090
1091 if (fname_len == 0) {
1092 goto invalid;
1093 }
1094
1095 for (name = ngx_http_log_fmt_ops; name->run; name++) {
1096 if (name->name.len == 0) {
1097 name = (ngx_http_log_op_name_t *) name->run;
1098 }
1099
1100 if (name->name.len == fname_len
1101 && ngx_strncmp(name->name.data, fname, fname_len) == 0)
1102 {
1103 if (name->compile == NULL) {
1104 if (arg.len) {
1105 fname[fname_len] = '\0';
1106 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1107 "\"%s\" must not have argument",
1108 data);
1109 return NGX_CONF_ERROR;
1110 }
1111
1112 op->len = name->len;
1113 op->getlen = name->getlen;
1114 op->run = name->run;
1115 op->data = 0;
1116
1117 break;
1118 }
1119
1120 if (arg.len == 0) {
1121 fname[fname_len] = '\0';
1122 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1123 "\"%s\" requires argument",
1124 data);
1125 return NGX_CONF_ERROR;
1126 }
1127
1128 a = ngx_palloc(cf->pool, sizeof(ngx_str_t));
1129 if (a == NULL) {
1130 return NGX_CONF_ERROR;
1131 }
1132
1133 *a = arg;
1134 if (name->compile(cf, op, a) == NGX_ERROR) {
1135 return NGX_CONF_ERROR;
1136 }
1137
1138 break;
1139 }
1140 }
1141
1142 if (name->name.len == 0) {
1143 goto invalid;
1144 }
1145
1146 } else if (value[s].data[i] == '$') {
1147
1148 if (++i == value[s].len) {
1149 goto invalid;
1150 }
1151
1152 if (value[s].data[i] == '{') {
1153 bracket = 1;
1154
1155 if (++i == value[s].len) {
1156 goto invalid;
1157 }
1158
1159 var.data = &value[s].data[i];
1160
1161 } else {
1162 bracket = 0;
1163 var.data = &value[s].data[i];
1164 }
1165
1166 for (var.len = 0; i < value[s].len; i++, var.len++) {
1167 ch = value[s].data[i];
1168
1169 if (ch == '}' && bracket) {
1170 i++;
1171 bracket = 0;
1172 break;
1173 }
1174
1175 if ((ch >= 'A' && ch <= 'Z')
1176 || (ch >= 'a' && ch <= 'z')
1177 || (ch >= '0' && ch <= '9')
1178 || ch == '_')
1179 {
1180 continue;
1181 }
1182
1183 break;
1184 }
1185
1186 if (bracket) {
1187 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1188 "the closing bracket in \"%V\" "
1189 "variable is missing", &var);
1190 return NGX_CONF_ERROR;
1191 }
1192
1193 if (var.len == 0) {
1194 goto invalid;
1195 }
1196
1197 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
1198 return NGX_CONF_ERROR;
1199 }
1200
1201 } else {
1202 i++;
1203
1204 while (i < value[s].len
1205 && value[s].data[i] != '$'
1206 && value[s].data[i] != '%')
1207 {
1208 i++;
1209 }
1210
1211 len = &value[s].data[i] - data;
1212
1213 if (len) {
1214
1215 op->len = len;
1216 op->getlen = NULL;
1217
1218 if (len <= sizeof(uintptr_t)) {
1219 op->run = ngx_http_log_copy_short;
1220 op->data = 0;
1221
1222 while (len--) {
1223 op->data <<= 8;
1224 op->data |= data[len];
1225 }
1226
1227 } else {
1228 op->run = ngx_http_log_copy_long;
1229
1230 p = ngx_palloc(cf->pool, len);
1231 if (p == NULL) {
1232 return NGX_CONF_ERROR;
1233 }
1234
1235 ngx_memcpy(p, data, len);
1236 op->data = (uintptr_t) p;
1237 }
1238 }
1239 }
1240 }
1241 }
1242
1243 return NGX_CONF_OK;
1244
1245 invalid:
1246
1247 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
1248
1249 return NGX_CONF_ERROR;
1250 }
1251
1252
1253 static ngx_int_t
1254 ngx_http_log_init(ngx_cycle_t *cycle)
1255 {
1256 ngx_http_core_main_conf_t *cmcf;
1257
1258 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
1259
1260 cmcf->log_handler = ngx_http_log_handler;
1261
1262 return NGX_OK;
1263 }