Skip to content

Commit 910a4f1

Browse files
authored
improve gRPC-Web client behavior with trailers with a space after the 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.
1 parent 88aed0e commit 910a4f1

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

tonic-web/src/call.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ fn decode_trailers_frame(mut buf: Bytes) -> Result<Option<HeaderMap>, Status> {
429429
let value = value
430430
.split(|b| b == &b'\r')
431431
.next()
432-
.ok_or_else(|| Status::internal("trailers was not escaped"))?;
432+
.ok_or_else(|| Status::internal("trailers was not escaped"))?
433+
.strip_prefix(&[b' '])
434+
.unwrap_or(value);
433435

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

644646
assert_eq!(trailers, expected);
645647
}
648+
649+
#[test]
650+
fn decode_trailers_with_space_after_colon() {
651+
let buf = b"\x80\0\0\0\x0fgrpc-status: 0\r\ngrpc-message: \r\n";
652+
653+
let trailers = decode_trailers_frame(Bytes::copy_from_slice(&buf[..]))
654+
.unwrap()
655+
.unwrap();
656+
657+
let mut expected = HeaderMap::new();
658+
expected.insert(Status::GRPC_STATUS, "0".parse().unwrap());
659+
expected.insert(Status::GRPC_MESSAGE, "".parse().unwrap());
660+
661+
assert_eq!(trailers, expected);
662+
}
646663
}

0 commit comments

Comments
 (0)