Mercurial > hg > nginx-mail
comparison src/http/modules/ngx_http_proxy_module.c @ 665:0b460e61bdcd default tip
Merge with nginx 1.0.0.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 25 Apr 2011 04:22:17 +0400 |
parents | bb20316269e4 |
children |
comparison
equal
deleted
inserted
replaced
572:06419a2298a9 | 665:0b460e61bdcd |
---|---|
49 ngx_array_t *headers_set_len; | 49 ngx_array_t *headers_set_len; |
50 ngx_array_t *headers_set; | 50 ngx_array_t *headers_set; |
51 ngx_hash_t headers_set_hash; | 51 ngx_hash_t headers_set_hash; |
52 | 52 |
53 ngx_array_t *headers_source; | 53 ngx_array_t *headers_source; |
54 ngx_array_t *headers_names; | |
55 | 54 |
56 ngx_array_t *proxy_lengths; | 55 ngx_array_t *proxy_lengths; |
57 ngx_array_t *proxy_values; | 56 ngx_array_t *proxy_values; |
58 | 57 |
59 ngx_array_t *redirects; | 58 ngx_array_t *redirects; |
76 ngx_uint_t headers_hash_bucket_size; | 75 ngx_uint_t headers_hash_bucket_size; |
77 } ngx_http_proxy_loc_conf_t; | 76 } ngx_http_proxy_loc_conf_t; |
78 | 77 |
79 | 78 |
80 typedef struct { | 79 typedef struct { |
81 ngx_uint_t status; | 80 ngx_http_status_t status; |
82 ngx_uint_t status_count; | |
83 u_char *status_start; | |
84 u_char *status_end; | |
85 | |
86 ngx_http_proxy_vars_t vars; | 81 ngx_http_proxy_vars_t vars; |
87 | |
88 size_t internal_body_length; | 82 size_t internal_body_length; |
89 } ngx_http_proxy_ctx_t; | 83 } ngx_http_proxy_ctx_t; |
90 | |
91 | |
92 #define NGX_HTTP_PROXY_PARSE_NO_HEADER 20 | |
93 | 84 |
94 | 85 |
95 static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r, | 86 static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r, |
96 ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf); | 87 ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf); |
97 #if (NGX_HTTP_CACHE) | 88 #if (NGX_HTTP_CACHE) |
98 static ngx_int_t ngx_http_proxy_create_key(ngx_http_request_t *r); | 89 static ngx_int_t ngx_http_proxy_create_key(ngx_http_request_t *r); |
99 #endif | 90 #endif |
100 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r); | 91 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r); |
101 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r); | 92 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r); |
102 static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r); | 93 static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r); |
103 static ngx_int_t ngx_http_proxy_parse_status_line(ngx_http_request_t *r, | |
104 ngx_http_proxy_ctx_t *ctx); | |
105 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r); | 94 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r); |
106 static void ngx_http_proxy_abort_request(ngx_http_request_t *r); | 95 static void ngx_http_proxy_abort_request(ngx_http_request_t *r); |
107 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r, | 96 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r, |
108 ngx_int_t rc); | 97 ngx_int_t rc); |
109 | 98 |
139 static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, | 128 static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, |
140 void *conf); | 129 void *conf); |
141 #endif | 130 #endif |
142 | 131 |
143 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data); | 132 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data); |
144 | |
145 static char *ngx_http_proxy_upstream_max_fails_unsupported(ngx_conf_t *cf, | |
146 ngx_command_t *cmd, void *conf); | |
147 static char *ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf, | |
148 ngx_command_t *cmd, void *conf); | |
149 | 133 |
150 #if (NGX_HTTP_SSL) | 134 #if (NGX_HTTP_SSL) |
151 static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf, | 135 static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf, |
152 ngx_http_proxy_loc_conf_t *plcf); | 136 ngx_http_proxy_loc_conf_t *plcf); |
153 #endif | 137 #endif |
171 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, | 155 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, |
172 { ngx_null_string, 0 } | 156 { ngx_null_string, 0 } |
173 }; | 157 }; |
174 | 158 |
175 | 159 |
176 static ngx_conf_bitmask_t ngx_http_proxy_ignore_headers_masks[] = { | |
177 { ngx_string("X-Accel-Redirect"), NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT }, | |
178 { ngx_string("X-Accel-Expires"), NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES }, | |
179 { ngx_string("Expires"), NGX_HTTP_UPSTREAM_IGN_EXPIRES }, | |
180 { ngx_string("Cache-Control"), NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL }, | |
181 { ngx_null_string, 0 } | |
182 }; | |
183 | |
184 | |
185 ngx_module_t ngx_http_proxy_module; | 160 ngx_module_t ngx_http_proxy_module; |
186 | 161 |
187 | 162 |
188 static ngx_command_t ngx_http_proxy_commands[] = { | 163 static ngx_command_t ngx_http_proxy_commands[] = { |
189 | 164 |
227 ngx_conf_set_flag_slot, | 202 ngx_conf_set_flag_slot, |
228 NGX_HTTP_LOC_CONF_OFFSET, | 203 NGX_HTTP_LOC_CONF_OFFSET, |
229 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort), | 204 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort), |
230 NULL }, | 205 NULL }, |
231 | 206 |
207 { ngx_string("proxy_bind"), | |
208 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
209 ngx_http_upstream_bind_set_slot, | |
210 NGX_HTTP_LOC_CONF_OFFSET, | |
211 offsetof(ngx_http_proxy_loc_conf_t, upstream.local), | |
212 NULL }, | |
213 | |
232 { ngx_string("proxy_connect_timeout"), | 214 { ngx_string("proxy_connect_timeout"), |
233 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
234 ngx_conf_set_msec_slot, | 216 ngx_conf_set_msec_slot, |
235 NGX_HTTP_LOC_CONF_OFFSET, | 217 NGX_HTTP_LOC_CONF_OFFSET, |
236 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout), | 218 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout), |
335 NULL }, | 317 NULL }, |
336 | 318 |
337 #if (NGX_HTTP_CACHE) | 319 #if (NGX_HTTP_CACHE) |
338 | 320 |
339 { ngx_string("proxy_cache"), | 321 { ngx_string("proxy_cache"), |
340 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | 322 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
341 ngx_http_proxy_cache, | 323 ngx_http_proxy_cache, |
342 NGX_HTTP_LOC_CONF_OFFSET, | 324 NGX_HTTP_LOC_CONF_OFFSET, |
343 0, | 325 0, |
344 NULL }, | 326 NULL }, |
345 | 327 |
346 { ngx_string("proxy_cache_key"), | 328 { ngx_string("proxy_cache_key"), |
347 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | 329 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
348 ngx_http_proxy_cache_key, | 330 ngx_http_proxy_cache_key, |
349 NGX_HTTP_LOC_CONF_OFFSET, | 331 NGX_HTTP_LOC_CONF_OFFSET, |
350 0, | 332 0, |
351 NULL }, | 333 NULL }, |
352 | 334 |
355 ngx_http_file_cache_set_slot, | 337 ngx_http_file_cache_set_slot, |
356 0, | 338 0, |
357 0, | 339 0, |
358 &ngx_http_proxy_module }, | 340 &ngx_http_proxy_module }, |
359 | 341 |
342 { ngx_string("proxy_cache_bypass"), | |
343 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
344 ngx_http_set_predicate_slot, | |
345 NGX_HTTP_LOC_CONF_OFFSET, | |
346 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_bypass), | |
347 NULL }, | |
348 | |
349 { ngx_string("proxy_no_cache"), | |
350 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
351 ngx_http_set_predicate_slot, | |
352 NGX_HTTP_LOC_CONF_OFFSET, | |
353 offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache), | |
354 NULL }, | |
355 | |
360 { ngx_string("proxy_cache_valid"), | 356 { ngx_string("proxy_cache_valid"), |
361 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | 357 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
362 ngx_http_file_cache_valid_set_slot, | 358 ngx_http_file_cache_valid_set_slot, |
363 NGX_HTTP_LOC_CONF_OFFSET, | 359 NGX_HTTP_LOC_CONF_OFFSET, |
364 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid), | 360 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid), |
413 ngx_conf_set_bitmask_slot, | 409 ngx_conf_set_bitmask_slot, |
414 NGX_HTTP_LOC_CONF_OFFSET, | 410 NGX_HTTP_LOC_CONF_OFFSET, |
415 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream), | 411 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream), |
416 &ngx_http_proxy_next_upstream_masks }, | 412 &ngx_http_proxy_next_upstream_masks }, |
417 | 413 |
418 { ngx_string("proxy_upstream_max_fails"), | 414 { ngx_string("proxy_pass_header"), |
419 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 415 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
420 ngx_http_proxy_upstream_max_fails_unsupported, | 416 ngx_conf_set_str_array_slot, |
421 0, | 417 NGX_HTTP_LOC_CONF_OFFSET, |
422 0, | 418 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers), |
423 NULL }, | 419 NULL }, |
424 | 420 |
425 { ngx_string("proxy_upstream_fail_timeout"), | 421 { ngx_string("proxy_hide_header"), |
426 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 422 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
427 ngx_http_proxy_upstream_fail_timeout_unsupported, | |
428 0, | |
429 0, | |
430 NULL }, | |
431 | |
432 { ngx_string("proxy_pass_header"), | |
433 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
434 ngx_conf_set_str_array_slot, | |
435 NGX_HTTP_LOC_CONF_OFFSET, | |
436 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers), | |
437 NULL }, | |
438 | |
439 { ngx_string("proxy_hide_header"), | |
440 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
441 ngx_conf_set_str_array_slot, | 423 ngx_conf_set_str_array_slot, |
442 NGX_HTTP_LOC_CONF_OFFSET, | 424 NGX_HTTP_LOC_CONF_OFFSET, |
443 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers), | 425 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers), |
444 NULL }, | 426 NULL }, |
445 | 427 |
446 { ngx_string("proxy_ignore_headers"), | 428 { ngx_string("proxy_ignore_headers"), |
447 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | 429 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
448 ngx_conf_set_bitmask_slot, | 430 ngx_conf_set_bitmask_slot, |
449 NGX_HTTP_LOC_CONF_OFFSET, | 431 NGX_HTTP_LOC_CONF_OFFSET, |
450 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers), | 432 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers), |
451 &ngx_http_proxy_ignore_headers_masks }, | 433 &ngx_http_upstream_ignore_headers_masks }, |
452 | 434 |
453 #if (NGX_HTTP_SSL) | 435 #if (NGX_HTTP_SSL) |
454 | 436 |
455 { ngx_string("proxy_ssl_session_reuse"), | 437 { ngx_string("proxy_ssl_session_reuse"), |
456 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | 438 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
528 { ngx_string("Connection"), ngx_string("close") }, | 510 { ngx_string("Connection"), ngx_string("close") }, |
529 { ngx_string("Keep-Alive"), ngx_string("") }, | 511 { ngx_string("Keep-Alive"), ngx_string("") }, |
530 { ngx_string("Expect"), ngx_string("") }, | 512 { ngx_string("Expect"), ngx_string("") }, |
531 { ngx_string("If-Modified-Since"), ngx_string("") }, | 513 { ngx_string("If-Modified-Since"), ngx_string("") }, |
532 { ngx_string("If-Unmodified-Since"), ngx_string("") }, | 514 { ngx_string("If-Unmodified-Since"), ngx_string("") }, |
533 { ngx_string("If-Match-None"), ngx_string("") }, | 515 { ngx_string("If-None-Match"), ngx_string("") }, |
534 { ngx_string("If-Match"), ngx_string("") }, | 516 { ngx_string("If-Match"), ngx_string("") }, |
535 { ngx_string("Range"), ngx_string("") }, | 517 { ngx_string("Range"), ngx_string("") }, |
536 { ngx_string("If-Range"), ngx_string("") }, | 518 { ngx_string("If-Range"), ngx_string("") }, |
537 { ngx_null_string, ngx_null_string } | 519 { ngx_null_string, ngx_null_string } |
538 }; | |
539 | |
540 | |
541 static ngx_str_t ngx_http_proxy_hide_cache_headers[] = { | |
542 ngx_string("Date"), | |
543 ngx_string("Server"), | |
544 ngx_string("X-Pad"), | |
545 ngx_string("X-Accel-Expires"), | |
546 ngx_string("X-Accel-Redirect"), | |
547 ngx_string("X-Accel-Limit-Rate"), | |
548 ngx_string("X-Accel-Buffering"), | |
549 ngx_string("X-Accel-Charset"), | |
550 ngx_string("Set-Cookie"), | |
551 ngx_string("P3P"), | |
552 ngx_null_string | |
553 }; | 520 }; |
554 | 521 |
555 #endif | 522 #endif |
556 | 523 |
557 | 524 |
603 | 570 |
604 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | 571 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); |
605 | 572 |
606 u = r->upstream; | 573 u = r->upstream; |
607 | 574 |
608 if (plcf->proxy_lengths == 0) { | 575 if (plcf->proxy_lengths == NULL) { |
609 ctx->vars = plcf->vars; | 576 ctx->vars = plcf->vars; |
610 u->schema = plcf->vars.schema; | 577 u->schema = plcf->vars.schema; |
611 #if (NGX_HTTP_SSL) | 578 #if (NGX_HTTP_SSL) |
612 u->ssl = (plcf->upstream.ssl != NULL); | 579 u->ssl = (plcf->upstream.ssl != NULL); |
613 #endif | 580 #endif |
628 u->create_request = ngx_http_proxy_create_request; | 595 u->create_request = ngx_http_proxy_create_request; |
629 u->reinit_request = ngx_http_proxy_reinit_request; | 596 u->reinit_request = ngx_http_proxy_reinit_request; |
630 u->process_header = ngx_http_proxy_process_status_line; | 597 u->process_header = ngx_http_proxy_process_status_line; |
631 u->abort_request = ngx_http_proxy_abort_request; | 598 u->abort_request = ngx_http_proxy_abort_request; |
632 u->finalize_request = ngx_http_proxy_finalize_request; | 599 u->finalize_request = ngx_http_proxy_finalize_request; |
600 r->state = 0; | |
633 | 601 |
634 if (plcf->redirects) { | 602 if (plcf->redirects) { |
635 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect; | 603 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect; |
636 } | 604 } |
637 | 605 |
715 } | 683 } |
716 | 684 |
717 return NGX_ERROR; | 685 return NGX_ERROR; |
718 } | 686 } |
719 | 687 |
720 if (url.uri.len && url.uri.data[0] == '?') { | 688 if (url.uri.len) { |
721 p = ngx_pnalloc(r->pool, url.uri.len + 1); | 689 if (url.uri.data[0] == '?') { |
722 if (p == NULL) { | 690 p = ngx_pnalloc(r->pool, url.uri.len + 1); |
723 return NGX_ERROR; | 691 if (p == NULL) { |
724 } | 692 return NGX_ERROR; |
725 | 693 } |
726 *p++ = '/'; | 694 |
727 ngx_memcpy(p, url.uri.data, url.uri.len); | 695 *p++ = '/'; |
728 | 696 ngx_memcpy(p, url.uri.data, url.uri.len); |
729 url.uri.len++; | 697 |
730 url.uri.data = p - 1; | 698 url.uri.len++; |
699 url.uri.data = p - 1; | |
700 } | |
701 | |
702 } else { | |
703 url.uri = r->unparsed_uri; | |
731 } | 704 } |
732 | 705 |
733 ctx->vars.key_start = u->schema; | 706 ctx->vars.key_start = u->schema; |
734 | 707 |
735 ngx_http_proxy_set_vars(&url, &ctx->vars); | 708 ngx_http_proxy_set_vars(&url, &ctx->vars); |
909 | 882 |
910 } else { | 883 } else { |
911 loc_len = (r->valid_location && ctx->vars.uri.len) ? | 884 loc_len = (r->valid_location && ctx->vars.uri.len) ? |
912 plcf->location.len : 0; | 885 plcf->location.len : 0; |
913 | 886 |
914 if (r->quoted_uri || r->internal) { | 887 if (r->quoted_uri || r->space_in_uri || r->internal) { |
915 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len, | 888 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len, |
916 r->uri.len - loc_len, NGX_ESCAPE_URI); | 889 r->uri.len - loc_len, NGX_ESCAPE_URI); |
917 } | 890 } |
918 | 891 |
919 uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape | 892 uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape |
1178 | 1151 |
1179 if (ctx == NULL) { | 1152 if (ctx == NULL) { |
1180 return NGX_OK; | 1153 return NGX_OK; |
1181 } | 1154 } |
1182 | 1155 |
1183 ctx->status = 0; | 1156 ctx->status.code = 0; |
1184 ctx->status_count = 0; | 1157 ctx->status.count = 0; |
1185 ctx->status_start = NULL; | 1158 ctx->status.start = NULL; |
1186 ctx->status_end = NULL; | 1159 ctx->status.end = NULL; |
1187 | 1160 |
1188 r->upstream->process_header = ngx_http_proxy_process_status_line; | 1161 r->upstream->process_header = ngx_http_proxy_process_status_line; |
1162 r->state = 0; | |
1189 | 1163 |
1190 return NGX_OK; | 1164 return NGX_OK; |
1191 } | 1165 } |
1192 | 1166 |
1193 | 1167 |
1194 static ngx_int_t | 1168 static ngx_int_t |
1195 ngx_http_proxy_process_status_line(ngx_http_request_t *r) | 1169 ngx_http_proxy_process_status_line(ngx_http_request_t *r) |
1196 { | 1170 { |
1171 size_t len; | |
1197 ngx_int_t rc; | 1172 ngx_int_t rc; |
1198 ngx_http_upstream_t *u; | 1173 ngx_http_upstream_t *u; |
1199 ngx_http_proxy_ctx_t *ctx; | 1174 ngx_http_proxy_ctx_t *ctx; |
1200 | 1175 |
1201 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | 1176 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); |
1202 | 1177 |
1203 if (ctx == NULL) { | 1178 if (ctx == NULL) { |
1204 return NGX_ERROR; | 1179 return NGX_ERROR; |
1205 } | 1180 } |
1206 | 1181 |
1207 rc = ngx_http_proxy_parse_status_line(r, ctx); | 1182 u = r->upstream; |
1183 | |
1184 rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status); | |
1208 | 1185 |
1209 if (rc == NGX_AGAIN) { | 1186 if (rc == NGX_AGAIN) { |
1210 return rc; | 1187 return rc; |
1211 } | 1188 } |
1212 | 1189 |
1213 u = r->upstream; | 1190 if (rc == NGX_ERROR) { |
1214 | |
1215 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) { | |
1216 | 1191 |
1217 #if (NGX_HTTP_CACHE) | 1192 #if (NGX_HTTP_CACHE) |
1218 | 1193 |
1219 if (r->cache) { | 1194 if (r->cache) { |
1220 r->http_version = NGX_HTTP_VERSION_9; | 1195 r->http_version = NGX_HTTP_VERSION_9; |
1221 u->headers_in.status_n = NGX_HTTP_OK; | |
1222 return NGX_OK; | 1196 return NGX_OK; |
1223 } | 1197 } |
1224 | 1198 |
1225 #endif | 1199 #endif |
1226 | 1200 |
1232 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | 1206 return NGX_HTTP_UPSTREAM_INVALID_HEADER; |
1233 } | 1207 } |
1234 #endif | 1208 #endif |
1235 | 1209 |
1236 r->http_version = NGX_HTTP_VERSION_9; | 1210 r->http_version = NGX_HTTP_VERSION_9; |
1237 u->headers_in.status_n = NGX_HTTP_OK; | |
1238 u->state->status = NGX_HTTP_OK; | 1211 u->state->status = NGX_HTTP_OK; |
1239 | 1212 |
1240 return NGX_OK; | 1213 return NGX_OK; |
1241 } | 1214 } |
1242 | 1215 |
1243 if (u->state) { | 1216 if (u->state) { |
1244 u->state->status = ctx->status; | 1217 u->state->status = ctx->status.code; |
1245 } | 1218 } |
1246 | 1219 |
1247 u->headers_in.status_n = ctx->status; | 1220 u->headers_in.status_n = ctx->status.code; |
1248 | 1221 |
1249 u->headers_in.status_line.len = ctx->status_end - ctx->status_start; | 1222 len = ctx->status.end - ctx->status.start; |
1250 u->headers_in.status_line.data = ngx_pnalloc(r->pool, | 1223 u->headers_in.status_line.len = len; |
1251 u->headers_in.status_line.len); | 1224 |
1225 u->headers_in.status_line.data = ngx_pnalloc(r->pool, len); | |
1252 if (u->headers_in.status_line.data == NULL) { | 1226 if (u->headers_in.status_line.data == NULL) { |
1253 return NGX_ERROR; | 1227 return NGX_ERROR; |
1254 } | 1228 } |
1255 | 1229 |
1256 ngx_memcpy(u->headers_in.status_line.data, ctx->status_start, | 1230 ngx_memcpy(u->headers_in.status_line.data, ctx->status.start, len); |
1257 u->headers_in.status_line.len); | |
1258 | 1231 |
1259 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | 1232 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1260 "http proxy status %ui \"%V\"", | 1233 "http proxy status %ui \"%V\"", |
1261 u->headers_in.status_n, &u->headers_in.status_line); | 1234 u->headers_in.status_n, &u->headers_in.status_line); |
1262 | 1235 |
1263 u->process_header = ngx_http_proxy_process_header; | 1236 u->process_header = ngx_http_proxy_process_header; |
1264 | 1237 |
1265 return ngx_http_proxy_process_header(r); | 1238 return ngx_http_proxy_process_header(r); |
1266 } | |
1267 | |
1268 | |
1269 static ngx_int_t | |
1270 ngx_http_proxy_parse_status_line(ngx_http_request_t *r, | |
1271 ngx_http_proxy_ctx_t *ctx) | |
1272 { | |
1273 u_char ch; | |
1274 u_char *p; | |
1275 ngx_http_upstream_t *u; | |
1276 enum { | |
1277 sw_start = 0, | |
1278 sw_H, | |
1279 sw_HT, | |
1280 sw_HTT, | |
1281 sw_HTTP, | |
1282 sw_first_major_digit, | |
1283 sw_major_digit, | |
1284 sw_first_minor_digit, | |
1285 sw_minor_digit, | |
1286 sw_status, | |
1287 sw_space_after_status, | |
1288 sw_status_text, | |
1289 sw_almost_done | |
1290 } state; | |
1291 | |
1292 u = r->upstream; | |
1293 | |
1294 state = r->state; | |
1295 | |
1296 for (p = u->buffer.pos; p < u->buffer.last; p++) { | |
1297 ch = *p; | |
1298 | |
1299 switch (state) { | |
1300 | |
1301 /* "HTTP/" */ | |
1302 case sw_start: | |
1303 switch (ch) { | |
1304 case 'H': | |
1305 state = sw_H; | |
1306 break; | |
1307 default: | |
1308 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1309 } | |
1310 break; | |
1311 | |
1312 case sw_H: | |
1313 switch (ch) { | |
1314 case 'T': | |
1315 state = sw_HT; | |
1316 break; | |
1317 default: | |
1318 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1319 } | |
1320 break; | |
1321 | |
1322 case sw_HT: | |
1323 switch (ch) { | |
1324 case 'T': | |
1325 state = sw_HTT; | |
1326 break; | |
1327 default: | |
1328 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1329 } | |
1330 break; | |
1331 | |
1332 case sw_HTT: | |
1333 switch (ch) { | |
1334 case 'P': | |
1335 state = sw_HTTP; | |
1336 break; | |
1337 default: | |
1338 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1339 } | |
1340 break; | |
1341 | |
1342 case sw_HTTP: | |
1343 switch (ch) { | |
1344 case '/': | |
1345 state = sw_first_major_digit; | |
1346 break; | |
1347 default: | |
1348 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1349 } | |
1350 break; | |
1351 | |
1352 /* the first digit of major HTTP version */ | |
1353 case sw_first_major_digit: | |
1354 if (ch < '1' || ch > '9') { | |
1355 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1356 } | |
1357 | |
1358 state = sw_major_digit; | |
1359 break; | |
1360 | |
1361 /* the major HTTP version or dot */ | |
1362 case sw_major_digit: | |
1363 if (ch == '.') { | |
1364 state = sw_first_minor_digit; | |
1365 break; | |
1366 } | |
1367 | |
1368 if (ch < '0' || ch > '9') { | |
1369 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1370 } | |
1371 | |
1372 break; | |
1373 | |
1374 /* the first digit of minor HTTP version */ | |
1375 case sw_first_minor_digit: | |
1376 if (ch < '0' || ch > '9') { | |
1377 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1378 } | |
1379 | |
1380 state = sw_minor_digit; | |
1381 break; | |
1382 | |
1383 /* the minor HTTP version or the end of the request line */ | |
1384 case sw_minor_digit: | |
1385 if (ch == ' ') { | |
1386 state = sw_status; | |
1387 break; | |
1388 } | |
1389 | |
1390 if (ch < '0' || ch > '9') { | |
1391 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1392 } | |
1393 | |
1394 break; | |
1395 | |
1396 /* HTTP status code */ | |
1397 case sw_status: | |
1398 if (ch == ' ') { | |
1399 break; | |
1400 } | |
1401 | |
1402 if (ch < '0' || ch > '9') { | |
1403 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1404 } | |
1405 | |
1406 ctx->status = ctx->status * 10 + ch - '0'; | |
1407 | |
1408 if (++ctx->status_count == 3) { | |
1409 state = sw_space_after_status; | |
1410 ctx->status_start = p - 2; | |
1411 } | |
1412 | |
1413 break; | |
1414 | |
1415 /* space or end of line */ | |
1416 case sw_space_after_status: | |
1417 switch (ch) { | |
1418 case ' ': | |
1419 state = sw_status_text; | |
1420 break; | |
1421 case '.': /* IIS may send 403.1, 403.2, etc */ | |
1422 state = sw_status_text; | |
1423 break; | |
1424 case CR: | |
1425 state = sw_almost_done; | |
1426 break; | |
1427 case LF: | |
1428 goto done; | |
1429 default: | |
1430 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1431 } | |
1432 break; | |
1433 | |
1434 /* any text until end of line */ | |
1435 case sw_status_text: | |
1436 switch (ch) { | |
1437 case CR: | |
1438 state = sw_almost_done; | |
1439 | |
1440 break; | |
1441 case LF: | |
1442 goto done; | |
1443 } | |
1444 break; | |
1445 | |
1446 /* end of status line */ | |
1447 case sw_almost_done: | |
1448 ctx->status_end = p - 1; | |
1449 switch (ch) { | |
1450 case LF: | |
1451 goto done; | |
1452 default: | |
1453 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1454 } | |
1455 } | |
1456 } | |
1457 | |
1458 u->buffer.pos = p; | |
1459 r->state = state; | |
1460 | |
1461 return NGX_AGAIN; | |
1462 | |
1463 done: | |
1464 | |
1465 u->buffer.pos = p + 1; | |
1466 | |
1467 if (ctx->status_end == NULL) { | |
1468 ctx->status_end = p; | |
1469 } | |
1470 | |
1471 r->state = sw_start; | |
1472 | |
1473 return NGX_OK; | |
1474 } | 1239 } |
1475 | 1240 |
1476 | 1241 |
1477 static ngx_int_t | 1242 static ngx_int_t |
1478 ngx_http_proxy_process_header(ngx_http_request_t *r) | 1243 ngx_http_proxy_process_header(ngx_http_request_t *r) |
1554 } | 1319 } |
1555 | 1320 |
1556 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash( | 1321 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash( |
1557 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r'); | 1322 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r'); |
1558 | 1323 |
1559 h->key.len = sizeof("Server") - 1; | 1324 ngx_str_set(&h->key, "Server"); |
1560 h->key.data = (u_char *) "Server"; | 1325 ngx_str_null(&h->value); |
1561 h->value.len = 0; | |
1562 h->value.data = NULL; | |
1563 h->lowcase_key = (u_char *) "server"; | 1326 h->lowcase_key = (u_char *) "server"; |
1564 } | 1327 } |
1565 | 1328 |
1566 if (r->upstream->headers_in.date == NULL) { | 1329 if (r->upstream->headers_in.date == NULL) { |
1567 h = ngx_list_push(&r->upstream->headers_in.headers); | 1330 h = ngx_list_push(&r->upstream->headers_in.headers); |
1569 return NGX_ERROR; | 1332 return NGX_ERROR; |
1570 } | 1333 } |
1571 | 1334 |
1572 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e'); | 1335 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e'); |
1573 | 1336 |
1574 h->key.len = sizeof("Date") - 1; | 1337 ngx_str_set(&h->key, "Date"); |
1575 h->key.data = (u_char *) "Date"; | 1338 ngx_str_null(&h->value); |
1576 h->value.len = 0; | |
1577 h->value.data = NULL; | |
1578 h->lowcase_key = (u_char *) "date"; | 1339 h->lowcase_key = (u_char *) "date"; |
1579 } | 1340 } |
1580 | 1341 |
1581 return NGX_OK; | 1342 return NGX_OK; |
1582 } | 1343 } |
1768 pr->redirect.len) != 0) | 1529 pr->redirect.len) != 0) |
1769 { | 1530 { |
1770 return NGX_DECLINED; | 1531 return NGX_DECLINED; |
1771 } | 1532 } |
1772 | 1533 |
1773 len = prefix + pr->replacement.text.len + h->value.len - pr->redirect.len; | 1534 len = pr->replacement.text.len + h->value.len - pr->redirect.len; |
1774 | 1535 |
1775 data = ngx_pnalloc(r->pool, len); | 1536 data = ngx_pnalloc(r->pool, len); |
1776 if (data == NULL) { | 1537 if (data == NULL) { |
1777 return NGX_ERROR; | 1538 return NGX_ERROR; |
1778 } | 1539 } |
1779 | 1540 |
1780 p = data; | 1541 p = ngx_copy(data, h->value.data, prefix); |
1781 | |
1782 p = ngx_copy(p, h->value.data, prefix); | |
1783 | 1542 |
1784 if (pr->replacement.text.len) { | 1543 if (pr->replacement.text.len) { |
1785 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len); | 1544 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len); |
1786 } | 1545 } |
1787 | 1546 |
1815 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | 1574 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); |
1816 | 1575 |
1817 e.ip = pr->replacement.vars.lengths; | 1576 e.ip = pr->replacement.vars.lengths; |
1818 e.request = r; | 1577 e.request = r; |
1819 | 1578 |
1820 len = prefix + h->value.len - pr->redirect.len; | 1579 len = h->value.len - pr->redirect.len; |
1821 | 1580 |
1822 while (*(uintptr_t *) e.ip) { | 1581 while (*(uintptr_t *) e.ip) { |
1823 lcode = *(ngx_http_script_len_code_pt *) e.ip; | 1582 lcode = *(ngx_http_script_len_code_pt *) e.ip; |
1824 len += lcode(&e); | 1583 len += lcode(&e); |
1825 } | 1584 } |
1827 data = ngx_pnalloc(r->pool, len); | 1586 data = ngx_pnalloc(r->pool, len); |
1828 if (data == NULL) { | 1587 if (data == NULL) { |
1829 return NGX_ERROR; | 1588 return NGX_ERROR; |
1830 } | 1589 } |
1831 | 1590 |
1832 p = data; | 1591 p = ngx_copy(data, h->value.data, prefix); |
1833 | |
1834 p = ngx_copy(p, h->value.data, prefix); | |
1835 | 1592 |
1836 e.ip = pr->replacement.vars.values; | 1593 e.ip = pr->replacement.vars.values; |
1837 e.pos = p; | 1594 e.pos = p; |
1838 | 1595 |
1839 while (*(uintptr_t *) e.ip) { | 1596 while (*(uintptr_t *) e.ip) { |
1901 * conf->headers_set = NULL; | 1658 * conf->headers_set = NULL; |
1902 * conf->headers_set_hash = NULL; | 1659 * conf->headers_set_hash = NULL; |
1903 * conf->body_set_len = NULL; | 1660 * conf->body_set_len = NULL; |
1904 * conf->body_set = NULL; | 1661 * conf->body_set = NULL; |
1905 * conf->body_source = { 0, NULL }; | 1662 * conf->body_source = { 0, NULL }; |
1906 * conf->rewrite_locations = NULL; | 1663 * conf->redirects = NULL; |
1907 */ | 1664 */ |
1908 | 1665 |
1909 conf->upstream.store = NGX_CONF_UNSET; | 1666 conf->upstream.store = NGX_CONF_UNSET; |
1910 conf->upstream.store_access = NGX_CONF_UNSET_UINT; | 1667 conf->upstream.store_access = NGX_CONF_UNSET_UINT; |
1911 conf->upstream.buffering = NGX_CONF_UNSET; | 1668 conf->upstream.buffering = NGX_CONF_UNSET; |
1926 conf->upstream.pass_request_body = NGX_CONF_UNSET; | 1683 conf->upstream.pass_request_body = NGX_CONF_UNSET; |
1927 | 1684 |
1928 #if (NGX_HTTP_CACHE) | 1685 #if (NGX_HTTP_CACHE) |
1929 conf->upstream.cache = NGX_CONF_UNSET_PTR; | 1686 conf->upstream.cache = NGX_CONF_UNSET_PTR; |
1930 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; | 1687 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; |
1688 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR; | |
1689 conf->upstream.no_cache = NGX_CONF_UNSET_PTR; | |
1931 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR; | 1690 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR; |
1932 #endif | 1691 #endif |
1933 | 1692 |
1934 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR; | 1693 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR; |
1935 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR; | 1694 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR; |
1956 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | 1715 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) |
1957 { | 1716 { |
1958 ngx_http_proxy_loc_conf_t *prev = parent; | 1717 ngx_http_proxy_loc_conf_t *prev = parent; |
1959 ngx_http_proxy_loc_conf_t *conf = child; | 1718 ngx_http_proxy_loc_conf_t *conf = child; |
1960 | 1719 |
1720 u_char *p; | |
1961 size_t size; | 1721 size_t size; |
1962 ngx_str_t *h; | |
1963 ngx_keyval_t *s; | 1722 ngx_keyval_t *s; |
1964 ngx_hash_init_t hash; | 1723 ngx_hash_init_t hash; |
1724 ngx_http_core_loc_conf_t *clcf; | |
1965 ngx_http_proxy_redirect_t *pr; | 1725 ngx_http_proxy_redirect_t *pr; |
1966 ngx_http_script_compile_t sc; | 1726 ngx_http_script_compile_t sc; |
1967 | 1727 |
1968 if (conf->upstream.store != 0) { | 1728 if (conf->upstream.store != 0) { |
1969 ngx_conf_merge_value(conf->upstream.store, | 1729 ngx_conf_merge_value(conf->upstream.store, |
2150 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) { | 1910 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) { |
2151 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET | 1911 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET |
2152 |NGX_HTTP_UPSTREAM_FT_OFF; | 1912 |NGX_HTTP_UPSTREAM_FT_OFF; |
2153 } | 1913 } |
2154 | 1914 |
1915 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass, | |
1916 prev->upstream.cache_bypass, NULL); | |
1917 | |
1918 ngx_conf_merge_ptr_value(conf->upstream.no_cache, | |
1919 prev->upstream.no_cache, NULL); | |
1920 | |
1921 if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) { | |
1922 ngx_log_error(NGX_LOG_WARN, cf->log, 0, | |
1923 "\"proxy_no_cache\" functionality has been changed in 0.8.46, " | |
1924 "now it should be used together with \"proxy_cache_bypass\""); | |
1925 } | |
1926 | |
2155 ngx_conf_merge_ptr_value(conf->upstream.cache_valid, | 1927 ngx_conf_merge_ptr_value(conf->upstream.cache_valid, |
2156 prev->upstream.cache_valid, NULL); | 1928 prev->upstream.cache_valid, NULL); |
2157 | 1929 |
2158 if (conf->cache_key.value.data == NULL) { | 1930 if (conf->cache_key.value.data == NULL) { |
2159 conf->cache_key = prev->cache_key; | 1931 conf->cache_key = prev->cache_key; |
2202 if (pr == NULL) { | 1974 if (pr == NULL) { |
2203 return NGX_CONF_ERROR; | 1975 return NGX_CONF_ERROR; |
2204 } | 1976 } |
2205 | 1977 |
2206 pr->handler = ngx_http_proxy_rewrite_redirect_text; | 1978 pr->handler = ngx_http_proxy_rewrite_redirect_text; |
2207 pr->redirect = conf->url; | |
2208 | 1979 |
2209 if (conf->vars.uri.len) { | 1980 if (conf->vars.uri.len) { |
1981 pr->redirect = conf->url; | |
2210 pr->replacement.text = conf->location; | 1982 pr->replacement.text = conf->location; |
2211 | 1983 |
2212 } else { | 1984 } else { |
2213 pr->replacement.text.len = 0; | 1985 pr->redirect.len = conf->url.len + sizeof("/") - 1; |
2214 pr->replacement.text.data = NULL; | 1986 |
1987 p = ngx_pnalloc(cf->pool, pr->redirect.len); | |
1988 if (p == NULL) { | |
1989 return NGX_CONF_ERROR; | |
1990 } | |
1991 | |
1992 pr->redirect.data = p; | |
1993 | |
1994 p = ngx_cpymem(p, conf->url.data, conf->url.len); | |
1995 *p = '/'; | |
1996 | |
1997 ngx_str_set(&pr->replacement.text, "/"); | |
2215 } | 1998 } |
2216 } | 1999 } |
2217 } | |
2218 | |
2219 /* STUB */ | |
2220 if (prev->proxy_lengths) { | |
2221 conf->proxy_lengths = prev->proxy_lengths; | |
2222 conf->proxy_values = prev->proxy_values; | |
2223 } | 2000 } |
2224 | 2001 |
2225 #if (NGX_HTTP_SSL) | 2002 #if (NGX_HTTP_SSL) |
2226 if (conf->upstream.ssl == NULL) { | 2003 if (conf->upstream.ssl == NULL) { |
2227 conf->upstream.ssl = prev->upstream.ssl; | 2004 conf->upstream.ssl = prev->upstream.ssl; |
2239 | 2016 |
2240 hash.max_size = conf->headers_hash_max_size; | 2017 hash.max_size = conf->headers_hash_max_size; |
2241 hash.bucket_size = conf->headers_hash_bucket_size; | 2018 hash.bucket_size = conf->headers_hash_bucket_size; |
2242 hash.name = "proxy_headers_hash"; | 2019 hash.name = "proxy_headers_hash"; |
2243 | 2020 |
2244 #if (NGX_HTTP_CACHE) | |
2245 | |
2246 h = conf->upstream.cache ? ngx_http_proxy_hide_cache_headers: | |
2247 ngx_http_proxy_hide_headers; | |
2248 #else | |
2249 | |
2250 h = ngx_http_proxy_hide_headers; | |
2251 | |
2252 #endif | |
2253 | |
2254 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream, | 2021 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream, |
2255 &prev->upstream, h, &hash) | 2022 &prev->upstream, ngx_http_proxy_hide_headers, &hash) |
2256 != NGX_OK) | 2023 != NGX_OK) |
2257 { | 2024 { |
2258 return NGX_CONF_ERROR; | 2025 return NGX_CONF_ERROR; |
2259 } | 2026 } |
2260 | 2027 |
2261 if (conf->upstream.upstream == NULL) { | 2028 if (conf->upstream.upstream == NULL) { |
2262 conf->upstream.upstream = prev->upstream.upstream; | 2029 conf->upstream.upstream = prev->upstream.upstream; |
2263 conf->vars = prev->vars; | 2030 conf->vars = prev->vars; |
2264 } | 2031 } |
2265 | 2032 |
2033 if (conf->proxy_lengths == NULL) { | |
2034 conf->proxy_lengths = prev->proxy_lengths; | |
2035 conf->proxy_values = prev->proxy_values; | |
2036 } | |
2037 | |
2038 if (conf->upstream.upstream || conf->proxy_lengths) { | |
2039 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2040 if (clcf->handler == NULL && clcf->lmt_excpt) { | |
2041 clcf->handler = ngx_http_proxy_handler; | |
2042 conf->location = prev->location; | |
2043 } | |
2044 } | |
2266 | 2045 |
2267 if (conf->body_source.data == NULL) { | 2046 if (conf->body_source.data == NULL) { |
2268 conf->body_source = prev->body_source; | 2047 conf->body_source = prev->body_source; |
2269 conf->body_set_len = prev->body_set_len; | 2048 conf->body_set_len = prev->body_set_len; |
2270 conf->body_set = prev->body_set; | 2049 conf->body_set = prev->body_set; |
2297 s = ngx_array_push(conf->headers_source); | 2076 s = ngx_array_push(conf->headers_source); |
2298 if (s == NULL) { | 2077 if (s == NULL) { |
2299 return NGX_CONF_ERROR; | 2078 return NGX_CONF_ERROR; |
2300 } | 2079 } |
2301 | 2080 |
2302 s->key.len = sizeof("Content-Length") - 1; | 2081 ngx_str_set(&s->key, "Content-Length"); |
2303 s->key.data = (u_char *) "Content-Length"; | 2082 ngx_str_set(&s->value, "$proxy_internal_body_length"); |
2304 s->value.len = sizeof("$proxy_internal_body_length") - 1; | |
2305 s->value.data = (u_char *) "$proxy_internal_body_length"; | |
2306 } | 2083 } |
2307 | 2084 |
2308 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) { | 2085 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) { |
2309 return NGX_CONF_ERROR; | 2086 return NGX_CONF_ERROR; |
2310 } | 2087 } |
2319 { | 2096 { |
2320 u_char *p; | 2097 u_char *p; |
2321 size_t size; | 2098 size_t size; |
2322 uintptr_t *code; | 2099 uintptr_t *code; |
2323 ngx_uint_t i; | 2100 ngx_uint_t i; |
2101 ngx_array_t headers_names; | |
2324 ngx_keyval_t *src, *s, *h; | 2102 ngx_keyval_t *src, *s, *h; |
2325 ngx_hash_key_t *hk; | 2103 ngx_hash_key_t *hk; |
2326 ngx_hash_init_t hash; | 2104 ngx_hash_init_t hash; |
2327 ngx_http_script_compile_t sc; | 2105 ngx_http_script_compile_t sc; |
2328 ngx_http_script_copy_code_t *copy; | 2106 ngx_http_script_copy_code_t *copy; |
2343 { | 2121 { |
2344 return NGX_OK; | 2122 return NGX_OK; |
2345 } | 2123 } |
2346 | 2124 |
2347 | 2125 |
2348 conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t)); | 2126 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) |
2349 if (conf->headers_names == NULL) { | 2127 != NGX_OK) |
2128 { | |
2350 return NGX_ERROR; | 2129 return NGX_ERROR; |
2351 } | 2130 } |
2352 | 2131 |
2353 if (conf->headers_source == NULL) { | 2132 if (conf->headers_source == NULL) { |
2354 conf->headers_source = ngx_array_create(cf->pool, 4, | 2133 conf->headers_source = ngx_array_create(cf->pool, 4, |
2405 | 2184 |
2406 | 2185 |
2407 src = conf->headers_source->elts; | 2186 src = conf->headers_source->elts; |
2408 for (i = 0; i < conf->headers_source->nelts; i++) { | 2187 for (i = 0; i < conf->headers_source->nelts; i++) { |
2409 | 2188 |
2410 hk = ngx_array_push(conf->headers_names); | 2189 hk = ngx_array_push(&headers_names); |
2411 if (hk == NULL) { | 2190 if (hk == NULL) { |
2412 return NGX_ERROR; | 2191 return NGX_ERROR; |
2413 } | 2192 } |
2414 | 2193 |
2415 hk->key = src[i].key; | 2194 hk->key = src[i].key; |
2553 hash.bucket_size = conf->headers_hash_bucket_size; | 2332 hash.bucket_size = conf->headers_hash_bucket_size; |
2554 hash.name = "proxy_headers_hash"; | 2333 hash.name = "proxy_headers_hash"; |
2555 hash.pool = cf->pool; | 2334 hash.pool = cf->pool; |
2556 hash.temp_pool = NULL; | 2335 hash.temp_pool = NULL; |
2557 | 2336 |
2558 if (ngx_hash_init(&hash, conf->headers_names->elts, | 2337 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts); |
2559 conf->headers_names->nelts) | |
2560 != NGX_OK) | |
2561 { | |
2562 return NGX_ERROR; | |
2563 } | |
2564 | |
2565 return NGX_OK; | |
2566 } | 2338 } |
2567 | 2339 |
2568 | 2340 |
2569 static char * | 2341 static char * |
2570 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 2342 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
2695 static char * | 2467 static char * |
2696 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 2468 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
2697 { | 2469 { |
2698 ngx_http_proxy_loc_conf_t *plcf = conf; | 2470 ngx_http_proxy_loc_conf_t *plcf = conf; |
2699 | 2471 |
2472 u_char *p; | |
2700 ngx_str_t *value; | 2473 ngx_str_t *value; |
2701 ngx_array_t *vars_lengths, *vars_values; | 2474 ngx_array_t *vars_lengths, *vars_values; |
2702 ngx_http_script_compile_t sc; | 2475 ngx_http_script_compile_t sc; |
2703 ngx_http_proxy_redirect_t *pr; | 2476 ngx_http_proxy_redirect_t *pr; |
2704 | 2477 |
2742 if (pr == NULL) { | 2515 if (pr == NULL) { |
2743 return NGX_CONF_ERROR; | 2516 return NGX_CONF_ERROR; |
2744 } | 2517 } |
2745 | 2518 |
2746 if (ngx_strcmp(value[1].data, "default") == 0) { | 2519 if (ngx_strcmp(value[1].data, "default") == 0) { |
2520 if (plcf->proxy_lengths) { | |
2521 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2522 "\"proxy_redirect default\" may not be used " | |
2523 "with \"proxy_pass\" directive with variables"); | |
2524 return NGX_CONF_ERROR; | |
2525 } | |
2526 | |
2747 if (plcf->url.data == NULL) { | 2527 if (plcf->url.data == NULL) { |
2748 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | 2528 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2749 "\"proxy_rewrite_location default\" must go " | 2529 "\"proxy_redirect default\" must go " |
2750 "after the \"proxy_pass\" directive"); | 2530 "after the \"proxy_pass\" directive"); |
2751 return NGX_CONF_ERROR; | 2531 return NGX_CONF_ERROR; |
2752 } | 2532 } |
2753 | 2533 |
2754 pr->handler = ngx_http_proxy_rewrite_redirect_text; | 2534 pr->handler = ngx_http_proxy_rewrite_redirect_text; |
2755 pr->redirect = plcf->url; | |
2756 | 2535 |
2757 if (plcf->vars.uri.len) { | 2536 if (plcf->vars.uri.len) { |
2537 pr->redirect = plcf->url; | |
2758 pr->replacement.text = plcf->location; | 2538 pr->replacement.text = plcf->location; |
2759 | 2539 |
2760 } else { | 2540 } else { |
2761 pr->replacement.text.len = 0; | 2541 pr->redirect.len = plcf->url.len + sizeof("/") - 1; |
2762 pr->replacement.text.data = NULL; | 2542 |
2543 p = ngx_pnalloc(cf->pool, pr->redirect.len); | |
2544 if (p == NULL) { | |
2545 return NGX_CONF_ERROR; | |
2546 } | |
2547 | |
2548 pr->redirect.data = p; | |
2549 | |
2550 p = ngx_cpymem(p, plcf->url.data, plcf->url.len); | |
2551 *p = '/'; | |
2552 | |
2553 ngx_str_set(&pr->replacement.text, "/"); | |
2763 } | 2554 } |
2764 | 2555 |
2765 return NGX_CONF_OK; | 2556 return NGX_CONF_OK; |
2766 } | 2557 } |
2767 | 2558 |
2946 | 2737 |
2947 return NGX_CONF_OK; | 2738 return NGX_CONF_OK; |
2948 } | 2739 } |
2949 | 2740 |
2950 | 2741 |
2951 static char * | |
2952 ngx_http_proxy_upstream_max_fails_unsupported(ngx_conf_t *cf, | |
2953 ngx_command_t *cmd, void *conf) | |
2954 { | |
2955 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2956 "\"proxy_upstream_max_fails\" is not supported, " | |
2957 "use the \"max_fails\" parameter of the \"server\" directive ", | |
2958 "inside the \"upstream\" block"); | |
2959 | |
2960 return NGX_CONF_ERROR; | |
2961 } | |
2962 | |
2963 | |
2964 static char * | |
2965 ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf, | |
2966 ngx_command_t *cmd, void *conf) | |
2967 { | |
2968 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2969 "\"proxy_upstream_fail_timeout\" is not supported, " | |
2970 "use the \"fail_timeout\" parameter of the \"server\" directive ", | |
2971 "inside the \"upstream\" block"); | |
2972 | |
2973 return NGX_CONF_ERROR; | |
2974 } | |
2975 | |
2976 | |
2977 #if (NGX_HTTP_SSL) | 2742 #if (NGX_HTTP_SSL) |
2978 | 2743 |
2979 static ngx_int_t | 2744 static ngx_int_t |
2980 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf) | 2745 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf) |
2981 { | 2746 { |
3017 if (u->no_port || u->port == u->default_port) { | 2782 if (u->no_port || u->port == u->default_port) { |
3018 | 2783 |
3019 v->host_header = u->host; | 2784 v->host_header = u->host; |
3020 | 2785 |
3021 if (u->default_port == 80) { | 2786 if (u->default_port == 80) { |
3022 v->port.len = sizeof("80") - 1; | 2787 ngx_str_set(&v->port, "80"); |
3023 v->port.data = (u_char *) "80"; | |
3024 | 2788 |
3025 } else { | 2789 } else { |
3026 v->port.len = sizeof("443") - 1; | 2790 ngx_str_set(&v->port, "443"); |
3027 v->port.data = (u_char *) "443"; | |
3028 } | 2791 } |
3029 | 2792 |
3030 } else { | 2793 } else { |
3031 v->host_header.len = u->host.len + 1 + u->port_text.len; | 2794 v->host_header.len = u->host.len + 1 + u->port_text.len; |
3032 v->host_header.data = u->host.data; | 2795 v->host_header.data = u->host.data; |
3034 } | 2797 } |
3035 | 2798 |
3036 v->key_start.len += v->host_header.len; | 2799 v->key_start.len += v->host_header.len; |
3037 | 2800 |
3038 } else { | 2801 } else { |
3039 v->host_header.len = sizeof("localhost") - 1; | 2802 ngx_str_set(&v->host_header, "localhost"); |
3040 v->host_header.data = (u_char *) "localhost"; | 2803 ngx_str_null(&v->port); |
3041 v->port.len = 0; | |
3042 v->port.data = (u_char *) ""; | |
3043 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1; | 2804 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1; |
3044 } | 2805 } |
3045 | 2806 |
3046 v->uri = u->uri; | 2807 v->uri = u->uri; |
3047 } | 2808 } |