comparison src/http/ngx_http_request.c @ 26:45fe5b98a9de NGINX_0_1_13

nginx 0.1.13 *) Feature: the server_names_hash and server_names_hash_threshold directives. *) Bugfix: the *.domain.tld names in the "server_name" directive did not work. *) Bugfix: the %request_length log parameter logged the incorrect length.
author Igor Sysoev <http://sysoev.ru>
date Tue, 21 Dec 2004 00:00:00 +0300
parents 420dd3f9e703
children 7ca9bdc82b3f
comparison
equal deleted inserted replaced
25:21488c53e135 26:45fe5b98a9de
18 static void ngx_http_process_request_headers(ngx_event_t *rev); 18 static void ngx_http_process_request_headers(ngx_event_t *rev);
19 static ssize_t ngx_http_read_request_header(ngx_http_request_t *r); 19 static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
20 static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r, 20 static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
21 ngx_uint_t request_line); 21 ngx_uint_t request_line);
22 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r); 22 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
23 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r);
23 24
24 static void ngx_http_set_write_handler(ngx_http_request_t *r); 25 static void ngx_http_set_write_handler(ngx_http_request_t *r);
25 26
26 static void ngx_http_block_read(ngx_event_t *ev); 27 static void ngx_http_block_read(ngx_event_t *ev);
27 static void ngx_http_read_discarded_body_event(ngx_event_t *rev); 28 static void ngx_http_read_discarded_body_event(ngx_event_t *rev);
244 245
245 hc->request = r; 246 hc->request = r;
246 } 247 }
247 248
248 #if (NGX_STAT_STUB) 249 #if (NGX_STAT_STUB)
249 r->stat_reading = 1; 250 (*ngx_stat_reading)--;
250 #endif 251 #endif
251 252
252 c->data = r; 253 c->data = r;
253 r->http_connection = hc; 254 r->http_connection = hc;
254 255
308 309
309 } else { 310 } else {
310 r->in_addr = in_addr[0].addr; 311 r->in_addr = in_addr[0].addr;
311 } 312 }
312 313
313 r->virtual_names = &in_addr[i].names; 314 r->virtual_names = &in_addr[i];
314 315
315 /* the default server configuration for the address:port */ 316 /* the default server configuration for the address:port */
316 cscf = in_addr[i].core_srv_conf; 317 cscf = in_addr[i].core_srv_conf;
317 318
318 r->main_conf = cscf->ctx->main_conf; 319 r->main_conf = cscf->ctx->main_conf;
331 == NGX_ERROR) 332 == NGX_ERROR)
332 { 333 {
333 ngx_http_close_connection(c); 334 ngx_http_close_connection(c);
334 return; 335 return;
335 } 336 }
337
338 rev->event_handler = ngx_http_ssl_handshake;
336 339
337 /* 340 /*
338 * The majority of browsers do not send the "close notify" alert. 341 * The majority of browsers do not send the "close notify" alert.
339 * Among them are MSIE, Mozilla, Netscape 4, Konqueror, and Links. 342 * Among them are MSIE, Mozilla, Netscape 4, Konqueror, and Links.
340 * And what is more MSIE ignores the server's alert. 343 * And what is more MSIE ignores the server's alert.
341 * 344 *
342 * Opera always sends the alert. 345 * Opera always sends the alert.
343 */ 346 */
344 347
345 c->ssl->no_rcv_shut = 1; 348 c->ssl->no_rcv_shut = 1;
346 rev->event_handler = ngx_http_ssl_handshake;
347 } 349 }
348 350
349 r->filter_need_in_memory = 1; 351 r->filter_need_in_memory = 1;
350 } 352 }
351 353
414 r->headers_out.last_modified_time = -1; 416 r->headers_out.last_modified_time = -1;
415 417
416 r->http_state = NGX_HTTP_READING_REQUEST_STATE; 418 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
417 419
418 #if (NGX_STAT_STUB) 420 #if (NGX_STAT_STUB)
421 (*ngx_stat_reading)++;
422 r->stat_reading = 1;
419 (*ngx_stat_requests)++; 423 (*ngx_stat_requests)++;
420 #endif 424 #endif
421 425
422 rev->event_handler(rev); 426 rev->event_handler(rev);
423 } 427 }
1052 } 1056 }
1053 1057
1054 1058
1055 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r) 1059 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r)
1056 { 1060 {
1057 u_char *ua, *user_agent; 1061 u_char *ua, *user_agent, ch;
1058 size_t len; 1062 size_t len;
1059 ngx_uint_t i;
1060 ngx_http_server_name_t *name;
1061 ngx_http_core_srv_conf_t *cscf;
1062 ngx_http_core_loc_conf_t *clcf;
1063 1063
1064 if (r->headers_in.host) { 1064 if (r->headers_in.host) {
1065 for (len = 0; len < r->headers_in.host->value.len; len++) { 1065 for (len = 0; len < r->headers_in.host->value.len; len++) {
1066 if (r->headers_in.host->value.data[len] == ':') { 1066 ch = r->headers_in.host->value.data[len];
1067
1068 if (ch == ':') {
1067 break; 1069 break;
1068 } 1070 }
1071
1072 r->headers_in.host->value.data[len] = ngx_tolower(ch);
1069 } 1073 }
1070 r->headers_in.host_name_len = len; 1074 r->headers_in.host_name_len = len;
1071 1075
1072 /* find the name based server configuration */ 1076 if (ngx_http_find_virtual_server(r) != NGX_OK) {
1073 1077 return NGX_HTTP_PARSE_INVALID_HOST;
1074 name = r->virtual_names->elts;
1075 for (i = 0; i < r->virtual_names->nelts; i++) {
1076
1077 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1078 "server name: %V", &name[i].name);
1079
1080 if (name[i].wildcard) {
1081 if (r->headers_in.host_name_len <= name[i].name.len) {
1082 continue;
1083 }
1084
1085 if (ngx_rstrncasecmp(r->headers_in.host->value.data,
1086 name[i].name.data,
1087 name[i].name.len) == 0)
1088 {
1089 continue;
1090 }
1091
1092 } else {
1093 if (r->headers_in.host_name_len != name[i].name.len) {
1094 continue;
1095 }
1096
1097 if (ngx_strncasecmp(r->headers_in.host->value.data,
1098 name[i].name.data,
1099 name[i].name.len) != 0)
1100 {
1101 continue;
1102 }
1103 }
1104
1105 r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
1106 r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
1107
1108 if (name[i].wildcard) {
1109 r->server_name.len = r->headers_in.host_name_len;
1110 r->server_name.data = r->headers_in.host->value.data;
1111
1112 } else {
1113 r->server_name = name[i].name;
1114 }
1115
1116 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1117 r->connection->log->file = clcf->err_log->file;
1118
1119 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1120 r->connection->log->log_level = clcf->err_log->log_level;
1121 }
1122
1123 break;
1124 }
1125
1126 if (i == r->virtual_names->nelts) {
1127 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1128
1129 if (cscf->restrict_host_names != NGX_HTTP_RESTRICT_HOST_OFF) {
1130 return NGX_HTTP_PARSE_INVALID_HOST;
1131 }
1132 } 1078 }
1133 1079
1134 } else { 1080 } else {
1135 if (r->http_version > NGX_HTTP_VERSION_10) { 1081 if (r->http_version > NGX_HTTP_VERSION_10) {
1136 return NGX_HTTP_PARSE_NO_HOST_HEADER; 1082 return NGX_HTTP_PARSE_NO_HOST_HEADER;
1223 1169
1224 return NGX_OK; 1170 return NGX_OK;
1225 } 1171 }
1226 1172
1227 1173
1174 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r)
1175 {
1176 ngx_int_t rc;
1177 ngx_uint_t i, n, key, found;
1178 ngx_http_server_name_t *name;
1179 ngx_http_core_main_conf_t *cmcf;
1180 ngx_http_core_srv_conf_t *cscf;
1181 ngx_http_core_loc_conf_t *clcf;
1182
1183 if (r->virtual_names->hash) {
1184 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
1185
1186 ngx_http_server_names_hash_key(key,
1187 r->headers_in.host->value.data,
1188 r->headers_in.host_name_len,
1189 cmcf->server_names_hash);
1190
1191 name = r->virtual_names->hash[key].elts;
1192 n = r->virtual_names->hash[key].nelts;
1193
1194 } else {
1195 name = r->virtual_names->names.elts;
1196 n = r->virtual_names->names.nelts;
1197 }
1198
1199 found = 0;
1200
1201 for (i = 0; i < n; i++) {
1202
1203 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1204 "server name: %V", &name[i].name);
1205
1206 if (r->headers_in.host_name_len != name[i].name.len) {
1207 continue;
1208 }
1209
1210 rc = ngx_strncmp(r->headers_in.host->value.data,
1211 name[i].name.data, name[i].name.len);
1212
1213 if (rc == 0) {
1214 r->server_name = name[i].name;
1215
1216 found = 1;
1217 break;
1218 }
1219
1220 if (rc < 0) {
1221 /* the server names are lexicographically sorted */
1222 break;
1223 }
1224 }
1225
1226 if (!found && r->virtual_names->wildcards.nelts) {
1227
1228 name = r->virtual_names->wildcards.elts;
1229 for (i = 0; i < r->virtual_names->wildcards.nelts; i++) {
1230
1231 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1232 "server name: %V", &name[i].name);
1233
1234 if (r->headers_in.host_name_len <= name[i].name.len) {
1235 continue;
1236 }
1237
1238 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1239 "server name: %s",
1240 r->headers_in.host->value.data
1241 + (r->headers_in.host_name_len - name[i].name.len));
1242
1243 if (ngx_strncmp(r->headers_in.host->value.data
1244 + (r->headers_in.host_name_len - name[i].name.len),
1245 name[i].name.data, name[i].name.len) == 0)
1246 {
1247 r->server_name.len = r->headers_in.host_name_len;
1248 r->server_name.data = r->headers_in.host->value.data;
1249
1250 found = 1;
1251 break;
1252 }
1253 }
1254 }
1255
1256 if (found) {
1257 r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
1258 r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
1259
1260 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1261 r->connection->log->file = clcf->err_log->file;
1262
1263 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1264 r->connection->log->log_level = clcf->err_log->log_level;
1265 }
1266
1267 return NGX_OK;
1268 }
1269
1270 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1271
1272 if (cscf->restrict_host_names != NGX_HTTP_RESTRICT_HOST_OFF) {
1273 return NGX_ERROR;
1274 }
1275
1276 return NGX_OK;
1277 }
1278
1279
1228 void ngx_http_finalize_request(ngx_http_request_t *r, int rc) 1280 void ngx_http_finalize_request(ngx_http_request_t *r, int rc)
1229 { 1281 {
1230 ngx_http_core_loc_conf_t *clcf; 1282 ngx_http_core_loc_conf_t *clcf;
1231 1283
1232 /* r can be already destroyed when rc == NGX_DONE */ 1284 /* r may be already destroyed when rc == NGX_DONE */
1233 1285
1234 if (rc == NGX_DONE || r->main) { 1286 if (rc == NGX_DONE || r->main) {
1235 return; 1287 return;
1236 } 1288 }
1237 1289
1585 if (hc->free == NULL) { 1637 if (hc->free == NULL) {
1586 hc->free = ngx_palloc(c->pool, 1638 hc->free = ngx_palloc(c->pool,
1587 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *)); 1639 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1588 1640
1589 if (hc->free == NULL) { 1641 if (hc->free == NULL) {
1642 ngx_http_close_request(r, 0);
1590 ngx_http_close_connection(c); 1643 ngx_http_close_connection(c);
1591 return; 1644 return;
1592 } 1645 }
1593 } 1646 }
1594 1647
1602 hc->busy[0] = b; 1655 hc->busy[0] = b;
1603 hc->nbusy = 1; 1656 hc->nbusy = 1;
1604 } 1657 }
1605 } 1658 }
1606 1659
1660 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1661
1607 ngx_http_close_request(r, 0); 1662 ngx_http_close_request(r, 0);
1608 c->data = hc; 1663 c->data = hc;
1609 1664
1610 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1611 ngx_add_timer(rev, clcf->keepalive_timeout); 1665 ngx_add_timer(rev, clcf->keepalive_timeout);
1612 1666
1613 if (ngx_handle_level_read_event(rev) == NGX_ERROR) { 1667 if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
1614 ngx_http_close_connection(c); 1668 ngx_http_close_connection(c);
1615 return; 1669 return;
1875 if (wev->active) { 1929 if (wev->active) {
1876 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { 1930 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
1877 if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT) 1931 if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
1878 == NGX_ERROR) 1932 == NGX_ERROR)
1879 { 1933 {
1934 ngx_http_close_request(r, 0);
1880 ngx_http_close_connection(c); 1935 ngx_http_close_connection(c);
1881 return; 1936 return;
1882 } 1937 }
1883 1938
1884 } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) { 1939 } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
1885 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) { 1940 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
1941 ngx_http_close_request(r, 0);
1886 ngx_http_close_connection(c); 1942 ngx_http_close_connection(c);
1887 return; 1943 return;
1888 } 1944 }
1889 } 1945 }
1890 } 1946 }
1943 } 1999 }
1944 2000
1945 } while (rev->ready); 2001 } while (rev->ready);
1946 2002
1947 if (ngx_handle_level_read_event(rev) == NGX_ERROR) { 2003 if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
2004 ngx_http_close_request(r, 0);
1948 ngx_http_close_connection(c); 2005 ngx_http_close_connection(c);
1949 return; 2006 return;
1950 } 2007 }
1951 2008
1952 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 2009 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1956 if (timer > clcf->lingering_timeout) { 2013 if (timer > clcf->lingering_timeout) {
1957 timer = clcf->lingering_timeout; 2014 timer = clcf->lingering_timeout;
1958 } 2015 }
1959 2016
1960 ngx_add_timer(rev, timer); 2017 ngx_add_timer(rev, timer);
1961
1962 return;
1963 } 2018 }
1964 2019
1965 2020
1966 void ngx_http_empty_handler(ngx_event_t *wev) 2021 void ngx_http_empty_handler(ngx_event_t *wev)
1967 { 2022 {
2091 ctx->request = NULL; 2146 ctx->request = NULL;
2092 2147
2093 r->request_line.len = 0; 2148 r->request_line.len = 0;
2094 2149
2095 ngx_destroy_pool(r->pool); 2150 ngx_destroy_pool(r->pool);
2096
2097 return;
2098 } 2151 }
2099 2152
2100 2153
2101 #if (NGX_HTTP_SSL) 2154 #if (NGX_HTTP_SSL)
2102 2155