comparison src/http/modules/ngx_http_proxy_module.c @ 4466:b404f34b5cb8

Proxy: added the "proxy_cookie_path" directive.
author Valentin Bartenev <vbart@nginx.com>
date Mon, 13 Feb 2012 11:08:05 +0000
parents 8293fc2d802b
children 778ef9c3fd2d
comparison
equal deleted inserted replaced
4465:8293fc2d802b 4466:b404f34b5cb8
54 ngx_array_t *proxy_lengths; 54 ngx_array_t *proxy_lengths;
55 ngx_array_t *proxy_values; 55 ngx_array_t *proxy_values;
56 56
57 ngx_array_t *redirects; 57 ngx_array_t *redirects;
58 ngx_array_t *cookie_domains; 58 ngx_array_t *cookie_domains;
59 ngx_array_t *cookie_paths;
59 60
60 ngx_str_t body_source; 61 ngx_str_t body_source;
61 62
62 ngx_str_t method; 63 ngx_str_t method;
63 ngx_str_t location; 64 ngx_str_t location;
143 void *conf); 144 void *conf);
144 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, 145 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
145 void *conf); 146 void *conf);
146 static char *ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, 147 static char *ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd,
147 void *conf); 148 void *conf);
149 static char *ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd,
150 void *conf);
148 static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, 151 static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd,
149 void *conf); 152 void *conf);
150 #if (NGX_HTTP_CACHE) 153 #if (NGX_HTTP_CACHE)
151 static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, 154 static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd,
152 void *conf); 155 void *conf);
212 NULL }, 215 NULL },
213 216
214 { ngx_string("proxy_cookie_domain"), 217 { ngx_string("proxy_cookie_domain"),
215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, 218 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
216 ngx_http_proxy_cookie_domain, 219 ngx_http_proxy_cookie_domain,
220 NGX_HTTP_LOC_CONF_OFFSET,
221 0,
222 NULL },
223
224 { ngx_string("proxy_cookie_path"),
225 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
226 ngx_http_proxy_cookie_path,
217 NGX_HTTP_LOC_CONF_OFFSET, 227 NGX_HTTP_LOC_CONF_OFFSET,
218 0, 228 0,
219 NULL }, 229 NULL },
220 230
221 { ngx_string("proxy_store"), 231 { ngx_string("proxy_store"),
667 677
668 if (plcf->redirects) { 678 if (plcf->redirects) {
669 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect; 679 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
670 } 680 }
671 681
672 if (plcf->cookie_domains) { 682 if (plcf->cookie_domains || plcf->cookie_paths) {
673 u->rewrite_cookie = ngx_http_proxy_rewrite_cookie; 683 u->rewrite_cookie = ngx_http_proxy_rewrite_cookie;
674 } 684 }
675 685
676 u->buffering = plcf->upstream.buffering; 686 u->buffering = plcf->upstream.buffering;
677 687
2327 static ngx_int_t 2337 static ngx_int_t
2328 ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h) 2338 ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h)
2329 { 2339 {
2330 size_t prefix; 2340 size_t prefix;
2331 u_char *p; 2341 u_char *p;
2332 ngx_int_t rc; 2342 ngx_int_t rc, rv;
2333 ngx_http_proxy_loc_conf_t *plcf; 2343 ngx_http_proxy_loc_conf_t *plcf;
2334 2344
2335 p = (u_char *) ngx_strchr(h->value.data, ';'); 2345 p = (u_char *) ngx_strchr(h->value.data, ';');
2336 if (p == NULL) { 2346 if (p == NULL) {
2337 return NGX_DECLINED; 2347 return NGX_DECLINED;
2338 } 2348 }
2339 2349
2340 prefix = p + 1 - h->value.data; 2350 prefix = p + 1 - h->value.data;
2341 2351
2342 rc = NGX_DECLINED; 2352 rv = NGX_DECLINED;
2343 2353
2344 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); 2354 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
2345 2355
2346 if (plcf->cookie_domains) { 2356 if (plcf->cookie_domains) {
2347 p = ngx_strcasestrn(h->value.data + prefix, "domain=", 7 - 1); 2357 p = ngx_strcasestrn(h->value.data + prefix, "domain=", 7 - 1);
2348 2358
2349 if (p) { 2359 if (p) {
2350 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 7, 2360 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 7,
2351 plcf->cookie_domains); 2361 plcf->cookie_domains);
2352 } 2362 if (rc == NGX_ERROR) {
2353 } 2363 return NGX_ERROR;
2354 2364 }
2355 return rc; 2365
2366 if (rc != NGX_DECLINED) {
2367 rv = rc;
2368 }
2369 }
2370 }
2371
2372 if (plcf->cookie_paths) {
2373 p = ngx_strcasestrn(h->value.data + prefix, "path=", 5 - 1);
2374
2375 if (p) {
2376 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 5,
2377 plcf->cookie_paths);
2378 if (rc == NGX_ERROR) {
2379 return NGX_ERROR;
2380 }
2381
2382 if (rc != NGX_DECLINED) {
2383 rv = rc;
2384 }
2385 }
2386 }
2387
2388 return rv;
2356 } 2389 }
2357 2390
2358 2391
2359 static ngx_int_t 2392 static ngx_int_t
2360 ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_table_elt_t *h, 2393 ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_table_elt_t *h,
2607 2640
2608 conf->redirect = NGX_CONF_UNSET; 2641 conf->redirect = NGX_CONF_UNSET;
2609 conf->upstream.change_buffering = 1; 2642 conf->upstream.change_buffering = 1;
2610 2643
2611 conf->cookie_domains = NGX_CONF_UNSET_PTR; 2644 conf->cookie_domains = NGX_CONF_UNSET_PTR;
2645 conf->cookie_paths = NGX_CONF_UNSET_PTR;
2612 2646
2613 conf->http_version = NGX_CONF_UNSET_UINT; 2647 conf->http_version = NGX_CONF_UNSET_UINT;
2614 2648
2615 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT; 2649 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
2616 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT; 2650 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
2923 } 2957 }
2924 } 2958 }
2925 } 2959 }
2926 2960
2927 ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL); 2961 ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL);
2962
2963 ngx_conf_merge_ptr_value(conf->cookie_paths, prev->cookie_paths, NULL);
2928 2964
2929 #if (NGX_HTTP_SSL) 2965 #if (NGX_HTTP_SSL)
2930 if (conf->upstream.ssl == NULL) { 2966 if (conf->upstream.ssl == NULL) {
2931 conf->upstream.ssl = prev->upstream.ssl; 2967 conf->upstream.ssl = prev->upstream.ssl;
2932 } 2968 }
3635 3671
3636 return NGX_CONF_OK; 3672 return NGX_CONF_OK;
3637 } 3673 }
3638 3674
3639 3675
3676 static char *
3677 ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3678 {
3679 ngx_http_proxy_loc_conf_t *plcf = conf;
3680
3681 ngx_str_t *value;
3682 ngx_http_proxy_rewrite_t *pr;
3683 ngx_http_compile_complex_value_t ccv;
3684
3685 if (plcf->cookie_paths == NULL) {
3686 return NGX_CONF_OK;
3687 }
3688
3689 value = cf->args->elts;
3690
3691 if (cf->args->nelts == 2) {
3692
3693 if (ngx_strcmp(value[1].data, "off") == 0) {
3694 plcf->cookie_paths = NULL;
3695 return NGX_CONF_OK;
3696 }
3697
3698 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3699 "invalid parameter \"%V\"", &value[1]);
3700 return NGX_CONF_ERROR;
3701 }
3702
3703 if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) {
3704 plcf->cookie_paths = ngx_array_create(cf->pool, 1,
3705 sizeof(ngx_http_proxy_rewrite_t));
3706 if (plcf->cookie_paths == NULL) {
3707 return NGX_CONF_ERROR;
3708 }
3709 }
3710
3711 pr = ngx_array_push(plcf->cookie_paths);
3712 if (pr == NULL) {
3713 return NGX_CONF_ERROR;
3714 }
3715
3716 if (value[1].data[0] == '~') {
3717 value[1].len--;
3718 value[1].data++;
3719
3720 if (value[1].data[0] == '*') {
3721 value[1].len--;
3722 value[1].data++;
3723
3724 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
3725 return NGX_CONF_ERROR;
3726 }
3727
3728 } else {
3729 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
3730 return NGX_CONF_ERROR;
3731 }
3732 }
3733
3734 } else {
3735
3736 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3737
3738 ccv.cf = cf;
3739 ccv.value = &value[1];
3740 ccv.complex_value = &pr->pattern.complex;
3741
3742 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3743 return NGX_CONF_ERROR;
3744 }
3745
3746 pr->handler = ngx_http_proxy_rewrite_complex_handler;
3747 }
3748
3749 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3750
3751 ccv.cf = cf;
3752 ccv.value = &value[2];
3753 ccv.complex_value = &pr->replacement;
3754
3755 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3756 return NGX_CONF_ERROR;
3757 }
3758
3759 return NGX_CONF_OK;
3760 }
3761
3762
3640 static ngx_int_t 3763 static ngx_int_t
3641 ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr, 3764 ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr,
3642 ngx_str_t *regex, ngx_uint_t caseless) 3765 ngx_str_t *regex, ngx_uint_t caseless)
3643 { 3766 {
3644 #if (NGX_PCRE) 3767 #if (NGX_PCRE)