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

Small tweaks to the HTTP wrapper #3099

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 28 additions & 3 deletions rust-runtime/aws-smithy-runtime-api/src/client/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl Headers {
key: impl AsHeaderComponent,
value: impl AsHeaderComponent,
) -> Result<Option<String>, HttpError> {
let key = header_name(key.into_maybe_static()?)?;
let key = header_name(key)?;
let value = header_value(value.into_maybe_static()?)?;
Ok(self
.headers
Expand All @@ -404,8 +404,10 @@ impl Headers {
/// Removes all headers with a given key
///
/// If there are multiple entries for this key, the first entry is returned
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be more explicit and add something like "all other entries are discarded" or "to get all values for a given key, keep calling this until it returns None"?

pub fn remove(&mut self, key: &str) -> Option<HeaderValue> {
self.headers.remove(key)
pub fn remove(&mut self, key: impl AsRef<str>) -> Option<String> {
self.headers
.remove(key.as_ref())
.map(|h| h.as_str().to_string())
}

/// Appends a value to a given key
Expand All @@ -427,6 +429,9 @@ mod sealed {
/// If the component can be represented as a Cow<'static, str>, return it
fn into_maybe_static(self) -> Result<MaybeStatic, HttpError>;

/// Return a string reference to this header
fn as_str(&self) -> Result<&str, HttpError>;

/// If a component is already internally represented as a `http02x::HeaderName`, return it
fn repr_as_http02x_header_name(self) -> Result<http0::HeaderName, Self>
where
Expand All @@ -440,18 +445,30 @@ mod sealed {
fn into_maybe_static(self) -> Result<MaybeStatic, HttpError> {
Ok(Cow::Borrowed(self))
}

fn as_str(&self) -> Result<&str, HttpError> {
Ok(self)
}
}

impl AsHeaderComponent for String {
fn into_maybe_static(self) -> Result<MaybeStatic, HttpError> {
Ok(Cow::Owned(self))
}

fn as_str(&self) -> Result<&str, HttpError> {
Ok(self)
}
}

impl AsHeaderComponent for Cow<'static, str> {
fn into_maybe_static(self) -> Result<MaybeStatic, HttpError> {
Ok(self)
}

fn as_str(&self) -> Result<&str, HttpError> {
Ok(self.as_ref())
}
}

impl AsHeaderComponent for http0::HeaderValue {
Expand All @@ -462,13 +479,21 @@ mod sealed {
.to_string(),
))
}

fn as_str(&self) -> Result<&str, HttpError> {
std::str::from_utf8(self.as_bytes()).map_err(HttpError::header_was_not_a_string)
}
}

impl AsHeaderComponent for http0::HeaderName {
fn into_maybe_static(self) -> Result<MaybeStatic, HttpError> {
Ok(self.to_string().into())
}

fn as_str(&self) -> Result<&str, HttpError> {
Ok(self.as_ref())
}

fn repr_as_http02x_header_name(self) -> Result<http0::HeaderName, Self>
where
Self: Sized,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn apply_endpoint(
for (header_name, header_values) in endpoint.headers() {
request.headers_mut().remove(header_name);
for value in header_values {
request.headers_mut().insert(
request.headers_mut().append(
Copy link
Contributor

Choose a reason for hiding this comment

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

would be nice to have a unit test for the use of append if we don't have one.

HeaderName::from_str(header_name).map_err(|err| {
ResolveEndpointError::message("invalid header name")
.with_source(Some(err.into()))
Expand Down
Loading