File tree Expand file tree Collapse file tree 2 files changed +13
-6
lines changed Expand file tree Collapse file tree 2 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -20,11 +20,7 @@ fn main() -> Result<(), Error> {
20
20
println ! ( "making request {}/2" , i + 1 ) ;
21
21
let url = Url :: parse ( & format ! ( "http://{}/foo" , peer_addr) ) . unwrap ( ) ;
22
22
let req = Request :: new ( Method :: Get , dbg ! ( url) ) ;
23
- let mut req = client:: encode ( req) . await ?;
24
- io:: copy ( & mut req, & mut stream. clone ( ) ) . await ?;
25
-
26
- // read the response
27
- let res = client:: decode ( stream. clone ( ) ) . await ?;
23
+ let res = client:: connect ( stream. clone ( ) , req) . await ?;
28
24
println ! ( "{:?}" , res) ;
29
25
}
30
26
Ok ( ( ) )
Original file line number Diff line number Diff line change 1
1
//! Process HTTP connections on the client.
2
2
3
- use async_std:: io:: { self , BufReader , Read } ;
3
+ use async_std:: io:: { self , BufReader , Read , Write } ;
4
4
use async_std:: prelude:: * ;
5
5
use async_std:: task:: { Context , Poll } ;
6
6
use futures_core:: ready;
@@ -48,6 +48,17 @@ impl Encoder {
48
48
}
49
49
}
50
50
51
+ /// Send an HTTP request over a stream.
52
+ pub async fn connect < RW > ( stream : RW , req : Request ) -> Result < Response , std:: io:: Error >
53
+ where
54
+ RW : Read + Write + Clone + Send + Sync + Unpin + ' static ,
55
+ {
56
+ let mut req = encode ( req) . await ?;
57
+ io:: copy ( & mut req, & mut stream. clone ( ) ) . await ?;
58
+ let res = decode ( stream. clone ( ) ) . await . unwrap ( ) ; // todo: convert to http_types::Error
59
+ Ok ( res)
60
+ }
61
+
51
62
/// Encode an HTTP request on the client.
52
63
pub async fn encode ( req : Request ) -> Result < Encoder , Error > {
53
64
let mut buf: Vec < u8 > = vec ! [ ] ;
You can’t perform that action at this time.
0 commit comments