comparison src/http/ngx_http_request.c @ 5085:7f1cbcc71327

The default server lookup is now done only once per connection. Previously, it was done for every request in a connection.
author Valentin Bartenev <vbart@nginx.com>
date Wed, 27 Feb 2013 16:53:01 +0000
parents f7fe817c92a2
children 1b204b8ea9a3
comparison
equal deleted inserted replaced
5084:f7fe817c92a2 5085:7f1cbcc71327
193 193
194 194
195 void 195 void
196 ngx_http_init_connection(ngx_connection_t *c) 196 ngx_http_init_connection(ngx_connection_t *c)
197 { 197 {
198 ngx_event_t *rev; 198 ngx_uint_t i;
199 ngx_http_log_ctx_t *ctx; 199 ngx_event_t *rev;
200 struct sockaddr_in *sin;
201 ngx_http_port_t *port;
202 ngx_http_in_addr_t *addr;
203 ngx_http_log_ctx_t *ctx;
204 ngx_http_connection_t *hc;
205 #if (NGX_HAVE_INET6)
206 struct sockaddr_in6 *sin6;
207 ngx_http_in6_addr_t *addr6;
208 #endif
209
210 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
211 if (hc == NULL) {
212 ngx_http_close_connection(c);
213 return;
214 }
215
216 c->data = hc;
217
218 /* find the server configuration for the address:port */
219
220 port = c->listening->servers;
221
222 if (port->naddrs > 1) {
223
224 /*
225 * there are several addresses on this port and one of them
226 * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
227 * is required to determine a server address
228 */
229
230 if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
231 ngx_http_close_connection(c);
232 return;
233 }
234
235 switch (c->local_sockaddr->sa_family) {
236
237 #if (NGX_HAVE_INET6)
238 case AF_INET6:
239 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
240
241 addr6 = port->addrs;
242
243 /* the last address is "*" */
244
245 for (i = 0; i < port->naddrs - 1; i++) {
246 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
247 break;
248 }
249 }
250
251 hc->addr_conf = &addr6[i].conf;
252
253 break;
254 #endif
255
256 default: /* AF_INET */
257 sin = (struct sockaddr_in *) c->local_sockaddr;
258
259 addr = port->addrs;
260
261 /* the last address is "*" */
262
263 for (i = 0; i < port->naddrs - 1; i++) {
264 if (addr[i].addr == sin->sin_addr.s_addr) {
265 break;
266 }
267 }
268
269 hc->addr_conf = &addr[i].conf;
270
271 break;
272 }
273
274 } else {
275
276 switch (c->local_sockaddr->sa_family) {
277
278 #if (NGX_HAVE_INET6)
279 case AF_INET6:
280 addr6 = port->addrs;
281 hc->addr_conf = &addr6[0].conf;
282 break;
283 #endif
284
285 default: /* AF_INET */
286 addr = port->addrs;
287 hc->addr_conf = &addr[0].conf;
288 break;
289 }
290 }
200 291
201 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t)); 292 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
202 if (ctx == NULL) { 293 if (ctx == NULL) {
203 ngx_http_close_connection(c); 294 ngx_http_close_connection(c);
204 return; 295 return;
249 340
250 static void 341 static void
251 ngx_http_init_request(ngx_event_t *rev) 342 ngx_http_init_request(ngx_event_t *rev)
252 { 343 {
253 ngx_time_t *tp; 344 ngx_time_t *tp;
254 ngx_uint_t i;
255 ngx_connection_t *c; 345 ngx_connection_t *c;
256 ngx_http_request_t *r; 346 ngx_http_request_t *r;
257 struct sockaddr_in *sin;
258 ngx_http_port_t *port;
259 ngx_http_in_addr_t *addr;
260 ngx_http_log_ctx_t *ctx; 347 ngx_http_log_ctx_t *ctx;
261 ngx_http_addr_conf_t *addr_conf;
262 ngx_http_connection_t *hc; 348 ngx_http_connection_t *hc;
263 ngx_http_core_srv_conf_t *cscf; 349 ngx_http_core_srv_conf_t *cscf;
264 ngx_http_core_loc_conf_t *clcf; 350 ngx_http_core_loc_conf_t *clcf;
265 ngx_http_core_main_conf_t *cmcf; 351 ngx_http_core_main_conf_t *cmcf;
266 #if (NGX_HAVE_INET6)
267 struct sockaddr_in6 *sin6;
268 ngx_http_in6_addr_t *addr6;
269 #endif
270 352
271 #if (NGX_STAT_STUB) 353 #if (NGX_STAT_STUB)
272 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1); 354 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
273 #endif 355 #endif
274 356
282 } 364 }
283 365
284 c->requests++; 366 c->requests++;
285 367
286 hc = c->data; 368 hc = c->data;
287
288 if (hc == NULL) {
289 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
290 if (hc == NULL) {
291 ngx_http_close_connection(c);
292 return;
293 }
294 }
295 369
296 r = hc->request; 370 r = hc->request;
297 371
298 if (r) { 372 if (r) {
299 ngx_memzero(r, sizeof(ngx_http_request_t)); 373 ngx_memzero(r, sizeof(ngx_http_request_t));
318 r->http_connection = hc; 392 r->http_connection = hc;
319 393
320 c->sent = 0; 394 c->sent = 0;
321 r->signature = NGX_HTTP_MODULE; 395 r->signature = NGX_HTTP_MODULE;
322 396
323 /* find the server configuration for the address:port */
324
325 port = c->listening->servers;
326
327 r->connection = c; 397 r->connection = c;
328 398
329 if (port->naddrs > 1) {
330
331 /*
332 * there are several addresses on this port and one of them
333 * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
334 * is required to determine a server address
335 */
336
337 if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
338 ngx_http_close_connection(c);
339 return;
340 }
341
342 switch (c->local_sockaddr->sa_family) {
343
344 #if (NGX_HAVE_INET6)
345 case AF_INET6:
346 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
347
348 addr6 = port->addrs;
349
350 /* the last address is "*" */
351
352 for (i = 0; i < port->naddrs - 1; i++) {
353 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
354 break;
355 }
356 }
357
358 addr_conf = &addr6[i].conf;
359
360 break;
361 #endif
362
363 default: /* AF_INET */
364 sin = (struct sockaddr_in *) c->local_sockaddr;
365
366 addr = port->addrs;
367
368 /* the last address is "*" */
369
370 for (i = 0; i < port->naddrs - 1; i++) {
371 if (addr[i].addr == sin->sin_addr.s_addr) {
372 break;
373 }
374 }
375
376 addr_conf = &addr[i].conf;
377
378 break;
379 }
380
381 } else {
382
383 switch (c->local_sockaddr->sa_family) {
384
385 #if (NGX_HAVE_INET6)
386 case AF_INET6:
387 addr6 = port->addrs;
388 addr_conf = &addr6[0].conf;
389 break;
390 #endif
391
392 default: /* AF_INET */
393 addr = port->addrs;
394 addr_conf = &addr[0].conf;
395 break;
396 }
397 }
398
399 r->virtual_names = addr_conf->virtual_names;
400
401 /* the default server configuration for the address:port */ 399 /* the default server configuration for the address:port */
402 cscf = addr_conf->default_server; 400 cscf = hc->addr_conf->default_server;
403 401
404 r->main_conf = cscf->ctx->main_conf; 402 r->main_conf = cscf->ctx->main_conf;
405 r->srv_conf = cscf->ctx->srv_conf; 403 r->srv_conf = cscf->ctx->srv_conf;
406 r->loc_conf = cscf->ctx->loc_conf; 404 r->loc_conf = cscf->ctx->loc_conf;
407 405
412 410
413 { 411 {
414 ngx_http_ssl_srv_conf_t *sscf; 412 ngx_http_ssl_srv_conf_t *sscf;
415 413
416 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); 414 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
417 if (sscf->enable || addr_conf->ssl) { 415 if (sscf->enable || hc->addr_conf->ssl) {
418 416
419 if (c->ssl == NULL) { 417 if (c->ssl == NULL) {
420 418
421 c->log->action = "SSL handshaking"; 419 c->log->action = "SSL handshaking";
422 420
423 if (addr_conf->ssl && sscf->ssl.ctx == NULL) { 421 if (hc->addr_conf->ssl && sscf->ssl.ctx == NULL) {
424 ngx_log_error(NGX_LOG_ERR, c->log, 0, 422 ngx_log_error(NGX_LOG_ERR, c->log, 0,
425 "no \"ssl_certificate\" is defined " 423 "no \"ssl_certificate\" is defined "
426 "in server listening on SSL port"); 424 "in server listening on SSL port");
427 ngx_http_close_connection(c); 425 ngx_http_close_connection(c);
428 return; 426 return;
1797 static ngx_int_t 1795 static ngx_int_t
1798 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len) 1796 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1799 { 1797 {
1800 ngx_http_core_loc_conf_t *clcf; 1798 ngx_http_core_loc_conf_t *clcf;
1801 ngx_http_core_srv_conf_t *cscf; 1799 ngx_http_core_srv_conf_t *cscf;
1802 1800 ngx_http_virtual_names_t *virtual_names;
1803 if (r->virtual_names == NULL) { 1801
1802 virtual_names = r->http_connection->addr_conf->virtual_names;
1803
1804 if (virtual_names == NULL) {
1804 return NGX_DECLINED; 1805 return NGX_DECLINED;
1805 } 1806 }
1806 1807
1807 cscf = ngx_hash_find_combined(&r->virtual_names->names, 1808 cscf = ngx_hash_find_combined(&virtual_names->names,
1808 ngx_hash_key(host, len), host, len); 1809 ngx_hash_key(host, len), host, len);
1809 1810
1810 if (cscf) { 1811 if (cscf) {
1811 goto found; 1812 goto found;
1812 } 1813 }
1813 1814
1814 #if (NGX_PCRE) 1815 #if (NGX_PCRE)
1815 1816
1816 if (len && r->virtual_names->nregex) { 1817 if (len && virtual_names->nregex) {
1817 ngx_int_t n; 1818 ngx_int_t n;
1818 ngx_uint_t i; 1819 ngx_uint_t i;
1819 ngx_str_t name; 1820 ngx_str_t name;
1820 ngx_http_server_name_t *sn; 1821 ngx_http_server_name_t *sn;
1821 1822
1822 name.len = len; 1823 name.len = len;
1823 name.data = host; 1824 name.data = host;
1824 1825
1825 sn = r->virtual_names->regex; 1826 sn = virtual_names->regex;
1826 1827
1827 for (i = 0; i < r->virtual_names->nregex; i++) { 1828 for (i = 0; i < virtual_names->nregex; i++) {
1828 1829
1829 n = ngx_http_regex_exec(r, sn[i].regex, &name); 1830 n = ngx_http_regex_exec(r, sn[i].regex, &name);
1830 1831
1831 if (n == NGX_OK) { 1832 if (n == NGX_OK) {
1832 cscf = sn[i].server; 1833 cscf = sn[i].server;