-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): Add
GrpcWebClientService
(#1472)
This adds `grpc-web` support for clients in `tonic-web`. This is done by reusing the server side encoding/decoding but wrapping it in different directions.
- Loading branch information
1 parent
388b177
commit dc29c17
Showing
5 changed files
with
357 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,28 @@ | ||
use bytes::{Buf, BufMut, Bytes, BytesMut}; | ||
use hello_world::{HelloReply, HelloRequest}; | ||
use http::header::{ACCEPT, CONTENT_TYPE}; | ||
use hello_world::{greeter_client::GreeterClient, HelloRequest}; | ||
use tonic_web::GrpcWebClientLayer; | ||
|
||
pub mod hello_world { | ||
tonic::include_proto!("helloworld"); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let msg = HelloRequest { | ||
name: "Bob".to_string(), | ||
}; | ||
// Must use hyper directly... | ||
let client = hyper::Client::builder().build_http(); | ||
|
||
// a good old http/1.1 request | ||
let request = http::Request::builder() | ||
.version(http::Version::HTTP_11) | ||
.method(http::Method::POST) | ||
.uri("http://127.0.0.1:3000/helloworld.Greeter/SayHello") | ||
.header(CONTENT_TYPE, "application/grpc-web") | ||
.header(ACCEPT, "application/grpc-web") | ||
.body(hyper::Body::from(encode_body(msg))) | ||
.unwrap(); | ||
let svc = tower::ServiceBuilder::new() | ||
.layer(GrpcWebClientLayer::new()) | ||
.service(client); | ||
|
||
let client = hyper::Client::new(); | ||
let mut client = GreeterClient::with_origin(svc, "http://127.0.0.1:3000".try_into()?); | ||
|
||
let response = client.request(request).await.unwrap(); | ||
let request = tonic::Request::new(HelloRequest { | ||
name: "Tonic".into(), | ||
}); | ||
|
||
assert_eq!( | ||
response.headers().get(CONTENT_TYPE).unwrap(), | ||
"application/grpc-web+proto" | ||
); | ||
let response = client.say_hello(request).await?; | ||
|
||
let body = response.into_body(); | ||
let reply = decode_body::<HelloReply>(body).await; | ||
|
||
println!("REPLY={:?}", reply); | ||
println!("RESPONSE={:?}", response); | ||
|
||
Ok(()) | ||
} | ||
|
||
// one byte for the compression flag plus four bytes for the length | ||
const GRPC_HEADER_SIZE: usize = 5; | ||
|
||
fn encode_body<T>(msg: T) -> Bytes | ||
where | ||
T: prost::Message, | ||
{ | ||
let msg_len = msg.encoded_len(); | ||
let mut buf = BytesMut::with_capacity(GRPC_HEADER_SIZE + msg_len); | ||
|
||
// compression flag, 0 means "no compression" | ||
buf.put_u8(0); | ||
buf.put_u32(msg_len as u32); | ||
|
||
msg.encode(&mut buf).unwrap(); | ||
buf.freeze() | ||
} | ||
|
||
async fn decode_body<T>(body: hyper::Body) -> T | ||
where | ||
T: Default + prost::Message, | ||
{ | ||
let mut body = hyper::body::to_bytes(body).await.unwrap(); | ||
|
||
// ignore the compression flag | ||
body.advance(1); | ||
|
||
let len = body.get_u32(); | ||
#[allow(clippy::let_and_return)] | ||
let msg = T::decode(&mut body.split_to(len as usize)).unwrap(); | ||
|
||
msg | ||
} |
Oops, something went wrong.