Skip to content
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

Add AddOrigin builder #1

Merged
merged 1 commit into from
Jan 23, 2018
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
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
extern crate tower_add_origin;

pub use tower_add_origin::AddOrigin;
pub mod add_origin {
pub use ::tower_add_origin::{
AddOrigin,
Builder,
BuilderError,
};
}

pub use add_origin::AddOrigin;
73 changes: 71 additions & 2 deletions tower-add-origin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ extern crate http;
extern crate tower;

use futures::Poll;
use http::Request;
use http::uri::{Authority, Scheme};
use http::{Request, HttpTryFrom};
use http::uri::{self, Authority, Scheme, Uri};
use tower::Service;

/// Wraps an HTTP service, injecting authority and scheme on every request.
#[derive(Debug)]
pub struct AddOrigin<T> {
inner: T,
scheme: Scheme,
authority: Authority,
}

/// Configure an `AddOrigin` instance
#[derive(Debug, Default)]
pub struct Builder {
uri: Option<Uri>,
}

/// Errors that can happen when building an `AddOrigin`.
#[derive(Debug)]
pub struct BuilderError {
_p: (),
}

// ===== impl AddOrigin ======

impl<T> AddOrigin<T> {
/// Create a new `AddOrigin`
pub fn new(inner: T, scheme: Scheme, authority: Authority) -> Self {
Expand Down Expand Up @@ -83,3 +98,57 @@ where T: Service<Request = Request<B>>,
self.inner.call(request)
}
}

// ===== impl Builder ======

impl Builder {
/// Return a new, default builder
pub fn new() -> Self {
Builder::default()
}

/// Set the URI to use as the origin for all requests.
pub fn uri<T>(&mut self, uri: T) -> &mut Self
where Uri: HttpTryFrom<T>,
{
self.uri = Uri::try_from(uri)
.map(Some)
.unwrap_or(None);

self
}

pub fn build<T>(&mut self, inner: T) -> Result<AddOrigin<T>, BuilderError> {
// Create the error just in case. It is a zero sized type anyway right
// now.
let err = BuilderError { _p: () };

let uri = match self.uri.take() {
Some(uri) => uri,
None => return Err(err),
};

let parts = uri::Parts::from(uri);

// Get the scheme
let scheme = match parts.scheme {
Some(scheme) => scheme,
None => return Err(err),
};

// Get the authority
let authority = match parts.authority {
Some(authority) => authority,
None => return Err(err),
};

// Ensure that the path is unsued
match parts.path_and_query {
None => {}
Some(ref path) if path == "/" => {}
_ => return Err(err),
}

Ok(AddOrigin::new(inner, scheme, authority))
}
}
16 changes: 16 additions & 0 deletions tower-add-origin/tests/add_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ fn adds_origin_to_requests() {

send_response.respond(response);
}

#[test]
fn does_not_build_with_relative_uri() {
let _ = Builder::new()
.uri("/")
.build(())
.unwrap_err();
}

#[test]
fn does_not_build_with_path() {
let _ = Builder::new()
.uri("http://www.example.com/foo")
.build(())
.unwrap_err();
}