|
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,8 +48,19 @@ 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 |
| -pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> { |
| 63 | +async fn encode(req: Request) -> Result<Encoder, std::io::Error> { |
53 | 64 | let mut buf: Vec<u8> = vec![];
|
54 | 65 |
|
55 | 66 | let mut url = req.url().path().to_owned();
|
@@ -98,7 +109,7 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
|
98 | 109 | }
|
99 | 110 |
|
100 | 111 | /// Decode an HTTP response on the client.
|
101 |
| -pub async fn decode<R>(reader: R) -> Result<Response, Exception> |
| 112 | +async fn decode<R>(reader: R) -> Result<Response, Exception> |
102 | 113 | where
|
103 | 114 | R: Read + Unpin + Send + Sync + 'static,
|
104 | 115 | {
|
|
0 commit comments