Skip to content

Fix header append #141

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

Merged
merged 2 commits into from
May 21, 2020
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
2 changes: 1 addition & 1 deletion src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Body {
/// assert_eq!(&body.into_string().await.unwrap(), "Hello Nori");
/// # Ok(()) }) }
/// ```
pub async fn into_string(mut self) -> io::Result<String> {
pub async fn into_string(mut self) -> crate::Result<String> {
let mut result = String::with_capacity(self.len().unwrap_or(0));
self.read_to_string(&mut result).await?;
Ok(result)
Expand Down
15 changes: 5 additions & 10 deletions src/headers/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,17 @@ impl Headers {
///
/// Unlike `insert` this function will not override the contents of a header, but insert a
/// header if there aren't any. Or else append to the existing list of headers.
pub fn append(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> crate::Result<()> {
pub fn append(&mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues) {
let name = name.into();
match self.get_mut(name.clone()) {
Some(headers) => {
let mut values: HeaderValues = values.to_header_values()?.collect();
let mut values: HeaderValues = values.to_header_values().unwrap().collect();
headers.append(&mut values);
}
None => {
self.insert(name, values);
}
}
Ok(())
}

/// Get a reference to a header.
Expand Down Expand Up @@ -194,9 +189,9 @@ mod tests {
let non_static_header = HeaderName::from_str("hello")?;

let mut headers = Headers::new();
headers.append(STATIC_HEADER, "foo0")?;
headers.append(static_header.clone(), "foo1")?;
headers.append(non_static_header.clone(), "foo2")?;
headers.append(STATIC_HEADER, "foo0");
headers.append(static_header.clone(), "foo1");
headers.append(non_static_header.clone(), "foo2");

assert_eq!(headers[STATIC_HEADER], ["foo0", "foo1", "foo2",][..]);
assert_eq!(headers[static_header], ["foo0", "foo1", "foo2",][..]);
Expand Down
10 changes: 3 additions & 7 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl Request {
/// assert_eq!(&req.body_string().await.unwrap(), "Hello Nori");
/// # Ok(()) }) }
/// ```
pub async fn body_string(self) -> io::Result<String> {
pub async fn body_string(self) -> crate::Result<String> {
self.body.into_string().await
}

Expand Down Expand Up @@ -441,15 +441,11 @@ impl Request {
/// use http_types::{Url, Method, Request};
///
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
/// req.append_header("Content-Type", "text/plain")?;
/// req.append_header("Content-Type", "text/plain");
/// #
/// # Ok(()) }
/// ```
pub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> crate::Result<()> {
pub fn append_header(&mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues) {
self.headers.append(name, values)
}

Expand Down
10 changes: 3 additions & 7 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,11 @@ impl Response {
/// use http_types::{Response, StatusCode};
///
/// let mut res = Response::new(StatusCode::Ok);
/// res.append_header("Content-Type", "text/plain")?;
/// res.append_header("Content-Type", "text/plain");
/// #
/// # Ok(()) }
/// ```
pub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> crate::Result<()> {
pub fn append_header(&mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues) {
self.headers.append(name, values)
}

Expand Down Expand Up @@ -265,7 +261,7 @@ impl Response {
/// assert_eq!(&res.body_string().await.unwrap(), "Hello Nori");
/// # Ok(()) }) }
/// ```
pub async fn body_string(self) -> io::Result<String> {
pub async fn body_string(self) -> crate::Result<String> {
self.body.into_string().await
}

Expand Down
2 changes: 1 addition & 1 deletion src/security/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,5 @@ pub fn referrer_policy(mut headers: impl AsMut<Headers>, referrer: Option<Referr

// We MUST allow for multiple Referrer-Policy headers to be set.
// See: https://w3c.github.io/webappsec-referrer-policy/#unknown-policy-values example #13
headers.as_mut().append("Referrer-Policy", policy).unwrap();
headers.as_mut().append("Referrer-Policy", policy);
}
8 changes: 2 additions & 6 deletions src/trailers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,11 @@ impl Trailers {
/// use http_types::Trailers;
///
/// let mut trailers = Trailers::new();
/// trailers.append("Content-Type", "text/plain")?;
/// trailers.append("Content-Type", "text/plain");
/// #
/// # Ok(()) }
/// ```
pub fn append(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> crate::Result<()> {
pub fn append(&mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues) {
self.headers.append(name, values)
}

Expand Down