Skip to content

Commit 517b7fc

Browse files
authored
Use constants for header names (#1933)
* tonic: rename header name consts and make public * tonic: use header name constants in more places
1 parent 3c900eb commit 517b7fc

File tree

9 files changed

+41
-40
lines changed

9 files changed

+41
-40
lines changed

tonic-build/src/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub(crate) fn generate_internal<T: Service>(
167167
_ => Box::pin(async move {
168168
let mut response = http::Response::new(empty_body());
169169
let headers = response.headers_mut();
170-
headers.insert("grpc-status", (tonic::Code::Unimplemented as i32).into());
170+
headers.insert(tonic::Status::GRPC_STATUS, (tonic::Code::Unimplemented as i32).into());
171171
headers.insert(http::header::CONTENT_TYPE, tonic::metadata::GRPC_CONTENT_TYPE);
172172
Ok(response)
173173
}),

tonic-health/src/generated/grpc_health_v1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ pub mod health_server {
423423
let headers = response.headers_mut();
424424
headers
425425
.insert(
426-
"grpc-status",
426+
tonic::Status::GRPC_STATUS,
427427
(tonic::Code::Unimplemented as i32).into(),
428428
);
429429
headers

tonic-reflection/src/generated/grpc_reflection_v1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub mod server_reflection_server {
425425
let headers = response.headers_mut();
426426
headers
427427
.insert(
428-
"grpc-status",
428+
tonic::Status::GRPC_STATUS,
429429
(tonic::Code::Unimplemented as i32).into(),
430430
);
431431
headers

tonic-reflection/src/generated/grpc_reflection_v1alpha.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub mod server_reflection_server {
425425
let headers = response.headers_mut();
426426
headers
427427
.insert(
428-
"grpc-status",
428+
tonic::Status::GRPC_STATUS,
429429
(tonic::Code::Unimplemented as i32).into(),
430430
);
431431
headers

tonic-web/src/call.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,11 @@ mod tests {
522522
#[test]
523523
fn decode_trailers() {
524524
let mut headers = HeaderMap::new();
525-
headers.insert("grpc-status", 0.into());
526-
headers.insert("grpc-message", "this is a message".try_into().unwrap());
525+
headers.insert(Status::GRPC_STATUS, 0.into());
526+
headers.insert(
527+
Status::GRPC_MESSAGE,
528+
"this is a message".try_into().unwrap(),
529+
);
527530

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

@@ -566,7 +569,7 @@ mod tests {
566569
let trailers = decode_trailers_frame(Bytes::copy_from_slice(&buf[81..]))
567570
.unwrap()
568571
.unwrap();
569-
let status = trailers.get("grpc-status").unwrap();
572+
let status = trailers.get(Status::GRPC_STATUS).unwrap();
570573
assert_eq!(status.to_str().unwrap(), "0")
571574
}
572575

@@ -624,8 +627,8 @@ mod tests {
624627
.unwrap();
625628

626629
let mut expected = HeaderMap::new();
627-
expected.insert("grpc-status", "0".parse().unwrap());
628-
expected.insert("grpc-message", "".parse().unwrap());
630+
expected.insert(Status::GRPC_STATUS, "0".parse().unwrap());
631+
expected.insert(Status::GRPC_MESSAGE, "".parse().unwrap());
629632
expected.insert("a", "1".parse().unwrap());
630633
expected.insert("b", "2".parse().unwrap());
631634

tonic-web/src/lib.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,17 @@ mod service;
109109

110110
use http::header::HeaderName;
111111
use std::time::Duration;
112-
use tonic::{body::BoxBody, server::NamedService};
112+
use tonic::{body::BoxBody, server::NamedService, Status};
113113
use tower_http::cors::{AllowOrigin, CorsLayer};
114114
use tower_layer::Layer;
115115
use tower_service::Service;
116116

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

@@ -136,13 +139,7 @@ where
136139
.allow_origin(AllowOrigin::mirror_request())
137140
.allow_credentials(true)
138141
.max_age(DEFAULT_MAX_AGE)
139-
.expose_headers(
140-
DEFAULT_EXPOSED_HEADERS
141-
.iter()
142-
.cloned()
143-
.map(HeaderName::from_static)
144-
.collect::<Vec<HeaderName>>(),
145-
)
142+
.expose_headers(DEFAULT_EXPOSED_HEADERS.iter().cloned().collect::<Vec<_>>())
146143
.allow_headers(
147144
DEFAULT_ALLOW_HEADERS
148145
.iter()

tonic/src/codec/prost.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ mod tests {
267267
frame
268268
.into_trailers()
269269
.expect("got trailers")
270-
.get("grpc-status")
270+
.get(Status::GRPC_STATUS)
271271
.expect("grpc-status header"),
272272
"11"
273273
);
@@ -304,7 +304,7 @@ mod tests {
304304
frame
305305
.into_trailers()
306306
.expect("got trailers")
307-
.get("grpc-status")
307+
.get(Status::GRPC_STATUS)
308308
.expect("grpc-status header"),
309309
"8"
310310
);

tonic/src/service/router.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use crate::{
22
body::{boxed, BoxBody},
33
metadata::GRPC_CONTENT_TYPE,
44
server::NamedService,
5+
Status,
56
};
6-
use http::{HeaderName, HeaderValue, Request, Response};
7+
use http::{HeaderValue, Request, Response};
78
use std::{
89
convert::Infallible,
910
fmt,
@@ -124,10 +125,7 @@ impl From<axum::Router> for Routes {
124125
async fn unimplemented() -> impl axum::response::IntoResponse {
125126
let status = http::StatusCode::OK;
126127
let headers = [
127-
(
128-
HeaderName::from_static("grpc-status"),
129-
HeaderValue::from_static("12"),
130-
),
128+
(Status::GRPC_STATUS, HeaderValue::from_static("12")),
131129
(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE),
132130
];
133131
(status, headers)

tonic/src/status.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ const ENCODING_SET: &AsciiSet = &CONTROLS
2121
.add(b'{')
2222
.add(b'}');
2323

24-
const GRPC_STATUS_HEADER_CODE: HeaderName = HeaderName::from_static("grpc-status");
25-
const GRPC_STATUS_MESSAGE_HEADER: HeaderName = HeaderName::from_static("grpc-message");
26-
const GRPC_STATUS_DETAILS_HEADER: HeaderName = HeaderName::from_static("grpc-status-details-bin");
27-
2824
/// A gRPC status describing the result of an RPC call.
2925
///
3026
/// Values can be created using the `new` function or one of the specialized
@@ -442,10 +438,10 @@ impl Status {
442438

443439
/// Extract a `Status` from a hyper `HeaderMap`.
444440
pub fn from_header_map(header_map: &HeaderMap) -> Option<Status> {
445-
header_map.get(GRPC_STATUS_HEADER_CODE).map(|code| {
441+
header_map.get(Self::GRPC_STATUS).map(|code| {
446442
let code = Code::from_bytes(code.as_ref());
447443
let error_message = header_map
448-
.get(GRPC_STATUS_MESSAGE_HEADER)
444+
.get(Self::GRPC_MESSAGE)
449445
.map(|header| {
450446
percent_decode(header.as_bytes())
451447
.decode_utf8()
@@ -454,7 +450,7 @@ impl Status {
454450
.unwrap_or_else(|| Ok(String::new()));
455451

456452
let details = header_map
457-
.get(GRPC_STATUS_DETAILS_HEADER)
453+
.get(Self::GRPC_STATUS_DETAILS)
458454
.map(|h| {
459455
crate::util::base64::STANDARD
460456
.decode(h.as_bytes())
@@ -464,9 +460,9 @@ impl Status {
464460
.unwrap_or_default();
465461

466462
let mut other_headers = header_map.clone();
467-
other_headers.remove(GRPC_STATUS_HEADER_CODE);
468-
other_headers.remove(GRPC_STATUS_MESSAGE_HEADER);
469-
other_headers.remove(GRPC_STATUS_DETAILS_HEADER);
463+
other_headers.remove(Self::GRPC_STATUS);
464+
other_headers.remove(Self::GRPC_MESSAGE);
465+
other_headers.remove(Self::GRPC_STATUS_DETAILS);
470466

471467
match error_message {
472468
Ok(message) => Status {
@@ -525,15 +521,15 @@ impl Status {
525521
pub fn add_header(&self, header_map: &mut HeaderMap) -> Result<(), Self> {
526522
header_map.extend(self.metadata.clone().into_sanitized_headers());
527523

528-
header_map.insert(GRPC_STATUS_HEADER_CODE, self.code.to_header_value());
524+
header_map.insert(Self::GRPC_STATUS, self.code.to_header_value());
529525

530526
if !self.message.is_empty() {
531527
let to_write = Bytes::copy_from_slice(
532528
Cow::from(percent_encode(self.message().as_bytes(), ENCODING_SET)).as_bytes(),
533529
);
534530

535531
header_map.insert(
536-
GRPC_STATUS_MESSAGE_HEADER,
532+
Self::GRPC_MESSAGE,
537533
HeaderValue::from_maybe_shared(to_write).map_err(invalid_header_value_byte)?,
538534
);
539535
}
@@ -542,7 +538,7 @@ impl Status {
542538
let details = crate::util::base64::STANDARD_NO_PAD.encode(&self.details[..]);
543539

544540
header_map.insert(
545-
GRPC_STATUS_DETAILS_HEADER,
541+
Self::GRPC_STATUS_DETAILS,
546542
HeaderValue::from_maybe_shared(details).map_err(invalid_header_value_byte)?,
547543
);
548544
}
@@ -591,6 +587,13 @@ impl Status {
591587
self.add_header(response.headers_mut()).unwrap();
592588
response
593589
}
590+
591+
#[doc(hidden)]
592+
pub const GRPC_STATUS: HeaderName = HeaderName::from_static("grpc-status");
593+
#[doc(hidden)]
594+
pub const GRPC_MESSAGE: HeaderName = HeaderName::from_static("grpc-message");
595+
#[doc(hidden)]
596+
pub const GRPC_STATUS_DETAILS: HeaderName = HeaderName::from_static("grpc-status-details-bin");
594597
}
595598

596599
fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option<Status> {
@@ -1005,7 +1008,7 @@ mod tests {
10051008

10061009
let b64_details = crate::util::base64::STANDARD_NO_PAD.encode(DETAILS);
10071010

1008-
assert_eq!(header_map[super::GRPC_STATUS_DETAILS_HEADER], b64_details);
1011+
assert_eq!(header_map[Status::GRPC_STATUS_DETAILS], b64_details);
10091012

10101013
let status = Status::from_header_map(&header_map).unwrap();
10111014

0 commit comments

Comments
 (0)