Tcp Connect Write then Read

So I am trying to create a simple tcpstream connect to fetch a website but I dont know why this wont work

let mut stream = TcpStream::connect(format!("{}:80", tcpstream_url.trim())).unwrap();
stream.write(format!("{} /{} HTTP/1.1{}\r\n\r\n{}", input.get(0).unwrap().trim().to_string(), tcpstream_url_path, headers, cont).as_bytes());
let mut buffer = String::new();
stream.read_to_string(&mut buffer).unwrap();

It works until the read_to_string, I have tested with beeceptor and it works, but I just cant read the content. It does not give any error or anything, all it does is freeze on the read_to_string.

How does your code even compile with all the double quotes escaped out? (you are using \" instead of ")

oh wait yea its hard to explain but I am doing other stuff just dont pretend they exist

The read_to_string reads into a string until the connection is closed (which internally causes an EOF to be sent). I am guessing the server assumes the connection to be keep-alive. So the connection doesn't close and your read hangs.

would there be any way to read the response but not wait for the connection to close? and also not having to read a fixed amount of data, or reading just the headers and then reading the rest from the content length?

Yes this would be a solution.

Doesn't tokio's codec come into play when we want to figure which bytes sequence is a HTTP frame when its working on top of TcpStream? (Just a random question)

I am trying to do this with 0 librarys

when it's HTTP that is working on top of TCP, packets maybe fragmented, so you might not know which for which HTTP frame, one is the header and which one is the data unless you combine the fragmented segments and read them as one. How do you plan to solve it, i'm just curious, coz i don't know too

The usual idea would be to wrap the TcpStream in a BufReader. Then, you can read exact number of bytes for the header, parse the length and then read exat bytes for the body. The underlying buffer takes care of packet fragmentation and recombination.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.