Skip to content

Commit

Permalink
optimize header name handling in Grpc::map_response (#1359)
Browse files Browse the repository at this point in the history
* tonic: optimize MetadataMap::into_sanitized_headers

* tonic: optimize Status::add_header

---------

Co-authored-by: tottoto <tottotodev@gmail.com>
  • Loading branch information
srijs and tottoto committed Sep 20, 2024
1 parent 99b663e commit a09d453
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
18 changes: 10 additions & 8 deletions tonic/src/metadata/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use http::HeaderName;

pub(crate) use self::as_encoding_agnostic_metadata_key::AsEncodingAgnosticMetadataKey;
pub(crate) use self::as_metadata_key::AsMetadataKey;
pub(crate) use self::into_metadata_key::IntoMetadataKey;
Expand Down Expand Up @@ -200,13 +202,13 @@ pub(crate) const GRPC_TIMEOUT_HEADER: &str = "grpc-timeout";

impl MetadataMap {
// Headers reserved by the gRPC protocol.
pub(crate) const GRPC_RESERVED_HEADERS: [&'static str; 6] = [
"te",
"user-agent",
"content-type",
"grpc-message",
"grpc-message-type",
"grpc-status",
pub(crate) const GRPC_RESERVED_HEADERS: [HeaderName; 6] = [
HeaderName::from_static("te"),
HeaderName::from_static("user-agent"),
HeaderName::from_static("content-type"),
HeaderName::from_static("grpc-message"),
HeaderName::from_static("grpc-message-type"),
HeaderName::from_static("grpc-status"),
];

/// Create an empty `MetadataMap`.
Expand Down Expand Up @@ -251,7 +253,7 @@ impl MetadataMap {

pub(crate) fn into_sanitized_headers(mut self) -> http::HeaderMap {
for r in &Self::GRPC_RESERVED_HEADERS {
self.headers.remove(*r);
self.headers.remove(r);
}
self.headers
}
Expand Down
8 changes: 6 additions & 2 deletions tonic/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,19 @@ pub(crate) enum SanitizeHeaders {
#[cfg(test)]
mod tests {
use super::*;
use crate::metadata::{MetadataKey, MetadataValue};

use http::Uri;

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

for header in &MetadataMap::GRPC_RESERVED_HEADERS {
r.metadata_mut()
.insert(*header, MetadataValue::from_static("invalid"));
r.metadata_mut().insert(
MetadataKey::unchecked_from_header_name(header.clone()),
MetadataValue::from_static("invalid"),
);
}

let http_request = r.into_http(
Expand Down
8 changes: 5 additions & 3 deletions tonic/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,17 @@ impl<T> Response<T> {
#[cfg(test)]
mod tests {
use super::*;
use crate::metadata::MetadataValue;
use crate::metadata::{MetadataKey, MetadataValue};

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

for header in &MetadataMap::GRPC_RESERVED_HEADERS {
r.metadata_mut()
.insert(*header, MetadataValue::from_static("invalid"));
r.metadata_mut().insert(
MetadataKey::unchecked_from_header_name(header.clone()),
MetadataValue::from_static("invalid"),
);
}

let http_response = r.into_http();
Expand Down
11 changes: 7 additions & 4 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::metadata::MetadataMap;
use crate::{body::BoxBody, metadata::GRPC_CONTENT_TYPE};
use base64::Engine as _;
use bytes::Bytes;
use http::header::{HeaderMap, HeaderValue};
use http::{
header::{HeaderMap, HeaderValue},
HeaderName,
};
use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS};
use std::{borrow::Cow, error::Error, fmt, sync::Arc};
use tracing::{debug, trace, warn};
Expand All @@ -18,9 +21,9 @@ const ENCODING_SET: &AsciiSet = &CONTROLS
.add(b'{')
.add(b'}');

const GRPC_STATUS_HEADER_CODE: &str = "grpc-status";
const GRPC_STATUS_MESSAGE_HEADER: &str = "grpc-message";
const GRPC_STATUS_DETAILS_HEADER: &str = "grpc-status-details-bin";
const GRPC_STATUS_HEADER_CODE: HeaderName = HeaderName::from_static("grpc-status");
const GRPC_STATUS_MESSAGE_HEADER: HeaderName = HeaderName::from_static("grpc-message");
const GRPC_STATUS_DETAILS_HEADER: HeaderName = HeaderName::from_static("grpc-status-details-bin");

/// A gRPC status describing the result of an RPC call.
///
Expand Down

0 comments on commit a09d453

Please sign in to comment.