in src/server.rs [107:162]
fn read(&mut self) -> Result<Vec<Request>> {
// Data came into the connection.
let mut parsed_requests = vec![];
match self.connection.try_read() {
Err(ConnectionError::ConnectionClosed) => {
// Connection timeout.
self.state = ClientConnectionState::Closed;
// We don't want to propagate this to the server and we will
// return no requests and wait for the connection to become
// safe to drop.
return Ok(vec![]);
}
Err(ConnectionError::StreamReadError(inner)) => {
// Reading from the connection failed.
// We should try to write an error message regardless.
let mut internal_error_response =
Response::new(Version::Http11, StatusCode::InternalServerError);
internal_error_response.set_body(Body::new(inner.to_string()));
self.connection.enqueue_response(internal_error_response);
}
Err(ConnectionError::ParseError(inner)) => {
// An error occurred while parsing the read bytes.
// Check if there are any valid parsed requests in the queue.
while let Some(_discarded_request) = self.connection.pop_parsed_request() {}
// Send an error response for the request that gave us the error.
let mut error_response = Response::new(Version::Http11, StatusCode::BadRequest);
error_response.set_body(Body::new(format!(
"{{ \"error\": \"{}\nAll previous unanswered requests will be dropped.\" }}",
inner.to_string()
)));
self.connection.enqueue_response(error_response);
}
Err(ConnectionError::InvalidWrite) | Err(ConnectionError::StreamWriteError(_)) => {
// This is unreachable because `HttpConnection::try_read()` cannot return this error variant.
unreachable!();
}
Ok(()) => {
while let Some(request) = self.connection.pop_parsed_request() {
// Add all valid requests to `parsed_requests`.
parsed_requests.push(request);
}
}
}
self.in_flight_response_count = self
.in_flight_response_count
.checked_add(parsed_requests.len() as u32)
.ok_or(ServerError::Overflow)?;
// If the state of the connection has changed, we need to update
// the event set in the `epoll` structure.
if self.connection.pending_write() {
self.state = ClientConnectionState::AwaitingOutgoing;
}
Ok(parsed_requests)
}