comparison src/http/ngx_http_header_filter_module.c @ 1804:d457a1576532

several changes in server_name: *) server_name_in_redirect directive and removal of the '*' stub *) use server address in redirect if host can not be detected *) ngx_http_server_addr() *) allow wildcard and regex names to be a main server_name *) DAV Destination header is tested against Host header
author Igor Sysoev <igor@sysoev.ru>
date Sat, 29 Dec 2007 15:30:39 +0000
parents 68d21fd1dc64
children 0d9c6fe7502b
comparison
equal deleted inserted replaced
1803:7405719e4848 1804:d457a1576532
151 static ngx_int_t 151 static ngx_int_t
152 ngx_http_header_filter(ngx_http_request_t *r) 152 ngx_http_header_filter(ngx_http_request_t *r)
153 { 153 {
154 u_char *p; 154 u_char *p;
155 size_t len; 155 size_t len;
156 ngx_str_t host;
156 ngx_buf_t *b; 157 ngx_buf_t *b;
157 ngx_uint_t status, i; 158 ngx_uint_t status, i;
158 ngx_chain_t out; 159 ngx_chain_t out;
159 ngx_list_part_t *part; 160 ngx_list_part_t *part;
160 ngx_table_elt_t *header; 161 ngx_table_elt_t *header;
161 ngx_http_core_loc_conf_t *clcf; 162 ngx_http_core_loc_conf_t *clcf;
163 /* AF_INET only */
164 u_char addr[INET_ADDRSTRLEN];
162 165
163 r->header_sent = 1; 166 r->header_sent = 1;
164 167
165 if (r != r->main) { 168 if (r != r->main) {
166 return NGX_OK; 169 return NGX_OK;
276 && r->headers_out.location->value.len 279 && r->headers_out.location->value.len
277 && r->headers_out.location->value.data[0] == '/') 280 && r->headers_out.location->value.data[0] == '/')
278 { 281 {
279 r->headers_out.location->hash = 0; 282 r->headers_out.location->hash = 0;
280 283
284 if (clcf->server_name_in_redirect) {
285 host = r->server_name;
286
287 } else if (r->headers_in.host) {
288 host.len = r->headers_in.host_name_len;
289 host.data = r->headers_in.host->value.data;
290
291 } else {
292 host.data = addr;
293
294 if (ngx_http_server_addr(r, &host) != NGX_OK) {
295 return NGX_ERROR;
296 }
297 }
298
281 #if (NGX_HTTP_SSL) 299 #if (NGX_HTTP_SSL)
282 if (r->connection->ssl) { 300 if (r->connection->ssl) {
283 len += sizeof("Location: https://") - 1 301 len += sizeof("Location: https://") - 1
284 + r->server_name.len 302 + host.len
285 + r->headers_out.location->value.len + 2; 303 + r->headers_out.location->value.len + 2;
286 304
287 if (clcf->port_in_redirect && r->port != 443) { 305 if (clcf->port_in_redirect && r->port != 443) {
288 len += r->port_text->len; 306 len += r->port_text->len;
289 } 307 }
290 308
291 } else 309 } else
292 #endif 310 #endif
293 { 311 {
294 len += sizeof("Location: http://") - 1 312 len += sizeof("Location: http://") - 1
295 + r->server_name.len 313 + host.len
296 + r->headers_out.location->value.len + 2; 314 + r->headers_out.location->value.len + 2;
297 315
298 if (clcf->port_in_redirect && r->port != 80) { 316 if (clcf->port_in_redirect && r->port != 80) {
299 len += r->port_text->len; 317 len += r->port_text->len;
300 } 318 }
301 } 319 }
320
321 } else {
322 host.len = 0;
323 host.data = NULL;
302 } 324 }
303 325
304 if (r->chunked) { 326 if (r->chunked) {
305 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1; 327 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
306 } 328 }
426 b->last = ngx_http_time(b->last, r->headers_out.last_modified_time); 448 b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
427 449
428 *b->last++ = CR; *b->last++ = LF; 450 *b->last++ = CR; *b->last++ = LF;
429 } 451 }
430 452
431 if (r->headers_out.location 453 if (host.data) {
432 && r->headers_out.location->value.len 454
433 && r->headers_out.location->value.data[0] == '/')
434 {
435 p = b->last + sizeof("Location: ") - 1; 455 p = b->last + sizeof("Location: ") - 1;
436 456
437 b->last = ngx_cpymem(b->last, "Location: http", 457 b->last = ngx_cpymem(b->last, "Location: http",
438 sizeof("Location: http") - 1); 458 sizeof("Location: http") - 1);
439 459
442 *b->last++ ='s'; 462 *b->last++ ='s';
443 } 463 }
444 #endif 464 #endif
445 465
446 *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/'; 466 *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
447 b->last = ngx_copy(b->last, r->server_name.data, r->server_name.len); 467 b->last = ngx_copy(b->last, host.data, host.len);
448 468
449 if (clcf->port_in_redirect) { 469 if (clcf->port_in_redirect) {
450 #if (NGX_HTTP_SSL) 470 #if (NGX_HTTP_SSL)
451 if (r->connection->ssl) { 471 if (r->connection->ssl) {
452 if (r->port != 443) { 472 if (r->port != 443) {