comparison src/http/ngx_http_request.c @ 5119:f10108c92fef

URI processing code moved to a separate function. This allows to reuse it in the upcoming SPDY module.
author Valentin Bartenev <vbart@nginx.com>
date Wed, 20 Mar 2013 09:36:27 +0000
parents a46c1cfe7263
children c0f7b94e88ba
comparison
equal deleted inserted replaced
5118:af155fc41deb 5119:f10108c92fef
29 static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r, 29 static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
30 ngx_table_elt_t *h, ngx_uint_t offset); 30 ngx_table_elt_t *h, ngx_uint_t offset);
31 static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r, 31 static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
32 ngx_table_elt_t *h, ngx_uint_t offset); 32 ngx_table_elt_t *h, ngx_uint_t offset);
33 33
34 static ngx_int_t ngx_http_process_request_uri(ngx_http_request_t *r);
34 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r); 35 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
35 static void ngx_http_process_request(ngx_http_request_t *r); 36 static void ngx_http_process_request(ngx_http_request_t *r);
36 static ngx_int_t ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool, 37 static ngx_int_t ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool,
37 ngx_uint_t alloc); 38 ngx_uint_t alloc);
38 static ngx_int_t ngx_http_set_virtual_server(ngx_http_request_t *r, 39 static ngx_int_t ngx_http_set_virtual_server(ngx_http_request_t *r,
836 837
837 838
838 static void 839 static void
839 ngx_http_process_request_line(ngx_event_t *rev) 840 ngx_http_process_request_line(ngx_event_t *rev)
840 { 841 {
841 ssize_t n; 842 ssize_t n;
842 ngx_int_t rc, rv; 843 ngx_int_t rc, rv;
843 ngx_str_t host; 844 ngx_str_t host;
844 ngx_connection_t *c; 845 ngx_connection_t *c;
845 ngx_http_request_t *r; 846 ngx_http_request_t *r;
846 ngx_http_core_srv_conf_t *cscf;
847 847
848 c = rev->data; 848 c = rev->data;
849 r = c->data; 849 r = c->data;
850 850
851 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, 851 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
878 878
879 r->request_line.len = r->request_end - r->request_start; 879 r->request_line.len = r->request_end - r->request_start;
880 r->request_line.data = r->request_start; 880 r->request_line.data = r->request_start;
881 r->request_length = r->header_in->pos - r->request_start; 881 r->request_length = r->header_in->pos - r->request_start;
882 882
883 883 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
884 if (r->args_start) { 884 "http request line: \"%V\"", &r->request_line);
885 r->uri.len = r->args_start - 1 - r->uri_start;
886 } else {
887 r->uri.len = r->uri_end - r->uri_start;
888 }
889
890
891 if (r->complex_uri || r->quoted_uri) {
892
893 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
894 if (r->uri.data == NULL) {
895 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
896 return;
897 }
898
899 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
900
901 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
902
903 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
904 ngx_log_error(NGX_LOG_INFO, c->log, 0,
905 "client sent invalid request");
906 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
907 return;
908 }
909
910 } else {
911 r->uri.data = r->uri_start;
912 }
913
914
915 r->unparsed_uri.len = r->uri_end - r->uri_start;
916 r->unparsed_uri.data = r->uri_start;
917
918 r->valid_unparsed_uri = r->space_in_uri ? 0 : 1;
919 885
920 r->method_name.len = r->method_end - r->request_start + 1; 886 r->method_name.len = r->method_end - r->request_start + 1;
921 r->method_name.data = r->request_line.data; 887 r->method_name.data = r->request_line.data;
922 888
923
924 if (r->http_protocol.data) { 889 if (r->http_protocol.data) {
925 r->http_protocol.len = r->request_end - r->http_protocol.data; 890 r->http_protocol.len = r->request_end - r->http_protocol.data;
926 } 891 }
927 892
928 893 if (ngx_http_process_request_uri(r) != NGX_OK) {
929 if (r->uri_ext) { 894 return;
930 if (r->args_start) { 895 }
931 r->exten.len = r->args_start - 1 - r->uri_ext;
932 } else {
933 r->exten.len = r->uri_end - r->uri_ext;
934 }
935
936 r->exten.data = r->uri_ext;
937 }
938
939
940 if (r->args_start && r->uri_end > r->args_start) {
941 r->args.len = r->uri_end - r->args_start;
942 r->args.data = r->args_start;
943 }
944
945 #if (NGX_WIN32)
946 {
947 u_char *p, *last;
948
949 p = r->uri.data;
950 last = r->uri.data + r->uri.len;
951
952 while (p < last) {
953
954 if (*p++ == ':') {
955
956 /*
957 * this check covers "::$data", "::$index_allocation" and
958 * ":$i30:$index_allocation"
959 */
960
961 if (p < last && *p == '$') {
962 ngx_log_error(NGX_LOG_INFO, c->log, 0,
963 "client sent unsafe win32 URI");
964 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
965 return;
966 }
967 }
968 }
969
970 p = r->uri.data + r->uri.len - 1;
971
972 while (p > r->uri.data) {
973
974 if (*p == ' ') {
975 p--;
976 continue;
977 }
978
979 if (*p == '.') {
980 p--;
981 continue;
982 }
983
984 break;
985 }
986
987 if (p != r->uri.data + r->uri.len - 1) {
988 r->uri.len = p + 1 - r->uri.data;
989 ngx_http_set_exten(r);
990 }
991
992 }
993 #endif
994
995 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
996 "http request line: \"%V\"", &r->request_line);
997
998 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
999 "http uri: \"%V\"", &r->uri);
1000
1001 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
1002 "http args: \"%V\"", &r->args);
1003
1004 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
1005 "http exten: \"%V\"", &r->exten);
1006 896
1007 if (r->host_start && r->host_end) { 897 if (r->host_start && r->host_end) {
1008 898
1009 host.len = r->host_end - r->host_start; 899 host.len = r->host_end - r->host_start;
1010 host.data = r->host_start; 900 host.data = r->host_start;
1090 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE); 980 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
1091 return; 981 return;
1092 } 982 }
1093 } 983 }
1094 } 984 }
985 }
986
987
988 static ngx_int_t
989 ngx_http_process_request_uri(ngx_http_request_t *r)
990 {
991 ngx_http_core_srv_conf_t *cscf;
992
993 if (r->args_start) {
994 r->uri.len = r->args_start - 1 - r->uri_start;
995 } else {
996 r->uri.len = r->uri_end - r->uri_start;
997 }
998
999 if (r->complex_uri || r->quoted_uri) {
1000
1001 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
1002 if (r->uri.data == NULL) {
1003 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1004 return NGX_ERROR;
1005 }
1006
1007 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1008
1009 if (ngx_http_parse_complex_uri(r, cscf->merge_slashes) != NGX_OK) {
1010 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1011 "client sent invalid request");
1012 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1013 return NGX_ERROR;
1014 }
1015
1016 } else {
1017 r->uri.data = r->uri_start;
1018 }
1019
1020 r->unparsed_uri.len = r->uri_end - r->uri_start;
1021 r->unparsed_uri.data = r->uri_start;
1022
1023 r->valid_unparsed_uri = r->space_in_uri ? 0 : 1;
1024
1025 if (r->uri_ext) {
1026 if (r->args_start) {
1027 r->exten.len = r->args_start - 1 - r->uri_ext;
1028 } else {
1029 r->exten.len = r->uri_end - r->uri_ext;
1030 }
1031
1032 r->exten.data = r->uri_ext;
1033 }
1034
1035 if (r->args_start && r->uri_end > r->args_start) {
1036 r->args.len = r->uri_end - r->args_start;
1037 r->args.data = r->args_start;
1038 }
1039
1040 #if (NGX_WIN32)
1041 {
1042 u_char *p, *last;
1043
1044 p = r->uri.data;
1045 last = r->uri.data + r->uri.len;
1046
1047 while (p < last) {
1048
1049 if (*p++ == ':') {
1050
1051 /*
1052 * this check covers "::$data", "::$index_allocation" and
1053 * ":$i30:$index_allocation"
1054 */
1055
1056 if (p < last && *p == '$') {
1057 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1058 "client sent unsafe win32 URI");
1059 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1060 return NGX_ERROR;
1061 }
1062 }
1063 }
1064
1065 p = r->uri.data + r->uri.len - 1;
1066
1067 while (p > r->uri.data) {
1068
1069 if (*p == ' ') {
1070 p--;
1071 continue;
1072 }
1073
1074 if (*p == '.') {
1075 p--;
1076 continue;
1077 }
1078
1079 break;
1080 }
1081
1082 if (p != r->uri.data + r->uri.len - 1) {
1083 r->uri.len = p + 1 - r->uri.data;
1084 ngx_http_set_exten(r);
1085 }
1086
1087 }
1088 #endif
1089
1090 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1091 "http uri: \"%V\"", &r->uri);
1092
1093 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1094 "http args: \"%V\"", &r->args);
1095
1096 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1097 "http exten: \"%V\"", &r->exten);
1098
1099 return NGX_OK;
1095 } 1100 }
1096 1101
1097 1102
1098 static void 1103 static void
1099 ngx_http_process_request_headers(ngx_event_t *rev) 1104 ngx_http_process_request_headers(ngx_event_t *rev)