Skip to content

Commit a9a343f

Browse files
authored
Merge pull request #35 from wacker-dev/body-stream
Initiate the HTTP connection before writing the body stream
2 parents cf0a3aa + e51c815 commit a9a343f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

test-programs/src/bin/client_post_with_body.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ struct Data {
88
}
99

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

2025
let data = resp.json::<Data>().unwrap();
21-
assert_eq!(data.data.len(), 5000);
26+
assert_eq!(data.data.len(), LEN * REPEAT);
2227
}

waki/src/request.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,20 @@ impl Request {
232232
.map_err(|()| anyhow!("failed to set path_with_query"))?;
233233
}
234234

235+
let outgoing_body = req
236+
.body()
237+
.map_err(|_| anyhow!("outgoing request write failed"))?;
238+
235239
let options = RequestOptions::new();
236240
options
237241
.set_connect_timeout(self.connect_timeout)
238242
.map_err(|()| anyhow!("failed to set connect_timeout"))?;
243+
let future_response = outgoing_handler::handle(req, Some(options))?;
239244

240-
let outgoing_body = req
241-
.body()
242-
.map_err(|_| anyhow!("outgoing request write failed"))?;
243245
let body = self.body.bytes()?;
244246
write_to_outgoing_body(&outgoing_body, body.as_slice())?;
245247
OutgoingBody::finish(outgoing_body, None)?;
246248

247-
let future_response = outgoing_handler::handle(req, Some(options))?;
248249
let incoming_response = match future_response.get() {
249250
Some(result) => result.map_err(|()| anyhow!("response already taken"))?,
250251
None => {

0 commit comments

Comments
 (0)