Mercurial > hg > nginx
comparison src/http/ngx_http_request.c @ 5087:66e0f9adbc8c
Changed interface of ngx_http_validate_host().
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Wed, 27 Feb 2013 17:03:14 +0000 |
parents | 1b204b8ea9a3 |
children | ac31fcecb464 |
comparison
equal
deleted
inserted
replaced
5086:1b204b8ea9a3 | 5087:66e0f9adbc8c |
---|---|
30 static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r, | 30 static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r, |
31 ngx_table_elt_t *h, ngx_uint_t offset); | 31 ngx_table_elt_t *h, ngx_uint_t offset); |
32 | 32 |
33 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r); | 33 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r); |
34 static void ngx_http_process_request(ngx_http_request_t *r); | 34 static void ngx_http_process_request(ngx_http_request_t *r); |
35 static ssize_t ngx_http_validate_host(ngx_http_request_t *r, u_char **host, | 35 static ngx_int_t ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool, |
36 size_t len, ngx_uint_t alloc); | 36 ngx_uint_t alloc); |
37 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r, | 37 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r, |
38 u_char *host, size_t len); | 38 u_char *host, size_t len); |
39 | 39 |
40 static void ngx_http_request_handler(ngx_event_t *ev); | 40 static void ngx_http_request_handler(ngx_event_t *ev); |
41 static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc); | 41 static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc); |
641 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | 641 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
642 | 642 |
643 int | 643 int |
644 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg) | 644 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg) |
645 { | 645 { |
646 size_t len; | 646 ngx_str_t host; |
647 u_char *host; | |
648 const char *servername; | 647 const char *servername; |
649 ngx_connection_t *c; | 648 ngx_connection_t *c; |
650 ngx_http_request_t *r; | 649 ngx_http_request_t *r; |
651 ngx_http_ssl_srv_conf_t *sscf; | 650 ngx_http_ssl_srv_conf_t *sscf; |
652 | 651 |
659 c = ngx_ssl_get_connection(ssl_conn); | 658 c = ngx_ssl_get_connection(ssl_conn); |
660 | 659 |
661 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, | 660 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
662 "SSL server name: \"%s\"", servername); | 661 "SSL server name: \"%s\"", servername); |
663 | 662 |
664 len = ngx_strlen(servername); | 663 host.len = ngx_strlen(servername); |
665 | 664 |
666 if (len == 0) { | 665 if (host.len == 0) { |
667 return SSL_TLSEXT_ERR_NOACK; | 666 return SSL_TLSEXT_ERR_NOACK; |
668 } | 667 } |
669 | 668 |
670 r = c->data; | 669 r = c->data; |
671 | 670 |
672 host = (u_char *) servername; | 671 host.data = (u_char *) servername; |
673 | 672 |
674 len = ngx_http_validate_host(r, &host, len, 1); | 673 if (ngx_http_validate_host(&host, r->pool, 1) != NGX_OK) { |
675 | |
676 if (len <= 0) { | |
677 return SSL_TLSEXT_ERR_NOACK; | 674 return SSL_TLSEXT_ERR_NOACK; |
678 } | 675 } |
679 | 676 |
680 if (ngx_http_find_virtual_server(r, host, len) != NGX_OK) { | 677 if (ngx_http_find_virtual_server(r, host.data, host.len) != NGX_OK) { |
681 return SSL_TLSEXT_ERR_NOACK; | 678 return SSL_TLSEXT_ERR_NOACK; |
682 } | 679 } |
683 | 680 |
684 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); | 681 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); |
685 | 682 |
714 | 711 |
715 | 712 |
716 static void | 713 static void |
717 ngx_http_process_request_line(ngx_event_t *rev) | 714 ngx_http_process_request_line(ngx_event_t *rev) |
718 { | 715 { |
719 u_char *host; | |
720 ssize_t n; | 716 ssize_t n; |
721 ngx_int_t rc, rv; | 717 ngx_int_t rc, rv; |
718 ngx_str_t host; | |
722 ngx_connection_t *c; | 719 ngx_connection_t *c; |
723 ngx_http_request_t *r; | 720 ngx_http_request_t *r; |
724 ngx_http_core_srv_conf_t *cscf; | 721 ngx_http_core_srv_conf_t *cscf; |
725 | 722 |
726 c = rev->data; | 723 c = rev->data; |
882 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, | 879 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
883 "http exten: \"%V\"", &r->exten); | 880 "http exten: \"%V\"", &r->exten); |
884 | 881 |
885 if (r->host_start && r->host_end) { | 882 if (r->host_start && r->host_end) { |
886 | 883 |
887 host = r->host_start; | 884 host.len = r->host_end - r->host_start; |
888 n = ngx_http_validate_host(r, &host, | 885 host.data = r->host_start; |
889 r->host_end - r->host_start, 0); | 886 |
890 | 887 rc = ngx_http_validate_host(&host, r->pool, 0); |
891 if (n == 0) { | 888 |
889 if (rc == NGX_DECLINED) { | |
892 ngx_log_error(NGX_LOG_INFO, c->log, 0, | 890 ngx_log_error(NGX_LOG_INFO, c->log, 0, |
893 "client sent invalid host in request line"); | 891 "client sent invalid host in request line"); |
894 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); | 892 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
895 return; | 893 return; |
896 } | 894 } |
897 | 895 |
898 if (n < 0) { | 896 if (rc == NGX_ERROR) { |
899 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 897 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
900 return; | 898 return; |
901 } | 899 } |
902 | 900 |
903 r->headers_in.server.len = n; | 901 r->headers_in.server = host; |
904 r->headers_in.server.data = host; | |
905 } | 902 } |
906 | 903 |
907 if (r->http_version < NGX_HTTP_VERSION_10) { | 904 if (r->http_version < NGX_HTTP_VERSION_10) { |
908 | 905 |
909 if (ngx_http_find_virtual_server(r, r->headers_in.server.data, | 906 if (ngx_http_find_virtual_server(r, r->headers_in.server.data, |
1399 | 1396 |
1400 static ngx_int_t | 1397 static ngx_int_t |
1401 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h, | 1398 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h, |
1402 ngx_uint_t offset) | 1399 ngx_uint_t offset) |
1403 { | 1400 { |
1404 u_char *host; | 1401 ngx_int_t rc; |
1405 ssize_t len; | 1402 ngx_str_t host; |
1406 | 1403 |
1407 if (r->headers_in.host == NULL) { | 1404 if (r->headers_in.host == NULL) { |
1408 r->headers_in.host = h; | 1405 r->headers_in.host = h; |
1409 } | 1406 } |
1410 | 1407 |
1411 host = h->value.data; | 1408 host = h->value; |
1412 len = ngx_http_validate_host(r, &host, h->value.len, 0); | 1409 |
1413 | 1410 rc = ngx_http_validate_host(&host, r->pool, 0); |
1414 if (len == 0) { | 1411 |
1412 if (rc == NGX_DECLINED) { | |
1415 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, | 1413 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
1416 "client sent invalid host header"); | 1414 "client sent invalid host header"); |
1417 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); | 1415 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
1418 return NGX_ERROR; | 1416 return NGX_ERROR; |
1419 } | 1417 } |
1420 | 1418 |
1421 if (len < 0) { | 1419 if (rc == NGX_ERROR) { |
1422 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 1420 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
1423 return NGX_ERROR; | 1421 return NGX_ERROR; |
1424 } | 1422 } |
1425 | 1423 |
1426 if (r->headers_in.server.len) { | 1424 if (r->headers_in.server.len) { |
1427 return NGX_OK; | 1425 return NGX_OK; |
1428 } | 1426 } |
1429 | 1427 |
1430 r->headers_in.server.len = len; | 1428 r->headers_in.server = host; |
1431 r->headers_in.server.data = host; | |
1432 | 1429 |
1433 return NGX_OK; | 1430 return NGX_OK; |
1434 } | 1431 } |
1435 | 1432 |
1436 | 1433 |
1702 | 1699 |
1703 ngx_http_run_posted_requests(c); | 1700 ngx_http_run_posted_requests(c); |
1704 } | 1701 } |
1705 | 1702 |
1706 | 1703 |
1707 static ssize_t | 1704 static ngx_int_t |
1708 ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len, | 1705 ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool, ngx_uint_t alloc) |
1709 ngx_uint_t alloc) | |
1710 { | 1706 { |
1711 u_char *h, ch; | 1707 u_char *h, ch; |
1712 size_t i, dot_pos, host_len; | 1708 size_t i, dot_pos, host_len; |
1713 | 1709 |
1714 enum { | 1710 enum { |
1715 sw_usual = 0, | 1711 sw_usual = 0, |
1716 sw_literal, | 1712 sw_literal, |
1717 sw_rest | 1713 sw_rest |
1718 } state; | 1714 } state; |
1719 | 1715 |
1720 dot_pos = len; | 1716 dot_pos = host->len; |
1721 host_len = len; | 1717 host_len = host->len; |
1722 | 1718 |
1723 h = *host; | 1719 h = host->data; |
1724 | 1720 |
1725 state = sw_usual; | 1721 state = sw_usual; |
1726 | 1722 |
1727 for (i = 0; i < len; i++) { | 1723 for (i = 0; i < host->len; i++) { |
1728 ch = h[i]; | 1724 ch = h[i]; |
1729 | 1725 |
1730 switch (ch) { | 1726 switch (ch) { |
1731 | 1727 |
1732 case '.': | 1728 case '.': |
1733 if (dot_pos == i - 1) { | 1729 if (dot_pos == i - 1) { |
1734 return 0; | 1730 return NGX_DECLINED; |
1735 } | 1731 } |
1736 dot_pos = i; | 1732 dot_pos = i; |
1737 break; | 1733 break; |
1738 | 1734 |
1739 case ':': | 1735 case ':': |
1755 state = sw_rest; | 1751 state = sw_rest; |
1756 } | 1752 } |
1757 break; | 1753 break; |
1758 | 1754 |
1759 case '\0': | 1755 case '\0': |
1760 return 0; | 1756 return NGX_DECLINED; |
1761 | 1757 |
1762 default: | 1758 default: |
1763 | 1759 |
1764 if (ngx_path_separator(ch)) { | 1760 if (ngx_path_separator(ch)) { |
1765 return 0; | 1761 return NGX_DECLINED; |
1766 } | 1762 } |
1767 | 1763 |
1768 if (ch >= 'A' && ch <= 'Z') { | 1764 if (ch >= 'A' && ch <= 'Z') { |
1769 alloc = 1; | 1765 alloc = 1; |
1770 } | 1766 } |
1775 | 1771 |
1776 if (dot_pos == host_len - 1) { | 1772 if (dot_pos == host_len - 1) { |
1777 host_len--; | 1773 host_len--; |
1778 } | 1774 } |
1779 | 1775 |
1776 if (host_len == 0) { | |
1777 return NGX_DECLINED; | |
1778 } | |
1779 | |
1780 if (alloc) { | 1780 if (alloc) { |
1781 *host = ngx_pnalloc(r->pool, host_len); | 1781 host->data = ngx_pnalloc(pool, host_len); |
1782 if (*host == NULL) { | 1782 if (host->data == NULL) { |
1783 return -1; | 1783 return NGX_ERROR; |
1784 } | 1784 } |
1785 | 1785 |
1786 ngx_strlow(*host, h, host_len); | 1786 ngx_strlow(host->data, h, host_len); |
1787 } | 1787 } |
1788 | 1788 |
1789 return host_len; | 1789 host->len = host_len; |
1790 | |
1791 return NGX_OK; | |
1790 } | 1792 } |
1791 | 1793 |
1792 | 1794 |
1793 static ngx_int_t | 1795 static ngx_int_t |
1794 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) |