Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http = "1.0" support to the http request wrapper #3373

Merged
merged 7 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ message = "Fix bug where overriding the credentials at the operation level faile
references = ["aws-sdk-rust#901", "smithy-rs#3363"]
meta = { "breaking" = false, "bug" = true, "tada" = false }
author = "rcoh"

[[smithy-rs]]
message = "Add `try_into_http1x` and `try_from_http1x` to Request and Response container types."
references = ["aws-sdk-rust#977", "smithy-rs#3365", "smithy-rs#3373"]
meta = { "breaking" = false, "bug" = false, "tada" = false, "target" = "all" }
author = "rcoh"

[[aws-sdk-rust]]
message = "Add `try_into_http1x` and `try_from_http1x` to Request and Response container types."
references = ["aws-sdk-rust#977", "smithy-rs#3365", "smithy-rs#3373"]
meta = { "breaking" = false, "bug" = false, "tada" = false }
author = "rcoh"
4 changes: 3 additions & 1 deletion rust-runtime/aws-smithy-runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ repository = "https://github.com/smithy-lang/smithy-rs"
default = []
client = []
http-auth = ["dep:zeroize"]
test-util = ["aws-smithy-types/test-util"]
test-util = ["aws-smithy-types/test-util", "http-1x"]
http-02x = []
http-1x = []

[dependencies]
aws-smithy-async = { path = "../aws-smithy-async" }
aws-smithy-types = { path = "../aws-smithy-types" }
bytes = "1"
http = "0.2.9"
http1 = { package = "http", version = "1" }
Comment on lines 24 to +25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these both be optional dependencies enabled by the features?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe eventually? But they're both so small that the cost of having them both in tree is very small. When we refactor to use http 1.0 representations under the hood, we can make the http 0x dependency optional

pin-project-lite = "0.2"
tokio = { version = "1.25", features = ["sync"] }
tracing = "0.1"
Expand Down
5 changes: 5 additions & 0 deletions rust-runtime/aws-smithy-runtime-api/src/http/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ impl HttpError {
HttpError(err.into())
}

#[allow(dead_code)]
pub(super) fn invalid_extensions() -> Self {
Self("Extensions were provided during initialization. This prevents the request format from being converted.".into())
}

pub(super) fn invalid_header_value(err: InvalidHeaderValue) -> Self {
Self(err.into())
}
Expand Down
91 changes: 86 additions & 5 deletions rust-runtime/aws-smithy-runtime-api/src/http/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ impl Headers {
Self::default()
}

#[cfg(feature = "http-1x")]
pub(crate) fn http1_headermap(self) -> http1::HeaderMap {
let mut headers = http1::HeaderMap::new();
headers.reserve(self.headers.len());
headers.extend(self.headers.into_iter().map(|(k, v)| {
(
k.map(|n| {
http1::HeaderName::from_bytes(n.as_str().as_bytes()).expect("proven valid")
}),
v.into_http1x(),
)
}));
headers
}

#[cfg(feature = "http-02x")]
pub(crate) fn http0_headermap(self) -> http0::HeaderMap {
let mut headers = http0::HeaderMap::new();
headers.reserve(self.headers.len());
headers.extend(self.headers.into_iter().map(|(k, v)| (k, v.into_http02x())));
headers
}

/// Returns the value for a given key
///
/// If multiple values are associated, the first value is returned
Expand Down Expand Up @@ -181,6 +204,34 @@ impl TryFrom<HeaderMap> for Headers {
}
}

#[cfg(feature = "http-1x")]
impl TryFrom<http1::HeaderMap> for Headers {
type Error = HttpError;

fn try_from(value: http1::HeaderMap) -> Result<Self, Self::Error> {
if let Some(e) = value
.values()
.filter_map(|value| std::str::from_utf8(value.as_bytes()).err())
.next()
{
Err(HttpError::header_was_not_a_string(e))
} else {
let mut string_safe_headers: http0::HeaderMap<HeaderValue> = Default::default();
string_safe_headers.extend(value.into_iter().map(|(k, v)| {
(
k.map(|v| {
http0::HeaderName::from_bytes(v.as_str().as_bytes()).expect("known valid")
}),
HeaderValue::from_http1x(v).expect("validated above"),
)
}));
Ok(Headers {
headers: string_safe_headers,
})
}
}
}

use sealed::AsHeaderComponent;

mod sealed {
Expand Down Expand Up @@ -273,25 +324,55 @@ mod header_value {
/// **Note**: Unlike `HeaderValue` in `http`, this only supports UTF-8 header values
#[derive(Debug, Clone)]
pub struct HeaderValue {
_private: http0::HeaderValue,
_private: Inner,
}

#[derive(Debug, Clone)]
enum Inner {
H0(http0::HeaderValue),
#[allow(dead_code)]
H1(http1::HeaderValue),
}

impl HeaderValue {
pub(crate) fn from_http02x(value: http0::HeaderValue) -> Result<Self, Utf8Error> {
let _ = std::str::from_utf8(value.as_bytes())?;
Ok(Self { _private: value })
Ok(Self {
_private: Inner::H0(value),
})
}

pub(crate) fn from_http1x(value: http1::HeaderValue) -> Result<Self, Utf8Error> {
let _ = std::str::from_utf8(value.as_bytes())?;
Ok(Self {
_private: Inner::H1(value),
})
}

#[allow(dead_code)]
pub(crate) fn into_http02x(self) -> http0::HeaderValue {
self._private
match self._private {
Inner::H0(v) => v,
Inner::H1(v) => http0::HeaderValue::from_maybe_shared(v).expect("unreachable"),
}
}

#[allow(dead_code)]
pub(crate) fn into_http1x(self) -> http1::HeaderValue {
match self._private {
Inner::H1(v) => v,
Inner::H0(v) => http1::HeaderValue::from_maybe_shared(v).expect("unreachable"),
}
}
}

impl AsRef<str> for HeaderValue {
fn as_ref(&self) -> &str {
std::str::from_utf8(self._private.as_bytes())
.expect("unreachable—only strings may be stored")
let bytes = match &self._private {
Inner::H0(v) => v.as_bytes(),
Inner::H1(v) => v.as_bytes(),
};
std::str::from_utf8(bytes).expect("unreachable—only strings may be stored")
}
}

Expand Down
Loading