Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chunking error when content length is not provided by the backend #1058

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use time::Duration;

use crate::{protocol::http::parser::Method, SessionMetrics};

#[derive(Debug)]
pub struct LogContext<'a> {
pub request_id: Ulid,
pub cluster_id: Option<&'a str>,
Expand Down Expand Up @@ -92,6 +93,7 @@ impl fmt::Display for LogDuration {
}
}

#[derive(Debug)]
pub enum Endpoint<'a> {
Http {
method: Option<&'a Method>,
Expand Down Expand Up @@ -127,6 +129,7 @@ impl fmt::Display for Endpoint<'_> {
}
}

#[derive(Debug)]
pub struct RequestRecord<'a> {
pub error: Option<&'a str>,
pub context: LogContext<'a>,
Expand Down
39 changes: 28 additions & 11 deletions lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
self.readable_parse(metrics)
}

pub fn readable_parse(&mut self, _metrics: &mut SessionMetrics) -> StateResult {
pub fn readable_parse(&mut self, metrics: &mut SessionMetrics) -> StateResult {
trace!("==============readable_parse");
let was_initial = self.request_stream.is_initial();
let was_not_proxying = !self.request_stream.is_main_phase();
Expand Down Expand Up @@ -399,6 +399,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
}
);
if self.response_stream.consumed {
self.log_request_error(metrics, "Parsing error on the request");
return StateResult::CloseSession;
} else {
self.set_answer(DefaultAnswerStatus::Answer400, None);
Expand Down Expand Up @@ -926,24 +927,25 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
}

fn writable_default_answer(&mut self, metrics: &mut SessionMetrics) -> StateResult {
let res = match self.status {
trace!("==============writable_default_answer");
let socket_result = match self.status {
SessionStatus::DefaultAnswer(status, ref buf, mut index) => {
let len = buf.len();

let mut sz = 0usize;
let mut res = SocketResult::Continue;
while res == SocketResult::Continue && index < len {
let mut socket_result = SocketResult::Continue;
while socket_result == SocketResult::Continue && index < len {
let (current_sz, current_res) =
self.frontend_socket.socket_write(&buf[index..]);
res = current_res;
socket_result = current_res;
sz += current_sz;
index += current_sz;
}

count!("bytes_out", sz as i64);
metrics.bout += sz;

if res != SocketResult::Continue {
if socket_result != SocketResult::Continue {
self.frontend_readiness.event.remove(Ready::WRITABLE);
}

Expand All @@ -955,12 +957,12 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
return StateResult::CloseSession;
}

res
socket_result
}
_ => return StateResult::CloseSession,
};

if res == SocketResult::Error {
if socket_result == SocketResult::Error {
self.frontend_socket.write_error();
self.log_request_error(
metrics,
Expand Down Expand Up @@ -1454,7 +1456,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
}
}

pub fn backend_hup(&mut self) -> StateResult {
pub fn backend_hup(&mut self, metrics: &mut SessionMetrics) -> StateResult {
// there might still data we can read on the socket
if self.backend_readiness.event.is_readable()
&& self.backend_readiness.interest.is_readable()
Expand All @@ -1477,7 +1479,22 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
"PROXY session {:?}, backend closed before session is over",
self.frontend_token
);
StateResult::CloseSession

trace!("backend hang-up, setting the parsing phase of the response stream to terminated, this also takes care of responses that lack length information.");
self.response_stream.parsing_phase = kawa::ParsingPhase::Terminated;

// check if there is anything left to write
if self.response_stream.is_completed() {
// we have to close the session now, because writable would short-cut
self.log_request_error(
metrics,
"backend hangs up, can not be sure that response is complete",
);
StateResult::CloseSession
} else {
// writable() will be called again and finish the session properly
StateResult::CloseBackend
}
}
// probably backend hup between keep alive request, change backend
(true, true) => {
Expand Down Expand Up @@ -1648,7 +1665,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
}

if backend_interest.is_hup() || backend_interest.is_error() {
let state_result = self.backend_hup();
let state_result = self.backend_hup(metrics);

trace!("backend_hup: {:?}", state_result);
match state_result {
Expand Down
Loading