18
18
*/
19
19
//! HTTP transport and connection components
20
20
21
- #[ cfg( all( target_arch = "wasm32" , any( feature = "native-tls" , feature = "rustls-tls" ) ) ) ]
21
+ #[ cfg( all(
22
+ target_arch = "wasm32" ,
23
+ any( feature = "native-tls" , feature = "rustls-tls" )
24
+ ) ) ]
22
25
compile_error ! ( "TLS features are not compatible with the wasm target" ) ;
23
26
24
27
#[ cfg( any( feature = "native-tls" , feature = "rustls-tls" ) ) ]
@@ -30,8 +33,8 @@ use crate::{
30
33
error:: Error ,
31
34
http:: {
32
35
headers:: {
33
- HeaderMap , HeaderName , HeaderValue , ACCEPT , AUTHORIZATION , CONTENT_TYPE ,
34
- DEFAULT_ACCEPT , DEFAULT_CONTENT_TYPE , DEFAULT_USER_AGENT , USER_AGENT ,
36
+ HeaderMap , HeaderName , HeaderValue , ACCEPT , AUTHORIZATION , CONTENT_ENCODING ,
37
+ CONTENT_TYPE , DEFAULT_ACCEPT , DEFAULT_CONTENT_TYPE , DEFAULT_USER_AGENT , USER_AGENT ,
35
38
} ,
36
39
request:: Body ,
37
40
response:: Response ,
@@ -40,6 +43,7 @@ use crate::{
40
43
} ;
41
44
use base64:: { engine:: general_purpose:: STANDARD as BASE64_STANDARD , write:: EncoderWriter , Engine } ;
42
45
use bytes:: BytesMut ;
46
+ use flate2:: { write:: GzEncoder , Compression } ;
43
47
use lazy_static:: lazy_static;
44
48
use serde:: Serialize ;
45
49
use serde_json:: Value ;
@@ -147,6 +151,7 @@ pub struct TransportBuilder {
147
151
credentials : Option < Credentials > ,
148
152
#[ cfg( any( feature = "native-tls" , feature = "rustls-tls" ) ) ]
149
153
cert_validation : Option < CertificateValidation > ,
154
+ request_body_compression : bool ,
150
155
#[ cfg( not( target_arch = "wasm32" ) ) ]
151
156
proxy : Option < Url > ,
152
157
#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -172,6 +177,7 @@ impl TransportBuilder {
172
177
credentials : None ,
173
178
#[ cfg( any( feature = "native-tls" , feature = "rustls-tls" ) ) ]
174
179
cert_validation : None ,
180
+ request_body_compression : false ,
175
181
#[ cfg( not( target_arch = "wasm32" ) ) ]
176
182
proxy : None ,
177
183
#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -215,6 +221,12 @@ impl TransportBuilder {
215
221
self
216
222
}
217
223
224
+ /// Gzip compress the body of requests, adds the `Content-Encoding: gzip` header.
225
+ pub fn request_body_compression ( mut self , enabled : bool ) -> Self {
226
+ self . request_body_compression = enabled;
227
+ self
228
+ }
229
+
218
230
/// Validation applied to the certificate provided to establish a HTTPS connection.
219
231
/// By default, full validation is applied. When using a self-signed certificate,
220
232
/// different validation can be applied.
@@ -335,6 +347,7 @@ impl TransportBuilder {
335
347
Ok ( Transport {
336
348
client,
337
349
conn_pool : self . conn_pool ,
350
+ request_body_compression : self . request_body_compression ,
338
351
credentials : self . credentials ,
339
352
send_meta : self . meta_header ,
340
353
} )
@@ -381,6 +394,7 @@ impl Connection {
381
394
pub struct Transport {
382
395
client : reqwest:: Client ,
383
396
credentials : Option < Credentials > ,
397
+ request_body_compression : bool ,
384
398
conn_pool : Arc < dyn ConnectionPool > ,
385
399
send_meta : bool ,
386
400
}
@@ -481,8 +495,7 @@ impl Transport {
481
495
headers : HeaderMap ,
482
496
query_string : Option < & Q > ,
483
497
body : Option < B > ,
484
- #[ allow( unused_variables) ]
485
- timeout : Option < Duration > ,
498
+ #[ allow( unused_variables) ] timeout : Option < Duration > ,
486
499
) -> Result < reqwest:: RequestBuilder , Error >
487
500
where
488
501
B : Body ,
@@ -552,7 +565,17 @@ impl Transport {
552
565
bytes_mut. split ( ) . freeze ( )
553
566
} ;
554
567
555
- request_builder = request_builder. body ( bytes) ;
568
+ match self . request_body_compression {
569
+ true => {
570
+ let mut encoder = GzEncoder :: new ( Vec :: new ( ) , Compression :: default ( ) ) ;
571
+ encoder. write_all ( & bytes) ?;
572
+ request_builder = request_builder. body ( encoder. finish ( ) ?) ;
573
+ request_builder = request_builder. header ( CONTENT_ENCODING , "gzip" ) ;
574
+ }
575
+ false => {
576
+ request_builder = request_builder. body ( bytes) ;
577
+ }
578
+ }
556
579
} ;
557
580
558
581
if let Some ( q) = query_string {
@@ -589,15 +612,17 @@ impl Transport {
589
612
let connection = self . conn_pool . next ( ) ;
590
613
591
614
// Build node info request
592
- let node_request = self . request_builder (
593
- & connection,
594
- Method :: Get ,
595
- "_nodes/http?filter_path=nodes.*.http" ,
596
- HeaderMap :: default ( ) ,
597
- None :: < & ( ) > ,
598
- None :: < ( ) > ,
599
- None ,
600
- ) . unwrap ( ) ;
615
+ let node_request = self
616
+ . request_builder (
617
+ & connection,
618
+ Method :: Get ,
619
+ "_nodes/http?filter_path=nodes.*.http" ,
620
+ HeaderMap :: default ( ) ,
621
+ None :: < & ( ) > ,
622
+ None :: < ( ) > ,
623
+ None ,
624
+ )
625
+ . unwrap ( ) ;
601
626
602
627
let scheme = connection. url . scheme ( ) ;
603
628
let resp = node_request. send ( ) . await . unwrap ( ) ;
0 commit comments