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
|
|
169 ngx_test_null(line, ngx_palloc(r->pool, len), NGX_ERROR);
|
|
170 p = line;
|
|
171
|
|
172 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
173 if (op[i].op == NGX_HTTP_LOG_COPY_SHORT) {
|
|
174 len = op[i].len;
|
|
175 data = op[i].data;
|
|
176 while (len--) {
|
|
177 *p++ = (char) (data & 0xff);
|
|
178 data >>= 8;
|
|
179 }
|
|
180
|
|
181 } else if (op[i].op == NGX_HTTP_LOG_COPY_LONG) {
|
|
182 p = ngx_cpymem(p, (void *) op[i].data, op[i].len);
|
|
183
|
|
184 } else {
|
|
185 p = op[i].op(r, p, op[i].data);
|
|
186 }
|
|
187 }
|
|
188
|
10
|
189 #if (NGX_WIN32)
|
0
|
190 *p++ = CR; *p++ = LF;
|
|
191 WriteFile(log[l].file->fd, line, p - line, &written, NULL);
|
|
192 #else
|
|
193 *p++ = LF;
|
|
194 write(log[l].file->fd, line, p - line);
|
|
195 #endif
|
|
196 }
|
|
197
|
|
198 return NGX_OK;
|
|
199 }
|
|
200
|
|
201
|
|
202 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
|
|
203 uintptr_t data)
|
|
204 {
|
|
205 return ngx_cpymem(buf, r->connection->addr_text.data,
|
|
206 r->connection->addr_text.len);
|
|
207 }
|
|
208
|
|
209
|
|
210 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
211 uintptr_t data)
|
|
212 {
|
4
|
213 return ngx_sprintf(buf, "%ui", r->connection->number);
|
0
|
214 }
|
|
215
|
|
216
|
|
217 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
|
|
218 uintptr_t data)
|
|
219 {
|
|
220 if (r->pipeline) {
|
|
221 *buf = 'p';
|
|
222 } else {
|
|
223 *buf = '.';
|
|
224 }
|
|
225
|
|
226 return buf + 1;
|
|
227 }
|
|
228
|
|
229
|
|
230 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
|
|
231 uintptr_t data)
|
|
232 {
|
|
233 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
|
|
234 ngx_cached_http_log_time.len);
|
|
235 }
|
|
236
|
|
237
|
|
238 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
|
|
239 uintptr_t data)
|
|
240 {
|
|
241 struct timeval tv;
|
|
242
|
|
243 ngx_gettimeofday(&tv);
|
|
244
|
4
|
245 return ngx_sprintf(buf, "%l.%03l", tv.tv_sec, tv.tv_usec / 1000);
|
0
|
246 }
|
|
247
|
|
248
|
|
249 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
|
|
250 uintptr_t data)
|
|
251 {
|
|
252 if (buf == NULL) {
|
|
253 /* find the request line length */
|
|
254 return (u_char *) r->request_line.len;
|
|
255 }
|
|
256
|
|
257 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
|
|
258 }
|
|
259
|
|
260
|
|
261 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
|
|
262 uintptr_t data)
|
|
263 {
|
4
|
264 return ngx_sprintf(buf, "%ui",
|
|
265 r->err_status ? r->err_status : r->headers_out.status);
|
0
|
266 }
|
|
267
|
|
268
|
|
269 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
|
|
270 uintptr_t data)
|
|
271 {
|
4
|
272 return ngx_sprintf(buf, "%O", r->connection->sent);
|
0
|
273 }
|
|
274
|
|
275
|
|
276 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
|
|
277 uintptr_t data)
|
|
278 {
|
4
|
279 return ngx_sprintf(buf, "%O", r->connection->sent - r->header_size);
|
0
|
280 }
|
|
281
|
|
282
|
|
283 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
|
|
284 uintptr_t data)
|
|
285 {
|
|
286 ngx_uint_t i;
|
|
287 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 */
|
|
307 return (u_char *) h->value.len;
|
|
308 }
|
|
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
|
|
341 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
|
|
342 u_char *buf, uintptr_t data)
|
|
343 {
|
|
344 ngx_uint_t i;
|
|
345 ngx_str_t *s;
|
|
346 ngx_list_part_t *part;
|
|
347 ngx_table_elt_t *h;
|
|
348
|
|
349 s = (ngx_str_t *) data;
|
|
350
|
|
351 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
|
|
366 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 */
|
|
373 return (u_char *) h[i].value.len;
|
|
374 }
|
|
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
|
|
390 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
|
|
391 uintptr_t data)
|
|
392 {
|
|
393 ngx_uint_t i;
|
|
394 ngx_str_t *s;
|
|
395 ngx_table_elt_t *h;
|
|
396 ngx_http_log_op_t *op;
|
|
397
|
|
398 if (r) {
|
|
399
|
|
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
|
|
410 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
|
|
411
|
|
412 if (h == NULL) {
|
|
413
|
|
414 /*
|
|
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) {
|
|
424 return (u_char *) ngx_cached_http_time.len;
|
|
425 }
|
|
426 return ngx_cpymem(buf, ngx_cached_http_time.data,
|
|
427 ngx_cached_http_time.len);
|
|
428 }
|
|
429
|
|
430 if (data == offsetof(ngx_http_headers_out_t, server)) {
|
|
431 if (buf == NULL) {
|
|
432 return (u_char *) (sizeof(NGINX_VER) - 1);
|
|
433 }
|
|
434 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
|
|
435 }
|
|
436
|
|
437 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) {
|
|
446 return (u_char *) NGX_OFF_T_LEN;
|
|
447 }
|
10
|
448 return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
|
0
|
449 }
|
|
450
|
|
451 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
|
|
452 if (r->headers_out.last_modified_time == -1) {
|
|
453 if (buf) {
|
|
454 *buf = '-';
|
|
455 }
|
|
456 return buf + 1;
|
|
457 }
|
|
458
|
|
459 if (buf == NULL) {
|
|
460 return (u_char *)
|
|
461 sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
|
|
462 }
|
4
|
463 return ngx_http_time(buf, r->headers_out.last_modified_time);
|
0
|
464 }
|
|
465
|
|
466 if (buf) {
|
|
467 *buf = '-';
|
|
468 }
|
|
469
|
|
470 return buf + 1;
|
|
471 }
|
|
472
|
|
473 if (buf == NULL) {
|
|
474 /* find the header length */
|
|
475 return (u_char *) h->value.len;
|
|
476 }
|
|
477
|
|
478 return ngx_cpymem(buf, h->value.data, h->value.len);
|
|
479 }
|
|
480
|
|
481 /* find an offset while a format string compilation */
|
|
482
|
|
483 op = (ngx_http_log_op_t *) buf;
|
|
484 s = (ngx_str_t *) data;
|
|
485
|
|
486 op->len = 0;
|
|
487
|
|
488 for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
|
|
489 if (ngx_http_headers_out[i].name.len != s->len) {
|
|
490 continue;
|
|
491 }
|
|
492
|
|
493 if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, s->data, s->len)
|
|
494 == 0)
|
|
495 {
|
|
496 op->op = ngx_http_log_header_out;
|
|
497 op->data = ngx_http_headers_out[i].offset;
|
|
498 return NULL;
|
|
499 }
|
|
500 }
|
|
501
|
|
502 if (s->len == sizeof("Connection") - 1
|
|
503 && ngx_strncasecmp(s->data, "Connection", s->len) == 0)
|
|
504 {
|
|
505 op->op = ngx_http_log_connection_header_out;
|
|
506 op->data = (uintptr_t) NULL;
|
|
507 return NULL;
|
|
508 }
|
|
509
|
|
510 if (s->len == sizeof("Transfer-Encoding") - 1
|
|
511 && ngx_strncasecmp(s->data, "Transfer-Encoding", s->len) == 0) {
|
|
512 op->op = ngx_http_log_transfer_encoding_header_out;
|
|
513 op->data = (uintptr_t) NULL;
|
|
514 return NULL;
|
|
515 }
|
|
516
|
|
517 op->op = ngx_http_log_unknown_header_out;
|
|
518 op->data = (uintptr_t) s;
|
|
519
|
|
520 return NULL;
|
|
521 }
|
|
522
|
|
523
|
|
524 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
|
|
525 u_char *buf, uintptr_t data)
|
|
526 {
|
|
527 if (buf == NULL) {
|
|
528 return (u_char *) ((r->keepalive) ? sizeof("keep-alive") - 1:
|
|
529 sizeof("close") - 1);
|
|
530 }
|
|
531
|
|
532 if (r->keepalive) {
|
|
533 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
|
|
534
|
|
535 } else {
|
|
536 return ngx_cpymem(buf, "close", sizeof("close") - 1);
|
|
537 }
|
|
538 }
|
|
539
|
|
540
|
|
541 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
|
|
542 u_char *buf,
|
|
543 uintptr_t data)
|
|
544 {
|
|
545 if (buf == NULL) {
|
|
546 return (u_char *) ((r->chunked) ? sizeof("chunked") - 1 : 1);
|
|
547 }
|
|
548
|
|
549 if (r->chunked) {
|
|
550 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
|
|
551 }
|
|
552
|
|
553 *buf = '-';
|
|
554
|
|
555 return buf + 1;
|
|
556 }
|
|
557
|
|
558
|
|
559 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
|
|
560 u_char *buf,
|
|
561 uintptr_t data)
|
|
562 {
|
|
563 ngx_uint_t i;
|
|
564 ngx_str_t *s;
|
|
565 ngx_list_part_t *part;
|
|
566 ngx_table_elt_t *h;
|
|
567
|
|
568 s = (ngx_str_t *) data;
|
|
569
|
|
570 part = &r->headers_out.headers.part;
|
|
571 h = part->elts;
|
|
572
|
|
573 for (i = 0; /* void */; i++) {
|
|
574
|
|
575 if (i >= part->nelts) {
|
|
576 if (part->next == NULL) {
|
|
577 break;
|
|
578 }
|
|
579
|
|
580 part = part->next;
|
|
581 h = part->elts;
|
|
582 i = 0;
|
|
583 }
|
|
584
|
|
585 if (h[i].key.len != s->len) {
|
|
586 continue;
|
|
587 }
|
|
588
|
|
589 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) {
|
|
590 if (buf == NULL) {
|
|
591 /* find the header length */
|
|
592 return (u_char *) h[i].value.len;
|
|
593 }
|
|
594
|
|
595 return ngx_cpymem(buf, h[i].value.data, h[i].value.len);
|
|
596 }
|
|
597 }
|
|
598
|
|
599 /* no header */
|
|
600
|
|
601 if (buf) {
|
|
602 *buf = '-';
|
|
603 }
|
|
604
|
|
605 return buf + 1;
|
|
606 }
|
|
607
|
|
608
|
|
609 static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf)
|
|
610 {
|
|
611 ngx_http_log_op_name_t *op;
|
|
612
|
|
613 for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
|
|
614 op->op = NULL;
|
|
615
|
|
616 return NGX_OK;
|
|
617 }
|
|
618
|
|
619
|
|
620 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
621 {
|
|
622 ngx_http_log_main_conf_t *conf;
|
|
623
|
|
624 char *rc;
|
|
625 ngx_str_t *value;
|
|
626
|
|
627 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t)))) {
|
|
628 return NGX_CONF_ERROR;
|
|
629 }
|
|
630
|
|
631 ngx_init_array(conf->formats, cf->pool, 5, sizeof(ngx_http_log_fmt_t),
|
|
632 NGX_CONF_ERROR);
|
|
633
|
|
634 cf->args->nelts = 0;
|
|
635
|
|
636 if (!(value = ngx_push_array(cf->args))) {
|
|
637 return NGX_CONF_ERROR;
|
|
638 }
|
|
639
|
|
640 if (!(value = ngx_push_array(cf->args))) {
|
|
641 return NGX_CONF_ERROR;
|
|
642 }
|
|
643
|
|
644 value->len = sizeof("combined") - 1;
|
|
645 value->data = (u_char *) "combined";
|
|
646
|
|
647 if (!(value = ngx_push_array(cf->args))) {
|
|
648 return NGX_CONF_ERROR;
|
|
649 }
|
|
650
|
|
651 *value = ngx_http_combined_fmt;
|
|
652
|
|
653 rc = ngx_http_log_set_format(cf, NULL, conf);
|
|
654 if (rc != NGX_CONF_OK) {
|
|
655 return NULL;
|
|
656 }
|
|
657
|
|
658 return conf;
|
|
659 }
|
|
660
|
|
661
|
|
662 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
663 {
|
|
664 ngx_http_log_loc_conf_t *conf;
|
|
665
|
|
666 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t)))) {
|
|
667 return NGX_CONF_ERROR;
|
|
668 }
|
|
669
|
|
670 return conf;
|
|
671 }
|
|
672
|
|
673
|
|
674 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
675 void *child)
|
|
676 {
|
|
677 ngx_http_log_loc_conf_t *prev = parent;
|
|
678 ngx_http_log_loc_conf_t *conf = child;
|
|
679
|
|
680 ngx_http_log_t *log;
|
|
681 ngx_http_log_fmt_t *fmt;
|
|
682 ngx_http_log_main_conf_t *lmcf;
|
|
683
|
|
684 if (conf->logs == NULL) {
|
|
685
|
|
686 if (conf->off) {
|
|
687 return NGX_CONF_OK;
|
|
688 }
|
|
689
|
|
690 if (prev->logs) {
|
|
691 conf->logs = prev->logs;
|
|
692
|
|
693 } else {
|
|
694
|
|
695 if (prev->off) {
|
|
696 conf->off = prev->off;
|
|
697 return NGX_CONF_OK;
|
|
698 }
|
|
699
|
|
700 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
701 if (conf->logs == NULL) {
|
|
702 return NGX_CONF_ERROR;
|
|
703 }
|
|
704
|
|
705 if (!(log = ngx_array_push(conf->logs))) {
|
|
706 return NGX_CONF_ERROR;
|
|
707 }
|
|
708
|
|
709 log->file = ngx_conf_open_file(cf->cycle, &http_access_log);
|
|
710 if (log->file == NULL) {
|
|
711 return NGX_CONF_ERROR;
|
|
712 }
|
|
713
|
|
714 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
715 fmt = lmcf->formats.elts;
|
|
716
|
|
717 /* the default "combined" format */
|
|
718 log->ops = fmt[0].ops;
|
|
719 }
|
|
720 }
|
|
721
|
|
722 return NGX_CONF_OK;
|
|
723 }
|
|
724
|
|
725
|
|
726 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
727 void *conf)
|
|
728 {
|
|
729 ngx_http_log_loc_conf_t *llcf = conf;
|
|
730
|
|
731 ngx_uint_t i;
|
|
732 ngx_str_t *value, name;
|
|
733 ngx_http_log_t *log;
|
|
734 ngx_http_log_fmt_t *fmt;
|
|
735 ngx_http_log_main_conf_t *lmcf;
|
|
736
|
|
737 value = cf->args->elts;
|
|
738
|
|
739 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
740 llcf->off = 1;
|
|
741 return NGX_CONF_OK;
|
|
742 }
|
|
743
|
|
744 if (llcf->logs == NULL) {
|
|
745 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
746 if (llcf->logs == NULL) {
|
|
747 return NGX_CONF_ERROR;
|
|
748 }
|
|
749 }
|
|
750
|
|
751 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
752
|
|
753 if (!(log = ngx_array_push(llcf->logs))) {
|
|
754 return NGX_CONF_ERROR;
|
|
755 }
|
|
756
|
|
757 if (!(log->file = ngx_conf_open_file(cf->cycle, &value[1]))) {
|
|
758 return NGX_CONF_ERROR;
|
|
759 }
|
|
760
|
|
761 if (cf->args->nelts == 3) {
|
|
762 name = value[2];
|
|
763 } else {
|
|
764 name.len = sizeof("combined") - 1;
|
|
765 name.data = (u_char *) "combined";
|
|
766 }
|
|
767
|
|
768 fmt = lmcf->formats.elts;
|
|
769 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
770 if (fmt[i].name.len == name.len
|
|
771 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
772 {
|
|
773 log->ops = fmt[i].ops;
|
|
774 return NGX_CONF_OK;
|
|
775 }
|
|
776 }
|
|
777
|
|
778 return NGX_CONF_OK;
|
|
779 }
|
|
780
|
|
781
|
|
782 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
783 void *conf)
|
|
784 {
|
|
785 ngx_http_log_main_conf_t *lmcf = conf;
|
|
786
|
|
787 ngx_uint_t s, f, invalid;
|
|
788 u_char *data, *p, *fname;
|
|
789 size_t i, len, fname_len;
|
|
790 ngx_str_t *value, arg, *a;
|
|
791 ngx_http_log_op_t *op;
|
|
792 ngx_http_log_fmt_t *fmt;
|
|
793 ngx_http_log_op_name_t *name;
|
|
794
|
|
795 value = cf->args->elts;
|
|
796
|
|
797 fmt = lmcf->formats.elts;
|
|
798 for (f = 0; f < lmcf->formats.nelts; f++) {
|
|
799 if (fmt[f].name.len == value[1].len
|
|
800 && ngx_strcmp(fmt->name.data, value[1].data) == 0)
|
|
801 {
|
|
802 return "duplicate \"log_format\" name";
|
|
803 }
|
|
804 }
|
|
805
|
|
806 if (!(fmt = ngx_push_array(&lmcf->formats))) {
|
|
807 return NGX_CONF_ERROR;
|
|
808 }
|
|
809
|
|
810 fmt->name = value[1];
|
|
811
|
|
812 if (!(fmt->ops = ngx_create_array(cf->pool, 20,
|
|
813 sizeof(ngx_http_log_op_t)))) {
|
|
814 return NGX_CONF_ERROR;
|
|
815 }
|
|
816
|
|
817 invalid = 0;
|
|
818 data = NULL;
|
|
819
|
|
820 for (s = 2; s < cf->args->nelts && !invalid; s++) {
|
|
821
|
|
822 i = 0;
|
|
823
|
|
824 while (i < value[s].len) {
|
|
825
|
|
826 if (!(op = ngx_push_array(fmt->ops))) {
|
|
827 return NGX_CONF_ERROR;
|
|
828 }
|
|
829
|
|
830 data = &value[s].data[i];
|
|
831
|
|
832 if (value[s].data[i] == '%') {
|
|
833 i++;
|
|
834
|
|
835 if (i == value[s].len) {
|
|
836 invalid = 1;
|
|
837 break;
|
|
838 }
|
|
839
|
|
840 if (value[s].data[i] == '{') {
|
|
841 i++;
|
|
842
|
|
843 arg.data = &value[s].data[i];
|
|
844
|
|
845 while (i < value[s].len && value[s].data[i] != '}') {
|
|
846 i++;
|
|
847 }
|
|
848
|
|
849 arg.len = &value[s].data[i] - arg.data;
|
|
850
|
|
851 if (i == value[s].len || arg.len == 0) {
|
|
852 invalid = 1;
|
|
853 break;
|
|
854 }
|
|
855
|
|
856 i++;
|
|
857
|
|
858 } else {
|
|
859 arg.len = 0;
|
|
860 }
|
|
861
|
|
862 fname = &value[s].data[i];
|
|
863
|
|
864 while (i < value[s].len
|
|
865 && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
|
|
866 || value[s].data[i] == '_'))
|
|
867 {
|
|
868 i++;
|
|
869 }
|
|
870
|
|
871 fname_len = &value[s].data[i] - fname;
|
|
872
|
|
873 if (fname_len == 0) {
|
|
874 invalid = 1;
|
|
875 break;
|
|
876 }
|
|
877
|
|
878 for (name = ngx_http_log_fmt_ops; name->op; name++) {
|
|
879 if (name->name.len == 0) {
|
|
880 name = (ngx_http_log_op_name_t *) name->op;
|
|
881 }
|
|
882
|
|
883 if (name->name.len == fname_len
|
|
884 && ngx_strncmp(name->name.data, fname, fname_len) == 0)
|
|
885 {
|
|
886 if (name->len != NGX_HTTP_LOG_ARG) {
|
|
887 if (arg.len) {
|
|
888 fname[fname_len] = '\0';
|
|
889 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
890 "\"%s\" must not have argument",
|
|
891 data);
|
|
892 return NGX_CONF_ERROR;
|
|
893 }
|
|
894
|
|
895 op->len = name->len;
|
|
896 op->op = name->op;
|
|
897 op->data = 0;
|
|
898
|
|
899 break;
|
|
900 }
|
|
901
|
|
902 if (arg.len == 0) {
|
|
903 fname[fname_len] = '\0';
|
|
904 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
905 "\"%s\" requires argument",
|
|
906 data);
|
|
907 return NGX_CONF_ERROR;
|
|
908 }
|
|
909
|
|
910 if (!(a = ngx_palloc(cf->pool, sizeof(ngx_str_t)))) {
|
|
911 return NGX_CONF_ERROR;
|
|
912 }
|
|
913
|
|
914 *a = arg;
|
|
915 name->op(NULL, (u_char *) op, (uintptr_t) a);
|
|
916
|
|
917 break;
|
|
918 }
|
|
919 }
|
|
920
|
|
921 if (name->name.len == 0) {
|
|
922 invalid = 1;
|
|
923 break;
|
|
924 }
|
|
925
|
|
926 } else {
|
|
927 i++;
|
|
928
|
|
929 while (i < value[s].len && value[s].data[i] != '%') {
|
|
930 i++;
|
|
931 }
|
|
932
|
|
933 len = &value[s].data[i] - data;
|
|
934
|
|
935 if (len) {
|
|
936
|
|
937 op->len = len;
|
|
938
|
|
939 if (len <= sizeof(uintptr_t)) {
|
|
940 op->op = NGX_HTTP_LOG_COPY_SHORT;
|
|
941 op->data = 0;
|
|
942
|
|
943 while (len--) {
|
|
944 op->data <<= 8;
|
|
945 op->data |= data[len];
|
|
946 }
|
|
947
|
|
948 } else {
|
|
949 op->op = NGX_HTTP_LOG_COPY_LONG;
|
|
950
|
|
951 if (!(p = ngx_palloc(cf->pool, len))) {
|
|
952 return NGX_CONF_ERROR;
|
|
953 }
|
|
954
|
|
955 ngx_memcpy(p, data, len);
|
|
956 op->data = (uintptr_t) p;
|
|
957 }
|
|
958 }
|
|
959 }
|
|
960 }
|
|
961 }
|
|
962
|
|
963 if (invalid) {
|
|
964 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
965 "invalid parameter \"%s\"", data);
|
|
966 return NGX_CONF_ERROR;
|
|
967 }
|
|
968
|
|
969 return NGX_CONF_OK;
|
|
970 }
|