fix(codec): Remove custom content-type (#104)

This removes custom content-types in favor of
just using `application/grpc`. There is some
confusion around the specification but most
grpc implementations ignore the `+` and
anything after.
This commit is contained in:
Lucio Franco
2019-10-29 16:12:41 -04:00
committed by GitHub
parent 4bb087b5ff
commit a17049f1f7
16 changed files with 2931 additions and 27 deletions
@@ -55,7 +55,7 @@ pub mod client {
request: tonic::Request<super::HelloRequest>,
) -> Result<tonic::Response<super::HelloReply>, tonic::Status> {
self.ready().await?;
let codec = tonic::codec::ProstCodec::new();
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static("/helloworld.Greeter/SayHello");
self.inner.unary(request, path, codec).await
}
@@ -146,7 +146,7 @@ pub mod server {
let inner = self.inner.clone();
let fut = async move {
let method = SayHello(inner);
let codec = tonic::codec::ProstCodec::new();
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec);
let res = grpc.unary(method, req).await;
Ok(res)
+1 -2
View File
@@ -156,10 +156,9 @@ impl<T> Grpc<T> {
.insert(TE, HeaderValue::from_static("trailers"));
// Set the content type
let content_type = <C as Codec>::CONTENT_TYPE;
request
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
.insert(CONTENT_TYPE, HeaderValue::from_static("application/grpc"));
let response = self
.inner
+1 -8
View File
@@ -21,7 +21,7 @@ pub use tokio_codec::{Decoder, Encoder};
use crate::Status;
/// Trait that knows how to encode and decode gRPC messages.
pub trait Codec {
pub trait Codec: Default {
/// The encodable message.
type Encode: Send + 'static;
/// The decodable message.
@@ -32,13 +32,6 @@ pub trait Codec {
/// The encoder that can decode a message.
type Decoder: Decoder<Item = Self::Decode, Error = Status> + Send + 'static;
/// The content type of this codec.
///
/// This should follow the `Content-Type` definition [here].
///
/// [here]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
const CONTENT_TYPE: &'static str;
/// Fetch the encoder.
fn encoder(&mut self) -> Self::Encoder;
/// Fetch the decoder.
+2 -5
View File
@@ -10,9 +10,8 @@ pub struct ProstCodec<T, U> {
_pd: PhantomData<(T, U)>,
}
impl<T, U> ProstCodec<T, U> {
/// Create a new codec that knows how to encode `T` and decode `U`.
pub fn new() -> Self {
impl<T, U> Default for ProstCodec<T, U> {
fn default() -> Self {
Self { _pd: PhantomData }
}
}
@@ -28,8 +27,6 @@ where
type Encoder = ProstEncoder<T>;
type Decoder = ProstDecoder<U>;
const CONTENT_TYPE: &'static str = "application/grpc+proto";
fn encoder(&mut self) -> Self::Encoder {
ProstEncoder(PhantomData)
}
+2 -2
View File
@@ -179,7 +179,7 @@ where
// Set the content type
parts.headers.insert(
http::header::CONTENT_TYPE,
http::header::HeaderValue::from_static(T::CONTENT_TYPE),
http::header::HeaderValue::from_static("application/grpc"),
);
let body = encode_server(self.codec.encoder(), body.into_stream());
@@ -191,7 +191,7 @@ where
parts.headers.insert(
http::header::CONTENT_TYPE,
http::header::HeaderValue::from_static(T::CONTENT_TYPE),
http::header::HeaderValue::from_static("application/grpc"),
);
status.add_header(&mut parts.headers).unwrap();