@@ -25,6 +25,7 @@ pub struct ServerHandler<M: Metadata = (), S: Middleware<M> = middleware::Noop>
25
25
rest_api : RestApi ,
26
26
health_api : Option < ( String , String ) > ,
27
27
max_request_body_size : usize ,
28
+ keep_alive : bool ,
28
29
}
29
30
30
31
impl < M : Metadata , S : Middleware < M > > ServerHandler < M , S > {
@@ -39,6 +40,7 @@ impl<M: Metadata, S: Middleware<M>> ServerHandler<M, S> {
39
40
rest_api : RestApi ,
40
41
health_api : Option < ( String , String ) > ,
41
42
max_request_body_size : usize ,
43
+ keep_alive : bool ,
42
44
) -> Self {
43
45
ServerHandler {
44
46
jsonrpc_handler,
@@ -50,6 +52,7 @@ impl<M: Metadata, S: Middleware<M>> ServerHandler<M, S> {
50
52
rest_api,
51
53
health_api,
52
54
max_request_body_size,
55
+ keep_alive,
53
56
}
54
57
}
55
58
}
@@ -89,6 +92,7 @@ impl<M: Metadata, S: Middleware<M>> Service for ServerHandler<M, S> {
89
92
cors_domains : self . cors_domains . clone ( ) ,
90
93
cors_headers : self . cors_allowed_headers . clone ( ) ,
91
94
continue_on_invalid_cors : should_continue_on_invalid_cors,
95
+ keep_alive : self . keep_alive ,
92
96
} ,
93
97
is_options : false ,
94
98
cors_max_age : self . cors_max_age ,
@@ -97,6 +101,8 @@ impl<M: Metadata, S: Middleware<M>> Service for ServerHandler<M, S> {
97
101
rest_api : self . rest_api ,
98
102
health_api : self . health_api . clone ( ) ,
99
103
max_request_body_size : self . max_request_body_size ,
104
+ // initial value, overwritten when reading client headers
105
+ keep_alive : true ,
100
106
} )
101
107
}
102
108
}
@@ -159,6 +165,7 @@ enum RpcHandlerState<M, F, G> where
159
165
cors_domains : CorsDomains ,
160
166
cors_headers : cors:: AccessControlAllowHeaders ,
161
167
continue_on_invalid_cors : bool ,
168
+ keep_alive : bool ,
162
169
} ,
163
170
ReadingBody {
164
171
body : hyper:: Body ,
@@ -210,6 +217,7 @@ pub struct RpcHandler<M: Metadata, S: Middleware<M>> {
210
217
rest_api : RestApi ,
211
218
health_api : Option < ( String , String ) > ,
212
219
max_request_body_size : usize ,
220
+ keep_alive : bool ,
213
221
}
214
222
215
223
impl < M : Metadata , S : Middleware < M > > Future for RpcHandler < M , S > {
@@ -218,10 +226,13 @@ impl<M: Metadata, S: Middleware<M>> Future for RpcHandler<M, S> {
218
226
219
227
fn poll ( & mut self ) -> Poll < Self :: Item , Self :: Error > {
220
228
let new_state = match mem:: replace ( & mut self . state , RpcHandlerState :: Done ) {
221
- RpcHandlerState :: ReadingHeaders { request, cors_domains, cors_headers, continue_on_invalid_cors, } => {
229
+ RpcHandlerState :: ReadingHeaders {
230
+ request, cors_domains, cors_headers, continue_on_invalid_cors, keep_alive,
231
+ } => {
222
232
// Read cors header
223
233
self . cors_allow_origin = utils:: cors_allow_origin ( & request, & cors_domains) ;
224
234
self . cors_allow_headers = utils:: cors_allow_headers ( & request, & cors_headers) ;
235
+ self . keep_alive = utils:: keep_alive ( & request, keep_alive) ;
225
236
self . is_options = * request. method ( ) == Method :: OPTIONS ;
226
237
// Read other headers
227
238
RpcPollState :: Ready ( self . read_headers ( request, continue_on_invalid_cors) )
@@ -288,6 +299,7 @@ impl<M: Metadata, S: Middleware<M>> Future for RpcHandler<M, S> {
288
299
self . cors_max_age ,
289
300
cors_allow_origin. into ( ) ,
290
301
cors_allow_headers. into ( ) ,
302
+ self . keep_alive ,
291
303
) ;
292
304
Ok ( Async :: Ready ( response) )
293
305
} ,
@@ -502,6 +514,7 @@ impl<M: Metadata, S: Middleware<M>> RpcHandler<M, S> {
502
514
cors_max_age : Option < u32 > ,
503
515
cors_allow_origin : Option < HeaderValue > ,
504
516
cors_allow_headers : Option < Vec < HeaderValue > > ,
517
+ keep_alive : bool ,
505
518
) {
506
519
let as_header = |m : Method | m. as_str ( ) . parse ( ) . expect ( "`Method` will always parse; qed" ) ;
507
520
let concat = |headers : & [ HeaderValue ] | {
@@ -540,6 +553,10 @@ impl<M: Metadata, S: Middleware<M>> RpcHandler<M, S> {
540
553
}
541
554
}
542
555
}
556
+
557
+ if !keep_alive {
558
+ headers. append ( header:: CONNECTION , HeaderValue :: from_static ( "close" ) ) ;
559
+ }
543
560
}
544
561
545
562
/// Returns true if the `content_type` header indicates a valid JSON
0 commit comments