Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Use tower-request-modifier. #337

Merged
merged 1 commit into from
Jul 11, 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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions azure-functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tower-hyper = "0.1.0"
tower-grpc = "0.1.0"
tower-service = "0.2.0"
tower-util = "0.1.0"
tower-request-modifier = "0.1.0"
log = { version = "0.4.6", features = ["std"] }
futures01 = { package = "futures", version = "0.1.28" }
futures-preview = { version = "0.3.0-alpha.17", features = ["compat"], optional = true }
Expand Down
63 changes: 11 additions & 52 deletions azure-functions/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ use crate::{
WorkerStatusResponse,
},
};
use http::{
uri::{Authority, Parts, Scheme, Uri},
Request as HttpRequest,
};
use futures01::{future::poll_fn, sync::mpsc::unbounded, Async, Future, Poll, Stream};
use http::uri::Uri;
use log::error;
use std::cell::RefCell;
use std::panic::{catch_unwind, set_hook, AssertUnwindSafe, PanicInfo};
Expand All @@ -25,57 +23,11 @@ use tower_hyper::{
util::{Connector, Destination, HttpConnector},
Connect,
};
use tower_service::Service;
use tower_request_modifier::Builder as RequestModifierBuilder;
use tower_util::MakeService;

use futures01::{future::poll_fn, sync::mpsc::unbounded, Async, Future, Poll, Stream};

pub type Sender = futures01::sync::mpsc::UnboundedSender<StreamingMessage>;

// TODO: replace with tower-request-modifier when published (see: https://github.com/tower-rs/tower-http/issues/24)
struct HttpOriginService<T> {
inner: T,
scheme: Scheme,
authority: Authority,
}

impl<T> HttpOriginService<T> {
pub fn new(inner: T, uri: Uri) -> Self {
let parts = Parts::from(uri);

HttpOriginService {
inner,
scheme: parts.scheme.unwrap(),
authority: parts.authority.unwrap(),
}
}
}

impl<T, B> Service<HttpRequest<B>> for HttpOriginService<T>
where
T: Service<HttpRequest<B>>,
{
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;

fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.inner.poll_ready()
}

fn call(&mut self, req: HttpRequest<B>) -> Self::Future {
let (mut head, body) = req.into_parts();
let mut parts = Parts::from(head.uri);

parts.authority = Some(self.authority.clone());
parts.scheme = Some(self.scheme.clone());

head.uri = Uri::from_parts(parts).expect("valid uri");

self.inner.call(HttpRequest::from_parts(head, body))
}
}

struct ContextFuture<F> {
inner: F,
invocation_id: String,
Expand Down Expand Up @@ -160,7 +112,14 @@ impl Worker {
HttpBuilder::new().http2_only(true).clone(),
)
.make_service(Destination::try_from_uri(host_uri.clone()).unwrap())
.map(move |conn| FunctionRpc::new(HttpOriginService::new(conn, host_uri)))
.map(move |conn| {
FunctionRpc::new(
RequestModifierBuilder::new()
.set_origin(host_uri)
.build(conn)
.unwrap(),
)
})
.map_err(|e| panic!("failed to connect to host: {}", e))
.and_then(|mut client| {
client
Expand Down