Skip to content
Closed
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
127 changes: 127 additions & 0 deletions pingora-core/src/protocols/http/v1/body_progress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2026 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Payload-only progress retained when a body write is cancelled or fails.

use std::{
io,
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncWrite;

/// Track accepted payload while excluding the chunk header and trailer.
pub(super) struct BodyProgress<'a, S> {
pub stream: &'a mut S,
pub sent: &'a mut usize,
pub skip: usize,
pub remaining: usize,
}

impl<S: AsyncWrite + Unpin> AsyncWrite for BodyProgress<'_, S> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
data: &[u8],
) -> Poll<io::Result<usize>> {
match Pin::new(&mut *self.stream).poll_write(cx, data) {
Poll::Ready(Ok(count)) => {
let framing = count.min(self.skip);
self.skip -= framing;
let payload = count.saturating_sub(framing).min(self.remaining);
self.remaining -= payload;
*self.sent = self.sent.saturating_add(payload);
Poll::Ready(Ok(count))
}
other => other,
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut *self.stream).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut *self.stream).poll_shutdown(cx)
}
}

#[cfg(test)]
mod tests {
use super::super::body::BodyWriter;
use super::*;
use std::time::Duration;
use tokio::io::AsyncReadExt;

#[tokio::test]
async fn cancelled_writes_preserve_payload_progress_without_chunk_framing() {
for chunked in [false, true] {
let (mut stream, mut reader) = tokio::io::duplex(8);
let mut writer = BodyWriter::new();
if chunked {
writer.init_chunked();
} else {
writer.init_content_length(32);
}
let mut sent = 0;
let mut progress = BodyProgress {
stream: &mut stream,
sent: &mut sent,
skip: if chunked { 4 } else { 0 },
remaining: 32,
};
assert!(tokio::time::timeout(
Duration::from_millis(5),
writer.write_body(&mut progress, &[b'x'; 32])
)
.await
.is_err());
assert_eq!(sent, if chunked { 4 } else { 8 });
let mut received = [0; 8];
reader.read_exact(&mut received).await.unwrap();
assert_eq!(received.iter().filter(|byte| **byte == b'x').count(), sent);
}
}

struct FlushFailure;
impl AsyncWrite for FlushFailure {
fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
data: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(data.len()))
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Err(io::Error::from(io::ErrorKind::ConnectionReset)))
}
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}

#[tokio::test]
async fn flush_failure_does_not_erase_accepted_payload() {
let mut writer = BodyWriter::new();
writer.init_content_length(4);
let mut stream = FlushFailure;
let mut sent = 0;
let mut progress = BodyProgress {
stream: &mut stream,
sent: &mut sent,
skip: 0,
remaining: 4,
};
assert!(writer.write_body(&mut progress, b"body").await.is_err());
assert_eq!(sent, 4);
}
}
57 changes: 57 additions & 0 deletions pingora-core/src/protocols/http/v1/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,34 @@ impl HttpSession {
}
}

/// Write body payload while retaining accepted progress on cancellation/error.
///
/// The connection must be abandoned if this future is interrupted: the
/// legacy body framer cannot resume an incomplete chunk or flush.
pub async fn write_body_controlled(&mut self, buf: &[u8]) -> Result<Option<usize>> {
let skip = if matches!(
self.body_writer.body_mode,
super::body::BodyMode::ChunkedEncoding(_)
) {
format!("{:X}\r\n", buf.len()).len()
} else {
0
};
let mut progress = super::body_progress::BodyProgress {
stream: &mut self.underlying_stream,
sent: &mut self.bytes_sent,
skip,
remaining: buf.len(),
};
let write = self.body_writer.write_body(&mut progress, buf);
match self.write_timeout {
Some(duration) => timeout(duration, write).await.unwrap_or_else(|_| {
Error::e_explain(WriteTimedout, "writing controlled request body")
}),
None => write.await,
}
}

fn maybe_force_close_body_reader(&mut self) {
if self.upgraded && self.received_upgrade_req_body && !self.body_reader.body_done() {
// request is done, reset the response body to close
Expand Down Expand Up @@ -1661,6 +1689,35 @@ mod tests_stream {
assert_eq!(res.unwrap_err().etype(), &WriteTimedout);
}

#[tokio::test]
async fn controlled_body_counter_survives_cancellation_after_partial_write() {
use tokio::io::{duplex, AsyncReadExt};
let (stream, mut origin) = duplex(128);
let mut session = HttpSession::new(Box::new(stream));
let mut request = RequestHeader::build("POST", b"/", None).unwrap();
request.insert_header("Content-Length", "256").unwrap();
session
.write_request_header(Box::new(request))
.await
.unwrap();
let mut head = Vec::new();
let mut byte = [0; 1];
while !head.ends_with(b"\r\n\r\n") {
origin.read_exact(&mut byte).await.unwrap();
head.push(byte[0]);
}
assert!(tokio::time::timeout(
Duration::from_millis(5),
session.write_body_controlled(&[b'x'; 256])
)
.await
.is_err());
assert_eq!(session.body_bytes_sent(), 128);
let mut accepted = [0; 128];
origin.read_exact(&mut accepted).await.unwrap();
assert_eq!(accepted, [b'x'; 128]);
}

#[tokio::test]
async fn body_bytes_sent_content_length() {
let header = b"POST /test HTTP/1.1\r\nContent-Length: 5\r\n\r\n";
Expand Down
2 changes: 2 additions & 0 deletions pingora-core/src/protocols/http/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ pub(crate) mod test_util {
}
impl Peek for FlushTrackingMock {}
}

mod body_progress;
20 changes: 17 additions & 3 deletions pingora-core/src/protocols/http/v2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub struct Http2Session {
ended: bool,
// Total DATA payload bytes received from upstream response
body_recv: usize,
/// DATA payload accepted by the upstream send stream, including interrupted writes.
body_sent: usize,
}

impl Drop for Http2Session {
Expand All @@ -92,6 +94,7 @@ impl Http2Session {
conn,
ended: false,
body_recv: 0,
body_sent: 0,
}
}

Expand Down Expand Up @@ -163,13 +166,24 @@ impl Http2Session {
.as_mut()
.expect("Try to write request body before sending request header");

super::write_body(body_writer, data, end, self.write_timeout)
.await
.map_err(|e| self.handle_err(e))?;
super::write_body_with_progress(
body_writer,
data,
end,
self.write_timeout,
&mut self.body_sent,
)
.await
.map_err(|e| self.handle_err(e))?;
self.ended = self.ended || end;
Ok(())
}

/// Request DATA payload accepted, including partial writes before failure.
pub fn body_bytes_sent(&self) -> usize {
self.body_sent
}

/// Signal that the request body has ended
pub fn finish_request_body(&mut self) -> Result<()> {
if self.ended {
Expand Down
18 changes: 18 additions & 0 deletions pingora-core/src/protocols/http/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ pub async fn write_body(
data: Bytes,
end: bool,
write_timeout: Option<Duration>,
) -> Result<()> {
write_body_with_progress(writer, data, end, write_timeout, &mut 0).await
}

/// Write HTTP/2 payload while retaining accepted DATA bytes on interruption.
pub async fn write_body_with_progress(
writer: &mut SendStream<Bytes>,
data: Bytes,
end: bool,
write_timeout: Option<Duration>,
accepted: &mut usize,
) -> Result<()> {
let mut remaining = data;

Expand All @@ -66,6 +77,7 @@ pub async fn write_body(
}

loop {
let before = remaining.len();
match write_timeout {
Some(t) => match timeout(t, reserve_and_send(writer, &mut remaining, end)).await {
Ok(res) => res?,
Expand All @@ -78,6 +90,7 @@ pub async fn write_body(
reserve_and_send(writer, &mut remaining, end).await?;
}
}
*accepted = accepted.saturating_add(before.saturating_sub(remaining.len()));
if remaining.is_empty() {
return Ok(());
}
Expand Down Expand Up @@ -138,6 +151,11 @@ mod test {
.err()
.unwrap();
assert_eq!(err.etype(), &pingora_error::ErrorType::WriteTimedout);
assert_eq!(
h2_stream.body_bytes_sent(),
1,
"accepted DATA remains charged after a write timeout"
);
}));

// Server
Expand Down
2 changes: 2 additions & 0 deletions pingora-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ regex = "1"
rand = "0.10"

[dev-dependencies]
# Unit tests create connectors without an embedding application's TLS setup.
rustls = { version = "0.23", default-features = false, features = ["aws_lc_rs", "std", "tls12"] }
reqwest = { version = "0.12", features = [
"gzip",
"rustls-tls",
Expand Down
7 changes: 7 additions & 0 deletions pingora-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ mod proxy_h1;
mod proxy_h2;
mod proxy_purge;
mod proxy_trait;
mod request_body;
pub use request_body::RequestBodyPacer;
use request_body::RequestBodyTask;
pub mod subrequest;

use subrequest::{BodyMode, Ctx as SubrequestCtx};
Expand Down Expand Up @@ -223,6 +226,10 @@ impl<SV> HttpProxy<SV, (), ()> {
/// // Use proxy.process_new_http() in your custom accept loop
/// ```
pub fn new(inner: SV, conf: Arc<ServerConf>) -> Self {
// Test binaries can unify reqwest's TLS features with multiple providers.
// Production applications continue to select their own provider.
#[cfg(all(test, feature = "rustls"))]
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
HttpProxy {
inner,
client_upstream: Connector::new(Some(ConnectorOptions::from_server_conf(&conf))),
Expand Down
Loading
Loading