Skip to content

Commit 25b52da

Browse files
committed
Auto merge of #1784 - jtgeibel:encapsulte-requests-proxy, r=jtgeibel
Encapsulate the RequestProxy use cases The RequestProxy value is constructed 3 places, overwriting 1 of 2 values. The fields are made private and 2 new constructors give each use case a name.
2 parents 79ff534 + 5baa165 commit 25b52da

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

src/middleware/ember_index_rewrite.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ impl Handler for EmberIndexRewrite {
3737
let is_api_path = req.path().starts_with("/api");
3838
let handler = self.handler.as_ref().unwrap();
3939
if wants_html && !is_api_path {
40-
handler.call(&mut RequestProxy {
41-
other: req,
42-
path: Some("/index.html"),
43-
method: None,
44-
})
40+
handler.call(&mut RequestProxy::rewrite_path(req, "/index.html"))
4541
} else {
4642
handler.call(req)
4743
}

src/middleware/head.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ impl AroundMiddleware for Head {
2222
impl Handler for Head {
2323
fn call(&self, req: &mut dyn Request) -> Result<Response, Box<dyn Error + Send>> {
2424
if req.method() == Method::Head {
25-
let mut req = RequestProxy {
26-
other: req,
27-
path: None,
28-
method: Some(Method::Get),
29-
};
25+
let mut req = RequestProxy::rewrite_method(req, Method::Get);
3026
self.handler
3127
.as_ref()
3228
.unwrap()

src/router.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ impl<H: Handler> Handler for R<H> {
146146
fn call(&self, req: &mut dyn Request) -> Result<Response, Box<dyn Error + Send>> {
147147
let path = req.params()["path"].to_string();
148148
let R(ref sub_router) = *self;
149-
sub_router.call(&mut RequestProxy {
150-
other: req,
151-
path: Some(&path),
152-
method: None,
153-
})
149+
sub_router.call(&mut RequestProxy::rewrite_path(req, &path))
154150
}
155151
}
156152

src/util/request_proxy.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
1+
//! A helper that wraps a request and can overwrite either the path or the method.
2+
13
use std::{io::Read, net::SocketAddr};
24

3-
use conduit::Request;
5+
use conduit::{Method, Request};
46
use conduit_hyper::semver;
57

8+
type RequestMutRef<'a> = &'a mut (dyn Request + 'a);
9+
610
// Can't derive Debug because of Request.
711
#[allow(missing_debug_implementations)]
812
pub struct RequestProxy<'a> {
9-
pub other: &'a mut (dyn Request + 'a),
10-
pub path: Option<&'a str>,
11-
pub method: Option<conduit::Method>,
13+
other: RequestMutRef<'a>,
14+
path: Option<&'a str>,
15+
method: Option<conduit::Method>,
16+
}
17+
18+
impl<'a> RequestProxy<'a> {
19+
/// Wrap a request and overwrite the path with the provided value.
20+
pub(crate) fn rewrite_path(req: RequestMutRef<'a>, path: &'a str) -> Self {
21+
RequestProxy {
22+
other: req,
23+
path: Some(path),
24+
method: None, // Defer to original request
25+
}
26+
}
27+
28+
/// Wrap a request and overwrite the method with the provided value.
29+
pub(crate) fn rewrite_method(req: RequestMutRef<'a>, method: Method) -> Self {
30+
RequestProxy {
31+
other: req,
32+
path: None, // Defer to original request
33+
method: Some(method),
34+
}
35+
}
1236
}
1337

1438
impl<'a> Request for RequestProxy<'a> {
39+
// Use local value if available, defer to the original request
40+
fn method(&self) -> conduit::Method {
41+
self.method.clone().unwrap_or_else(|| self.other.method())
42+
}
43+
44+
fn path(&self) -> &str {
45+
self.path.unwrap_or_else(|| self.other.path())
46+
}
47+
48+
// Pass-through
1549
fn http_version(&self) -> semver::Version {
1650
self.other.http_version()
1751
}
1852
fn conduit_version(&self) -> semver::Version {
1953
self.other.conduit_version()
2054
}
21-
fn method(&self) -> conduit::Method {
22-
self.method.clone().unwrap_or_else(|| self.other.method())
23-
}
2455
fn scheme(&self) -> conduit::Scheme {
2556
self.other.scheme()
2657
}
@@ -30,9 +61,6 @@ impl<'a> Request for RequestProxy<'a> {
3061
fn virtual_root(&self) -> Option<&str> {
3162
self.other.virtual_root()
3263
}
33-
fn path(&self) -> &str {
34-
self.path.unwrap_or_else(|| self.other.path())
35-
}
3664
fn query_string(&self) -> Option<&str> {
3765
self.other.query_string()
3866
}

0 commit comments

Comments
 (0)