Skip to content

Commit

Permalink
fix(tonic): Preserve HTTP method in interceptor (#912)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Dahlquist <joe@arcanyx.com>
  • Loading branch information
jdahlq and jdahlq authored Feb 21, 2022
1 parent 6763d19 commit e623562
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
7 changes: 6 additions & 1 deletion tonic/src/client/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ impl<T> Grpc<T> {
})
.map(BoxBody::new);

let mut request = request.into_http(uri, SanitizeHeaders::Yes);
let mut request = request.into_http(
uri,
http::Method::POST,
http::Version::HTTP_2,
SanitizeHeaders::Yes,
);

// Add the gRPC related HTTP headers
request
Expand Down
13 changes: 10 additions & 3 deletions tonic/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ impl<T> Request<T> {
pub(crate) fn into_http(
self,
uri: http::Uri,
method: http::Method,
version: http::Version,
sanitize_headers: SanitizeHeaders,
) -> http::Request<T> {
let mut request = http::Request::new(self.message);

*request.version_mut() = http::Version::HTTP_2;
*request.method_mut() = http::Method::POST;
*request.version_mut() = version;
*request.method_mut() = method;
*request.uri_mut() = uri;
*request.headers_mut() = match sanitize_headers {
SanitizeHeaders::Yes => self.metadata.into_sanitized_headers(),
Expand Down Expand Up @@ -441,7 +443,12 @@ mod tests {
.insert(*header, MetadataValue::from_static("invalid"));
}

let http_request = r.into_http(Uri::default(), SanitizeHeaders::Yes);
let http_request = r.into_http(
Uri::default(),
http::Method::POST,
http::Version::HTTP_2,
SanitizeHeaders::Yes,
);
assert!(http_request.headers().is_empty());
}

Expand Down
27 changes: 26 additions & 1 deletion tonic/src/service/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ where
}

fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
// It is bad practice to modify the body (i.e. Message) of the request via an interceptor.
// To avoid exposing the body of the request to the interceptor function, we first remove it
// here, allow the interceptor to modify the metadata and extensions, and then recreate the
// HTTP request with the body. Tonic requests do not preserve the URI, HTTP version, and
// HTTP method of the HTTP request, so we extract them here and then add them back in below.
let uri = req.uri().clone();
let method = req.method().clone();
let version = req.version().clone();
let req = crate::Request::from_http(req);
let (metadata, extensions, msg) = req.into_parts();

Expand All @@ -173,7 +180,7 @@ where
Ok(req) => {
let (metadata, extensions, _) = req.into_parts();
let req = crate::Request::from_parts(metadata, extensions, msg);
let req = req.into_http(uri, SanitizeHeaders::No);
let req = req.into_http(uri, method, version, SanitizeHeaders::No);
ResponseFuture::future(self.inner.call(req))
}
Err(status) => ResponseFuture::status(status),
Expand Down Expand Up @@ -333,4 +340,22 @@ mod tests {
assert_eq!(expected.version(), response.version());
assert_eq!(expected.headers(), response.headers());
}

#[tokio::test]
async fn doesnt_change_http_method() {
let svc = tower::service_fn(|request: http::Request<hyper::Body>| async move {
assert_eq!(request.method(), http::Method::OPTIONS);

Ok::<_, hyper::Error>(hyper::Response::new(hyper::Body::empty()))
});

let svc = InterceptedService::new(svc, |request: crate::Request<()>| Ok(request));

let request = http::Request::builder()
.method(http::Method::OPTIONS)
.body(hyper::Body::empty())
.unwrap();

svc.oneshot(request).await.unwrap();
}
}

0 comments on commit e623562

Please sign in to comment.