Tokio unable to read data

Hello everyone,

today i started taking a look into tokio-rs/bytes so i wrote a small program to start with,

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut buf = [0; 1024];
    let req = b"GET /Chunked HTTP/1.1\r\n";
    let mut tcp_stream = TcpStream::connect("anglesharp.azurewebsites.net:80").await?;

    tcp_stream.write_all(req).await?;

    timeout(Duration::from_secs(5), tcp_stream.read(&mut buf)).await??;

    println!("{:?}", &buf[..]);

    Ok(())
}

i know above that the request i am sending is not correct (as it should terminate with \r\n) and when i do correct it to GET /Chunked HTTP/1.1\r\n\r\n read works as expected.

when i send the incorrect request, i expect to receive a 400 http client error, and i would like to be able to read that error, i did inspect wireshark and indeed i saw the response, my question is why tokio hang on tcp_stream.read(&mut buf) when provided when incorrect http request?

This is expected, because it's a correct, but incomplete request. The receiving side is waiting for you to finish sending.

@kornel what i don't understand is why wireshark shows 400 bad request, when i do follow http stream

"Bad request" response may be sent after your program ends, which closes the connection. Only then it makes it clear to the receiving side that the rest of the request won't come.

1 Like

@kornel thank you, i almost went crazy with it :crazy_face: :crazy_face: