1
1
use async_std:: io:: { self , BufRead , Read } ;
2
2
3
+ use std:: mem;
3
4
use std:: pin:: Pin ;
4
5
use std:: task:: { Context , Poll } ;
5
6
@@ -11,6 +12,19 @@ use crate::{Body, Cookie, StatusCode, Version};
11
12
12
13
pin_project_lite:: pin_project! {
13
14
/// 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
+ /// ```
14
28
#[ derive( Debug ) ]
15
29
pub struct Response {
16
30
status: StatusCode ,
@@ -74,6 +88,19 @@ impl Response {
74
88
}
75
89
76
90
/// 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
+ /// ```
77
104
pub fn set_body ( & mut self , body : impl Into < Body > ) {
78
105
self . body = body. into ( ) ;
79
106
if self . header ( & CONTENT_TYPE ) . is_none ( ) {
@@ -82,6 +109,89 @@ impl Response {
82
109
}
83
110
}
84
111
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
+
85
195
/// Set the response MIME.
86
196
pub fn set_content_type ( & mut self , mime : Mime ) -> Option < Vec < HeaderValue > > {
87
197
let value: HeaderValue = mime. into ( ) ;
0 commit comments