I’m writing an HTTP client library for a
no_std environment (can’t use
hyper there). I have one question about how to handle incoming data. Let’s say the user of my lib makes a request and we get N bytes of body in the response (assume that response headers are already parsed). The user does not read the body but makes a second request. At this point we have those N stray bytes sitting on the TCP connection. For the second request, I would want to clear those bytes so they don’t interfere with the second request’s response. One way to do so is to drain them before you write the second request to the connection by reading until EOF. But that may not be a good idea because the body in some cases could be a few GBs. If you start draining, you’ll end pulling all those GBs of data and sitting there for a long time. The other technique I can think of is drain a few KB. If that empties the socket then proceed with the second request. If not, then close the connection and start a new connection.
I have never written an HTTP implementation. So just wanted to check with the experts here if this is the right way or if there’s a better technique.
@seanmonstar Can probably provide a quick answer to this.