Skip to content

Commit

Permalink
errors: Set content-type for synthesized grpc errors (#750)
Browse files Browse the repository at this point in the history
The proxy will synthesize responses for some error conditions.
Currently, there's special logic for synthesizing errors for gRPC
requests: we emit `grpc-status` and `grpc-message` trailers rather than
HTTP status codes for gRPC requests. However, we *don't* currently set a
`content-type` header for gRPC error responses. This makes some clients
angry.

This commit adds a `content-type: application/grpc` header to
synthesized error responses for gRPC requests.

Fixes linkerd/linkerd2#5273
  • Loading branch information
hawkw authored Nov 23, 2020
1 parent 04431a3 commit f0da619
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion linkerd/app/core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum ResponseBody<B> {
},
}

const GRPC_CONTENT_TYPE: &'static str = "application/grpc";

impl<B: hyper::body::HttpBody> hyper::body::HttpBody for ResponseBody<B>
where
B::Error: Into<Error>,
Expand Down Expand Up @@ -149,7 +151,7 @@ impl<ReqB, RspB: Default + hyper::body::HttpBody>
let is_grpc = req
.headers()
.get(http::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok().map(|s| s.starts_with("application/grpc")))
.and_then(|v| v.to_str().ok().map(|s| s.starts_with(GRPC_CONTENT_TYPE)))
.unwrap_or(false);
Respond {
is_grpc,
Expand Down Expand Up @@ -200,6 +202,7 @@ impl<RspB: Default + hyper::body::HttpBody> respond::Respond<http::Response<RspB
let mut rsp = http::Response::builder()
.version(http::Version::HTTP_2)
.header(http::header::CONTENT_LENGTH, "0")
.header(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE)
.body(ResponseBody::default())
.expect("app::errors response is valid");
let code = set_grpc_status(&*error, rsp.headers_mut());
Expand Down

0 comments on commit f0da619

Please sign in to comment.