comparison src/http/ngx_http_variables.c @ 4928:ec7d97006a30

Request body: $content_length variable to honor real body size. This allows to handle requests with chunked body by fastcgi and uwsgi modules, and also simplifies handling of various request body modifications.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 21 Nov 2012 01:05:08 +0000
parents fbc0791bebb2
children 8b635cf36ccc
comparison
equal deleted inserted replaced
4927:93294110728f 4928:ec7d97006a30
37 #if (NGX_HAVE_TCP_INFO) 37 #if (NGX_HAVE_TCP_INFO)
38 static ngx_int_t ngx_http_variable_tcpinfo(ngx_http_request_t *r, 38 static ngx_int_t ngx_http_variable_tcpinfo(ngx_http_request_t *r,
39 ngx_http_variable_value_t *v, uintptr_t data); 39 ngx_http_variable_value_t *v, uintptr_t data);
40 #endif 40 #endif
41 41
42 static ngx_int_t ngx_http_variable_content_length(ngx_http_request_t *r,
43 ngx_http_variable_value_t *v, uintptr_t data);
42 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r, 44 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r,
43 ngx_http_variable_value_t *v, uintptr_t data); 45 ngx_http_variable_value_t *v, uintptr_t data);
44 static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r, 46 static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
45 ngx_http_variable_value_t *v, uintptr_t data); 47 ngx_http_variable_value_t *v, uintptr_t data);
46 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r, 48 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r,
151 #endif 153 #endif
152 154
153 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers, 155 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
154 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 }, 156 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },
155 157
156 { ngx_string("content_length"), NULL, ngx_http_variable_header, 158 { ngx_string("content_length"), NULL, ngx_http_variable_content_length,
157 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 }, 159 0, 0, 0 },
158 160
159 { ngx_string("content_type"), NULL, ngx_http_variable_header, 161 { ngx_string("content_type"), NULL, ngx_http_variable_header,
160 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 }, 162 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
161 163
162 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 }, 164 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },
985 987
986 return NGX_OK; 988 return NGX_OK;
987 } 989 }
988 990
989 #endif 991 #endif
992
993
994 static ngx_int_t
995 ngx_http_variable_content_length(ngx_http_request_t *r,
996 ngx_http_variable_value_t *v, uintptr_t data)
997 {
998 u_char *p;
999
1000 if (r->headers_in.content_length) {
1001 v->len = r->headers_in.content_length->value.len;
1002 v->data = r->headers_in.content_length->value.data;
1003 v->valid = 1;
1004 v->no_cacheable = 0;
1005 v->not_found = 0;
1006
1007 } else if (r->headers_in.content_length_n >= 0) {
1008 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
1009 if (p == NULL) {
1010 return NGX_ERROR;
1011 }
1012
1013 v->len = ngx_sprintf(p, "%O", r->headers_in.content_length_n) - p;
1014 v->data = p;
1015 v->valid = 1;
1016 v->no_cacheable = 0;
1017 v->not_found = 0;
1018
1019 } else {
1020 v->not_found = 1;
1021 }
1022
1023 return NGX_OK;
1024 }
990 1025
991 1026
992 static ngx_int_t 1027 static ngx_int_t
993 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v, 1028 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v,
994 uintptr_t data) 1029 uintptr_t data)