@@ -24,14 +24,10 @@ use crate::response::ResponseUrl;
24
24
25
25
/// A Response to a submitted `Request`.
26
26
pub struct Response {
27
- status : StatusCode ,
28
- headers : HeaderMap ,
27
+ pub ( super ) res : hyper:: Response < Decoder > ,
29
28
// Boxed to save space (11 words to 1 word), and it's not accessed
30
29
// frequently internally.
31
30
url : Box < Url > ,
32
- body : Decoder ,
33
- version : Version ,
34
- extensions : http:: Extensions ,
35
31
}
36
32
37
33
impl Response {
@@ -41,46 +37,38 @@ impl Response {
41
37
accepts : Accepts ,
42
38
timeout : Option < Pin < Box < Sleep > > > ,
43
39
) -> Response {
44
- let ( parts, body) = res. into_parts ( ) ;
45
- let status = parts. status ;
46
- let version = parts. version ;
47
- let extensions = parts. extensions ;
48
-
49
- let mut headers = parts. headers ;
50
- let decoder = Decoder :: detect ( & mut headers, Body :: response ( body, timeout) , accepts) ;
40
+ let ( mut parts, body) = res. into_parts ( ) ;
41
+ let decoder = Decoder :: detect ( & mut parts. headers , Body :: response ( body, timeout) , accepts) ;
42
+ let res = hyper:: Response :: from_parts ( parts, decoder) ;
51
43
52
44
Response {
53
- status,
54
- headers,
45
+ res,
55
46
url : Box :: new ( url) ,
56
- body : decoder,
57
- version,
58
- extensions,
59
47
}
60
48
}
61
49
62
50
/// Get the `StatusCode` of this `Response`.
63
51
#[ inline]
64
52
pub fn status ( & self ) -> StatusCode {
65
- self . status
53
+ self . res . status ( )
66
54
}
67
55
68
56
/// Get the HTTP `Version` of this `Response`.
69
57
#[ inline]
70
58
pub fn version ( & self ) -> Version {
71
- self . version
59
+ self . res . version ( )
72
60
}
73
61
74
62
/// Get the `Headers` of this `Response`.
75
63
#[ inline]
76
64
pub fn headers ( & self ) -> & HeaderMap {
77
- & self . headers
65
+ self . res . headers ( )
78
66
}
79
67
80
68
/// Get a mutable reference to the `Headers` of this `Response`.
81
69
#[ inline]
82
70
pub fn headers_mut ( & mut self ) -> & mut HeaderMap {
83
- & mut self . headers
71
+ self . res . headers_mut ( )
84
72
}
85
73
86
74
/// Get the content-length of this response, if known.
@@ -93,7 +81,7 @@ impl Response {
93
81
pub fn content_length ( & self ) -> Option < u64 > {
94
82
use hyper:: body:: HttpBody ;
95
83
96
- HttpBody :: size_hint ( & self . body ) . exact ( )
84
+ HttpBody :: size_hint ( self . res . body ( ) ) . exact ( )
97
85
}
98
86
99
87
/// Retrieve the cookies contained in the response.
@@ -106,7 +94,7 @@ impl Response {
106
94
#[ cfg( feature = "cookies" ) ]
107
95
#[ cfg_attr( docsrs, doc( cfg( feature = "cookies" ) ) ) ]
108
96
pub fn cookies < ' a > ( & ' a self ) -> impl Iterator < Item = cookie:: Cookie < ' a > > + ' a {
109
- cookie:: extract_response_cookies ( & self . headers ) . filter_map ( Result :: ok)
97
+ cookie:: extract_response_cookies ( self . res . headers ( ) ) . filter_map ( Result :: ok)
110
98
}
111
99
112
100
/// Get the final `Url` of this `Response`.
@@ -117,19 +105,20 @@ impl Response {
117
105
118
106
/// Get the remote address used to get this `Response`.
119
107
pub fn remote_addr ( & self ) -> Option < SocketAddr > {
120
- self . extensions
108
+ self . res
109
+ . extensions ( )
121
110
. get :: < HttpInfo > ( )
122
111
. map ( |info| info. remote_addr ( ) )
123
112
}
124
113
125
114
/// Returns a reference to the associated extensions.
126
115
pub fn extensions ( & self ) -> & http:: Extensions {
127
- & self . extensions
116
+ self . res . extensions ( )
128
117
}
129
118
130
119
/// Returns a mutable reference to the associated extensions.
131
120
pub fn extensions_mut ( & mut self ) -> & mut http:: Extensions {
132
- & mut self . extensions
121
+ self . res . extensions_mut ( )
133
122
}
134
123
135
124
// body methods
@@ -183,7 +172,7 @@ impl Response {
183
172
/// ```
184
173
pub async fn text_with_charset ( self , default_encoding : & str ) -> crate :: Result < String > {
185
174
let content_type = self
186
- . headers
175
+ . headers ( )
187
176
. get ( crate :: header:: CONTENT_TYPE )
188
177
. and_then ( |value| value. to_str ( ) . ok ( ) )
189
178
. and_then ( |value| value. parse :: < Mime > ( ) . ok ( ) ) ;
@@ -271,7 +260,7 @@ impl Response {
271
260
/// # }
272
261
/// ```
273
262
pub async fn bytes ( self ) -> crate :: Result < Bytes > {
274
- hyper:: body:: to_bytes ( self . body ) . await
263
+ hyper:: body:: to_bytes ( self . res . into_body ( ) ) . await
275
264
}
276
265
277
266
/// Stream a chunk of the response body.
@@ -291,7 +280,7 @@ impl Response {
291
280
/// # }
292
281
/// ```
293
282
pub async fn chunk ( & mut self ) -> crate :: Result < Option < Bytes > > {
294
- if let Some ( item) = self . body . next ( ) . await {
283
+ if let Some ( item) = self . res . body_mut ( ) . next ( ) . await {
295
284
Ok ( Some ( item?) )
296
285
} else {
297
286
Ok ( None )
@@ -323,7 +312,7 @@ impl Response {
323
312
#[ cfg( feature = "stream" ) ]
324
313
#[ cfg_attr( docsrs, doc( cfg( feature = "stream" ) ) ) ]
325
314
pub fn bytes_stream ( self ) -> impl futures_core:: Stream < Item = crate :: Result < Bytes > > {
326
- self . body
315
+ self . res . into_body ( )
327
316
}
328
317
329
318
// util methods
@@ -350,8 +339,9 @@ impl Response {
350
339
/// # fn main() {}
351
340
/// ```
352
341
pub fn error_for_status ( self ) -> crate :: Result < Self > {
353
- if self . status . is_client_error ( ) || self . status . is_server_error ( ) {
354
- Err ( crate :: error:: status_code ( * self . url , self . status ) )
342
+ let status = self . status ( ) ;
343
+ if status. is_client_error ( ) || status. is_server_error ( ) {
344
+ Err ( crate :: error:: status_code ( * self . url , status) )
355
345
} else {
356
346
Ok ( self )
357
347
}
@@ -379,8 +369,9 @@ impl Response {
379
369
/// # fn main() {}
380
370
/// ```
381
371
pub fn error_for_status_ref ( & self ) -> crate :: Result < & Self > {
382
- if self . status . is_client_error ( ) || self . status . is_server_error ( ) {
383
- Err ( crate :: error:: status_code ( * self . url . clone ( ) , self . status ) )
372
+ let status = self . status ( ) ;
373
+ if status. is_client_error ( ) || status. is_server_error ( ) {
374
+ Err ( crate :: error:: status_code ( * self . url . clone ( ) , status) )
384
375
} else {
385
376
Ok ( self )
386
377
}
@@ -395,7 +386,7 @@ impl Response {
395
386
// This method is just used by the blocking API.
396
387
#[ cfg( feature = "blocking" ) ]
397
388
pub ( crate ) fn body_mut ( & mut self ) -> & mut Decoder {
398
- & mut self . body
389
+ self . res . body_mut ( )
399
390
}
400
391
}
401
392
@@ -413,27 +404,24 @@ impl<T: Into<Body>> From<http::Response<T>> for Response {
413
404
fn from ( r : http:: Response < T > ) -> Response {
414
405
let ( mut parts, body) = r. into_parts ( ) ;
415
406
let body = body. into ( ) ;
416
- let body = Decoder :: detect ( & mut parts. headers , body, Accepts :: none ( ) ) ;
407
+ let decoder = Decoder :: detect ( & mut parts. headers , body, Accepts :: none ( ) ) ;
417
408
let url = parts
418
409
. extensions
419
410
. remove :: < ResponseUrl > ( )
420
411
. unwrap_or_else ( || ResponseUrl ( Url :: parse ( "http://no.url.provided.local" ) . unwrap ( ) ) ) ;
421
412
let url = url. 0 ;
413
+ let res = hyper:: Response :: from_parts ( parts, decoder) ;
422
414
Response {
423
- status : parts. status ,
424
- headers : parts. headers ,
415
+ res,
425
416
url : Box :: new ( url) ,
426
- body,
427
- version : parts. version ,
428
- extensions : parts. extensions ,
429
417
}
430
418
}
431
419
}
432
420
433
421
/// A `Response` can be piped as the `Body` of another request.
434
422
impl From < Response > for Body {
435
423
fn from ( r : Response ) -> Body {
436
- Body :: stream ( r. body )
424
+ Body :: stream ( r. res . into_body ( ) )
437
425
}
438
426
}
439
427
0 commit comments