Skip to content

Commit

Permalink
Fix bug in calculation of chunk overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Nov 22, 2024
1 parent 30f0ec0 commit 0b6e500
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _test = []

[dependencies]
base64 = "0.22.1"
ureq-proto = "0.1.0"
ureq-proto = "0.2.0"
# ureq-proto = { path = "../ureq-proto" }
log = "0.4.22"
once_cell = "1.19.0"
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ mk_method!(trace, TRACE, WithoutBody);

#[cfg(test)]
pub(crate) mod test {
use std::io;

use assert_no_alloc::AllocDisabler;
use config::Config;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -717,6 +719,16 @@ pub(crate) mod test {
assert_eq!(err.to_string(), "http: invalid uri character");
}

#[test]
fn post_big_body_chunked() {
// https://github.com/algesten/ureq/issues/879
let mut data = io::Cursor::new(vec![42; 153_600]);
post("http://httpbin.org/post")
.content_type("application/octet-stream")
.send(SendBody::from_reader(&mut data))
.expect("to send correctly");
}

#[test]
#[cfg(all(feature = "cookies", feature = "_test"))]
fn store_response_cookies() {
Expand Down
25 changes: 16 additions & 9 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,12 @@ fn send_request(
}

timings.record_time(Timeout::SendRequest);
Ok(flow.proceed().unwrap())

// The request might be misconfigured.
let flow = flow.proceed()?;

// We checked can_proceed() above, this unwrap is fine.
Ok(flow.unwrap())
}

fn await_100(
Expand Down Expand Up @@ -396,7 +401,11 @@ fn await_100(
}

timings.record_time(Timeout::Await100);
Ok(flow.proceed())

// A misconfigured request might surface here.
let flow = flow.proceed()?;

Ok(flow)
}

fn send_body(
Expand All @@ -416,14 +425,12 @@ fn send_body(

let input_len = tmp.len();

let overhead = flow.calculate_output_overhead(output.len())?;
assert!(input_len > overhead);
let max_input = input_len - overhead;
let input_fitting_in_output = flow.calculate_max_input(output.len());
let max_input = input_len.min(input_fitting_in_output);

let output_used = if overhead == 0 {
// overhead == 0 means we are not doing chunked transfer. The body can be written
// directly to the output. This optimizes away a memcopy if we were to go via
// flow.write().
let output_used = if !flow.is_chunked() {
// For non-chunked, The body can be written directly to the output.
// This optimizes away a memcopy if we were to go via flow.write().
let output_used = body.read(output)?;

// Size checking is still in the flow.
Expand Down

0 comments on commit 0b6e500

Please sign in to comment.