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