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

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 committed Nov 16, 2024
1 parent eb66b91 commit 338c086
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' '])
.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 @@ -645,4 +647,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 338c086

Please sign in to comment.