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> {
2020 println ! ( "making request {}/2" , i + 1 ) ;
2121 let url = Url :: parse ( & format ! ( "http://{}/foo" , peer_addr) ) . unwrap ( ) ;
2222 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 ?;
2824 println ! ( "{:?}" , res) ;
2925 }
3026 Ok ( ( ) )
Original file line number Diff line number Diff line change 11//! Process HTTP connections on the client.
22
3- use async_std:: io:: { self , BufReader , Read } ;
3+ use async_std:: io:: { self , BufReader , Read , Write } ;
44use async_std:: prelude:: * ;
55use async_std:: task:: { Context , Poll } ;
66use futures_core:: ready;
@@ -48,6 +48,17 @@ impl Encoder {
4848 }
4949}
5050
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+
5162/// Encode an HTTP request on the client.
5263pub async fn encode ( req : Request ) -> Result < Encoder , Error > {
5364 let mut buf: Vec < u8 > = vec ! [ ] ;
You can’t perform that action at this time.
0 commit comments