blob: 3de6b10dd65d64da64cb797a401d51c803f8a5b5 [file] [log] [blame]
Igor Sysoev74e95c22003-11-09 20:03:38 +00001
2#include <ngx_config.h>
3#include <ngx_core.h>
4#include <ngx_http.h>
5#include <nginx.h>
6
7
Igor Sysoev10a543a2004-03-16 07:10:12 +00008static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
Igor Sysoev74e95c22003-11-09 20:03:38 +00009 uintptr_t data);
Igor Sysoev10a543a2004-03-16 07:10:12 +000010static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
11 uintptr_t data);
12static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
Igor Sysoev74e95c22003-11-09 20:03:38 +000013 uintptr_t data);
Igor Sysoev10a543a2004-03-16 07:10:12 +000014static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
15 uintptr_t data);
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +000016static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
17 uintptr_t data);
Igor Sysoev10a543a2004-03-16 07:10:12 +000018static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
Igor Sysoev74e95c22003-11-09 20:03:38 +000019 uintptr_t data);
Igor Sysoev10a543a2004-03-16 07:10:12 +000020static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
21 uintptr_t data);
22static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
23 uintptr_t data);
24static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
25 uintptr_t data);
26static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
27 uintptr_t data);
28static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
29 u_char *buf, uintptr_t data);
30static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
31 u_char *buf,
32 uintptr_t data);
33static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
34 u_char *buf, uintptr_t data);
35static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
36 uintptr_t data);
37static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
38 uintptr_t data);
Igor Sysoev74e95c22003-11-09 20:03:38 +000039
Igor Sysoev2f657222004-06-16 15:32:11 +000040static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf);
Igor Sysoev74e95c22003-11-09 20:03:38 +000041static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
42static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
43static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
44 void *child);
45static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
46 void *conf);
47static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
48 void *conf);
Igor Sysoev2f657222004-06-16 15:32:11 +000049static ngx_int_t ngx_http_log_parse_format(ngx_conf_t *cf, ngx_array_t *ops,
50 ngx_str_t *line);
Igor Sysoev74e95c22003-11-09 20:03:38 +000051
52
53static ngx_command_t ngx_http_log_commands[] = {
54
55 {ngx_string("log_format"),
56 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
57 ngx_http_log_set_format,
58 NGX_HTTP_MAIN_CONF_OFFSET,
59 0,
60 NULL},
61
62 {ngx_string("access_log"),
63 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
64 ngx_http_log_set_log,
65 NGX_HTTP_LOC_CONF_OFFSET,
66 0,
67 NULL},
68
69 ngx_null_command
70};
71
72
73ngx_http_module_t ngx_http_log_module_ctx = {
Igor Sysoev78329332003-11-10 17:17:31 +000074 ngx_http_log_pre_conf, /* pre conf */
75
Igor Sysoev74e95c22003-11-09 20:03:38 +000076 ngx_http_log_create_main_conf, /* create main configuration */
77 NULL, /* init main configuration */
78
79 NULL, /* create server configuration */
80 NULL, /* merge server configuration */
81
82 ngx_http_log_create_loc_conf, /* create location configration */
83 ngx_http_log_merge_loc_conf /* merge location configration */
84};
85
86
87ngx_module_t ngx_http_log_module = {
88 NGX_MODULE,
89 &ngx_http_log_module_ctx, /* module context */
90 ngx_http_log_commands, /* module directives */
91 NGX_HTTP_MODULE, /* module type */
92 NULL, /* init module */
93 NULL /* init child */
94};
95
96
Igor Sysoev980a9242004-09-05 19:54:02 +000097static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
Igor Sysoev74e95c22003-11-09 20:03:38 +000098
99
Igor Sysoev74e95c22003-11-09 20:03:38 +0000100static ngx_str_t ngx_http_combined_fmt =
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000101 ngx_string("%addr - - [%time] \"%request\" %status %apache_length "
Igor Sysoevfc68ea62003-12-22 17:27:24 +0000102 "\"%{Referer}i\" \"%{User-Agent}i\"");
Igor Sysoev74e95c22003-11-09 20:03:38 +0000103
104
Igor Sysoev78329332003-11-10 17:17:31 +0000105ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000106 { ngx_string("addr"), INET_ADDRSTRLEN - 1, ngx_http_log_addr },
107 { ngx_string("conn"), NGX_INT32_LEN, ngx_http_log_connection },
108 { ngx_string("pipe"), 1, ngx_http_log_pipe },
109 { ngx_string("time"), sizeof("28/Sep/1970:12:00:00") - 1,
110 ngx_http_log_time },
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000111 { ngx_string("msec"), TIME_T_LEN + 4, ngx_http_log_msec },
Igor Sysoev74e95c22003-11-09 20:03:38 +0000112 { ngx_string("request"), 0, ngx_http_log_request },
113 { ngx_string("status"), 3, ngx_http_log_status },
Igor Sysoev5f800782003-12-08 20:48:12 +0000114 { ngx_string("length"), NGX_OFF_T_LEN, ngx_http_log_length },
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000115 { ngx_string("apache_length"), NGX_OFF_T_LEN, ngx_http_log_apache_length },
Igor Sysoev74e95c22003-11-09 20:03:38 +0000116 { ngx_string("i"), NGX_HTTP_LOG_ARG, ngx_http_log_header_in },
117 { ngx_string("o"), NGX_HTTP_LOG_ARG, ngx_http_log_header_out },
118 { ngx_null_string, 0, NULL }
119};
120
121
Igor Sysoev2f657222004-06-16 15:32:11 +0000122ngx_int_t ngx_http_log_handler(ngx_http_request_t *r)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000123{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000124 ngx_uint_t i, l;
125 uintptr_t data;
126 u_char *line, *p;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000127 size_t len;
128 ngx_http_log_t *log;
129 ngx_http_log_op_t *op;
130 ngx_http_log_loc_conf_t *lcf;
131#if (WIN32)
Igor Sysoevab9d5fd2004-03-05 08:34:24 +0000132 u_long written;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000133#endif
134
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000135 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
136 "http log handler");
Igor Sysoev74e95c22003-11-09 20:03:38 +0000137
138 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
139
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000140 if (lcf->off) {
141 return NGX_OK;
142 }
143
Igor Sysoev74e95c22003-11-09 20:03:38 +0000144 log = lcf->logs->elts;
145 for (l = 0; l < lcf->logs->nelts; l++) {
146
147 len = 0;
148 op = log[l].ops->elts;
149 for (i = 0; i < log[l].ops->nelts; i++) {
150 if (op[i].len == 0) {
151 len += (size_t) op[i].op(r, NULL, op[i].data);
152
153 } else {
154 len += op[i].len;
155 }
156 }
157
158#if (WIN32)
159 len += 2;
160#else
161 len++;
162#endif
163
164 ngx_test_null(line, ngx_palloc(r->pool, len), NGX_ERROR);
165 p = line;
166
167 for (i = 0; i < log[l].ops->nelts; i++) {
168 if (op[i].op == NGX_HTTP_LOG_COPY_SHORT) {
169 len = op[i].len;
170 data = op[i].data;
171 while (len--) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000172 *p++ = (char) (data & 0xff);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000173 data >>= 8;
174 }
175
176 } else if (op[i].op == NGX_HTTP_LOG_COPY_LONG) {
177 p = ngx_cpymem(p, (void *) op[i].data, op[i].len);
178
179 } else {
180 p = op[i].op(r, p, op[i].data);
181 }
182 }
183
184#if (WIN32)
185 *p++ = CR; *p++ = LF;
186 WriteFile(log[l].file->fd, line, p - line, &written, NULL);
187#else
188 *p++ = LF;
189 write(log[l].file->fd, line, p - line);
190#endif
191 }
192
193 return NGX_OK;
194}
195
196
Igor Sysoev10a543a2004-03-16 07:10:12 +0000197static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
198 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000199{
200 return ngx_cpymem(buf, r->connection->addr_text.data,
201 r->connection->addr_text.len);
202}
203
204
Igor Sysoev10a543a2004-03-16 07:10:12 +0000205static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
Igor Sysoev74e95c22003-11-09 20:03:38 +0000206 uintptr_t data)
207{
Igor Sysoev2f657222004-06-16 15:32:11 +0000208 return buf + ngx_snprintf((char *) buf, NGX_INT_T_LEN + 1,
209 "%" NGX_UINT_T_FMT,
Igor Sysoev74e95c22003-11-09 20:03:38 +0000210 r->connection->number);
211}
212
213
Igor Sysoev10a543a2004-03-16 07:10:12 +0000214static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
215 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000216{
217 if (r->pipeline) {
218 *buf = 'p';
219 } else {
220 *buf = '.';
221 }
222
223 return buf + 1;
224}
225
226
Igor Sysoev10a543a2004-03-16 07:10:12 +0000227static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
228 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000229{
Igor Sysoevd59a0472003-11-10 21:09:22 +0000230 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
231 ngx_cached_http_log_time.len);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000232}
233
234
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000235static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
236 uintptr_t data)
237{
238 struct timeval tv;
239
240 ngx_gettimeofday(&tv);
241
242 return buf + ngx_snprintf((char *) buf, TIME_T_LEN + 5, "%ld.%03ld",
243 tv.tv_sec, tv.tv_usec / 1000);
244}
245
246
Igor Sysoev10a543a2004-03-16 07:10:12 +0000247static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
248 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000249{
250 if (buf == NULL) {
251 /* find the request line length */
Igor Sysoev10a543a2004-03-16 07:10:12 +0000252 return (u_char *) r->request_line.len;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000253 }
254
255 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
256}
257
258
Igor Sysoev10a543a2004-03-16 07:10:12 +0000259static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
260 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000261{
Igor Sysoev2f657222004-06-16 15:32:11 +0000262 return buf + ngx_snprintf((char *) buf, 4, "%" NGX_UINT_T_FMT,
Igor Sysoevd59a0472003-11-10 21:09:22 +0000263 r->err_status ? r->err_status : r->headers_out.status);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000264}
265
266
Igor Sysoev10a543a2004-03-16 07:10:12 +0000267static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
268 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000269{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000270 return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
Igor Sysoev74e95c22003-11-09 20:03:38 +0000271 r->connection->sent);
272}
273
274
Igor Sysoev10a543a2004-03-16 07:10:12 +0000275static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
276 uintptr_t data)
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000277{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000278 return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000279 r->connection->sent - r->header_size);
280}
281
282
Igor Sysoev10a543a2004-03-16 07:10:12 +0000283static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
284 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000285{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000286 ngx_uint_t i;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000287 ngx_str_t *s;
288 ngx_table_elt_t *h;
289 ngx_http_log_op_t *op;
290
291 if (r) {
292 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
293
294 if (h == NULL) {
295
296 /* no header */
297
298 if (buf) {
299 *buf = '-';
300 }
301
302 return buf + 1;
303 }
304
305 if (buf == NULL) {
306 /* find the header length */
Igor Sysoev10a543a2004-03-16 07:10:12 +0000307 return (u_char *) h->value.len;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000308 }
309
310 return ngx_cpymem(buf, h->value.data, h->value.len);
311 }
312
313 /* find an offset while a format string compilation */
314
315 op = (ngx_http_log_op_t *) buf;
316 s = (ngx_str_t *) data;
317
318 op->len = 0;
319
320 for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
321 if (ngx_http_headers_in[i].name.len != s->len) {
322 continue;
323 }
324
325 if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, s->data, s->len)
326 == 0)
327 {
328 op->op = ngx_http_log_header_in;
329 op->data = ngx_http_headers_in[i].offset;
330 return NULL;
331 }
332 }
333
334 op->op = ngx_http_log_unknown_header_in;
335 op->data = (uintptr_t) s;
336
337 return NULL;
338}
339
340
Igor Sysoev10a543a2004-03-16 07:10:12 +0000341static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
342 u_char *buf, uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000343{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000344 ngx_uint_t i;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000345 ngx_str_t *s;
Igor Sysoev980a9242004-09-05 19:54:02 +0000346 ngx_list_part_t *part;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000347 ngx_table_elt_t *h;
348
349 s = (ngx_str_t *) data;
350
Igor Sysoev980a9242004-09-05 19:54:02 +0000351 part = &r->headers_in.headers.part;
352 h = part->elts;
353
354 for (i = 0; /* void */; i++) {
355
356 if (i >= part->nelts) {
357 if (part->next == NULL) {
358 break;
359 }
360
361 part = part->next;
362 h = part->elts;
363 i = 0;
364 }
365
Igor Sysoev74e95c22003-11-09 20:03:38 +0000366 if (h[i].key.len != s->len) {
367 continue;
368 }
369
370 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) {
371 if (buf == NULL) {
372 /* find the header length */
Igor Sysoev10a543a2004-03-16 07:10:12 +0000373 return (u_char *) h[i].value.len;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000374 }
375
376 return ngx_cpymem(buf, h[i].value.data, h[i].value.len);
377 }
378 }
379
380 /* no header */
381
382 if (buf) {
383 *buf = '-';
384 }
385
386 return buf + 1;
387}
388
389
Igor Sysoev10a543a2004-03-16 07:10:12 +0000390static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
391 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000392{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000393 ngx_uint_t i;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000394 ngx_str_t *s;
395 ngx_table_elt_t *h;
396 ngx_http_log_op_t *op;
397
398 if (r) {
Igor Sysoev5f800782003-12-08 20:48:12 +0000399
400 /* run-time execution */
401
402 if (r->http_version < NGX_HTTP_VERSION_10) {
403 if (buf) {
404 *buf = '-';
405 }
406
407 return buf + 1;
408 }
409
Igor Sysoev74e95c22003-11-09 20:03:38 +0000410 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
411
412 if (h == NULL) {
413
Igor Sysoev5f800782003-12-08 20:48:12 +0000414 /*
415 * No header pointer was found.
416 * However, some headers: "Date", "Server", "Content-Length",
417 * and "Last-Modified" have a special handling in the header filter
418 * but we do not set up their pointers in the filter because
419 * they are too seldom needed to be logged.
420 */
421
422 if (data == offsetof(ngx_http_headers_out_t, date)) {
423 if (buf == NULL) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000424 return (u_char *) ngx_cached_http_time.len;
Igor Sysoev5f800782003-12-08 20:48:12 +0000425 }
426 return ngx_cpymem(buf, ngx_cached_http_time.data,
427 ngx_cached_http_time.len);
428 }
Igor Sysoev74e95c22003-11-09 20:03:38 +0000429
430 if (data == offsetof(ngx_http_headers_out_t, server)) {
431 if (buf == NULL) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000432 return (u_char *) (sizeof(NGINX_VER) - 1);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000433 }
434 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
435 }
436
Igor Sysoev5f800782003-12-08 20:48:12 +0000437 if (data == offsetof(ngx_http_headers_out_t, content_length)) {
438 if (r->headers_out.content_length_n == -1) {
439 if (buf) {
440 *buf = '-';
441 }
442 return buf + 1;
443 }
444
445 if (buf == NULL) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000446 return (u_char *) NGX_OFF_T_LEN;
Igor Sysoev5f800782003-12-08 20:48:12 +0000447 }
Igor Sysoev10a543a2004-03-16 07:10:12 +0000448 return buf + ngx_snprintf((char *) buf,
449 NGX_OFF_T_LEN + 2, OFF_T_FMT,
Igor Sysoev5f800782003-12-08 20:48:12 +0000450 r->headers_out.content_length_n);
451 }
452
453 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
454 if (r->headers_out.last_modified_time == -1) {
455 if (buf) {
456 *buf = '-';
457 }
458 return buf + 1;
459 }
460
461 if (buf == NULL) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000462 return (u_char *)
463 sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
Igor Sysoev5f800782003-12-08 20:48:12 +0000464 }
465 return buf + ngx_http_time(buf,
466 r->headers_out.last_modified_time);
467 }
468
Igor Sysoev74e95c22003-11-09 20:03:38 +0000469 if (buf) {
470 *buf = '-';
471 }
472
473 return buf + 1;
474 }
475
476 if (buf == NULL) {
477 /* find the header length */
Igor Sysoev10a543a2004-03-16 07:10:12 +0000478 return (u_char *) h->value.len;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000479 }
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
Igor Sysoev5f800782003-12-08 20:48:12 +0000505 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;
Igor Sysoev3b30a902003-12-25 20:26:58 +0000509 op->data = (uintptr_t) NULL;
Igor Sysoev5f800782003-12-08 20:48:12 +0000510 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;
Igor Sysoev3b30a902003-12-25 20:26:58 +0000516 op->data = (uintptr_t) NULL;
Igor Sysoev5f800782003-12-08 20:48:12 +0000517 return NULL;
518 }
519
Igor Sysoev74e95c22003-11-09 20:03:38 +0000520 op->op = ngx_http_log_unknown_header_out;
521 op->data = (uintptr_t) s;
522
523 return NULL;
524}
525
526
Igor Sysoev10a543a2004-03-16 07:10:12 +0000527static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
528 u_char *buf, uintptr_t data)
Igor Sysoev5f800782003-12-08 20:48:12 +0000529{
530 if (buf == NULL) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000531 return (u_char *) ((r->keepalive) ? sizeof("keep-alive") - 1:
532 sizeof("close") - 1);
Igor Sysoev5f800782003-12-08 20:48:12 +0000533 }
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
Igor Sysoev10a543a2004-03-16 07:10:12 +0000544static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
545 u_char *buf,
546 uintptr_t data)
Igor Sysoev5f800782003-12-08 20:48:12 +0000547{
548 if (buf == NULL) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000549 return (u_char *) ((r->chunked) ? sizeof("chunked") - 1 : 1);
Igor Sysoev5f800782003-12-08 20:48:12 +0000550 }
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
Igor Sysoev10a543a2004-03-16 07:10:12 +0000562static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
563 u_char *buf,
564 uintptr_t data)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000565{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000566 ngx_uint_t i;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000567 ngx_str_t *s;
Igor Sysoev980a9242004-09-05 19:54:02 +0000568 ngx_list_part_t *part;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000569 ngx_table_elt_t *h;
570
571 s = (ngx_str_t *) data;
572
Igor Sysoev980a9242004-09-05 19:54:02 +0000573 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
Igor Sysoev74e95c22003-11-09 20:03:38 +0000588 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 */
Igor Sysoev10a543a2004-03-16 07:10:12 +0000595 return (u_char *) h[i].value.len;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000596 }
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
Igor Sysoev2f657222004-06-16 15:32:11 +0000612static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf)
Igor Sysoev78329332003-11-10 17:17:31 +0000613{
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
Igor Sysoev74e95c22003-11-09 20:03:38 +0000623static 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;
Igor Sysoevda85f7f2004-03-16 21:26:01 +0000648 value->data = (u_char *) "combined";
Igor Sysoev74e95c22003-11-09 20:03:38 +0000649
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) {
Igor Sysoev78329332003-11-10 17:17:31 +0000658 return NULL;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000659 }
660
661 return conf;
662}
663
664
665static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf)
666{
667 ngx_http_log_loc_conf_t *conf;
668
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000669 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t)))) {
670 return NGX_CONF_ERROR;
671 }
Igor Sysoev74e95c22003-11-09 20:03:38 +0000672
673 return conf;
674}
675
676
677static 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) {
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000688
689 if (conf->off) {
690 return NGX_CONF_OK;
691 }
692
Igor Sysoev74e95c22003-11-09 20:03:38 +0000693 if (prev->logs) {
694 conf->logs = prev->logs;
695
696 } else {
697
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000698 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));
Igor Sysoev74e95c22003-11-09 20:03:38 +0000704 if (conf->logs == NULL) {
705 return NGX_CONF_ERROR;
706 }
707
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000708 if (!(log = ngx_array_push(conf->logs))) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000709 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
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000717 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000718 fmt = lmcf->formats.elts;
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000719
Igor Sysoev74e95c22003-11-09 20:03:38 +0000720 /* the default "combined" format */
721 log->ops = fmt[0].ops;
722 }
723 }
724
725 return NGX_CONF_OK;
726}
727
728
729static 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
Igor Sysoev10a543a2004-03-16 07:10:12 +0000734 ngx_uint_t i;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000735 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
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000740 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
Igor Sysoev74e95c22003-11-09 20:03:38 +0000747 if (llcf->logs == NULL) {
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000748 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
749 if (llcf->logs == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000750 return NGX_CONF_ERROR;
751 }
752 }
753
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000754 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000755
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000756 if (!(log = ngx_array_push(llcf->logs))) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000757 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;
Igor Sysoevda85f7f2004-03-16 21:26:01 +0000768 name.data = (u_char *) "combined";
Igor Sysoev74e95c22003-11-09 20:03:38 +0000769 }
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
785static 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
Igor Sysoev10a543a2004-03-16 07:10:12 +0000790 ngx_uint_t s, f, invalid;
791 u_char *data, *p, *fname;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000792 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;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000799
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
Igor Sysoev78329332003-11-10 17:17:31 +0000868 && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
869 || value[s].data[i] == '_'))
Igor Sysoev74e95c22003-11-09 20:03:38 +0000870 {
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
Igor Sysoev78329332003-11-10 17:17:31 +0000881 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
Igor Sysoev74e95c22003-11-09 20:03:38 +0000886 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;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000918 name->op(NULL, (u_char *) op, (uintptr_t) a);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000919
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}