r2138, r2308, r2360 merge:

$arg_... and $cookie_ variables
This commit is contained in:
Igor Sysoev 2009-04-01 15:36:15 +00:00
parent 0625c7898c
commit 9f958f5b9b
2 changed files with 121 additions and 0 deletions

View File

@ -650,6 +650,7 @@ ngx_http_process_request_line(ngx_event_t *rev)
r->request_line.len = r->request_end - r->request_start; r->request_line.len = r->request_end - r->request_start;
r->request_line.data = r->request_start; r->request_line.data = r->request_start;
*r->request_end = '\0';
if (r->args_start) { if (r->args_start) {

View File

@ -26,6 +26,10 @@ static ngx_int_t ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data); ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r, static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data); ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_cookie(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r, static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data); ngx_http_variable_value_t *v, uintptr_t data);
@ -481,6 +485,24 @@ ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key,
return NULL; return NULL;
} }
if (ngx_strncmp(name->data, "cookie_", 7) == 0) {
if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) {
return vv;
}
return NULL;
}
if (ngx_strncmp(name->data, "arg_", 4) == 0) {
if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) {
return vv;
}
return NULL;
}
vv->not_found = 1; vv->not_found = 1;
if (nowarn == 0) { if (nowarn == 0) {
@ -711,6 +733,90 @@ ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var,
} }
static ngx_int_t
ngx_http_variable_cookie(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
ngx_str_t *name = (ngx_str_t *) data;
ngx_str_t cookie, s;
s.len = name->len - (sizeof("cookie_") - 1);
s.data = name->data + sizeof("cookie_") - 1;
if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &s, &cookie)
== NGX_DECLINED)
{
v->not_found = 1;
return NGX_OK;
}
v->len = cookie.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = cookie.data;
return NGX_OK;
}
static ngx_int_t
ngx_http_variable_argument(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
ngx_str_t *name = (ngx_str_t *) data;
u_char *p, *arg;
size_t len;
if (r->args.len == 0) {
v->not_found = 1;
return NGX_OK;
}
len = name->len - 1 - (sizeof("arg_") - 1);
arg = name->data + sizeof("arg_") - 1;
for (p = r->args.data; *p && *p != ' '; p++) {
/*
* although r->args.data is not null-terminated by itself,
* however, there is null in the end of request line
*/
p = ngx_strcasestrn(p, (char *) arg, len);
if (p == NULL) {
v->not_found = 1;
return NGX_OK;
}
if ((p == r->args.data || *(p - 1) == '&') && *(p + len + 1) == '=') {
v->data = p + len + 2;
p = (u_char *) ngx_strchr(p, '&');
if (p == NULL) {
p = r->args.data + r->args.len;
}
v->len = p - v->data;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
}
v->not_found = 1;
return NGX_OK;
}
static ngx_int_t static ngx_int_t
ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v, ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data) uintptr_t data)
@ -1396,6 +1502,20 @@ ngx_http_variables_init_vars(ngx_conf_t *cf)
continue; continue;
} }
if (ngx_strncmp(v[i].name.data, "cookie_", 7) == 0) {
v[i].get_handler = ngx_http_variable_cookie;
v[i].data = (uintptr_t) &v[i].name;
continue;
}
if (ngx_strncmp(v[i].name.data, "arg_", 4) == 0) {
v[i].get_handler = ngx_http_variable_argument;
v[i].data = (uintptr_t) &v[i].name;
continue;
}
ngx_log_error(NGX_LOG_EMERG, cf->log, 0, ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"unknown \"%V\" variable", &v[i].name); "unknown \"%V\" variable", &v[i].name);