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