Skip to content

Encapsulate the RequestProxy use cases #1784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/middleware/ember_index_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ impl Handler for EmberIndexRewrite {
let is_api_path = req.path().starts_with("/api");
let handler = self.handler.as_ref().unwrap();
if wants_html && !is_api_path {
handler.call(&mut RequestProxy {
other: req,
path: Some("/index.html"),
method: None,
})
handler.call(&mut RequestProxy::rewrite_path(req, "/index.html"))
} else {
handler.call(req)
}
Expand Down
6 changes: 1 addition & 5 deletions src/middleware/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ impl AroundMiddleware for Head {
impl Handler for Head {
fn call(&self, req: &mut dyn Request) -> Result<Response, Box<dyn Error + Send>> {
if req.method() == Method::Head {
let mut req = RequestProxy {
other: req,
path: None,
method: Some(Method::Get),
};
let mut req = RequestProxy::rewrite_method(req, Method::Get);
self.handler
.as_ref()
.unwrap()
Expand Down
6 changes: 1 addition & 5 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@ impl<H: Handler> Handler for R<H> {
fn call(&self, req: &mut dyn Request) -> Result<Response, Box<dyn Error + Send>> {
let path = req.params()["path"].to_string();
let R(ref sub_router) = *self;
sub_router.call(&mut RequestProxy {
other: req,
path: Some(&path),
method: None,
})
sub_router.call(&mut RequestProxy::rewrite_path(req, &path))
}
}

Expand Down
48 changes: 38 additions & 10 deletions src/util/request_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
//! A helper that wraps a request and can overwrite either the path or the method.

use std::{io::Read, net::SocketAddr};

use conduit::Request;
use conduit::{Method, Request};
use conduit_hyper::semver;

type RequestMutRef<'a> = &'a mut (dyn Request + 'a);

// Can't derive Debug because of Request.
#[allow(missing_debug_implementations)]
pub struct RequestProxy<'a> {
pub other: &'a mut (dyn Request + 'a),
pub path: Option<&'a str>,
pub method: Option<conduit::Method>,
other: RequestMutRef<'a>,
path: Option<&'a str>,
method: Option<conduit::Method>,
}

impl<'a> RequestProxy<'a> {
/// Wrap a request and overwrite the path with the provided value.
pub(crate) fn rewrite_path(req: RequestMutRef<'a>, path: &'a str) -> Self {
RequestProxy {
other: req,
path: Some(path),
method: None, // Defer to original request
}
}

/// Wrap a request and overwrite the method with the provided value.
pub(crate) fn rewrite_method(req: RequestMutRef<'a>, method: Method) -> Self {
RequestProxy {
other: req,
path: None, // Defer to original request
method: Some(method),
}
}
}

impl<'a> Request for RequestProxy<'a> {
// Use local value if available, defer to the original request
fn method(&self) -> conduit::Method {
self.method.clone().unwrap_or_else(|| self.other.method())
}

fn path(&self) -> &str {
self.path.unwrap_or_else(|| self.other.path())
}

// Pass-through
fn http_version(&self) -> semver::Version {
self.other.http_version()
}
fn conduit_version(&self) -> semver::Version {
self.other.conduit_version()
}
fn method(&self) -> conduit::Method {
self.method.clone().unwrap_or_else(|| self.other.method())
}
fn scheme(&self) -> conduit::Scheme {
self.other.scheme()
}
Expand All @@ -30,9 +61,6 @@ impl<'a> Request for RequestProxy<'a> {
fn virtual_root(&self) -> Option<&str> {
self.other.virtual_root()
}
fn path(&self) -> &str {
self.path.unwrap_or_else(|| self.other.path())
}
fn query_string(&self) -> Option<&str> {
self.other.query_string()
}
Expand Down