Skip to content

Commit

Permalink
Small tweaks to the HTTP wrapper (#3099)
Browse files Browse the repository at this point in the history
## Motivation and Context
- small improvements / consistency updates to http wrapper
- fix bug where `.insert` was called but it should have been `append`

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
  • Loading branch information
rcoh authored Oct 30, 2023
1 parent 7768e03 commit 5a8b07c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
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
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(
HeaderName::from_str(header_name).map_err(|err| {
ResolveEndpointError::message("invalid header name")
.with_source(Some(err.into()))
Expand Down

0 comments on commit 5a8b07c

Please sign in to comment.