-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ext): support non-canonical HTTP/1 reason phrases
Add a new extension type `hyper::ext::ReasonPhrase` gated by a new feature flag `http1_reason_phrase`. When enabled, store any non-canonical reason phrases in this extension when parsing responses, and write this reason phrase instead of the canonical reason phrase when emitting responses. Reason phrases are a disused corner of the spec that implementations ought to treat as opaque blobs of bytes. Unfortunately, real-world traffic sometimes does depend on being able to inspect and manipulate them. My main outstanding question is: should this new feature flag be included in `full`? Excluding it means this patch also has to modify the CI configuration in order to run the `client` and `server` tests.
- Loading branch information
Showing
8 changed files
with
251 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use std::str::Utf8Error; | ||
|
||
use bytes::Bytes; | ||
|
||
/// A reason phrase in an HTTP/1 response. | ||
/// | ||
/// # Clients | ||
/// | ||
/// For clients, a `ReasonPhrase` will be present in the extensions of the `http::Response` returned | ||
/// for a request if the reason phrase is different from the canonical reason phrase for the | ||
/// response's status code. For example, if a server returns `HTTP/1.1 200 Awesome`, the | ||
/// `ReasonPhrase` will be present and contain `Awesome`, but if a server returns `HTTP/1.1 200 OK`, | ||
/// the response will not contain a `ReasonPhrase`. | ||
/// | ||
/// ```no_run | ||
/// # #[cfg(all(feature = "tcp", feature = "client", feature = "http1_reason_phrase"))] | ||
/// # async fn fake_fetch() -> hyper::Result<()> { | ||
/// use hyper::{Client, Uri}; | ||
/// use hyper::ext::ReasonPhrase; | ||
/// | ||
/// let res = Client::new().get(Uri::from_static("http://example.com/non_canonical_reason")).await?; | ||
/// | ||
/// // Print out the non-canonical reason phrase, if it has one... | ||
/// if let Some(reason) = res.extensions().get::<ReasonPhrase>() { | ||
/// println!("non-canonical reason: {}", reason.to_str().unwrap()); | ||
/// } | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
/// | ||
/// # Servers | ||
/// | ||
/// When a `ReasonPhrase` is present in the extensions of the `http::Response` written by a server, | ||
/// its contents will be written in place of the canonical reason phrase when responding via HTTP/1. | ||
#[cfg(any(feature = "http1_reason_phrase", feature = "ffi"))] | ||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct ReasonPhrase(pub(crate) Bytes); | ||
|
||
impl ReasonPhrase { | ||
/// Gets the reason phrase as bytes. | ||
pub fn as_bytes(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
|
||
/// Gets the reason phrase as a `&str` if the phrase is valid UTF-8. | ||
pub fn to_str(&self) -> Result<&str, Utf8Error> { | ||
std::str::from_utf8(self.as_bytes()) | ||
} | ||
|
||
/// Converts a static byte slice to a reason phrase. | ||
pub const fn from_static(reason: &'static [u8]) -> Self { | ||
Self(Bytes::from_static(reason)) | ||
} | ||
|
||
/// Converts a static string to a reason phrase. | ||
pub const fn from_static_str(reason: &'static str) -> Self { | ||
Self(Bytes::from_static(reason.as_bytes())) | ||
} | ||
} | ||
|
||
impl From<&'static [u8]> for ReasonPhrase { | ||
fn from(reason: &'static [u8]) -> Self { | ||
Self(reason.into()) | ||
} | ||
} | ||
|
||
impl From<&'static str> for ReasonPhrase { | ||
fn from(reason: &'static str) -> Self { | ||
Self(reason.into()) | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for ReasonPhrase { | ||
fn from(reason: Vec<u8>) -> Self { | ||
Self(reason.into()) | ||
} | ||
} | ||
|
||
impl From<String> for ReasonPhrase { | ||
fn from(reason: String) -> Self { | ||
Self(reason.into()) | ||
} | ||
} | ||
|
||
impl From<Bytes> for ReasonPhrase { | ||
fn from(reason: Bytes) -> Self { | ||
Self(reason) | ||
} | ||
} | ||
|
||
impl Into<Bytes> for ReasonPhrase { | ||
fn into(self) -> Bytes { | ||
self.0 | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for ReasonPhrase { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters