Skip to content

Commit

Permalink
improve gRPC-Web client behavior with trailers with a space after the…
Browse files Browse the repository at this point in the history
… colon (#2053)

According to the gRPC Web Spec, Trailers should have a space after the colon, like this:

grpc-status: 0
grpc-message: Message Here

But tonic-web client and server both omit the space, like:

grpc-status:0
grpc-message:Message Here

This is likely fine for the server to do since most clients can handle having the space or not
but the tonic-web client should also handle the space character being there, which is what this
change does.
  • Loading branch information
sudorandom authored Jan 23, 2025
1 parent 88aed0e commit 910a4f1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tonic-web/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ fn decode_trailers_frame(mut buf: Bytes) -> Result<Option<HeaderMap>, Status> {
let value = value
.split(|b| b == &b'\r')
.next()
.ok_or_else(|| Status::internal("trailers was not escaped"))?;
.ok_or_else(|| Status::internal("trailers was not escaped"))?
.strip_prefix(&[b' '])

Check failure on line 433 in tonic-web/src/call.rs

View workflow job for this annotation

GitHub Actions / clippy

can be more succinctly written as a byte str

Check failure on line 433 in tonic-web/src/call.rs

View workflow job for this annotation

GitHub Actions / clippy

can be more succinctly written as a byte str
.unwrap_or(value);

let header_key = HeaderName::try_from(key)
.map_err(|e| Status::internal(format!("Unable to parse HeaderName: {}", e)))?;
Expand Down Expand Up @@ -643,4 +645,19 @@ mod tests {

assert_eq!(trailers, expected);
}

#[test]
fn decode_trailers_with_space_after_colon() {
let buf = b"\x80\0\0\0\x0fgrpc-status: 0\r\ngrpc-message: \r\n";

let trailers = decode_trailers_frame(Bytes::copy_from_slice(&buf[..]))
.unwrap()
.unwrap();

let mut expected = HeaderMap::new();
expected.insert(Status::GRPC_STATUS, "0".parse().unwrap());
expected.insert(Status::GRPC_MESSAGE, "".parse().unwrap());

assert_eq!(trailers, expected);
}
}

0 comments on commit 910a4f1

Please sign in to comment.