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 headers/headers_mut accessors. #293

Merged
merged 1 commit into from Apr 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/request.rs
Expand Up @@ -565,7 +565,6 @@ impl<T> Request<T> {
&mut self.head.headers
}


/// Returns a reference to the associated extensions.
///
/// # Examples
Expand Down Expand Up @@ -940,6 +939,36 @@ impl Builder {
}
}

/// Get header on this request builder.
/// when builder has error returns None
///
/// # Example
///
/// ```
/// # use http::*;
/// # use http::header::HeaderValue;
/// # use http::request::Builder;
/// let mut req = Request::builder();
/// {
/// let headers = req.headers_mut().unwrap();
/// headers.insert("Accept", HeaderValue::from_static("text/html"));
/// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
/// }
/// let headers = req.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>> {
if self.err.is_some() {
return None;
}
match self.head
{
Some(ref mut head) => Some(&mut head.headers),
None => None
}
}

/// Adds an extension to this builder
///
/// # Examples
Expand Down
57 changes: 57 additions & 0 deletions src/response.rs
Expand Up @@ -630,6 +630,63 @@ impl Builder {
self
}

/// Get header on this response builder.
/// when builder has error returns None
///
/// # Example
///
/// ```
/// # use http::*;
/// # use http::header::HeaderValue;
/// # use http::response::Builder;
/// let mut res = Response::builder();
/// res.header("Accept", "text/html")
/// .header("X-Custom-Foo", "bar");
/// let headers = res.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>> {
if self.err.is_some() {
return None;
}
match self.head
{
Some(ref head) => Some(&head.headers),
None => None
}
}

/// Get header on this response builder.
/// when builder has error returns None
///
/// # Example
///
/// ```
/// # use http::*;
/// # use http::header::HeaderValue;
/// # use http::response::Builder;
/// let mut res = Response::builder();
/// {
/// let headers = res.headers_mut().unwrap();
/// headers.insert("Accept", HeaderValue::from_static("text/html"));
/// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
/// }
/// let headers = res.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>> {
if self.err.is_some() {
return None;
}
match self.head
{
Some(ref mut head) => Some(&mut head.headers),
None => None
}
}

/// Adds an extension to this builder
///
/// # Examples
Expand Down