@@ -67,7 +67,7 @@ impl HttpClient {
67
67
}
68
68
69
69
/// Sends a `GET` request for a resource identified by `uri` at the `host`.
70
- async fn get < F > ( mut self , uri : & str , host : & str ) -> std:: io:: Result < F >
70
+ async fn get < F > ( & mut self , uri : & str , host : & str ) -> std:: io:: Result < F >
71
71
where F : TryFrom < Vec < u8 > , Error = std:: io:: Error > {
72
72
let request = format ! (
73
73
"GET {} HTTP/1.1\r \n \
@@ -88,7 +88,7 @@ impl HttpClient {
88
88
///
89
89
/// The request body consists of the provided JSON `content`. Returns the response body in `F`
90
90
/// format.
91
- async fn post < F > ( mut self , uri : & str , host : & str , auth : & str , content : serde_json:: Value ) -> std:: io:: Result < F >
91
+ async fn post < F > ( & mut self , uri : & str , host : & str , auth : & str , content : serde_json:: Value ) -> std:: io:: Result < F >
92
92
where F : TryFrom < Vec < u8 > , Error = std:: io:: Error > {
93
93
let content = content. to_string ( ) ;
94
94
let request = format ! (
@@ -110,8 +110,14 @@ impl HttpClient {
110
110
}
111
111
112
112
/// Reads an HTTP response message.
113
- async fn read_response ( self ) -> std:: io:: Result < Vec < u8 > > {
114
- let limited_stream = self . stream . take ( MAX_HTTP_MESSAGE_HEADER_SIZE as u64 ) ;
113
+ async fn read_response ( & mut self ) -> std:: io:: Result < Vec < u8 > > {
114
+ #[ cfg( feature = "tokio" ) ]
115
+ let stream = self . stream . split ( ) . 0 ;
116
+ #[ cfg( not( feature = "tokio" ) ) ]
117
+ let stream = std:: io:: Read :: by_ref ( & mut self . stream ) ;
118
+
119
+ let limited_stream = stream. take ( MAX_HTTP_MESSAGE_HEADER_SIZE as u64 ) ;
120
+
115
121
#[ cfg( feature = "tokio" ) ]
116
122
let mut reader = tokio:: io:: BufReader :: new ( limited_stream) ;
117
123
#[ cfg( not( feature = "tokio" ) ) ]
@@ -306,7 +312,7 @@ impl RESTClient {
306
312
let host = format ! ( "{}:{}" , self . endpoint. host( ) , self . endpoint. port( ) ) ;
307
313
let uri = format ! ( "{}/{}" , self . endpoint. path( ) . trim_end_matches( "/" ) , resource_path) ;
308
314
309
- let client = HttpClient :: connect ( & self . endpoint ) ?;
315
+ let mut client = HttpClient :: connect ( & self . endpoint ) ?;
310
316
client. get :: < F > ( & uri, & host) . await ?. try_into ( )
311
317
}
312
318
}
@@ -338,7 +344,7 @@ impl RPCClient {
338
344
"id" : & self . id. fetch_add( 1 , Ordering :: AcqRel ) . to_string( )
339
345
} ) ;
340
346
341
- let client = HttpClient :: connect ( & self . endpoint ) ?;
347
+ let mut client = HttpClient :: connect ( & self . endpoint ) ?;
342
348
let mut response = client. post :: < JsonResponse > ( & uri, & host, & self . basic_auth , content) . await ?. 0 ;
343
349
if !response. is_object ( ) {
344
350
return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , "expected JSON object" ) ) ;
@@ -629,6 +635,7 @@ mod tests {
629
635
fn responding_with ( response : String ) -> Self {
630
636
let listener = std:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
631
637
let address = listener. local_addr ( ) . unwrap ( ) ;
638
+
632
639
let _handler = std:: thread:: spawn ( move || {
633
640
let ( mut stream, _) = listener. accept ( ) . unwrap ( ) ;
634
641
let lines_read = std:: io:: BufReader :: new ( & stream)
@@ -740,7 +747,7 @@ mod tests {
740
747
async fn read_empty_message ( ) {
741
748
let server = HttpServer :: responding_with ( "" . to_string ( ) ) ;
742
749
743
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
750
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
744
751
drop ( server) ;
745
752
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
746
753
Err ( e) => {
@@ -755,7 +762,7 @@ mod tests {
755
762
async fn read_incomplete_message ( ) {
756
763
let server = HttpServer :: responding_with ( "HTTP/1.1 200 OK" . to_string ( ) ) ;
757
764
758
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
765
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
759
766
drop ( server) ;
760
767
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
761
768
Err ( e) => {
@@ -774,7 +781,7 @@ mod tests {
774
781
\r \n ", "Z" . repeat( MAX_HTTP_MESSAGE_HEADER_SIZE ) ) ;
775
782
let server = HttpServer :: responding_with ( response) ;
776
783
777
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
784
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
778
785
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
779
786
Err ( e) => {
780
787
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
@@ -789,7 +796,7 @@ mod tests {
789
796
let body = "Z" . repeat ( MAX_HTTP_MESSAGE_BODY_SIZE + 1 ) ;
790
797
let server = HttpServer :: responding_with_ok :: < String > ( MessageBody :: Content ( body) ) ;
791
798
792
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
799
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
793
800
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
794
801
Err ( e) => {
795
802
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
@@ -808,7 +815,7 @@ mod tests {
808
815
foobar") ;
809
816
let server = HttpServer :: responding_with ( response) ;
810
817
811
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
818
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
812
819
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
813
820
Err ( e) => {
814
821
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: InvalidInput ) ;
@@ -822,7 +829,7 @@ mod tests {
822
829
async fn read_empty_message_body ( ) {
823
830
let server = HttpServer :: responding_with_ok :: < String > ( MessageBody :: Empty ) ;
824
831
825
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
832
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
826
833
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
827
834
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
828
835
Ok ( bytes) => assert_eq ! ( bytes. 0 , Vec :: <u8 >:: new( ) ) ,
@@ -835,7 +842,7 @@ mod tests {
835
842
let content = MessageBody :: Content ( body. clone ( ) ) ;
836
843
let server = HttpServer :: responding_with_ok :: < String > ( content) ;
837
844
838
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
845
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
839
846
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
840
847
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
841
848
Ok ( bytes) => assert_eq ! ( bytes. 0 , body. as_bytes( ) ) ,
@@ -848,7 +855,7 @@ mod tests {
848
855
let chunked_content = MessageBody :: ChunkedContent ( body. clone ( ) ) ;
849
856
let server = HttpServer :: responding_with_ok :: < String > ( chunked_content) ;
850
857
851
- let client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
858
+ let mut client = HttpClient :: connect ( & server. endpoint ( ) ) . unwrap ( ) ;
852
859
match client. get :: < BinaryResponse > ( "/foo" , "foo.com" ) . await {
853
860
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
854
861
Ok ( bytes) => assert_eq ! ( bytes. 0 , body. as_bytes( ) ) ,
0 commit comments