hirotake111 / rust_http_parser

HTTP parser in Rust
MIT License
0 stars 0 forks source link

HTTPRequest::try_from blocks when used on TcpStream #1

Open seanakdh opened 3 weeks ago

seanakdh commented 3 weeks ago

I'm just learning, so thank you very much for your examples.

I've noticed that the implementation of try_from for the HTTPRequest blocks after generating the headers when used on a BufReader created with a TcpStream. This also happens with the code in your repo, not only my implementation.

Specifically, on the .peek() part in this code:

let body = if iter.peek().is_some() {
            Some(iter.collect())
} else {
            None
};

So far I don't understand why, I assume because it awaits further bytes since when I cancel the request it finishes up. Probably awaits an EOF, which you get when reading a file, but not with the HTTP protocol.

Is this TryFrom implementation for BufReader really feasible when there is no EOF? How else would you handle this?

Any insight would be appreciated.

hirotake111 commented 3 weeks ago

Hello, thank you for the comment. I really appreciate it 😊 Unfortunately I still don't understand why you need to make HTTP request out of TCP stream.

I think you mean HTTPResponse. If so - you're right, my implementation cannot handle TCP stream properly. The reason is that (as you mentioned) There may not be EOF in TCP stream. I have another repository, that tackled the issue (here). I also wrote about it in my blog.I hope this helps your project.

hirotake111 commented 3 weeks ago

Now I get it. Yeah from a server stand point it for sure needs to parse the request out of TCP stream. I'll think about it in my spare time 😄