micro_http: drop pending requests in case of error
In case of errors in `try_read`, we now drop all requests that
we have not yet yielded to the user and mark the connection as
being closed.
@@ -318,12 +318,13 @@ impl<T: Read + Write> HttpConnection<T> {
}
let mut response_fully_written = false;
+ let mut connection_closed = false;
if let Some(response_buffer_vec) = self.response_buffer.as_mut() {
let bytes_to_be_written = response_buffer_vec.len();
match self.stream.write(response_buffer_vec.as_slice()) {
- Ok(0) => {
- return Err(ConnectionError::ConnectionClosed);
+ Ok(0) | Err(_) => {
+ connection_closed = true;
}
Ok(bytes_written) => {
if bytes_written != bytes_to_be_written {
@@ -332,19 +333,24 @@ impl<T: Read + Write> HttpConnection<T> {
response_fully_written = true;
}
}
- Err(io_error) => {
- return Err(ConnectionError::StreamError(io_error));
- }
}
}
- if response_fully_written {
+ if connection_closed {
+ self.clear_write_buffer();
+ return Err(ConnectionError::ConnectionClosed);
+ } else if response_fully_written {
self.response_buffer.take();
}
Ok(())
}
+ fn clear_write_buffer(&mut self) {
+ self.response_queue.clear();
+ self.response_buffer.take();
+ }
+
/// Send a response back to the source of a request.
pub fn enqueue_response(&mut self, response: Response) {
self.response_queue.push_back(response);
