nginx 0.1.19
[nginx-catap.git] / src / http / ngx_http_log_handler.c
blob0d031fe0df3e505b89368535b1132c36c0f00529
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10 #include <nginx.h>
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_status(ngx_http_request_t *r, u_char *buf,
24 ngx_http_log_op_t *op);
25 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
26 ngx_http_log_op_t *op);
27 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
28 ngx_http_log_op_t *op);
29 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
30 ngx_http_log_op_t *op);
32 static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
33 uintptr_t data);
34 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
35 ngx_http_log_op_t *op);
37 static ngx_int_t ngx_http_log_header_in_compile(ngx_http_log_op_t *op,
38 ngx_str_t *value);
39 static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
40 uintptr_t data);
41 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
42 ngx_http_log_op_t *op);
43 static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
44 uintptr_t data);
45 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
46 u_char *buf,
47 ngx_http_log_op_t *op);
49 static ngx_int_t ngx_http_log_header_out_compile(ngx_http_log_op_t *op,
50 ngx_str_t *value);
51 static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
52 uintptr_t data);
53 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
54 ngx_http_log_op_t *op);
55 static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
56 uintptr_t data);
57 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
58 u_char *buf,
59 ngx_http_log_op_t *op);
61 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
62 u_char *buf,
63 ngx_http_log_op_t *op);
64 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
65 u_char *buf,
66 ngx_http_log_op_t *op);
68 static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
69 ngx_str_t *value);
71 static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf);
72 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
73 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
74 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
75 void *child);
76 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
77 void *conf);
78 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
79 void *conf);
80 static ngx_int_t ngx_http_log_parse_format(ngx_conf_t *cf, ngx_array_t *ops,
81 ngx_str_t *line);
84 static ngx_command_t ngx_http_log_commands[] = {
86 {ngx_string("log_format"),
87 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
88 ngx_http_log_set_format,
89 NGX_HTTP_MAIN_CONF_OFFSET,
91 NULL},
93 {ngx_string("access_log"),
94 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
95 ngx_http_log_set_log,
96 NGX_HTTP_LOC_CONF_OFFSET,
98 NULL},
100 ngx_null_command
104 ngx_http_module_t ngx_http_log_module_ctx = {
105 ngx_http_log_set_formats, /* pre conf */
107 ngx_http_log_create_main_conf, /* create main configuration */
108 NULL, /* init main configuration */
110 NULL, /* create server configuration */
111 NULL, /* merge server configuration */
113 ngx_http_log_create_loc_conf, /* create location configration */
114 ngx_http_log_merge_loc_conf /* merge location configration */
118 ngx_module_t ngx_http_log_module = {
119 NGX_MODULE,
120 &ngx_http_log_module_ctx, /* module context */
121 ngx_http_log_commands, /* module directives */
122 NGX_HTTP_MODULE, /* module type */
123 NULL, /* init module */
124 NULL /* init child */
128 static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
131 static ngx_str_t ngx_http_combined_fmt =
132 ngx_string("%addr - - [%time] \"%request\" %status %apache_length "
133 "\"%{Referer}i\" \"%{User-Agent}i\"");
136 ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
137 { ngx_string("addr"), INET_ADDRSTRLEN - 1, NULL, NULL, ngx_http_log_addr },
138 { ngx_string("conn"), NGX_INT32_LEN, NULL, NULL, ngx_http_log_connection },
139 { ngx_string("pipe"), 1, NULL, NULL, ngx_http_log_pipe },
140 { ngx_string("time"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
141 NULL, NULL, ngx_http_log_time },
142 { ngx_string("msec"), NGX_TIME_T_LEN + 4, NULL, NULL, ngx_http_log_msec },
143 { ngx_string("status"), 3, NULL, NULL, ngx_http_log_status },
144 { ngx_string("length"), NGX_OFF_T_LEN, NULL, NULL, ngx_http_log_length },
145 { ngx_string("apache_length"), NGX_OFF_T_LEN,
146 NULL, NULL, ngx_http_log_apache_length },
147 { ngx_string("request_length"), NGX_SIZE_T_LEN,
148 NULL, NULL, ngx_http_log_request_length },
150 { ngx_string("request"), 0, NULL,
151 ngx_http_log_request_getlen,
152 ngx_http_log_request },
154 { ngx_string("i"), 0, ngx_http_log_header_in_compile,
155 ngx_http_log_header_in_getlen,
156 ngx_http_log_header_in },
158 { ngx_string("o"), 0, ngx_http_log_header_out_compile,
159 ngx_http_log_header_out_getlen,
160 ngx_http_log_header_out },
162 { ngx_null_string, 0, NULL, NULL, NULL }
166 ngx_int_t ngx_http_log_handler(ngx_http_request_t *r)
168 ngx_uint_t i, l;
169 u_char *line, *p;
170 size_t len;
171 ngx_http_log_t *log;
172 ngx_http_log_op_t *op;
173 ngx_http_log_loc_conf_t *lcf;
174 #if (NGX_WIN32)
175 u_long written;
176 #endif
178 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
179 "http log handler");
181 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
183 if (lcf->off) {
184 return NGX_OK;
187 log = lcf->logs->elts;
188 for (l = 0; l < lcf->logs->nelts; l++) {
190 len = 0;
191 op = log[l].ops->elts;
192 for (i = 0; i < log[l].ops->nelts; i++) {
193 if (op[i].len == 0) {
194 len += op[i].getlen(r, op[i].data);
196 } else {
197 len += op[i].len;
201 #if (NGX_WIN32)
202 len += 2;
203 #else
204 len++;
205 #endif
207 if (!(line = ngx_palloc(r->pool, len))) {
208 return NGX_ERROR;
211 p = line;
213 for (i = 0; i < log[l].ops->nelts; i++) {
214 p = op[i].run(r, p, &op[i]);
217 #if (NGX_WIN32)
218 *p++ = CR; *p++ = LF;
219 WriteFile(log[l].file->fd, line, p - line, &written, NULL);
220 #else
221 *p++ = LF;
222 write(log[l].file->fd, line, p - line);
223 #endif
226 return NGX_OK;
230 static u_char *ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
231 ngx_http_log_op_t *op)
233 size_t len;
234 uintptr_t data;
236 len = op->len;
237 data = op->data;
239 while (len--) {
240 *buf++ = (u_char) (data & 0xff);
241 data >>= 8;
244 return buf;
248 static u_char *ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
249 ngx_http_log_op_t *op)
251 return ngx_cpymem(buf, (u_char *) op->data, op->len);
255 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
256 ngx_http_log_op_t *op)
258 return ngx_cpymem(buf, r->connection->addr_text.data,
259 r->connection->addr_text.len);
263 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
264 ngx_http_log_op_t *op)
266 return ngx_sprintf(buf, "%ui", r->connection->number);
270 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
271 ngx_http_log_op_t *op)
273 if (r->pipeline) {
274 *buf = 'p';
275 } else {
276 *buf = '.';
279 return buf + 1;
283 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
284 ngx_http_log_op_t *op)
286 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
287 ngx_cached_http_log_time.len);
291 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
292 ngx_http_log_op_t *op)
294 struct timeval tv;
296 ngx_gettimeofday(&tv);
298 return ngx_sprintf(buf, "%l.%03l", tv.tv_sec, tv.tv_usec / 1000);
302 static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
303 uintptr_t data)
305 return r->request_line.len;
309 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
310 ngx_http_log_op_t *op)
312 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
316 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
317 ngx_http_log_op_t *op)
319 return ngx_sprintf(buf, "%ui",
320 r->err_status ? r->err_status : r->headers_out.status);
324 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
325 ngx_http_log_op_t *op)
327 return ngx_sprintf(buf, "%O", r->connection->sent);
331 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
332 ngx_http_log_op_t *op)
334 return ngx_sprintf(buf, "%O", r->connection->sent - r->header_size);
338 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
339 ngx_http_log_op_t *op)
341 return ngx_sprintf(buf, "%z", r->request_length);
345 static ngx_int_t ngx_http_log_header_in_compile(ngx_http_log_op_t *op,
346 ngx_str_t *value)
348 ngx_uint_t i;
350 op->len = 0;
352 for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
354 if (ngx_http_headers_in[i].name.len != value->len) {
355 continue;
358 if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, value->data,
359 value->len) == 0)
361 op->getlen = ngx_http_log_header_in_getlen;
362 op->run = ngx_http_log_header_in;
363 op->data = ngx_http_headers_in[i].offset;
365 return NGX_OK;
369 op->getlen = ngx_http_log_unknown_header_in_getlen;
370 op->run = ngx_http_log_unknown_header_in;
371 op->data = (uintptr_t) value;
373 return NGX_OK;
377 static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
378 uintptr_t data)
380 ngx_table_elt_t *h;
382 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
384 if (h) {
385 return h->value.len;
388 return 1;
392 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
393 ngx_http_log_op_t *op)
395 ngx_table_elt_t *h;
397 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + op->data);
399 if (h) {
400 return ngx_cpymem(buf, h->value.data, h->value.len);
403 *buf = '-';
405 return buf + 1;
409 static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
410 uintptr_t data)
412 ngx_table_elt_t *h;
414 h = ngx_http_log_unknown_header(&r->headers_in.headers, (ngx_str_t *) data);
416 if (h) {
417 return h->value.len;
420 return 1;
424 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
425 u_char *buf,
426 ngx_http_log_op_t *op)
428 ngx_table_elt_t *h;
430 h = ngx_http_log_unknown_header(&r->headers_in.headers,
431 (ngx_str_t *) op->data);
433 if (h) {
434 return ngx_cpymem(buf, h->value.data, h->value.len);
437 *buf = '-';
439 return buf + 1;
443 static ngx_int_t ngx_http_log_header_out_compile(ngx_http_log_op_t *op,
444 ngx_str_t *value)
446 ngx_uint_t i;
448 op->len = 0;
450 for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
452 if (ngx_http_headers_out[i].name.len != value->len) {
453 continue;
456 if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, value->data,
457 value->len) == 0)
459 op->getlen = ngx_http_log_header_out_getlen;
460 op->run = ngx_http_log_header_out;
461 op->data = ngx_http_headers_out[i].offset;
463 return NGX_OK;
467 if (value->len == sizeof("Connection") - 1
468 && ngx_strncasecmp(value->data, "Connection", value->len) == 0)
470 op->len = sizeof("keep-alive") - 1;
471 op->getlen = NULL;
472 op->run = ngx_http_log_connection_header_out;
473 op->data = 0;
474 return NGX_OK;
477 if (value->len == sizeof("Transfer-Encoding") - 1
478 && ngx_strncasecmp(value->data, "Transfer-Encoding", value->len) == 0)
480 op->len = sizeof("chunked") - 1;
481 op->getlen = NULL;
482 op->run = ngx_http_log_transfer_encoding_header_out;
483 op->data = 0;
484 return NGX_OK;
487 op->getlen = ngx_http_log_unknown_header_out_getlen;
488 op->run = ngx_http_log_unknown_header_out;
489 op->data = (uintptr_t) value;
491 return NGX_OK;
495 static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
496 uintptr_t data)
498 ngx_table_elt_t *h;
500 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
502 if (h) {
503 return h->value.len;
507 * No header pointer was found.
508 * However, some headers: "Date", "Server", "Content-Length",
509 * and "Last-Modified" have a special handling in the header filter
510 * but we do not set up their pointers in the filter because
511 * they are too seldom needed to be logged.
514 if (data == offsetof(ngx_http_headers_out_t, date)) {
515 return ngx_cached_http_time.len;
518 if (data == offsetof(ngx_http_headers_out_t, server)) {
519 return (sizeof(NGINX_VER) - 1);
522 if (data == offsetof(ngx_http_headers_out_t, content_length)) {
523 if (r->headers_out.content_length_n == -1) {
524 return 1;
527 return NGX_OFF_T_LEN;
530 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
531 if (r->headers_out.last_modified_time == -1) {
532 return 1;
535 return sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
538 return 1;
542 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
543 ngx_http_log_op_t *op)
545 ngx_table_elt_t *h;
547 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + op->data);
549 if (h) {
550 return ngx_cpymem(buf, h->value.data, h->value.len);
554 * No header pointer was found.
555 * However, some headers: "Date", "Server", "Content-Length",
556 * and "Last-Modified" have a special handling in the header filter
557 * but we do not set up their pointers in the filter because
558 * they are too seldom needed to be logged.
561 if (op->data == offsetof(ngx_http_headers_out_t, date)) {
562 return ngx_cpymem(buf, ngx_cached_http_time.data,
563 ngx_cached_http_time.len);
566 if (op->data == offsetof(ngx_http_headers_out_t, server)) {
567 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
570 if (op->data == offsetof(ngx_http_headers_out_t, content_length)) {
571 if (r->headers_out.content_length_n == -1) {
572 *buf = '-';
574 return buf + 1;
577 return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
580 if (op->data == offsetof(ngx_http_headers_out_t, last_modified)) {
581 if (r->headers_out.last_modified_time == -1) {
582 *buf = '-';
584 return buf + 1;
587 return ngx_http_time(buf, r->headers_out.last_modified_time);
590 *buf = '-';
592 return buf + 1;
596 static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
597 uintptr_t data)
599 ngx_table_elt_t *h;
601 h = ngx_http_log_unknown_header(&r->headers_out.headers,
602 (ngx_str_t *) data);
604 if (h) {
605 return h->value.len;
608 return 1;
612 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
613 u_char *buf,
614 ngx_http_log_op_t *op)
616 ngx_table_elt_t *h;
618 h = ngx_http_log_unknown_header(&r->headers_out.headers,
619 (ngx_str_t *) op->data);
621 if (h) {
622 return ngx_cpymem(buf, h->value.data, h->value.len);
625 *buf = '-';
627 return buf + 1;
631 static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
632 ngx_str_t *value)
634 ngx_uint_t i;
635 ngx_list_part_t *part;
636 ngx_table_elt_t *h;
638 part = &headers->part;
639 h = part->elts;
641 for (i = 0; /* void */; i++) {
643 if (i >= part->nelts) {
644 if (part->next == NULL) {
645 break;
648 part = part->next;
649 h = part->elts;
650 i = 0;
653 if (h[i].key.len != value->len) {
654 continue;
657 if (ngx_strncasecmp(h[i].key.data, value->data, value->len) == 0) {
658 return &h[i];
662 return NULL;
666 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
667 u_char *buf,
668 ngx_http_log_op_t *op)
670 if (r->keepalive) {
671 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
673 } else {
674 return ngx_cpymem(buf, "close", sizeof("close") - 1);
679 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
680 u_char *buf,
681 ngx_http_log_op_t *op)
683 if (r->chunked) {
684 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
687 *buf = '-';
689 return buf + 1;
693 static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf)
695 ngx_http_log_op_name_t *op;
697 for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
698 op->run = NULL;
700 return NGX_OK;
704 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf)
706 ngx_http_log_main_conf_t *conf;
708 char *rc;
709 ngx_str_t *value;
711 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t)))) {
712 return NGX_CONF_ERROR;
715 ngx_init_array(conf->formats, cf->pool, 5, sizeof(ngx_http_log_fmt_t),
716 NGX_CONF_ERROR);
718 cf->args->nelts = 0;
720 if (!(value = ngx_push_array(cf->args))) {
721 return NGX_CONF_ERROR;
724 if (!(value = ngx_push_array(cf->args))) {
725 return NGX_CONF_ERROR;
728 value->len = sizeof("combined") - 1;
729 value->data = (u_char *) "combined";
731 if (!(value = ngx_push_array(cf->args))) {
732 return NGX_CONF_ERROR;
735 *value = ngx_http_combined_fmt;
737 rc = ngx_http_log_set_format(cf, NULL, conf);
738 if (rc != NGX_CONF_OK) {
739 return NULL;
742 return conf;
746 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf)
748 ngx_http_log_loc_conf_t *conf;
750 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t)))) {
751 return NGX_CONF_ERROR;
754 return conf;
758 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
759 void *child)
761 ngx_http_log_loc_conf_t *prev = parent;
762 ngx_http_log_loc_conf_t *conf = child;
764 ngx_http_log_t *log;
765 ngx_http_log_fmt_t *fmt;
766 ngx_http_log_main_conf_t *lmcf;
768 if (conf->logs == NULL) {
770 if (conf->off) {
771 return NGX_CONF_OK;
774 if (prev->logs) {
775 conf->logs = prev->logs;
777 } else {
779 if (prev->off) {
780 conf->off = prev->off;
781 return NGX_CONF_OK;
784 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
785 if (conf->logs == NULL) {
786 return NGX_CONF_ERROR;
789 if (!(log = ngx_array_push(conf->logs))) {
790 return NGX_CONF_ERROR;
793 log->file = ngx_conf_open_file(cf->cycle, &http_access_log);
794 if (log->file == NULL) {
795 return NGX_CONF_ERROR;
798 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
799 fmt = lmcf->formats.elts;
801 /* the default "combined" format */
802 log->ops = fmt[0].ops;
806 return NGX_CONF_OK;
810 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
811 void *conf)
813 ngx_http_log_loc_conf_t *llcf = conf;
815 ngx_uint_t i;
816 ngx_str_t *value, name;
817 ngx_http_log_t *log;
818 ngx_http_log_fmt_t *fmt;
819 ngx_http_log_main_conf_t *lmcf;
821 value = cf->args->elts;
823 if (ngx_strcmp(value[1].data, "off") == 0) {
824 llcf->off = 1;
825 return NGX_CONF_OK;
828 if (llcf->logs == NULL) {
829 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
830 if (llcf->logs == NULL) {
831 return NGX_CONF_ERROR;
835 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
837 if (!(log = ngx_array_push(llcf->logs))) {
838 return NGX_CONF_ERROR;
841 if (!(log->file = ngx_conf_open_file(cf->cycle, &value[1]))) {
842 return NGX_CONF_ERROR;
845 if (cf->args->nelts == 3) {
846 name = value[2];
847 } else {
848 name.len = sizeof("combined") - 1;
849 name.data = (u_char *) "combined";
852 fmt = lmcf->formats.elts;
853 for (i = 0; i < lmcf->formats.nelts; i++) {
854 if (fmt[i].name.len == name.len
855 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
857 log->ops = fmt[i].ops;
858 return NGX_CONF_OK;
862 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
863 "unknown log format \"%V\"", &name);
865 return NGX_CONF_ERROR;
869 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
870 void *conf)
872 ngx_http_log_main_conf_t *lmcf = conf;
874 ngx_uint_t s, f, invalid;
875 u_char *data, *p, *fname;
876 size_t i, len, fname_len;
877 ngx_str_t *value, arg, *a;
878 ngx_http_log_op_t *op;
879 ngx_http_log_fmt_t *fmt;
880 ngx_http_log_op_name_t *name;
882 value = cf->args->elts;
884 fmt = lmcf->formats.elts;
885 for (f = 0; f < lmcf->formats.nelts; f++) {
886 if (fmt[f].name.len == value[1].len
887 && ngx_strcmp(fmt->name.data, value[1].data) == 0)
889 return "duplicate \"log_format\" name";
893 if (!(fmt = ngx_push_array(&lmcf->formats))) {
894 return NGX_CONF_ERROR;
897 fmt->name = value[1];
899 if (!(fmt->ops = ngx_array_create(cf->pool, 20, sizeof(ngx_http_log_op_t))))
901 return NGX_CONF_ERROR;
904 invalid = 0;
905 data = NULL;
907 for (s = 2; s < cf->args->nelts && !invalid; s++) {
909 i = 0;
911 while (i < value[s].len) {
913 if (!(op = ngx_push_array(fmt->ops))) {
914 return NGX_CONF_ERROR;
917 data = &value[s].data[i];
919 if (value[s].data[i] == '%') {
920 i++;
922 if (i == value[s].len) {
923 invalid = 1;
924 break;
927 if (value[s].data[i] == '{') {
928 i++;
930 arg.data = &value[s].data[i];
932 while (i < value[s].len && value[s].data[i] != '}') {
933 i++;
936 arg.len = &value[s].data[i] - arg.data;
938 if (i == value[s].len || arg.len == 0) {
939 invalid = 1;
940 break;
943 i++;
945 } else {
946 arg.len = 0;
949 fname = &value[s].data[i];
951 while (i < value[s].len
952 && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
953 || value[s].data[i] == '_'))
955 i++;
958 fname_len = &value[s].data[i] - fname;
960 if (fname_len == 0) {
961 invalid = 1;
962 break;
965 for (name = ngx_http_log_fmt_ops; name->run; name++) {
966 if (name->name.len == 0) {
967 name = (ngx_http_log_op_name_t *) name->run;
970 if (name->name.len == fname_len
971 && ngx_strncmp(name->name.data, fname, fname_len) == 0)
973 if (name->compile == NULL) {
974 if (arg.len) {
975 fname[fname_len] = '\0';
976 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
977 "\"%s\" must not have argument",
978 data);
979 return NGX_CONF_ERROR;
982 op->len = name->len;
983 op->getlen = name->getlen;
984 op->run = name->run;
985 op->data = 0;
987 break;
990 if (arg.len == 0) {
991 fname[fname_len] = '\0';
992 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
993 "\"%s\" requires argument",
994 data);
995 return NGX_CONF_ERROR;
998 if (!(a = ngx_palloc(cf->pool, sizeof(ngx_str_t)))) {
999 return NGX_CONF_ERROR;
1002 *a = arg;
1003 if (name->compile(op, a) == NGX_ERROR) {
1004 return NGX_CONF_ERROR;
1007 break;
1011 if (name->name.len == 0) {
1012 invalid = 1;
1013 break;
1016 } else {
1017 i++;
1019 while (i < value[s].len && value[s].data[i] != '%') {
1020 i++;
1023 len = &value[s].data[i] - data;
1025 if (len) {
1027 op->len = len;
1028 op->getlen = NULL;
1030 if (len <= sizeof(uintptr_t)) {
1031 op->run = ngx_http_log_copy_short;
1032 op->data = 0;
1034 while (len--) {
1035 op->data <<= 8;
1036 op->data |= data[len];
1039 } else {
1040 op->run = ngx_http_log_copy_long;
1042 if (!(p = ngx_palloc(cf->pool, len))) {
1043 return NGX_CONF_ERROR;
1046 ngx_memcpy(p, data, len);
1047 op->data = (uintptr_t) p;
1054 if (invalid) {
1055 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1056 "invalid parameter \"%s\"", data);
1057 return NGX_CONF_ERROR;
1060 return NGX_CONF_OK;