Skip to content

Commit

Permalink
Correctly identify gRPC requests in default on_response callback (#278
Browse files Browse the repository at this point in the history
)

* use grpc status instead of html status in grpc-web on_response_trace

* add changelog entry for pr#278

Co-authored-by: Joe Dahlquist <joe@arcanyx.com>
  • Loading branch information
jdahlq and jdahlq committed Jul 4, 2022
1 parent bab200c commit 9b18aa7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tower-http/CHANGELOG.md
Expand Up @@ -21,7 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Fixed

- None.
- **trace:** Correctly identify gRPC requests in default `on_response` callback ([#278])

[#278]: https://github.com/tower-rs/tower-http/pull/278

# 0.3.4 (June 06, 2022)

Expand Down
15 changes: 14 additions & 1 deletion tower-http/src/trace/on_response.rs
Expand Up @@ -264,10 +264,23 @@ impl<B> OnResponse<B> for DefaultOnResponse {
fn status<B>(res: &Response<B>) -> Option<i32> {
use crate::classify::grpc_errors_as_failures::ParsedGrpcStatus;

// gRPC-over-HTTP2 uses the "application/grpc[+format]" content type, and gRPC-Web uses
// "application/grpc-web[+format]" or "application/grpc-web-text[+format]", where "format" is
// the message format, e.g. +proto, +json.
//
// So, valid grpc content types include (but are not limited to):
// - application/grpc
// - application/grpc+proto
// - application/grpc-web+proto
// - application/grpc-web-text+proto
//
// For simplicity, we simply check that the content type starts with "application/grpc".
let is_grpc = res
.headers()
.get(http::header::CONTENT_TYPE)
.map_or(false, |value| value == "application/grpc");
.map_or(false, |value| {
value.as_bytes().starts_with("application/grpc".as_bytes())
});

if is_grpc {
match crate::classify::grpc_errors_as_failures::classify_grpc_metadata(
Expand Down

0 comments on commit 9b18aa7

Please sign in to comment.