Skip to content

Commit a09d453

Browse files
srijstottoto
andauthored
optimize header name handling in Grpc::map_response (#1359)
* tonic: optimize MetadataMap::into_sanitized_headers * tonic: optimize Status::add_header --------- Co-authored-by: tottoto <tottotodev@gmail.com>
1 parent 99b663e commit a09d453

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

tonic/src/metadata/map.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use http::HeaderName;
2+
13
pub(crate) use self::as_encoding_agnostic_metadata_key::AsEncodingAgnosticMetadataKey;
24
pub(crate) use self::as_metadata_key::AsMetadataKey;
35
pub(crate) use self::into_metadata_key::IntoMetadataKey;
@@ -200,13 +202,13 @@ pub(crate) const GRPC_TIMEOUT_HEADER: &str = "grpc-timeout";
200202

201203
impl MetadataMap {
202204
// Headers reserved by the gRPC protocol.
203-
pub(crate) const GRPC_RESERVED_HEADERS: [&'static str; 6] = [
204-
"te",
205-
"user-agent",
206-
"content-type",
207-
"grpc-message",
208-
"grpc-message-type",
209-
"grpc-status",
205+
pub(crate) const GRPC_RESERVED_HEADERS: [HeaderName; 6] = [
206+
HeaderName::from_static("te"),
207+
HeaderName::from_static("user-agent"),
208+
HeaderName::from_static("content-type"),
209+
HeaderName::from_static("grpc-message"),
210+
HeaderName::from_static("grpc-message-type"),
211+
HeaderName::from_static("grpc-status"),
210212
];
211213

212214
/// Create an empty `MetadataMap`.
@@ -251,7 +253,7 @@ impl MetadataMap {
251253

252254
pub(crate) fn into_sanitized_headers(mut self) -> http::HeaderMap {
253255
for r in &Self::GRPC_RESERVED_HEADERS {
254-
self.headers.remove(*r);
256+
self.headers.remove(r);
255257
}
256258
self.headers
257259
}

tonic/src/request.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,19 @@ pub(crate) enum SanitizeHeaders {
438438
#[cfg(test)]
439439
mod tests {
440440
use super::*;
441+
use crate::metadata::{MetadataKey, MetadataValue};
442+
441443
use http::Uri;
442444

443445
#[test]
444446
fn reserved_headers_are_excluded() {
445447
let mut r = Request::new(1);
446448

447449
for header in &MetadataMap::GRPC_RESERVED_HEADERS {
448-
r.metadata_mut()
449-
.insert(*header, MetadataValue::from_static("invalid"));
450+
r.metadata_mut().insert(
451+
MetadataKey::unchecked_from_header_name(header.clone()),
452+
MetadataValue::from_static("invalid"),
453+
);
450454
}
451455

452456
let http_request = r.into_http(

tonic/src/response.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,17 @@ impl<T> Response<T> {
130130
#[cfg(test)]
131131
mod tests {
132132
use super::*;
133-
use crate::metadata::MetadataValue;
133+
use crate::metadata::{MetadataKey, MetadataValue};
134134

135135
#[test]
136136
fn reserved_headers_are_excluded() {
137137
let mut r = Response::new(1);
138138

139139
for header in &MetadataMap::GRPC_RESERVED_HEADERS {
140-
r.metadata_mut()
141-
.insert(*header, MetadataValue::from_static("invalid"));
140+
r.metadata_mut().insert(
141+
MetadataKey::unchecked_from_header_name(header.clone()),
142+
MetadataValue::from_static("invalid"),
143+
);
142144
}
143145

144146
let http_response = r.into_http();

tonic/src/status.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use crate::metadata::MetadataMap;
22
use crate::{body::BoxBody, metadata::GRPC_CONTENT_TYPE};
33
use base64::Engine as _;
44
use bytes::Bytes;
5-
use http::header::{HeaderMap, HeaderValue};
5+
use http::{
6+
header::{HeaderMap, HeaderValue},
7+
HeaderName,
8+
};
69
use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS};
710
use std::{borrow::Cow, error::Error, fmt, sync::Arc};
811
use tracing::{debug, trace, warn};
@@ -18,9 +21,9 @@ const ENCODING_SET: &AsciiSet = &CONTROLS
1821
.add(b'{')
1922
.add(b'}');
2023

21-
const GRPC_STATUS_HEADER_CODE: &str = "grpc-status";
22-
const GRPC_STATUS_MESSAGE_HEADER: &str = "grpc-message";
23-
const GRPC_STATUS_DETAILS_HEADER: &str = "grpc-status-details-bin";
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");
2427

2528
/// A gRPC status describing the result of an RPC call.
2629
///

0 commit comments

Comments
 (0)