tonic-web: GrpcWebCall size_hint doesn't match the translated body
Bug Report
Version
tonic v0.14.6
tonic-health v0.14.6
tonic-web v0.14.6Same code on master.
Platform
Darwin 25.6.0 arm64 (macOS), rustc 1.98.0
Crates
tonic-web
Description
GrpcWebCall::size_hint returns the inner body's hint, but the body it yields
has a different length: base64 decoding shrinks a grpc-web-text request,
encoding grows a response, and trailers are added to or taken out of the data.
hyper sends an exact hint as content-length, so a translated message that
goes on over the network announces the wrong length.
I tried this code, a grpc-web to gRPC gateway in front of a remote server:
use base64::Engine as _;
use http_body_util::{BodyExt, Full};
use tower::{Layer, ServiceExt};
#[tokio::main]
async fn main() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (_reporter, health) = tonic_health::server::health_reporter();
tokio::spawn(
tonic::transport::Server::builder()
.add_service(health)
.serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener)),
);
let channel = tonic::transport::Channel::from_shared(format!("http://{addr}"))
.unwrap()
.connect()
.await
.unwrap();
let gateway = tonic_web::GrpcWebLayer::new().layer(channel);
// grpc-web-text `Check` with an empty message; `Full` has an exact length,
// as a request with `content-length` does.
let body = base64::engine::general_purpose::STANDARD.encode([0u8, 0, 0, 0, 0]);
let request = http::Request::post("/grpc.health.v1.Health/Check")
.header("content-type", "application/grpc-web-text")
.header("accept", "application/grpc-web-text")
.body(tonic::body::Body::new(Full::new(body.into_bytes().into())))
.unwrap();
let response = gateway.oneshot(request).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
println!("{}", String::from_utf8_lossy(&body));
}I expected to see this happen: the call succeeds, as it does with binary grpc-web or with the service in process.
Instead, this happened: the request goes out with the base64 length (8) as
content-length for 5 bytes of body, and the server resets the stream:
tonic::transport::Error(Transport, hyper::Error(Http2, Error { kind: Reset(StreamId(1), PROTOCOL_ERROR, Remote) }))A possible fix is in #2896
Source: grpc/grpc-rust