comparison src/http/ngx_http_log_handler.c @ 290:87e73f067470

nginx-0.0.2-2004-03-16-10:10:12 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 16 Mar 2004 07:10:12 +0000
parents 30310107dbc9
children a472bfb778b3
comparison
equal deleted inserted replaced
289:0750faf8d7e3 290:87e73f067470
3 #include <ngx_core.h> 3 #include <ngx_core.h>
4 #include <ngx_http.h> 4 #include <ngx_http.h>
5 #include <nginx.h> 5 #include <nginx.h>
6 6
7 7
8 static char *ngx_http_log_addr(ngx_http_request_t *r, char *buf, 8 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
9 uintptr_t data);
10 static char *ngx_http_log_connection(ngx_http_request_t *r, char *buf,
11 uintptr_t data);
12 static char *ngx_http_log_pipe(ngx_http_request_t *r, char *buf,
13 uintptr_t data);
14 static char *ngx_http_log_time(ngx_http_request_t *r, char *buf,
15 uintptr_t data);
16 static char *ngx_http_log_request(ngx_http_request_t *r, char *buf,
17 uintptr_t data);
18 static char *ngx_http_log_status(ngx_http_request_t *r, char *buf,
19 uintptr_t data); 9 uintptr_t data);
20 static char *ngx_http_log_length(ngx_http_request_t *r, char *buf, 10 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
11 uintptr_t data);
12 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
21 uintptr_t data); 13 uintptr_t data);
22 static char *ngx_http_log_apache_length(ngx_http_request_t *r, char *buf, 14 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
23 uintptr_t data); 15 uintptr_t data);
24 static char *ngx_http_log_header_in(ngx_http_request_t *r, char *buf, 16 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
25 uintptr_t data); 17 uintptr_t data);
26 static char *ngx_http_log_connection_header_out(ngx_http_request_t *r, 18 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
27 char *buf, uintptr_t data); 19 uintptr_t data);
28 static char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, 20 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
29 char *buf, 21 uintptr_t data);
30 uintptr_t data); 22 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
31 static char *ngx_http_log_unknown_header_in(ngx_http_request_t *r, char *buf, 23 uintptr_t data);
32 uintptr_t data); 24 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
33 static char *ngx_http_log_header_out(ngx_http_request_t *r, char *buf, 25 uintptr_t data);
34 uintptr_t data); 26 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
35 static char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, char *buf, 27 u_char *buf, uintptr_t data);
36 uintptr_t data); 28 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
29 u_char *buf,
30 uintptr_t data);
31 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
32 u_char *buf, uintptr_t data);
33 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
34 uintptr_t data);
35 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
36 uintptr_t data);
37 37
38 static int ngx_http_log_pre_conf(ngx_conf_t *cf); 38 static int ngx_http_log_pre_conf(ngx_conf_t *cf);
39 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf); 39 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
40 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf); 40 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
41 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, 41 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
116 }; 116 };
117 117
118 118
119 int ngx_http_log_handler(ngx_http_request_t *r) 119 int ngx_http_log_handler(ngx_http_request_t *r)
120 { 120 {
121 int i, l; 121 ngx_uint_t i, l;
122 u_int data; 122 uintptr_t data;
123 char *line, *p; 123 u_char *line, *p;
124 size_t len; 124 size_t len;
125 ngx_http_log_t *log; 125 ngx_http_log_t *log;
126 ngx_http_log_op_t *op; 126 ngx_http_log_op_t *op;
127 ngx_http_log_loc_conf_t *lcf; 127 ngx_http_log_loc_conf_t *lcf;
128 #if (WIN32) 128 #if (WIN32)
160 for (i = 0; i < log[l].ops->nelts; i++) { 160 for (i = 0; i < log[l].ops->nelts; i++) {
161 if (op[i].op == NGX_HTTP_LOG_COPY_SHORT) { 161 if (op[i].op == NGX_HTTP_LOG_COPY_SHORT) {
162 len = op[i].len; 162 len = op[i].len;
163 data = op[i].data; 163 data = op[i].data;
164 while (len--) { 164 while (len--) {
165 *p++ = data & 0xff; 165 *p++ = (char) (data & 0xff);
166 data >>= 8; 166 data >>= 8;
167 } 167 }
168 168
169 } else if (op[i].op == NGX_HTTP_LOG_COPY_LONG) { 169 } else if (op[i].op == NGX_HTTP_LOG_COPY_LONG) {
170 p = ngx_cpymem(p, (void *) op[i].data, op[i].len); 170 p = ngx_cpymem(p, (void *) op[i].data, op[i].len);
185 185
186 return NGX_OK; 186 return NGX_OK;
187 } 187 }
188 188
189 189
190 static char *ngx_http_log_addr(ngx_http_request_t *r, char *buf, uintptr_t data) 190 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
191 uintptr_t data)
191 { 192 {
192 return ngx_cpymem(buf, r->connection->addr_text.data, 193 return ngx_cpymem(buf, r->connection->addr_text.data,
193 r->connection->addr_text.len); 194 r->connection->addr_text.len);
194 } 195 }
195 196
196 197
197 static char *ngx_http_log_connection(ngx_http_request_t *r, char *buf, 198 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
198 uintptr_t data) 199 uintptr_t data)
199 { 200 {
200 return buf + ngx_snprintf(buf, NGX_INT32_LEN + 1, "%u", 201 return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 1, "%u",
201 r->connection->number); 202 r->connection->number);
202 } 203 }
203 204
204 205
205 static char *ngx_http_log_pipe(ngx_http_request_t *r, char *buf, uintptr_t data) 206 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
207 uintptr_t data)
206 { 208 {
207 if (r->pipeline) { 209 if (r->pipeline) {
208 *buf = 'p'; 210 *buf = 'p';
209 } else { 211 } else {
210 *buf = '.'; 212 *buf = '.';
212 214
213 return buf + 1; 215 return buf + 1;
214 } 216 }
215 217
216 218
217 static char *ngx_http_log_time(ngx_http_request_t *r, char *buf, uintptr_t data) 219 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
220 uintptr_t data)
218 { 221 {
219 return ngx_cpymem(buf, ngx_cached_http_log_time.data, 222 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
220 ngx_cached_http_log_time.len); 223 ngx_cached_http_log_time.len);
221 } 224 }
222 225
223 226
224 static char *ngx_http_log_request(ngx_http_request_t *r, char *buf, 227 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
225 uintptr_t data) 228 uintptr_t data)
226 { 229 {
227 if (buf == NULL) { 230 if (buf == NULL) {
228 /* find the request line length */ 231 /* find the request line length */
229 return (char *) r->request_line.len; 232 return (u_char *) r->request_line.len;
230 } 233 }
231 234
232 return ngx_cpymem(buf, r->request_line.data, r->request_line.len); 235 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
233 } 236 }
234 237
235 238
236 static char *ngx_http_log_status(ngx_http_request_t *r, char *buf, 239 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
237 uintptr_t data) 240 uintptr_t data)
238 { 241 {
239 return buf + ngx_snprintf(buf, 4, "%d", 242 return buf + ngx_snprintf((char *) buf, 4, "%d",
240 r->err_status ? r->err_status : r->headers_out.status); 243 r->err_status ? r->err_status : r->headers_out.status);
241 } 244 }
242 245
243 246
244 static char *ngx_http_log_length(ngx_http_request_t *r, char *buf, 247 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
245 uintptr_t data) 248 uintptr_t data)
246 { 249 {
247 return buf + ngx_snprintf(buf, NGX_OFF_T_LEN + 1, OFF_T_FMT, 250 return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
248 r->connection->sent); 251 r->connection->sent);
249 } 252 }
250 253
251 254
252 static char *ngx_http_log_apache_length(ngx_http_request_t *r, char *buf, 255 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
253 uintptr_t data) 256 uintptr_t data)
254 { 257 {
255 return buf + ngx_snprintf(buf, NGX_OFF_T_LEN + 1, OFF_T_FMT, 258 return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
256 r->connection->sent - r->header_size); 259 r->connection->sent - r->header_size);
257 } 260 }
258 261
259 262
260 static char *ngx_http_log_header_in(ngx_http_request_t *r, char *buf, 263 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
261 uintptr_t data) 264 uintptr_t data)
262 { 265 {
263 int i; 266 ngx_uint_t i;
264 ngx_str_t *s; 267 ngx_str_t *s;
265 ngx_table_elt_t *h; 268 ngx_table_elt_t *h;
266 ngx_http_log_op_t *op; 269 ngx_http_log_op_t *op;
267 270
268 if (r) { 271 if (r) {
279 return buf + 1; 282 return buf + 1;
280 } 283 }
281 284
282 if (buf == NULL) { 285 if (buf == NULL) {
283 /* find the header length */ 286 /* find the header length */
284 return (char *) h->value.len; 287 return (u_char *) h->value.len;
285 } 288 }
286 289
287 return ngx_cpymem(buf, h->value.data, h->value.len); 290 return ngx_cpymem(buf, h->value.data, h->value.len);
288 } 291 }
289 292
313 316
314 return NULL; 317 return NULL;
315 } 318 }
316 319
317 320
318 static char *ngx_http_log_unknown_header_in(ngx_http_request_t *r, char *buf, 321 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
319 uintptr_t data) 322 u_char *buf, uintptr_t data)
320 { 323 {
321 int i; 324 ngx_uint_t i;
322 ngx_str_t *s; 325 ngx_str_t *s;
323 ngx_table_elt_t *h; 326 ngx_table_elt_t *h;
324 327
325 s = (ngx_str_t *) data; 328 s = (ngx_str_t *) data;
326 329
331 } 334 }
332 335
333 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) { 336 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) {
334 if (buf == NULL) { 337 if (buf == NULL) {
335 /* find the header length */ 338 /* find the header length */
336 return (char *) h[i].value.len; 339 return (u_char *) h[i].value.len;
337 } 340 }
338 341
339 return ngx_cpymem(buf, h[i].value.data, h[i].value.len); 342 return ngx_cpymem(buf, h[i].value.data, h[i].value.len);
340 } 343 }
341 } 344 }
348 351
349 return buf + 1; 352 return buf + 1;
350 } 353 }
351 354
352 355
353 static char *ngx_http_log_header_out(ngx_http_request_t *r, char *buf, 356 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
354 uintptr_t data) 357 uintptr_t data)
355 { 358 {
356 int i; 359 ngx_uint_t i;
357 ngx_str_t *s; 360 ngx_str_t *s;
358 ngx_table_elt_t *h; 361 ngx_table_elt_t *h;
359 ngx_http_log_op_t *op; 362 ngx_http_log_op_t *op;
360 363
361 if (r) { 364 if (r) {
382 * they are too seldom needed to be logged. 385 * they are too seldom needed to be logged.
383 */ 386 */
384 387
385 if (data == offsetof(ngx_http_headers_out_t, date)) { 388 if (data == offsetof(ngx_http_headers_out_t, date)) {
386 if (buf == NULL) { 389 if (buf == NULL) {
387 return (char *) ngx_cached_http_time.len; 390 return (u_char *) ngx_cached_http_time.len;
388 } 391 }
389 return ngx_cpymem(buf, ngx_cached_http_time.data, 392 return ngx_cpymem(buf, ngx_cached_http_time.data,
390 ngx_cached_http_time.len); 393 ngx_cached_http_time.len);
391 } 394 }
392 395
393 if (data == offsetof(ngx_http_headers_out_t, server)) { 396 if (data == offsetof(ngx_http_headers_out_t, server)) {
394 if (buf == NULL) { 397 if (buf == NULL) {
395 return (char *) (sizeof(NGINX_VER) - 1); 398 return (u_char *) (sizeof(NGINX_VER) - 1);
396 } 399 }
397 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1); 400 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
398 } 401 }
399 402
400 if (data == offsetof(ngx_http_headers_out_t, content_length)) { 403 if (data == offsetof(ngx_http_headers_out_t, content_length)) {
404 } 407 }
405 return buf + 1; 408 return buf + 1;
406 } 409 }
407 410
408 if (buf == NULL) { 411 if (buf == NULL) {
409 return (char *) NGX_OFF_T_LEN; 412 return (u_char *) NGX_OFF_T_LEN;
410 } 413 }
411 return buf + ngx_snprintf(buf, NGX_OFF_T_LEN + 2, OFF_T_FMT, 414 return buf + ngx_snprintf((char *) buf,
415 NGX_OFF_T_LEN + 2, OFF_T_FMT,
412 r->headers_out.content_length_n); 416 r->headers_out.content_length_n);
413 } 417 }
414 418
415 if (data == offsetof(ngx_http_headers_out_t, last_modified)) { 419 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
416 if (r->headers_out.last_modified_time == -1) { 420 if (r->headers_out.last_modified_time == -1) {
419 } 423 }
420 return buf + 1; 424 return buf + 1;
421 } 425 }
422 426
423 if (buf == NULL) { 427 if (buf == NULL) {
424 return (char *) sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; 428 return (u_char *)
429 sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
425 } 430 }
426 return buf + ngx_http_time(buf, 431 return buf + ngx_http_time(buf,
427 r->headers_out.last_modified_time); 432 r->headers_out.last_modified_time);
428 } 433 }
429 434
434 return buf + 1; 439 return buf + 1;
435 } 440 }
436 441
437 if (buf == NULL) { 442 if (buf == NULL) {
438 /* find the header length */ 443 /* find the header length */
439 return (char *) h->value.len; 444 return (u_char *) h->value.len;
440 } 445 }
441 446
442 return ngx_cpymem(buf, h->value.data, h->value.len); 447 return ngx_cpymem(buf, h->value.data, h->value.len);
443 } 448 }
444 449
483 488
484 return NULL; 489 return NULL;
485 } 490 }
486 491
487 492
488 static char *ngx_http_log_connection_header_out(ngx_http_request_t *r, 493 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
489 char *buf, uintptr_t data) 494 u_char *buf, uintptr_t data)
490 { 495 {
491 if (buf == NULL) { 496 if (buf == NULL) {
492 return (char *) ((r->keepalive) ? sizeof("keep-alive") - 1: 497 return (u_char *) ((r->keepalive) ? sizeof("keep-alive") - 1:
493 sizeof("close") - 1); 498 sizeof("close") - 1);
494 } 499 }
495 500
496 if (r->keepalive) { 501 if (r->keepalive) {
497 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1); 502 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
498 503
500 return ngx_cpymem(buf, "close", sizeof("close") - 1); 505 return ngx_cpymem(buf, "close", sizeof("close") - 1);
501 } 506 }
502 } 507 }
503 508
504 509
505 static char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, 510 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
506 char *buf, 511 u_char *buf,
507 uintptr_t data) 512 uintptr_t data)
508 { 513 {
509 if (buf == NULL) { 514 if (buf == NULL) {
510 return (char *) ((r->chunked) ? sizeof("chunked") - 1 : 1); 515 return (u_char *) ((r->chunked) ? sizeof("chunked") - 1 : 1);
511 } 516 }
512 517
513 if (r->chunked) { 518 if (r->chunked) {
514 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1); 519 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
515 } 520 }
518 523
519 return buf + 1; 524 return buf + 1;
520 } 525 }
521 526
522 527
523 static char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, char *buf, 528 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
524 uintptr_t data) 529 u_char *buf,
525 { 530 uintptr_t data)
526 int i; 531 {
532 ngx_uint_t i;
527 ngx_str_t *s; 533 ngx_str_t *s;
528 ngx_table_elt_t *h; 534 ngx_table_elt_t *h;
529 535
530 s = (ngx_str_t *) data; 536 s = (ngx_str_t *) data;
531 537
536 } 542 }
537 543
538 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) { 544 if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) {
539 if (buf == NULL) { 545 if (buf == NULL) {
540 /* find the header length */ 546 /* find the header length */
541 return (char *) h[i].value.len; 547 return (u_char *) h[i].value.len;
542 } 548 }
543 549
544 return ngx_cpymem(buf, h[i].value.data, h[i].value.len); 550 return ngx_cpymem(buf, h[i].value.data, h[i].value.len);
545 } 551 }
546 } 552 }
663 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, 669 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
664 void *conf) 670 void *conf)
665 { 671 {
666 ngx_http_log_loc_conf_t *llcf = conf; 672 ngx_http_log_loc_conf_t *llcf = conf;
667 673
668 int i; 674 ngx_uint_t i;
669 ngx_str_t *value, name; 675 ngx_str_t *value, name;
670 ngx_http_log_t *log; 676 ngx_http_log_t *log;
671 ngx_http_log_fmt_t *fmt; 677 ngx_http_log_fmt_t *fmt;
672 ngx_http_log_main_conf_t *lmcf; 678 ngx_http_log_main_conf_t *lmcf;
673 679
713 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, 719 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
714 void *conf) 720 void *conf)
715 { 721 {
716 ngx_http_log_main_conf_t *lmcf = conf; 722 ngx_http_log_main_conf_t *lmcf = conf;
717 723
718 int s, f, invalid; 724 ngx_uint_t s, f, invalid;
719 char *data, *p, *fname; 725 u_char *data, *p, *fname;
720 size_t i, len, fname_len; 726 size_t i, len, fname_len;
721 ngx_str_t *value, arg, *a; 727 ngx_str_t *value, arg, *a;
722 ngx_http_log_op_t *op; 728 ngx_http_log_op_t *op;
723 ngx_http_log_fmt_t *fmt; 729 ngx_http_log_fmt_t *fmt;
724 ngx_http_log_op_name_t *name; 730 ngx_http_log_op_name_t *name;
841 if (!(a = ngx_palloc(cf->pool, sizeof(ngx_str_t)))) { 847 if (!(a = ngx_palloc(cf->pool, sizeof(ngx_str_t)))) {
842 return NGX_CONF_ERROR; 848 return NGX_CONF_ERROR;
843 } 849 }
844 850
845 *a = arg; 851 *a = arg;
846 name->op(NULL, (char *) op, (uintptr_t) a); 852 name->op(NULL, (u_char *) op, (uintptr_t) a);
847 853
848 break; 854 break;
849 } 855 }
850 } 856 }
851 857