|
| 1 | +extern crate alloc; |
| 2 | +use alloc::vec::Vec; |
| 3 | + |
| 4 | +use uefi::proto::device_path::text::{AllowShortcuts, DisplayOnly}; |
| 5 | +use uefi::proto::device_path::DevicePath; |
| 6 | +use uefi::proto::network::http::{HttpBinding, HttpHelper}; |
| 7 | +use uefi::proto::network::ip4config2::Ip4Config2; |
| 8 | +use uefi::{boot, Handle}; |
| 9 | + |
| 10 | +use uefi_raw::protocol::network::http::HttpStatusCode; |
| 11 | + |
| 12 | +pub fn print_handle_devpath(prefix: &str, handle: &Handle) { |
| 13 | + let Ok(dp) = boot::open_protocol_exclusive::<DevicePath>(*handle) else { |
| 14 | + info!("{}no device path for handle", prefix); |
| 15 | + return; |
| 16 | + }; |
| 17 | + if let Ok(string) = dp.to_string(DisplayOnly(true), AllowShortcuts(true)) { |
| 18 | + info!("{}{}", prefix, string); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn fetch_http(handle: Handle, url: &str) -> Option<Vec<u8>> { |
| 23 | + info!("http: fetching {} ...", url); |
| 24 | + |
| 25 | + let http_res = HttpHelper::new(handle); |
| 26 | + if let Err(e) = http_res { |
| 27 | + error!("http new: {}", e); |
| 28 | + return None; |
| 29 | + } |
| 30 | + let mut http = http_res.unwrap(); |
| 31 | + |
| 32 | + let res = http.configure(); |
| 33 | + if let Err(e) = res { |
| 34 | + error!("http configure: {}", e); |
| 35 | + return None; |
| 36 | + } |
| 37 | + |
| 38 | + let res = http.request_get(url); |
| 39 | + if let Err(e) = res { |
| 40 | + error!("http request: {}", e); |
| 41 | + return None; |
| 42 | + } |
| 43 | + |
| 44 | + let res = http.response_first(true); |
| 45 | + if let Err(e) = res { |
| 46 | + error!("http response: {}", e); |
| 47 | + return None; |
| 48 | + } |
| 49 | + |
| 50 | + let rsp = res.unwrap(); |
| 51 | + if rsp.status != HttpStatusCode::STATUS_200_OK { |
| 52 | + error!("http server error: {:?}", rsp.status); |
| 53 | + return None; |
| 54 | + } |
| 55 | + let Some(cl_hdr) = rsp.headers.iter().find(|h| h.0 == "content-length") else { |
| 56 | + error!("no content length"); |
| 57 | + return None; |
| 58 | + }; |
| 59 | + let Ok(cl) = cl_hdr.1.parse::<usize>() else { |
| 60 | + error!("parse content length ({})", cl_hdr.1); |
| 61 | + return None; |
| 62 | + }; |
| 63 | + info!("http: size is {} bytes", cl); |
| 64 | + |
| 65 | + let mut data = rsp.body; |
| 66 | + loop { |
| 67 | + if data.len() >= cl { |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + let res = http.response_more(); |
| 72 | + if let Err(e) = res { |
| 73 | + error!("read response: {}", e); |
| 74 | + return None; |
| 75 | + } |
| 76 | + |
| 77 | + let mut buf = res.unwrap(); |
| 78 | + data.append(&mut buf); |
| 79 | + } |
| 80 | + |
| 81 | + Some(data) |
| 82 | +} |
| 83 | + |
| 84 | +pub fn test() { |
| 85 | + info!("Testing ip4 config2 + http protocols"); |
| 86 | + |
| 87 | + let Ok(handles) = boot::locate_handle_buffer(boot::SearchType::from_proto::<HttpBinding>()) |
| 88 | + else { |
| 89 | + info!("No NICs found."); |
| 90 | + return; |
| 91 | + }; |
| 92 | + |
| 93 | + for h in handles.as_ref() { |
| 94 | + print_handle_devpath("nic: ", h); |
| 95 | + |
| 96 | + info!("Bring up interface (ip4 config2 protool)"); |
| 97 | + let mut ip4 = Ip4Config2::new(*h).expect("open ip4 config2 protool"); |
| 98 | + ip4.ifup(true).expect("aquire ipv4 address"); |
| 99 | + |
| 100 | + // hard to find web sites which still allow plain http these days ... |
| 101 | + info!("Testing HTTP"); |
| 102 | + let Some(_) = fetch_http(*h, "http://boot.netboot.xyz/robots.txt") else { |
| 103 | + // network can be flaky, so not assert |
| 104 | + info!("FAILED"); |
| 105 | + return; |
| 106 | + }; |
| 107 | + |
| 108 | + info!("Testing HTTPS"); |
| 109 | + let Some(_) = fetch_http(*h, "https://boot.netboot.xyz/robots.txt") else { |
| 110 | + // network can be flaky, so not assert |
| 111 | + info!("FAILED"); |
| 112 | + return; |
| 113 | + }; |
| 114 | + |
| 115 | + info!("PASSED"); |
| 116 | + } |
| 117 | +} |
0 commit comments