Skip to content

Commit 2568277

Browse files
committed
uefi: http: improve code by directly passing in the vector
This makes usage more natural as one can pass in the possibly uncomplete body of the http response type.
1 parent e118227 commit 2568277

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

  • uefi-test-runner/src/proto/network
  • uefi/src/proto/network

uefi-test-runner/src/proto/network/http.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,11 @@ fn fetch_http(handle: Handle, url: &str) -> Option<Vec<u8>> {
7474
break;
7575
}
7676

77-
let res = http.response_more();
77+
let res = http.response_more(&mut data);
7878
if let Err(e) = res {
7979
error!("read response: {e}");
8080
return None;
8181
}
82-
83-
let mut buf = res.unwrap();
84-
data.append(&mut buf);
8582
}
8683

8784
Some(data)

uefi/src/proto/network/http.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,13 @@ impl HttpHelper {
347347
Ok(rsp)
348348
}
349349

350-
/// Receive more body data.
351-
pub fn response_more(&mut self) -> uefi::Result<Vec<u8>> {
352-
let mut body = vec![0; 16 * 1024];
350+
/// Try to receive more of the HTTP response and append any new data to the
351+
/// provided `body` vector.
352+
pub fn response_more<'a>(&mut self, body: &'a mut Vec<u8>) -> uefi::Result<&'a [u8]> {
353+
let mut body_recv_buffer = vec![0; 16 * 1024];
353354
let mut rx_msg = HttpMessage {
354-
body_length: body.len(),
355-
body: body.as_mut_ptr().cast::<c_void>(),
355+
body_length: body_recv_buffer.len(),
356+
body: body_recv_buffer.as_mut_ptr().cast::<c_void>(),
356357
..Default::default()
357358
};
358359

@@ -378,9 +379,16 @@ impl HttpHelper {
378379
return Err(rx_token.status.into());
379380
};
380381

381-
debug!("http: body: {}/{}", rx_msg.body_length, body.len());
382+
debug!(
383+
"http: body: {}/{}",
384+
rx_msg.body_length,
385+
body_recv_buffer.len()
386+
);
382387

383-
Ok(body[0..rx_msg.body_length].to_vec())
388+
let new_data = &body_recv_buffer[0..rx_msg.body_length];
389+
body.extend(new_data);
390+
let new_data_slice = &body[body.len() - new_data.len()..];
391+
Ok(new_data_slice)
384392
}
385393
}
386394

0 commit comments

Comments
 (0)