comparison src/http/ngx_http_variables.c @ 692:6db6e93f55ee NGINX_1_3_9

nginx 1.3.9 *) Feature: support for chunked transfer encoding while reading client request body. *) Feature: the $request_time and $msec variables can now be used not only in the "log_format" directive. *) Bugfix: cache manager and cache loader processes might not be able to start if more than 512 listen sockets were used. *) Bugfix: in the ngx_http_dav_module.
author Igor Sysoev <http://sysoev.ru>
date Tue, 27 Nov 2012 00:00:00 +0400
parents b5b7eea22fda
children 88a1b4797f2e
comparison
equal deleted inserted replaced
691:acfd484db0ca 692:6db6e93f55ee
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,
77 ngx_http_variable_value_t *v, uintptr_t data); 79 ngx_http_variable_value_t *v, uintptr_t data);
78 static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r, 80 static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r,
79 ngx_http_variable_value_t *v, uintptr_t data); 81 ngx_http_variable_value_t *v, uintptr_t data);
80 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r, 82 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r,
81 ngx_http_variable_value_t *v, uintptr_t data); 83 ngx_http_variable_value_t *v, uintptr_t data);
84 static ngx_int_t ngx_http_variable_request_time(ngx_http_request_t *r,
85 ngx_http_variable_value_t *v, uintptr_t data);
82 static ngx_int_t ngx_http_variable_status(ngx_http_request_t *r, 86 static ngx_int_t ngx_http_variable_status(ngx_http_request_t *r,
83 ngx_http_variable_value_t *v, uintptr_t data); 87 ngx_http_variable_value_t *v, uintptr_t data);
84 88
85 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r, 89 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r,
86 ngx_http_variable_value_t *v, uintptr_t data); 90 ngx_http_variable_value_t *v, uintptr_t data);
105 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r, 109 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r,
106 ngx_http_variable_value_t *v, uintptr_t data); 110 ngx_http_variable_value_t *v, uintptr_t data);
107 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r, 111 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
108 ngx_http_variable_value_t *v, uintptr_t data); 112 ngx_http_variable_value_t *v, uintptr_t data);
109 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r, 113 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r,
114 ngx_http_variable_value_t *v, uintptr_t data);
115 static ngx_int_t ngx_http_variable_msec(ngx_http_request_t *r,
110 ngx_http_variable_value_t *v, uintptr_t data); 116 ngx_http_variable_value_t *v, uintptr_t data);
111 117
112 /* 118 /*
113 * TODO: 119 * TODO:
114 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED 120 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED
147 #endif 153 #endif
148 154
149 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers, 155 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
150 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 }, 156 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },
151 157
152 { ngx_string("content_length"), NULL, ngx_http_variable_header, 158 { ngx_string("content_length"), NULL, ngx_http_variable_content_length,
153 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 }, 159 0, 0, 0 },
154 160
155 { ngx_string("content_type"), NULL, ngx_http_variable_header, 161 { ngx_string("content_type"), NULL, ngx_http_variable_header,
156 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 }, 162 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
157 163
158 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 }, 164 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },
235 241
236 { ngx_string("request_body_file"), NULL, 242 { ngx_string("request_body_file"), NULL,
237 ngx_http_variable_request_body_file, 243 ngx_http_variable_request_body_file,
238 0, 0, 0 }, 244 0, 0, 0 },
239 245
246 { ngx_string("request_time"), NULL, ngx_http_variable_request_time,
247 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
248
240 { ngx_string("status"), NULL, 249 { ngx_string("status"), NULL,
241 ngx_http_variable_status, 0, 250 ngx_http_variable_status, 0,
242 NGX_HTTP_VAR_NOCACHEABLE, 0 }, 251 NGX_HTTP_VAR_NOCACHEABLE, 0 },
243 252
244 { ngx_string("sent_http_content_type"), NULL, 253 { ngx_string("sent_http_content_type"), NULL,
282 { ngx_string("hostname"), NULL, ngx_http_variable_hostname, 291 { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
283 0, 0, 0 }, 292 0, 0, 0 },
284 293
285 { ngx_string("pid"), NULL, ngx_http_variable_pid, 294 { ngx_string("pid"), NULL, ngx_http_variable_pid,
286 0, 0, 0 }, 295 0, 0, 0 },
296
297 { ngx_string("msec"), NULL, ngx_http_variable_msec,
298 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
287 299
288 #if (NGX_HAVE_TCP_INFO) 300 #if (NGX_HAVE_TCP_INFO)
289 { ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo, 301 { ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,
290 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, 302 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
291 303
978 990
979 #endif 991 #endif
980 992
981 993
982 static ngx_int_t 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 }
1025
1026
1027 static ngx_int_t
983 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,
984 uintptr_t data) 1029 uintptr_t data)
985 { 1030 {
986 ngx_http_core_srv_conf_t *cscf; 1031 ngx_http_core_srv_conf_t *cscf;
987 1032
1755 ngx_http_variable_request_body(ngx_http_request_t *r, 1800 ngx_http_variable_request_body(ngx_http_request_t *r,
1756 ngx_http_variable_value_t *v, uintptr_t data) 1801 ngx_http_variable_value_t *v, uintptr_t data)
1757 { 1802 {
1758 u_char *p; 1803 u_char *p;
1759 size_t len; 1804 size_t len;
1760 ngx_buf_t *buf, *next; 1805 ngx_buf_t *buf;
1761 ngx_chain_t *cl; 1806 ngx_chain_t *cl;
1762 1807
1763 if (r->request_body == NULL 1808 if (r->request_body == NULL
1764 || r->request_body->bufs == NULL 1809 || r->request_body->bufs == NULL
1765 || r->request_body->temp_file) 1810 || r->request_body->temp_file)
1780 v->data = buf->pos; 1825 v->data = buf->pos;
1781 1826
1782 return NGX_OK; 1827 return NGX_OK;
1783 } 1828 }
1784 1829
1785 next = cl->next->buf; 1830 len = buf->last - buf->pos;
1786 len = (buf->last - buf->pos) + (next->last - next->pos); 1831 cl = cl->next;
1832
1833 for ( /* void */ ; cl; cl = cl->next) {
1834 buf = cl->buf;
1835 len += buf->last - buf->pos;
1836 }
1787 1837
1788 p = ngx_pnalloc(r->pool, len); 1838 p = ngx_pnalloc(r->pool, len);
1789 if (p == NULL) { 1839 if (p == NULL) {
1790 return NGX_ERROR; 1840 return NGX_ERROR;
1791 } 1841 }
1792 1842
1793 v->data = p; 1843 v->data = p;
1794 1844 cl = r->request_body->bufs;
1795 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos); 1845
1796 ngx_memcpy(p, next->pos, next->last - next->pos); 1846 for ( /* void */ ; cl; cl = cl->next) {
1847 buf = cl->buf;
1848 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
1849 }
1797 1850
1798 v->len = len; 1851 v->len = len;
1799 v->valid = 1; 1852 v->valid = 1;
1800 v->no_cacheable = 0; 1853 v->no_cacheable = 0;
1801 v->not_found = 0; 1854 v->not_found = 0;
1823 return NGX_OK; 1876 return NGX_OK;
1824 } 1877 }
1825 1878
1826 1879
1827 static ngx_int_t 1880 static ngx_int_t
1881 ngx_http_variable_request_time(ngx_http_request_t *r,
1882 ngx_http_variable_value_t *v, uintptr_t data)
1883 {
1884 u_char *p;
1885 ngx_time_t *tp;
1886 ngx_msec_int_t ms;
1887
1888 p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
1889 if (p == NULL) {
1890 return NGX_ERROR;
1891 }
1892
1893 tp = ngx_timeofday();
1894
1895 ms = (ngx_msec_int_t)
1896 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
1897 ms = ngx_max(ms, 0);
1898
1899 v->len = ngx_sprintf(p, "%T.%03M", ms / 1000, ms % 1000) - p;
1900 v->valid = 1;
1901 v->no_cacheable = 0;
1902 v->not_found = 0;
1903 v->data = p;
1904
1905 return NGX_OK;
1906 }
1907
1908
1909 static ngx_int_t
1828 ngx_http_variable_connection(ngx_http_request_t *r, 1910 ngx_http_variable_connection(ngx_http_request_t *r,
1829 ngx_http_variable_value_t *v, uintptr_t data) 1911 ngx_http_variable_value_t *v, uintptr_t data)
1830 { 1912 {
1831 u_char *p; 1913 u_char *p;
1832 1914
1904 if (p == NULL) { 1986 if (p == NULL) {
1905 return NGX_ERROR; 1987 return NGX_ERROR;
1906 } 1988 }
1907 1989
1908 v->len = ngx_sprintf(p, "%P", ngx_pid) - p; 1990 v->len = ngx_sprintf(p, "%P", ngx_pid) - p;
1991 v->valid = 1;
1992 v->no_cacheable = 0;
1993 v->not_found = 0;
1994 v->data = p;
1995
1996 return NGX_OK;
1997 }
1998
1999
2000 static ngx_int_t
2001 ngx_http_variable_msec(ngx_http_request_t *r,
2002 ngx_http_variable_value_t *v, uintptr_t data)
2003 {
2004 u_char *p;
2005 ngx_time_t *tp;
2006
2007 p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
2008 if (p == NULL) {
2009 return NGX_ERROR;
2010 }
2011
2012 tp = ngx_timeofday();
2013
2014 v->len = ngx_sprintf(p, "%T.%03M", tp->sec, tp->msec) - p;
1909 v->valid = 1; 2015 v->valid = 1;
1910 v->no_cacheable = 0; 2016 v->no_cacheable = 0;
1911 v->not_found = 0; 2017 v->not_found = 0;
1912 v->data = p; 2018 v->data = p;
1913 2019