Skip to content

Commit e4d2911

Browse files
add workaround for sync issues
1 parent b2477f7 commit e4d2911

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/chunked.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::pin::Pin;
44
use std::str::FromStr;
55
use std::task::{Context, Poll};
66

7-
use async_std::io::{self, BufRead, Read};
7+
use async_std::io::{self, Read};
88
use async_std::sync::{channel, Arc, Receiver, Sender};
99
use byte_pool::{Block, BytePool};
1010
use http_types::headers::{HeaderName, HeaderValue};
@@ -19,7 +19,7 @@ lazy_static::lazy_static! {
1919

2020
/// Decodes a chunked body according to
2121
/// https://tools.ietf.org/html/rfc7230#section-4.1
22-
pub struct ChunkedDecoder<R: BufRead> {
22+
pub struct ChunkedDecoder<R: Read> {
2323
/// The underlying stream
2424
inner: R,
2525
/// Buffer for the already read, but not yet parsed data.
@@ -37,7 +37,7 @@ pub struct ChunkedDecoder<R: BufRead> {
3737
trailer_receiver: Receiver<Vec<(HeaderName, HeaderValue)>>,
3838
}
3939

40-
impl<R: BufRead> ChunkedDecoder<R> {
40+
impl<R: Read> ChunkedDecoder<R> {
4141
pub fn new(inner: R) -> Self {
4242
let (sender, receiver) = channel(1);
4343

@@ -152,7 +152,7 @@ fn decode_trailer(buffer: Block<'static>, pos: &Position) -> io::Result<DecodeRe
152152
}
153153
}
154154

155-
impl<R: BufRead + Unpin + Send + 'static> ChunkedDecoder<R> {
155+
impl<R: Read + Unpin> ChunkedDecoder<R> {
156156
fn poll_read_chunk(
157157
&mut self,
158158
cx: &mut Context<'_>,
@@ -277,7 +277,7 @@ impl<R: BufRead + Unpin + Send + 'static> ChunkedDecoder<R> {
277277
}
278278
}
279279

280-
impl<R: BufRead + Unpin + Send + 'static> Read for ChunkedDecoder<R> {
280+
impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
281281
#[allow(missing_doc_code_examples)]
282282
fn poll_read(
283283
mut self: Pin<&mut Self>,

src/client.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
5959
url.push_str(query);
6060
}
6161

62-
write!(&mut buf, "{} {} HTTP/1.1\r\n", req.method(), url).await?;
62+
let val = format!("{} {} HTTP/1.1\r\n", req.method(), url);
63+
buf.write_all(val.as_bytes()).await?;
6364

6465
// If the body isn't streaming, we can set the content-length ahead of time. Else we need to
6566
// send all items in chunks.
6667
if let Some(len) = req.len() {
67-
write!(&mut buf, "Content-Length: {}\r\n", len).await?;
68+
let val = format!("Content-Length: {}\r\n", len);
69+
buf.write_all(val.as_bytes()).await?;
6870
} else {
6971
// write!(&mut buf, "Transfer-Encoding: chunked\r\n")?;
7072
panic!("chunked encoding is not implemented yet");
@@ -73,18 +75,20 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
7375
}
7476
for (header, values) in req.iter() {
7577
for value in values.iter() {
76-
write!(&mut buf, "{}: {}\r\n", header, value).await?;
78+
let val = format!("{}: {}\r\n", header, value);
79+
buf.write_all(val.as_bytes()).await?;
7780
}
7881
}
7982

80-
write!(&mut buf, "\r\n").await?;
83+
buf.write_all(b"\r\n").await?;
84+
8185
Ok(Encoder::new(buf, req))
8286
}
8387

8488
/// Decode an HTTP respons on the client.
8589
pub async fn decode<R>(reader: R) -> Result<Response, Exception>
8690
where
87-
R: Read + Unpin + Send + 'static,
91+
R: Read + Unpin + Send + Sync + 'static,
8892
{
8993
let mut reader = BufReader::new(reader);
9094
let mut buf = Vec::new();

src/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Error for HttpError {
5050
/// Supports `KeepAlive` requests by default.
5151
pub async fn accept<RW, F, Fut>(addr: &str, mut io: RW, endpoint: F) -> Result<(), Exception>
5252
where
53-
RW: Read + Write + Clone + Send + Unpin + 'static,
53+
RW: Read + Write + Clone + Send + Sync + Unpin + 'static,
5454
F: Fn(Request) -> Fut,
5555
Fut: Future<Output = Result<Response, Exception>>,
5656
{
@@ -359,7 +359,7 @@ const HTTP_1_1_VERSION: u8 = 1;
359359
/// Decode an HTTP request on the server.
360360
async fn decode<R>(addr: &str, reader: R) -> Result<Option<Request>, Exception>
361361
where
362-
R: Read + Unpin + Send + 'static,
362+
R: Read + Unpin + Send + Sync + 'static,
363363
{
364364
let mut reader = BufReader::new(reader);
365365
let mut buf = Vec::new();

0 commit comments

Comments
 (0)