Skip to content

Commit dca7620

Browse files
committed
Allow non-standard HTTP methods
1 parent f4c2485 commit dca7620

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ _doc = ["rustls?/aws-lc-rs"]
5656

5757
[dependencies]
5858
base64 = "0.22.1"
59-
ureq-proto = "0.3.1"
59+
ureq-proto = "0.3.2"
6060
# ureq-proto = { path = "../ureq-proto" }
6161
log = "0.4.25"
6262
utf-8 = "0.7.6"

src/config.rs

+21
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub struct Config {
156156
max_idle_connections: usize,
157157
max_idle_connections_per_host: usize,
158158
max_idle_age: Duration,
159+
allow_non_standard_methods: bool,
159160

160161
// Chain built for middleware.
161162
pub(crate) middleware: MiddlewareChain,
@@ -390,6 +391,15 @@ impl Config {
390391
pub fn max_idle_age(&self) -> Duration {
391392
self.max_idle_age
392393
}
394+
395+
/// Whether to allow non-standard HTTP methods.
396+
///
397+
/// By default the methods are limited by the HTTP version.
398+
///
399+
/// Defaults to false
400+
pub fn allow_non_standard_methods(&self) -> bool {
401+
self.allow_non_standard_methods
402+
}
393403
}
394404

395405
/// Builder of [`Config`]
@@ -614,6 +624,16 @@ impl<Scope: private::ConfigScope> ConfigBuilder<Scope> {
614624
self
615625
}
616626

627+
/// Whether to allow non-standard HTTP methods.
628+
///
629+
/// By default the methods are limited by the HTTP version.
630+
///
631+
/// Defaults to false
632+
pub fn allow_non_standard_methods(mut self, v: bool) -> Self {
633+
self.config().allow_non_standard_methods = v;
634+
self
635+
}
636+
617637
/// Add middleware to use for each request in this agent.
618638
///
619639
/// Defaults to no middleware.
@@ -844,6 +864,7 @@ impl Default for Config {
844864
max_idle_connections: 10,
845865
max_idle_connections_per_host: 3,
846866
max_idle_age: Duration::from_secs(15),
867+
allow_non_standard_methods: false,
847868
middleware: MiddlewareChain::default(),
848869
}
849870
}

src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,28 @@ pub(crate) mod test {
11441144
assert!(matches!(err, Error::LargeResponseHeader(65, 5)));
11451145
}
11461146

1147+
#[test]
1148+
#[cfg(feature = "_test")]
1149+
fn non_standard_method() {
1150+
init_test_log();
1151+
let method = Method::from_bytes(b"FNORD").unwrap();
1152+
1153+
let req = Request::builder()
1154+
.method(method)
1155+
.uri("http://httpbin.org/fnord")
1156+
.body(())
1157+
.unwrap();
1158+
1159+
let agent = Agent::new_with_defaults();
1160+
1161+
let req = agent
1162+
.configure_request(req)
1163+
.allow_non_standard_methods(true)
1164+
.build();
1165+
1166+
agent.run(req).unwrap();
1167+
}
1168+
11471169
// This doesn't need to run, just compile.
11481170
fn _ensure_send_sync() {
11491171
fn is_send(_t: impl Send) {}

src/run.rs

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub(crate) fn run(
5555
flow.send_body_despite_method();
5656
}
5757

58+
flow.allow_non_standard_methods(config.allow_non_standard_methods());
59+
5860
let (response, handler) = loop {
5961
let timeout = timings.next_timeout(Timeout::Global);
6062
let timed_out = match timeout.after {

src/unversioned/transport/test.rs

+20
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,26 @@ fn setup_default_handlers(handlers: &mut Vec<TestHandler>) {
372372
handlers,
373373
);
374374

375+
maybe_add(
376+
TestHandler::new("/fnord", |_uri, req, w| {
377+
assert_eq!(req.method().as_str(), "FNORD");
378+
379+
write!(
380+
w,
381+
"HTTP/1.1 200 OK\r\n\
382+
Content-Type: application/json\r\n\
383+
Content-Length: {}\r\n\
384+
\r\n",
385+
HTTPBIN_GET.as_bytes().len()
386+
)?;
387+
if req.method() != Method::HEAD {
388+
w.write_all(HTTPBIN_GET.as_bytes())?;
389+
}
390+
Ok(())
391+
}),
392+
handlers,
393+
);
394+
375395
#[cfg(feature = "charset")]
376396
{
377397
let (cow, _, _) =

0 commit comments

Comments
 (0)