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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user