comparison src/http/ngx_http_core_module.c @ 1235:f8a40ccafb04

optimize regex location search
author Igor Sysoev <igor@sysoev.ru>
date Tue, 05 Jun 2007 06:44:08 +0000
parents 6be3d9073266
children 5e4d3b5d7ad0
comparison
equal deleted inserted replaced
1234:6be3d9073266 1235:f8a40ccafb04
27 #define NGX_HTTP_REQUEST_BODY_FILE_ON 1 27 #define NGX_HTTP_REQUEST_BODY_FILE_ON 1
28 #define NGX_HTTP_REQUEST_BODY_FILE_CLEAN 2 28 #define NGX_HTTP_REQUEST_BODY_FILE_CLEAN 2
29 29
30 30
31 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r, 31 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r,
32 ngx_array_t *locations, size_t len); 32 ngx_array_t *locations, ngx_uint_t regex_start, size_t len);
33 33
34 static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf); 34 static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf);
35 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf); 35 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf);
36 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf); 36 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
37 static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf); 37 static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf);
627 r->content_handler = NULL; 627 r->content_handler = NULL;
628 r->uri_changed = 0; 628 r->uri_changed = 0;
629 629
630 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); 630 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
631 631
632 rc = ngx_http_core_find_location(r, &cscf->locations, 0); 632 rc = ngx_http_core_find_location(r, &cscf->locations, cscf->regex_start, 0);
633 633
634 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { 634 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
635 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 635 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
636 return NGX_OK; 636 return NGX_OK;
637 } 637 }
928 } 928 }
929 929
930 930
931 static ngx_int_t 931 static ngx_int_t
932 ngx_http_core_find_location(ngx_http_request_t *r, 932 ngx_http_core_find_location(ngx_http_request_t *r,
933 ngx_array_t *locations, size_t len) 933 ngx_array_t *locations, ngx_uint_t regex_start, size_t len)
934 { 934 {
935 ngx_int_t n, rc; 935 ngx_int_t n, rc;
936 ngx_uint_t i, found, noregex; 936 ngx_uint_t i, found, noregex;
937 ngx_http_core_loc_conf_t *clcf, **clcfp; 937 ngx_http_core_loc_conf_t *clcf, **clcfp;
938 938
1007 1007
1008 if (found) { 1008 if (found) {
1009 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 1009 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1010 1010
1011 if (clcf->locations.nelts) { 1011 if (clcf->locations.nelts) {
1012 rc = ngx_http_core_find_location(r, &clcf->locations, len); 1012 rc = ngx_http_core_find_location(r, &clcf->locations,
1013 clcf->regex_start, len);
1013 1014
1014 if (rc != NGX_OK) { 1015 if (rc != NGX_OK) {
1015 return rc; 1016 return rc;
1016 } 1017 }
1017 } 1018 }
1023 return NGX_HTTP_LOCATION_NOREGEX; 1024 return NGX_HTTP_LOCATION_NOREGEX;
1024 } 1025 }
1025 1026
1026 /* regex matches */ 1027 /* regex matches */
1027 1028
1028 for (/* void */; i < locations->nelts; i++) { 1029 for (i = regex_start; i < locations->nelts; i++) {
1029
1030 if (!clcfp[i]->regex) {
1031 continue;
1032 }
1033 1030
1034 if (clcfp[i]->noname) { 1031 if (clcfp[i]->noname) {
1035 break; 1032 break;
1036 } 1033 }
1037 1034
1551 1548
1552 1549
1553 static char * 1550 static char *
1554 ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) 1551 ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
1555 { 1552 {
1556 char *rv; 1553 char *rv;
1557 void *mconf; 1554 void *mconf;
1558 ngx_uint_t m; 1555 ngx_uint_t i, m;
1559 ngx_conf_t pcf; 1556 ngx_conf_t pcf;
1560 ngx_http_module_t *module; 1557 ngx_http_module_t *module;
1561 ngx_http_conf_ctx_t *ctx, *http_ctx; 1558 ngx_http_conf_ctx_t *ctx, *http_ctx;
1562 ngx_http_core_srv_conf_t *cscf, **cscfp; 1559 ngx_http_core_srv_conf_t *cscf, **cscfp;
1563 ngx_http_core_main_conf_t *cmcf; 1560 ngx_http_core_loc_conf_t **clcfp;
1561 ngx_http_core_main_conf_t *cmcf;
1564 1562
1565 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); 1563 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
1566 if (ctx == NULL) { 1564 if (ctx == NULL) {
1567 return NGX_CONF_ERROR; 1565 return NGX_CONF_ERROR;
1568 } 1566 }
1642 } 1640 }
1643 1641
1644 ngx_sort(cscf->locations.elts, (size_t) cscf->locations.nelts, 1642 ngx_sort(cscf->locations.elts, (size_t) cscf->locations.nelts,
1645 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations); 1643 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
1646 1644
1645 cscf->regex_start = cscf->locations.nelts;
1646 clcfp = cscf->locations.elts;
1647
1648 for (i = 0; i < cscf->locations.nelts; i++) {
1649 if (clcfp[i]->regex) {
1650 cscf->regex_start = i;
1651 break;
1652 }
1653 }
1654
1647 return rv; 1655 return rv;
1648 } 1656 }
1649 1657
1650 1658
1651 static char * 1659 static char *
1652 ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) 1660 ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
1653 { 1661 {
1654 char *rv; 1662 char *rv;
1655 ngx_int_t m; 1663 ngx_uint_t i, m;
1656 ngx_str_t *value; 1664 ngx_str_t *value;
1657 ngx_conf_t save; 1665 ngx_conf_t save;
1658 ngx_http_module_t *module; 1666 ngx_http_module_t *module;
1659 ngx_http_conf_ctx_t *ctx, *pctx; 1667 ngx_http_conf_ctx_t *ctx, *pctx;
1660 ngx_http_core_srv_conf_t *cscf; 1668 ngx_http_core_srv_conf_t *cscf;
1813 return rv; 1821 return rv;
1814 } 1822 }
1815 1823
1816 ngx_sort(clcf->locations.elts, (size_t) clcf->locations.nelts, 1824 ngx_sort(clcf->locations.elts, (size_t) clcf->locations.nelts,
1817 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations); 1825 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
1826
1827 clcf->regex_start = clcf->locations.nelts;
1828 clcfp = clcf->locations.elts;
1829
1830 for (i = 0; i < clcf->locations.nelts; i++) {
1831 if (clcfp[i]->regex) {
1832 clcf->regex_start = i;
1833 break;
1834 }
1835 }
1818 1836
1819 return rv; 1837 return rv;
1820 } 1838 }
1821 1839
1822 1840