comparison src/http/ngx_http_request.c @ 5092:c4d3310574e0

Apply server configuration as soon as host is known. Previously, this was done only after the whole request header was parsed, and if an error occurred earlier then the request was processed in the default server (or server chosen by SNI), while r->headers_in.server might be set to the value from the Host: header or host from request line. r->headers_in.server is in turn used for $host variable and in HTTP redirects if "server_name_in_redirect" is disabled. Without the change, configurations that rely on this during error handling are potentially unsafe if SNI is used. This change also allows to use server specific settings of "underscores_in_headers", "ignore_invalid_headers", and "large_client_header_buffers" directives for HTTP requests and HTTPS requests without SNI.
author Valentin Bartenev <vbart@nginx.com>
date Wed, 27 Feb 2013 17:27:15 +0000
parents d3e256c67d6d
children 68ca3e824115
comparison
equal deleted inserted replaced
5091:d3e256c67d6d 5092:c4d3310574e0
917 if (rc == NGX_ERROR) { 917 if (rc == NGX_ERROR) {
918 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 918 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
919 return; 919 return;
920 } 920 }
921 921
922 if (ngx_http_set_virtual_server(r, &host) == NGX_ERROR) {
923 return;
924 }
925
922 r->headers_in.server = host; 926 r->headers_in.server = host;
923 } 927 }
924 928
925 if (r->http_version < NGX_HTTP_VERSION_10) { 929 if (r->http_version < NGX_HTTP_VERSION_10) {
926 930
927 if (ngx_http_set_virtual_server(r, &r->headers_in.server) 931 if (r->headers_in.server.len == 0
928 == NGX_ERROR) 932 && ngx_http_set_virtual_server(r, &r->headers_in.server)
933 == NGX_ERROR)
929 { 934 {
930 return; 935 return;
931 } 936 }
932 937
933 ngx_http_process_request(r); 938 ngx_http_process_request(r);
1012 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); 1017 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
1013 return; 1018 return;
1014 } 1019 }
1015 1020
1016 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); 1021 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
1017 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1018 1022
1019 rc = NGX_AGAIN; 1023 rc = NGX_AGAIN;
1020 1024
1021 for ( ;; ) { 1025 for ( ;; ) {
1022 1026
1066 if (n == NGX_AGAIN || n == NGX_ERROR) { 1070 if (n == NGX_AGAIN || n == NGX_ERROR) {
1067 return; 1071 return;
1068 } 1072 }
1069 } 1073 }
1070 1074
1075 /* the host header could change the server configuration context */
1076 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1077
1071 rc = ngx_http_parse_header_line(r, r->header_in, 1078 rc = ngx_http_parse_header_line(r, r->header_in,
1072 cscf->underscores_in_headers); 1079 cscf->underscores_in_headers);
1073 1080
1074 if (rc == NGX_OK) { 1081 if (rc == NGX_OK) {
1075 1082
1440 return NGX_ERROR; 1447 return NGX_ERROR;
1441 } 1448 }
1442 1449
1443 if (r->headers_in.server.len) { 1450 if (r->headers_in.server.len) {
1444 return NGX_OK; 1451 return NGX_OK;
1452 }
1453
1454 if (ngx_http_set_virtual_server(r, &host) == NGX_ERROR) {
1455 return NGX_ERROR;
1445 } 1456 }
1446 1457
1447 r->headers_in.server = host; 1458 r->headers_in.server = host;
1448 1459
1449 return NGX_OK; 1460 return NGX_OK;
1568 1579
1569 1580
1570 static ngx_int_t 1581 static ngx_int_t
1571 ngx_http_process_request_header(ngx_http_request_t *r) 1582 ngx_http_process_request_header(ngx_http_request_t *r)
1572 { 1583 {
1573 if (ngx_http_set_virtual_server(r, &r->headers_in.server) == NGX_ERROR) { 1584 if (r->headers_in.server.len == 0
1585 && ngx_http_set_virtual_server(r, &r->headers_in.server)
1586 == NGX_ERROR)
1587 {
1574 return NGX_ERROR; 1588 return NGX_ERROR;
1575 } 1589 }
1576 1590
1577 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) { 1591 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
1578 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, 1592 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,