@@ -13,33 +13,62 @@ mod encode;
13
13
use decode:: decode;
14
14
use encode:: Encoder ;
15
15
16
+ /// Configure the server.
17
+ #[ derive( Debug , Clone ) ]
18
+ pub struct ServerOptions {
19
+ /// Timeout to handle headers. Defaults to 60s.
20
+ headers_timeout : Option < Duration > ,
21
+ }
22
+
23
+ impl Default for ServerOptions {
24
+ fn default ( ) -> Self {
25
+ Self {
26
+ headers_timeout : Some ( Duration :: from_secs ( 60 ) ) ,
27
+ }
28
+ }
29
+ }
30
+
16
31
/// Accept a new incoming HTTP/1.1 connection.
17
32
///
18
33
/// Supports `KeepAlive` requests by default.
19
- pub async fn accept < RW , F , Fut > ( addr : & str , mut io : RW , endpoint : F ) -> http_types:: Result < ( ) >
34
+ pub async fn accept < RW , F , Fut > ( addr : & str , io : RW , endpoint : F ) -> http_types:: Result < ( ) >
20
35
where
21
36
RW : Read + Write + Clone + Send + Sync + Unpin + ' static ,
22
37
F : Fn ( Request ) -> Fut ,
23
38
Fut : Future < Output = http_types:: Result < Response > > ,
24
39
{
25
- // TODO: make these values configurable
26
- let timeout_duration = Duration :: from_secs ( 10 ) ;
27
- const MAX_REQUESTS : usize = 200 ;
28
- let mut num_requests = 0 ;
40
+ accept_with_opts ( addr, io, endpoint, Default :: default ( ) ) . await
41
+ }
29
42
43
+ /// Accept a new incoming HTTP/1.1 connection.
44
+ ///
45
+ /// Supports `KeepAlive` requests by default.
46
+ pub async fn accept_with_opts < RW , F , Fut > (
47
+ addr : & str ,
48
+ mut io : RW ,
49
+ endpoint : F ,
50
+ opts : ServerOptions ,
51
+ ) -> http_types:: Result < ( ) >
52
+ where
53
+ RW : Read + Write + Clone + Send + Sync + Unpin + ' static ,
54
+ F : Fn ( Request ) -> Fut ,
55
+ Fut : Future < Output = http_types:: Result < Response > > ,
56
+ {
30
57
loop {
31
- // Stop parsing requests if we exceed the threshold.
32
- match num_requests {
33
- MAX_REQUESTS => return Ok ( ( ) ) ,
34
- _ => num_requests += 1 ,
35
- } ;
58
+ // Decode a new request, timing out if this takes longer than the timeout duration.
59
+ let fut = decode ( addr, io. clone ( ) ) ;
36
60
37
- // Decode a new request, timing out if this takes longer than the
38
- // timeout duration.
39
- let req = match timeout ( timeout_duration, decode ( addr, io. clone ( ) ) ) . await {
40
- Ok ( Ok ( Some ( r) ) ) => r,
41
- Ok ( Ok ( None ) ) | Err ( TimeoutError { .. } ) => break , /* EOF or timeout */
42
- Ok ( Err ( e) ) => return Err ( e) ,
61
+ let req = if let Some ( timeout_duration) = opts. headers_timeout {
62
+ match timeout ( timeout_duration, fut) . await {
63
+ Ok ( Ok ( Some ( r) ) ) => r,
64
+ Ok ( Ok ( None ) ) | Err ( TimeoutError { .. } ) => break , /* EOF or timeout */
65
+ Ok ( Err ( e) ) => return Err ( e) ,
66
+ }
67
+ } else {
68
+ match fut. await ? {
69
+ Some ( r) => r,
70
+ None => break , /* EOF */
71
+ }
43
72
} ;
44
73
45
74
// Pass the request to the endpoint and encode the response.
0 commit comments