Mercurial > hg > nginx
comparison src/http/modules/ngx_http_log_module.c @ 2008:66dc85397a90
escape 0x00-0x1f, ", and \ in access log variables
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 15 May 2008 15:09:39 +0000 |
parents | 02a22cd5282a |
children | 1c009c5a163b |
comparison
equal
deleted
inserted
replaced
2007:b9de93d804ea | 2008:66dc85397a90 |
---|---|
86 ngx_http_log_op_t *op, ngx_str_t *value); | 86 ngx_http_log_op_t *op, ngx_str_t *value); |
87 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r, | 87 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r, |
88 uintptr_t data); | 88 uintptr_t data); |
89 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, | 89 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, |
90 ngx_http_log_op_t *op); | 90 ngx_http_log_op_t *op); |
91 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size); | |
91 | 92 |
92 | 93 |
93 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf); | 94 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf); |
94 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf); | 95 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf); |
95 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, | 96 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, |
476 | 477 |
477 | 478 |
478 static size_t | 479 static size_t |
479 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data) | 480 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data) |
480 { | 481 { |
482 uintptr_t len; | |
481 ngx_http_variable_value_t *value; | 483 ngx_http_variable_value_t *value; |
482 | 484 |
483 value = ngx_http_get_indexed_variable(r, data); | 485 value = ngx_http_get_indexed_variable(r, data); |
484 | 486 |
485 if (value == NULL || value->not_found) { | 487 if (value == NULL || value->not_found) { |
486 return 1; | 488 return 1; |
487 } | 489 } |
488 | 490 |
489 return value->len; | 491 len = ngx_http_log_escape(NULL, value->data, value->len); |
492 | |
493 value->escape = len ? 1 : 0; | |
494 | |
495 return value->len + len * 3; | |
490 } | 496 } |
491 | 497 |
492 | 498 |
493 static u_char * | 499 static u_char * |
494 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op) | 500 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op) |
500 if (value == NULL || value->not_found) { | 506 if (value == NULL || value->not_found) { |
501 *buf = '-'; | 507 *buf = '-'; |
502 return buf + 1; | 508 return buf + 1; |
503 } | 509 } |
504 | 510 |
505 return ngx_cpymem(buf, value->data, value->len); | 511 if (value->escape == 0) { |
512 return ngx_cpymem(buf, value->data, value->len); | |
513 | |
514 } else { | |
515 return (u_char *) ngx_http_log_escape(buf, value->data, value->len); | |
516 } | |
517 } | |
518 | |
519 | |
520 static uintptr_t | |
521 ngx_http_log_escape(u_char *dst, u_char *src, size_t size) | |
522 { | |
523 ngx_uint_t i, n; | |
524 static u_char hex[] = "0123456789ABCDEF"; | |
525 | |
526 static uint32_t escape[] = { | |
527 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | |
528 | |
529 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ | |
530 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ | |
531 | |
532 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ | |
533 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ | |
534 | |
535 /* ~}| {zyx wvut srqp onml kjih gfed cba` */ | |
536 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ | |
537 | |
538 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ | |
539 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ | |
540 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ | |
541 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ | |
542 }; | |
543 | |
544 | |
545 if (dst == NULL) { | |
546 | |
547 /* find the number of the characters to be escaped */ | |
548 | |
549 n = 0; | |
550 | |
551 for (i = 0; i < size; i++) { | |
552 if (escape[*src >> 5] & (1 << (*src & 0x1f))) { | |
553 n++; | |
554 } | |
555 src++; | |
556 } | |
557 | |
558 return (uintptr_t) n; | |
559 } | |
560 | |
561 for (i = 0; i < size; i++) { | |
562 if (escape[*src >> 5] & (1 << (*src & 0x1f))) { | |
563 *dst++ = '\\'; | |
564 *dst++ = 'x'; | |
565 *dst++ = hex[*src >> 4]; | |
566 *dst++ = hex[*src & 0xf]; | |
567 src++; | |
568 | |
569 } else { | |
570 *dst++ = *src++; | |
571 } | |
572 } | |
573 | |
574 return (uintptr_t) dst; | |
506 } | 575 } |
507 | 576 |
508 | 577 |
509 static void * | 578 static void * |
510 ngx_http_log_create_main_conf(ngx_conf_t *cf) | 579 ngx_http_log_create_main_conf(ngx_conf_t *cf) |