1
1
use std:: convert:: TryFrom ;
2
2
use std:: fmt;
3
+ use std:: time:: Duration ;
3
4
4
5
use bytes:: Bytes ;
5
6
use http:: { request:: Parts , Method , Request as HttpRequest } ;
@@ -18,6 +19,7 @@ pub struct Request {
18
19
url : Url ,
19
20
headers : HeaderMap ,
20
21
body : Option < Body > ,
22
+ timeout : Option < Duration > ,
21
23
pub ( super ) cors : bool ,
22
24
pub ( super ) credentials : Option < RequestCredentials > ,
23
25
}
@@ -37,6 +39,7 @@ impl Request {
37
39
url,
38
40
headers : HeaderMap :: new ( ) ,
39
41
body : None ,
42
+ timeout : None ,
40
43
cors : true ,
41
44
credentials : None ,
42
45
}
@@ -90,6 +93,18 @@ impl Request {
90
93
& mut self . body
91
94
}
92
95
96
+ /// Get the timeout.
97
+ #[ inline]
98
+ pub fn timeout ( & self ) -> Option < & Duration > {
99
+ self . timeout . as_ref ( )
100
+ }
101
+
102
+ /// Get a mutable reference to the timeout.
103
+ #[ inline]
104
+ pub fn timeout_mut ( & mut self ) -> & mut Option < Duration > {
105
+ & mut self . timeout
106
+ }
107
+
93
108
/// Attempts to clone the `Request`.
94
109
///
95
110
/// None is returned if a body is which can not be cloned.
@@ -104,6 +119,7 @@ impl Request {
104
119
url : self . url . clone ( ) ,
105
120
headers : self . headers . clone ( ) ,
106
121
body,
122
+ timeout : self . timeout . clone ( ) ,
107
123
cors : self . cors ,
108
124
credentials : self . credentials ,
109
125
} )
@@ -233,6 +249,14 @@ impl RequestBuilder {
233
249
self
234
250
}
235
251
252
+ /// Enables a request timeout.
253
+ pub fn timeout ( mut self , timeout : Duration ) -> RequestBuilder {
254
+ if let Ok ( ref mut req) = self . request {
255
+ * req. timeout_mut ( ) = Some ( timeout) ;
256
+ }
257
+ self
258
+ }
259
+
236
260
/// TODO
237
261
#[ cfg( feature = "multipart" ) ]
238
262
#[ cfg_attr( docsrs, doc( cfg( feature = "multipart" ) ) ) ]
@@ -449,6 +473,7 @@ where
449
473
url,
450
474
headers,
451
475
body : Some ( body. into ( ) ) ,
476
+ timeout : None ,
452
477
cors : true ,
453
478
credentials : None ,
454
479
} )
0 commit comments