Skip to content

Commit

Permalink
Add a GRPC_CONTENT_TYPE for "application/grpc". (#1733)
Browse files Browse the repository at this point in the history
* Add a `GRPC_CONTENT_TYPE` for "application/grpc".

Currently defined in `tonic::metadata`. Would need guidance from
maintainers for where it might be better located.

* Fixed tests

---------

Co-authored-by: gibbz00 <[email protected]>
  • Loading branch information
ikrivosheev and gibbz00 committed Jun 17, 2024
1 parent 3457f92 commit e2c506a
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ pub(crate) fn generate_internal<T: Service>(
_ => Box::pin(async move {
Ok(http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.header("grpc-status", tonic::Code::Unimplemented as i32)
.header(http::header::CONTENT_TYPE, tonic::metadata::GRPC_CONTENT_TYPE)
.body(empty_body())
.unwrap())
}),
Expand Down
7 changes: 5 additions & 2 deletions tonic-health/src/generated/grpc_health_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,11 @@ pub mod health_server {
Ok(
http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.header("grpc-status", tonic::Code::Unimplemented as i32)
.header(
http::header::CONTENT_TYPE,
tonic::metadata::GRPC_CONTENT_TYPE,
)
.body(empty_body())
.unwrap(),
)
Expand Down
7 changes: 5 additions & 2 deletions tonic-reflection/src/generated/grpc_reflection_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,11 @@ pub mod server_reflection_server {
Ok(
http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.header("grpc-status", tonic::Code::Unimplemented as i32)
.header(
http::header::CONTENT_TYPE,
tonic::metadata::GRPC_CONTENT_TYPE,
)
.body(empty_body())
.unwrap(),
)
Expand Down
7 changes: 5 additions & 2 deletions tonic-reflection/src/generated/grpc_reflection_v1alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,11 @@ pub mod server_reflection_server {
Ok(
http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.header("grpc-status", tonic::Code::Unimplemented as i32)
.header(
http::header::CONTENT_TYPE,
tonic::metadata::GRPC_CONTENT_TYPE,
)
.body(empty_body())
.unwrap(),
)
Expand Down
9 changes: 4 additions & 5 deletions tonic-web/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::task::{ready, Context, Poll};
use http::{header, HeaderMap, HeaderValue, Method, Request, Response, StatusCode, Version};
use http_body_util::BodyExt;
use pin_project::pin_project;
use tonic::metadata::GRPC_CONTENT_TYPE;
use tonic::{
body::{empty_body, BoxBody},
server::NamedService,
Expand All @@ -16,8 +17,6 @@ use crate::call::content_types::is_grpc_web;
use crate::call::{Encoding, GrpcWebCall};
use crate::BoxError;

const GRPC: &str = "application/grpc";

/// Service implementing the grpc-web protocol.
#[derive(Debug, Clone)]
pub struct GrpcWebService<S> {
Expand Down Expand Up @@ -206,7 +205,7 @@ fn coerce_request(mut req: Request<BoxBody>, encoding: Encoding) -> Request<BoxB
req.headers_mut().remove(header::CONTENT_LENGTH);

req.headers_mut()
.insert(header::CONTENT_TYPE, HeaderValue::from_static(GRPC));
.insert(header::CONTENT_TYPE, GRPC_CONTENT_TYPE);

req.headers_mut()
.insert(header::TE, HeaderValue::from_static("trailers"));
Expand Down Expand Up @@ -373,7 +372,7 @@ mod tests {
fn request() -> Request<BoxBody> {
Request::builder()
.version(Version::HTTP_2)
.header(CONTENT_TYPE, GRPC)
.header(CONTENT_TYPE, GRPC_CONTENT_TYPE)
.body(empty_body())
.unwrap()
}
Expand All @@ -393,7 +392,7 @@ mod tests {
let mut svc = crate::enable(Svc);

let req = Request::builder()
.header(CONTENT_TYPE, GRPC)
.header(CONTENT_TYPE, GRPC_CONTENT_TYPE)
.body(empty_body())
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion tonic/src/client/grpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::codec::compression::{CompressionEncoding, EnabledCompressionEncodings};
use crate::metadata::GRPC_CONTENT_TYPE;
use crate::{
body::BoxBody,
client::GrpcService,
Expand Down Expand Up @@ -404,7 +405,7 @@ impl GrpcConfig {
// Set the content type
request
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/grpc"));
.insert(CONTENT_TYPE, GRPC_CONTENT_TYPE);

#[cfg(any(feature = "gzip", feature = "zstd"))]
if let Some(encoding) = self.send_compression_encodings {
Expand Down
4 changes: 4 additions & 0 deletions tonic/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ pub use self::map::ValuesMut;
pub use self::value::AsciiMetadataValue;
pub use self::value::BinaryMetadataValue;
pub use self::value::MetadataValue;
use http::HeaderValue;

pub(crate) use self::map::GRPC_TIMEOUT_HEADER;

/// HTTP Header `content-type` value for gRPC calls.
pub const GRPC_CONTENT_TYPE: HeaderValue = HeaderValue::from_static("application/grpc");

/// The metadata::errors module contains types for errors that can occur
/// while handling gRPC custom metadata.
pub mod errors {
Expand Down
8 changes: 4 additions & 4 deletions tonic/src/server/grpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::codec::compression::{
CompressionEncoding, EnabledCompressionEncodings, SingleMessageCompressionOverride,
};
use crate::metadata::GRPC_CONTENT_TYPE;
use crate::{
body::BoxBody,
codec::{encode_server, Codec, Streaming},
Expand Down Expand Up @@ -433,10 +434,9 @@ where
let (mut parts, body) = response.into_http().into_parts();

// Set the content type
parts.headers.insert(
http::header::CONTENT_TYPE,
http::header::HeaderValue::from_static("application/grpc"),
);
parts
.headers
.insert(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE);

#[cfg(any(feature = "gzip", feature = "zstd"))]
if let Some(encoding) = accept_encoding {
Expand Down
11 changes: 9 additions & 2 deletions tonic/src/service/router.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
body::{boxed, BoxBody},
metadata::GRPC_CONTENT_TYPE,
server::NamedService,
};
use http::{Request, Response};
use http::{HeaderName, HeaderValue, Request, Response};
use pin_project::pin_project;
use std::{
convert::Infallible,
Expand Down Expand Up @@ -98,7 +99,13 @@ impl Routes {

async fn unimplemented() -> impl axum::response::IntoResponse {
let status = http::StatusCode::OK;
let headers = [("grpc-status", "12"), ("content-type", "application/grpc")];
let headers = [
(
HeaderName::from_static("grpc-status"),
HeaderValue::from_static("12"),
),
(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE),
];
(status, headers)
}

Expand Down
9 changes: 4 additions & 5 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::body::BoxBody;
use crate::metadata::MetadataMap;
use crate::{body::BoxBody, metadata::GRPC_CONTENT_TYPE};
use base64::Engine as _;
use bytes::Bytes;
use http::header::{HeaderMap, HeaderValue};
Expand Down Expand Up @@ -583,10 +583,9 @@ impl Status {
/// Build an `http::Response` from the given `Status`.
pub fn into_http(self) -> http::Response<BoxBody> {
let mut response = http::Response::new(crate::body::empty_body());
response.headers_mut().insert(
http::header::CONTENT_TYPE,
http::header::HeaderValue::from_static("application/grpc"),
);
response
.headers_mut()
.insert(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE);
self.add_header(response.headers_mut()).unwrap();
response
}
Expand Down

0 comments on commit e2c506a

Please sign in to comment.