Skip to content

Commit e8b6076

Browse files
lobsterwiseSergioBenitez
authored andcommitted
Fix support for HTTP extension methods.
1 parent 7d4fcd8 commit e8b6076

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

core/lib/src/request/request.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{io, fmt};
22
use std::ops::RangeFrom;
33
use std::sync::{Arc, atomic::Ordering};
44
use std::borrow::Cow;
5+
use std::str::FromStr;
56
use std::future::Future;
67
use std::net::IpAddr;
78

@@ -1086,7 +1087,7 @@ impl<'r> Request<'r> {
10861087
// Keep track of parsing errors; emit a `BadRequest` if any exist.
10871088
let mut errors = vec![];
10881089

1089-
// Ensure that the method is known. TODO: Allow made-up methods?
1090+
// Ensure that the method is known.
10901091
let method = match hyper.method {
10911092
hyper::Method::GET => Method::Get,
10921093
hyper::Method::PUT => Method::Put,
@@ -1097,10 +1098,10 @@ impl<'r> Request<'r> {
10971098
hyper::Method::TRACE => Method::Trace,
10981099
hyper::Method::CONNECT => Method::Connect,
10991100
hyper::Method::PATCH => Method::Patch,
1100-
_ => {
1101+
ref ext => Method::from_str(ext.as_str()).unwrap_or_else(|_| {
11011102
errors.push(RequestError::BadMethod(hyper.method.clone()));
11021103
Method::Get
1103-
}
1104+
}),
11041105
};
11051106

11061107
// TODO: Keep around not just the path/query, but the rest, if there?

testbench/src/client.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ impl Client {
2626
.connect_timeout(Duration::from_secs(5))
2727
}
2828

29-
pub fn request(&self, server: &Server, method: Method, url: &str) -> Result<RequestBuilder> {
29+
pub fn request<M>(&self, server: &Server, method: M, url: &str) -> Result<RequestBuilder>
30+
where M: AsRef<str>
31+
{
3032
let uri = match Uri::parse_any(url).map_err(|e| e.into_owned())? {
3133
Uri::Origin(uri) => {
3234
let proto = if server.tls { "https" } else { "http" };
@@ -45,7 +47,7 @@ impl Client {
4547
uri => return Err(Error::InvalidUri(uri.into_owned())),
4648
};
4749

48-
let method = reqwest::Method::from_str(method.as_str()).unwrap();
50+
let method = reqwest::Method::from_str(method.as_ref()).unwrap();
4951
Ok(self.client.request(method, uri.to_string()))
5052
}
5153

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Test that HTTP method extensions unlike POST or GET work.
2+
3+
use crate::prelude::*;
4+
5+
use rocket::http::Method;
6+
7+
#[route(PROPFIND, uri = "/")]
8+
fn route() -> &'static str {
9+
"Hello, World!"
10+
}
11+
12+
pub fn test_http_extensions() -> Result<()> {
13+
let server = spawn! {
14+
Rocket::default().mount("/", routes![route])
15+
}?;
16+
17+
let client = Client::default();
18+
let response = client.request(&server, Method::PropFind, "/")?.send()?;
19+
assert_eq!(response.status(), 200);
20+
assert_eq!(response.text()?, "Hello, World!");
21+
22+
// Make sure that verbs outside of extensions are marked as errors
23+
let res = client.request(&server, "BAKEMEACOOKIE", "/")?.send()?;
24+
assert_eq!(res.status(), 400);
25+
26+
Ok(())
27+
}
28+
29+
register!(test_http_extensions);

testbench/src/servers/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod ignite_failure;
22
pub mod bind;
3+
pub mod http_extensions;
34
pub mod infinite_stream;
45
pub mod tls_resolver;
56
pub mod mtls;

0 commit comments

Comments
 (0)