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

Initiate the HTTP connection before writing the body stream #35

Merged
merged 1 commit into from
Dec 12, 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
11 changes: 8 additions & 3 deletions test-programs/src/bin/client_post_with_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ struct Data {
}

fn main() {
let buffer = [0; 5000];
// Make sure the final body is larger than 1024*1024, but we cannot allocate
// so much memory directly in the wasm program, so we use the `repeat`
// method to increase the body size.
const LEN: usize = 1024;
const REPEAT: usize = 1025;
let buffer = [0; LEN];
let resp = Client::new()
.post("https://httpbin.org/post")
.body(buffer)
.body(buffer.repeat(REPEAT))
.connect_timeout(Duration::from_secs(5))
.send()
.unwrap();
assert_eq!(resp.status_code(), 200);

let data = resp.json::<Data>().unwrap();
assert_eq!(data.data.len(), 5000);
assert_eq!(data.data.len(), LEN * REPEAT);
}
9 changes: 5 additions & 4 deletions waki/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,20 @@ impl Request {
.map_err(|()| anyhow!("failed to set path_with_query"))?;
}

let outgoing_body = req
.body()
.map_err(|_| anyhow!("outgoing request write failed"))?;

let options = RequestOptions::new();
options
.set_connect_timeout(self.connect_timeout)
.map_err(|()| anyhow!("failed to set connect_timeout"))?;
let future_response = outgoing_handler::handle(req, Some(options))?;

let outgoing_body = req
.body()
.map_err(|_| anyhow!("outgoing request write failed"))?;
let body = self.body.bytes()?;
write_to_outgoing_body(&outgoing_body, body.as_slice())?;
OutgoingBody::finish(outgoing_body, None)?;

let future_response = outgoing_handler::handle(req, Some(options))?;
let incoming_response = match future_response.get() {
Some(result) => result.map_err(|()| anyhow!("response already taken"))?,
None => {
Expand Down
Loading