Skip to content

Commit

Permalink
Fix support for HTTP extension methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterwise authored and SergioBenitez committed Aug 17, 2024
1 parent 51d4ed4 commit 4c71ed6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
7 changes: 4 additions & 3 deletions core/lib/src/request/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{io, fmt};
use std::ops::RangeFrom;
use std::sync::{Arc, atomic::Ordering};
use std::borrow::Cow;
use std::str::FromStr;
use std::future::Future;
use std::net::IpAddr;

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

// Ensure that the method is known. TODO: Allow made-up methods?
// Ensure that the method is known.
let method = match hyper.method {
hyper::Method::GET => Method::Get,
hyper::Method::PUT => Method::Put,
Expand All @@ -1097,10 +1098,10 @@ impl<'r> Request<'r> {
hyper::Method::TRACE => Method::Trace,
hyper::Method::CONNECT => Method::Connect,
hyper::Method::PATCH => Method::Patch,
_ => {
ref ext => Method::from_str(ext.as_str()).unwrap_or_else(|_| {
errors.push(RequestError::BadMethod(hyper.method.clone()));
Method::Get
}
}),
};

// TODO: Keep around not just the path/query, but the rest, if there?
Expand Down
6 changes: 4 additions & 2 deletions testbench/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl Client {
.connect_timeout(Duration::from_secs(5))
}

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

let method = reqwest::Method::from_str(method.as_str()).unwrap();
let method = reqwest::Method::from_str(method.as_ref()).unwrap();
Ok(self.client.request(method, uri.to_string()))
}

Expand Down
29 changes: 29 additions & 0 deletions testbench/src/servers/http_extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Test that HTTP method extensions unlike POST or GET work.
use crate::prelude::*;

use rocket::http::Method;

#[route(PROPFIND, uri = "/")]
fn route() -> &'static str {
"Hello, World!"
}

pub fn test_http_extensions() -> Result<()> {
let server = spawn! {
Rocket::default().mount("/", routes![route])
}?;

let client = Client::default();
let response = client.request(&server, Method::PropFind, "/")?.send()?;
assert_eq!(response.status(), 200);
assert_eq!(response.text()?, "Hello, World!");

// Make sure that verbs outside of extensions are marked as errors
let res = client.request(&server, "BAKEMEACOOKIE", "/")?.send()?;
assert_eq!(res.status(), 400);

Ok(())
}

register!(test_http_extensions);
1 change: 1 addition & 0 deletions testbench/src/servers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod ignite_failure;
pub mod bind;
pub mod http_extensions;
pub mod infinite_stream;
pub mod tls_resolver;
pub mod mtls;
Expand Down

0 comments on commit 4c71ed6

Please sign in to comment.