Skip to content

Commit 7fea064

Browse files
Merge pull request #1850 from rust-osdev/http
uefi: http: fix integration test
2 parents 8cbc3c0 + eb0a349 commit 7fea064

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ fn fetch_http(handle: Handle, url: &str) -> Option<Vec<u8>> {
5353
error!("http server error: {:?}", rsp.status);
5454
return None;
5555
}
56-
let Some(cl_hdr) = rsp.headers.iter().find(|h| h.0 == "content-length") else {
56+
let cl_hdr = rsp.headers.iter().find(|h| h.0 == "content-length");
57+
if cl_hdr.is_none() {
5758
// The only way to figure when your transfer is complete is to
5859
// get the content length header and count the bytes you got.
59-
// So missing header -> fatal error.
60-
error!("no content length");
61-
return None;
60+
// So missing header -> give up and pretend things are okay.
61+
warn!("no content length header, we might not have the whole body");
62+
return Some(rsp.body);
6263
};
64+
let cl_hdr = cl_hdr.unwrap();
6365
let Ok(cl) = cl_hdr.1.parse::<usize>() else {
6466
error!("parse content length ({})", cl_hdr.1);
6567
return None;
@@ -72,14 +74,11 @@ fn fetch_http(handle: Handle, url: &str) -> Option<Vec<u8>> {
7274
break;
7375
}
7476

75-
let res = http.response_more();
77+
let res = http.response_more(&mut data);
7678
if let Err(e) = res {
7779
error!("read response: {e}");
7880
return None;
7981
}
80-
81-
let mut buf = res.unwrap();
82-
data.append(&mut buf);
8382
}
8483

8584
Some(data)
@@ -100,7 +99,7 @@ pub fn test() {
10099

101100
// hard to find web sites which still allow plain http these days ...
102101
info!("Testing HTTP");
103-
fetch_http(*h, "http://example.com/").expect("http request failed");
102+
fetch_http(*h, "http://example.com/").expect("http request failed: http://example.com");
104103

105104
// FYI: not all firmware builds support modern tls versions.
106105
// request() -> ABORTED typically is a tls handshake error.

uefi/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- Return request with status as error data object for `proto::ata::pass_thru::AtaDevice`.
1414
- **Breaking:** `proto::network::snp::SimpleNetwork::wait_for_packet` now
1515
returns `Option<Event>` instead of `&Event`.
16+
- `Http::get_mode_data` doesn't consume a parameter anymore and instead return
17+
an owned value of type `HttpConfigData`
1618

1719
# uefi - v0.36.1 (2025-11-05)
1820

uefi/src/proto/network/http.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ pub struct Http(HttpProtocol);
3131

3232
impl Http {
3333
/// Receive HTTP Protocol configuration.
34-
pub fn get_mode_data(&mut self, config_data: &mut HttpConfigData) -> uefi::Result<()> {
35-
let status = unsafe { (self.0.get_mode_data)(&mut self.0, config_data) };
34+
pub fn get_mode_data(&mut self) -> uefi::Result<HttpConfigData> {
35+
let mut config_data = HttpConfigData::default();
36+
let status = unsafe { (self.0.get_mode_data)(&mut self.0, &mut config_data) };
3637
match status {
37-
Status::SUCCESS => Ok(()),
38+
Status::SUCCESS => Ok(config_data),
3839
_ => Err(status.into()),
3940
}
4041
}
@@ -116,18 +117,23 @@ impl HttpBinding {
116117
}
117118
}
118119

119-
/// HTTP Response data
120+
/// Representation of the underlying UEFI HTTP response.
121+
///
122+
/// Helper type for [`HttpHelper`].
120123
#[derive(Debug)]
121124
pub struct HttpHelperResponse {
122125
/// HTTP Status
123126
pub status: HttpStatusCode,
124127
/// HTTP Response Headers
125128
pub headers: Vec<(String, String)>,
126-
/// HTTP Body
129+
/// Partial or entire HTTP body, depending on context.
127130
pub body: Vec<u8>,
128131
}
129132

130-
/// HTTP Helper, makes using the HTTP protocol more convenient.
133+
/// HTTP Helper, makes using the [HTTP] [`Protocol`] more convenient.
134+
///
135+
/// [HTTP]: Http
136+
/// [`Protocol`]: uefi::proto::Protocol
131137
#[derive(Debug)]
132138
pub struct HttpHelper {
133139
child_handle: Handle,
@@ -270,7 +276,12 @@ impl HttpHelper {
270276
Ok(())
271277
}
272278

273-
/// Receive the start of the http response, the headers and (parts of) the body.
279+
/// Receive the start of the http response, the headers and (parts of) the
280+
/// body.
281+
///
282+
/// Depending on the HTTP response, its length, its encoding, and its
283+
/// transmission method (chunked or not), users may have to call
284+
/// [`Self::response_more`] afterward.
274285
pub fn response_first(&mut self, expect_body: bool) -> uefi::Result<HttpHelperResponse> {
275286
let mut rx_rsp = HttpResponseData {
276287
status_code: HttpStatusCode::STATUS_UNSUPPORTED,
@@ -336,12 +347,13 @@ impl HttpHelper {
336347
Ok(rsp)
337348
}
338349

339-
/// Receive more body data.
340-
pub fn response_more(&mut self) -> uefi::Result<Vec<u8>> {
341-
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];
342354
let mut rx_msg = HttpMessage {
343-
body_length: body.len(),
344-
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>(),
345357
..Default::default()
346358
};
347359

@@ -367,9 +379,16 @@ impl HttpHelper {
367379
return Err(rx_token.status.into());
368380
};
369381

370-
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+
);
371387

372-
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)
373392
}
374393
}
375394

0 commit comments

Comments
 (0)