@@ -131,7 +131,8 @@ where
131
131
}
132
132
133
133
fn url_from_httparse_req ( req : & httparse:: Request < ' _ , ' _ > ) -> http_types:: Result < Url > {
134
- let path = req. path . ok_or_else ( || format_err ! ( "No uri found" ) ) ?;
134
+ let path_and_query = req. path . ok_or_else ( || format_err ! ( "No uri found" ) ) ?;
135
+
135
136
let host = req
136
137
. headers
137
138
. iter ( )
@@ -141,15 +142,25 @@ fn url_from_httparse_req(req: &httparse::Request<'_, '_>) -> http_types::Result<
141
142
142
143
let host = std:: str:: from_utf8 ( host) ?;
143
144
144
- if path . starts_with ( "http://" ) || path . starts_with ( "https://" ) {
145
- Ok ( Url :: parse ( path ) ?)
146
- } else if path . starts_with ( '/' ) {
145
+ if path_and_query . starts_with ( "http://" ) || path_and_query . starts_with ( "https://" ) {
146
+ Ok ( Url :: parse ( path_and_query ) ?)
147
+ } else if path_and_query . starts_with ( '/' ) {
147
148
let mut url = Url :: parse ( & format ! ( "http://{}/" , host) ) ?;
148
149
// path might start with `//`, so set the path explicitly using `set_path`
149
- url. set_path ( path) ;
150
+
151
+ let mut split = path_and_query. split ( "?" ) ;
152
+ if let Some ( path) = split. next ( ) {
153
+ url. set_path ( path) ;
154
+ } else {
155
+ return Err ( format_err ! ( "unexpected uri format" ) ) ;
156
+ }
157
+ if let Some ( query) = split. next ( ) {
158
+ url. set_query ( Some ( query) ) ;
159
+ }
160
+
150
161
Ok ( url)
151
162
} else if req. method . unwrap ( ) . eq_ignore_ascii_case ( "connect" ) {
152
- Ok ( Url :: parse ( & format ! ( "http://{}/" , path ) ) ?)
163
+ Ok ( Url :: parse ( & format ! ( "http://{}/" , path_and_query ) ) ?)
153
164
} else {
154
165
Err ( format_err ! ( "unexpected uri format" ) )
155
166
}
@@ -233,4 +244,15 @@ mod tests {
233
244
} ,
234
245
)
235
246
}
247
+
248
+ #[ test]
249
+ fn url_for_query ( ) {
250
+ httparse_req (
251
+ "GET /foo?bar=1 HTTP/1.1\r \n Host: server.example.com:443\r \n " ,
252
+ |req| {
253
+ let url = url_from_httparse_req ( & req) . unwrap ( ) ;
254
+ assert_eq ! ( url. as_str( ) , "http://server.example.com:443/foo?bar=1" ) ;
255
+ } ,
256
+ )
257
+ }
236
258
}
0 commit comments