Skip to content

Commit cb0c863

Browse files
authored
Merge pull request #56 from yoshuawuyts/body-methods
add {take,swap,replace}_body
2 parents ee0cb4f + 87c8fa8 commit cb0c863

File tree

3 files changed

+203
-1
lines changed

3 files changed

+203
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ http = { version = "0.2.0", optional = true }
2828

2929
[dev-dependencies]
3030
http = "0.2.0"
31+
async-std = "1.4.0"

src/request.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use async_std::io::{self, BufRead, Read};
22

3+
use std::mem;
34
use std::pin::Pin;
45
use std::task::{Context, Poll};
56

@@ -19,7 +20,7 @@ pin_project_lite::pin_project! {
1920
/// use http_types::{Url, Method, Request};
2021
///
2122
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
22-
/// req.set_body("hello world");
23+
/// req.set_body("Hello, Nori!");
2324
/// ```
2425
#[derive(Debug)]
2526
pub struct Request {
@@ -103,6 +104,15 @@ impl Request {
103104
}
104105

105106
/// Set the request body.
107+
///
108+
/// # Examples
109+
///
110+
/// ```
111+
/// use http_types::{Url, Method, Request};
112+
///
113+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
114+
/// req.set_body("Hello, Nori!");
115+
/// ```
106116
pub fn set_body(&mut self, body: impl Into<Body>) {
107117
self.body = body.into();
108118
if self.header(&CONTENT_TYPE).is_none() {
@@ -111,6 +121,87 @@ impl Request {
111121
}
112122
}
113123

124+
/// Swaps the value of the body with another body, without deinitializing
125+
/// either one.
126+
///
127+
/// # Examples
128+
///
129+
/// ```
130+
/// # use async_std::io::prelude::*;
131+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
132+
/// # async_std::task::block_on(async {
133+
/// #
134+
/// use http_types::{Body, Url, Method, Request};
135+
///
136+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
137+
/// req.set_body("Hello, Nori!");
138+
/// let mut body: Body = req.replace_body("Hello, Chashu!");
139+
///
140+
/// let mut string = String::new();
141+
/// body.read_to_string(&mut string).await?;
142+
/// assert_eq!(&string, "Hello, Nori!");
143+
/// #
144+
/// # Ok(()) }) }
145+
/// ```
146+
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body {
147+
mem::replace(&mut self.body, body.into())
148+
}
149+
150+
/// Replace the request body with a new body, and return the old body.
151+
///
152+
/// # Examples
153+
///
154+
/// ```
155+
/// # use async_std::io::prelude::*;
156+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
157+
/// # async_std::task::block_on(async {
158+
/// #
159+
/// use http_types::{Body, Url, Method, Request};
160+
///
161+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
162+
/// req.set_body("Hello, Nori!");
163+
/// let mut body = "Hello, Chashu!".into();
164+
/// req.swap_body(&mut body);
165+
///
166+
/// let mut string = String::new();
167+
/// body.read_to_string(&mut string).await?;
168+
/// assert_eq!(&string, "Hello, Nori!");
169+
/// #
170+
/// # Ok(()) }) }
171+
/// ```
172+
pub fn swap_body(&mut self, body: &mut Body) {
173+
mem::swap(&mut self.body, body);
174+
}
175+
176+
/// Take the request body, replacing it with an empty body.
177+
///
178+
/// # Examples
179+
///
180+
/// ```
181+
/// # use async_std::io::prelude::*;
182+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
183+
/// # async_std::task::block_on(async {
184+
/// #
185+
/// use http_types::{Body, Url, Method, Request};
186+
///
187+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
188+
/// req.set_body("Hello, Nori!");
189+
/// let mut body: Body = req.take_body();
190+
///
191+
/// let mut string = String::new();
192+
/// body.read_to_string(&mut string).await?;
193+
/// assert_eq!(&string, "Hello, Nori!");
194+
///
195+
/// # let mut string = String::new();
196+
/// # req.read_to_string(&mut string).await?;
197+
/// # assert_eq!(&string, "");
198+
/// #
199+
/// # Ok(()) }) }
200+
/// ```
201+
pub fn take_body(&mut self) -> Body {
202+
self.replace_body(Body::empty())
203+
}
204+
114205
/// Get an HTTP header.
115206
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
116207
self.headers.get(name)

src/response.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use async_std::io::{self, BufRead, Read};
22

3+
use std::mem;
34
use std::pin::Pin;
45
use std::task::{Context, Poll};
56

@@ -11,6 +12,19 @@ use crate::{Body, Cookie, StatusCode, Version};
1112

1213
pin_project_lite::pin_project! {
1314
/// An HTTP response.
15+
///
16+
/// # Examples
17+
///
18+
/// ```
19+
/// # fn main() -> Result<(), http_types::url::ParseError> {
20+
/// #
21+
/// use http_types::{Url, Method, Request};
22+
///
23+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
24+
/// req.set_body("Hello, Nori!");
25+
/// #
26+
/// # Ok(()) }
27+
/// ```
1428
#[derive(Debug)]
1529
pub struct Response {
1630
status: StatusCode,
@@ -74,6 +88,19 @@ impl Response {
7488
}
7589

7690
/// Set the body reader.
91+
///
92+
/// # Examples
93+
///
94+
/// ```
95+
/// # fn main() -> Result<(), http_types::url::ParseError> {
96+
/// #
97+
/// use http_types::{Url, Method, Request};
98+
///
99+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
100+
/// req.set_body("Hello, Nori!");
101+
/// #
102+
/// # Ok(()) }
103+
/// ```
77104
pub fn set_body(&mut self, body: impl Into<Body>) {
78105
self.body = body.into();
79106
if self.header(&CONTENT_TYPE).is_none() {
@@ -82,6 +109,89 @@ impl Response {
82109
}
83110
}
84111

112+
/// Replace the request body with a new body, returning the old body.
113+
///
114+
/// # Examples
115+
///
116+
/// ```
117+
/// # use async_std::io::prelude::*;
118+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
119+
/// # async_std::task::block_on(async {
120+
/// #
121+
/// use http_types::{Body, Url, Method, Request};
122+
///
123+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
124+
/// req.set_body("Hello, Nori!");
125+
///
126+
/// let mut body: Body = req.replace_body("Hello, Chashu");
127+
///
128+
/// let mut string = String::new();
129+
/// body.read_to_string(&mut string).await?;
130+
/// assert_eq!(&string, "Hello, Nori!");
131+
/// #
132+
/// # Ok(()) }) }
133+
/// ```
134+
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body {
135+
mem::replace(&mut self.body, body.into())
136+
}
137+
138+
/// Swaps the value of the body with another body, without deinitializing
139+
/// either one.
140+
///
141+
/// # Examples
142+
///
143+
/// ```
144+
/// # use async_std::io::prelude::*;
145+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
146+
/// # async_std::task::block_on(async {
147+
/// #
148+
/// use http_types::{Body, Url, Method, Request};
149+
///
150+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
151+
/// req.set_body("Hello, Nori!");
152+
///
153+
/// let mut body = "Hello, Chashu!".into();
154+
/// req.swap_body(&mut body);
155+
///
156+
/// let mut string = String::new();
157+
/// body.read_to_string(&mut string).await?;
158+
/// assert_eq!(&string, "Hello, Nori!");
159+
/// #
160+
/// # Ok(()) }) }
161+
/// ```
162+
pub fn swap_body(&mut self, body: &mut Body) {
163+
mem::swap(&mut self.body, body);
164+
}
165+
166+
/// Take the request body, replacing it with an empty body.
167+
///
168+
/// # Examples
169+
///
170+
/// ```
171+
/// # use async_std::io::prelude::*;
172+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
173+
/// # async_std::task::block_on(async {
174+
/// #
175+
/// use http_types::{Body, Url, Method, Request};
176+
///
177+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
178+
/// req.set_body("Hello, Nori!");
179+
/// let mut body: Body = req.take_body();
180+
///
181+
/// let mut string = String::new();
182+
/// body.read_to_string(&mut string).await?;
183+
/// assert_eq!(&string, "Hello, Nori!");
184+
///
185+
/// # let mut string = String::new();
186+
/// # req.read_to_string(&mut string).await?;
187+
/// # assert_eq!(&string, "");
188+
/// #
189+
/// # Ok(()) }) }
190+
/// ```
191+
pub fn take_body(&mut self) -> Body {
192+
self.replace_body(Body::empty())
193+
}
194+
85195
/// Set the response MIME.
86196
pub fn set_content_type(&mut self, mime: Mime) -> Option<Vec<HeaderValue>> {
87197
let value: HeaderValue = mime.into();

0 commit comments

Comments
 (0)