Skip to content

Commit

Permalink
tonic: use header name constants in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Sep 9, 2024
1 parent a3fdcaf commit 81dbefa
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub(crate) fn generate_internal<T: Service>(
_ => Box::pin(async move {
let mut response = http::Response::new(empty_body());
let headers = response.headers_mut();
headers.insert("grpc-status", (tonic::Code::Unimplemented as i32).into());
headers.insert(tonic::Status::GRPC_STATUS, (tonic::Code::Unimplemented as i32).into());
headers.insert(http::header::CONTENT_TYPE, tonic::metadata::GRPC_CONTENT_TYPE);
Ok(response)
}),
Expand Down
2 changes: 1 addition & 1 deletion tonic-health/src/generated/grpc_health_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ pub mod health_server {
let headers = response.headers_mut();
headers
.insert(
"grpc-status",
tonic::Status::GRPC_STATUS,
(tonic::Code::Unimplemented as i32).into(),
);
headers
Expand Down
2 changes: 1 addition & 1 deletion tonic-reflection/src/generated/grpc_reflection_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub mod server_reflection_server {
let headers = response.headers_mut();
headers
.insert(
"grpc-status",
tonic::Status::GRPC_STATUS,
(tonic::Code::Unimplemented as i32).into(),
);
headers
Expand Down
2 changes: 1 addition & 1 deletion tonic-reflection/src/generated/grpc_reflection_v1alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub mod server_reflection_server {
let headers = response.headers_mut();
headers
.insert(
"grpc-status",
tonic::Status::GRPC_STATUS,
(tonic::Code::Unimplemented as i32).into(),
);
headers
Expand Down
13 changes: 8 additions & 5 deletions tonic-web/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,11 @@ mod tests {
#[test]
fn decode_trailers() {
let mut headers = HeaderMap::new();
headers.insert("grpc-status", 0.into());
headers.insert("grpc-message", "this is a message".try_into().unwrap());
headers.insert(Status::GRPC_STATUS, 0.into());
headers.insert(
Status::GRPC_MESSAGE,
"this is a message".try_into().unwrap(),
);

let trailers = make_trailers_frame(headers.clone());

Expand Down Expand Up @@ -566,7 +569,7 @@ mod tests {
let trailers = decode_trailers_frame(Bytes::copy_from_slice(&buf[81..]))
.unwrap()
.unwrap();
let status = trailers.get("grpc-status").unwrap();
let status = trailers.get(Status::GRPC_STATUS).unwrap();
assert_eq!(status.to_str().unwrap(), "0")
}

Expand Down Expand Up @@ -624,8 +627,8 @@ mod tests {
.unwrap();

let mut expected = HeaderMap::new();
expected.insert("grpc-status", "0".parse().unwrap());
expected.insert("grpc-message", "".parse().unwrap());
expected.insert(Status::GRPC_STATUS, "0".parse().unwrap());
expected.insert(Status::GRPC_MESSAGE, "".parse().unwrap());
expected.insert("a", "1".parse().unwrap());
expected.insert("b", "2".parse().unwrap());

Expand Down
17 changes: 7 additions & 10 deletions tonic-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,17 @@ mod service;

use http::header::HeaderName;
use std::time::Duration;
use tonic::{body::BoxBody, server::NamedService};
use tonic::{body::BoxBody, server::NamedService, Status};
use tower_http::cors::{AllowOrigin, CorsLayer};
use tower_layer::Layer;
use tower_service::Service;

const DEFAULT_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
const DEFAULT_EXPOSED_HEADERS: [&str; 3] =
["grpc-status", "grpc-message", "grpc-status-details-bin"];
const DEFAULT_EXPOSED_HEADERS: [HeaderName; 3] = [
Status::GRPC_STATUS,
Status::GRPC_MESSAGE,
Status::GRPC_STATUS_DETAILS,
];
const DEFAULT_ALLOW_HEADERS: [&str; 4] =
["x-grpc-web", "content-type", "x-user-agent", "grpc-timeout"];

Expand All @@ -136,13 +139,7 @@ where
.allow_origin(AllowOrigin::mirror_request())
.allow_credentials(true)
.max_age(DEFAULT_MAX_AGE)
.expose_headers(
DEFAULT_EXPOSED_HEADERS
.iter()
.cloned()
.map(HeaderName::from_static)
.collect::<Vec<HeaderName>>(),
)
.expose_headers(DEFAULT_EXPOSED_HEADERS.iter().cloned().collect::<Vec<_>>())
.allow_headers(
DEFAULT_ALLOW_HEADERS
.iter()
Expand Down
4 changes: 2 additions & 2 deletions tonic/src/codec/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ mod tests {
frame
.into_trailers()
.expect("got trailers")
.get("grpc-status")
.get(Status::GRPC_STATUS)
.expect("grpc-status header"),
"11"
);
Expand Down Expand Up @@ -302,7 +302,7 @@ mod tests {
frame
.into_trailers()
.expect("got trailers")
.get("grpc-status")
.get(Status::GRPC_STATUS)
.expect("grpc-status header"),
"8"
);
Expand Down
8 changes: 3 additions & 5 deletions tonic/src/service/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::{
body::{boxed, BoxBody},
metadata::GRPC_CONTENT_TYPE,
server::NamedService,
Status,
};
use http::{HeaderName, HeaderValue, Request, Response};
use http::{HeaderValue, Request, Response};
use std::{
convert::Infallible,
fmt,
Expand Down Expand Up @@ -124,10 +125,7 @@ impl From<axum::Router> for Routes {
async fn unimplemented() -> impl axum::response::IntoResponse {
let status = http::StatusCode::OK;
let headers = [
(
HeaderName::from_static("grpc-status"),
HeaderValue::from_static("12"),
),
(Status::GRPC_STATUS, HeaderValue::from_static("12")),
(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE),
];
(status, headers)
Expand Down

0 comments on commit 81dbefa

Please sign in to comment.