work around various shortcomings in the net crate to download images

This commit is contained in:
bunnie 2022-06-14 19:06:08 +08:00
parent 3757441677
commit 18fc6663b2
2 changed files with 67 additions and 47 deletions

View file

@ -289,27 +289,14 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
} }
#[cfg(feature="ditherpunk")] #[cfg(feature="ditherpunk")]
"image" => { "image" => {
const MAX_IMAGE_LEN: usize = 256 * 1024; // includes HTTP headers // use std::io::BufRead; // not implemented yet!
if let Some(url) = tokens.next() { if let Some(url) = tokens.next() {
match url.split_once('/') { match url.split_once('/') {
Some((host, path)) => { Some((host, path)) => {
match TcpStream::connect((host, 80)) { match TcpStream::connect((host, 80)) {
Ok(mut stream) => { Ok(mut stream) => {
let mut heap_image = match xous::syscall::map_memory( stream.set_read_timeout(Some(Duration::from_millis(5_000))).unwrap();
None, stream.set_write_timeout(Some(Duration::from_millis(5_000))).unwrap();
None,
MAX_IMAGE_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W,
) {
Ok(d) => d,
Err(e) => {
log::error!("couldn't allocate data for image, aborting");
write!(ret, "Couldn't allocate memory for image, aborting: {:?}", e).unwrap();
return Ok(Some(ret));
}
};
stream.set_read_timeout(Some(Duration::from_millis(10_000))).unwrap();
stream.set_write_timeout(Some(Duration::from_millis(10_000))).unwrap();
match write!(stream, "GET /{} HTTP/1.1\r\n", path) { match write!(stream, "GET /{} HTTP/1.1\r\n", path) {
Ok(_) => log::trace!("sent GET"), Ok(_) => log::trace!("sent GET"),
Err(e) => { Err(e) => {
@ -321,22 +308,57 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
write!(stream, "Connection: close\r\n").expect("stream error"); write!(stream, "Connection: close\r\n").expect("stream error");
write!(stream, "\r\n").expect("stream error"); write!(stream, "\r\n").expect("stream error");
log::info!("fetching response...."); log::info!("fetching response....");
let init_len = match stream.read(heap_image.as_slice_mut::<u8>()) { // let mut reader = std::io::BufReader::new(&mut stream);
Ok(init_len) => { log::info!("trying to read entire response...");
log::info!("fetched {} bytes of http header data", init_len); let mut received = Vec::<u8>::new();
init_len received.reserve(128 * 1024); // up to 128k image size...
} let mut rx_len = 0;
const CHUNK: usize = 4096;
/*
let rx_len = match reader.read_to_end(&mut received) {
Ok(len) => len,
Err(e) => { Err(e) => {
write!(ret, "Didn't get response from host: {:?}", e).unwrap(); log::error!("couldn't read image: {:?}", e);
return Ok(Some(ret)); write!(ret, "Couldn't read image: {:?}", e).unwrap();
}, return Ok(Some(ret))
}; }
}; */
let mut rx_buf = [0u8; CHUNK];
log::info!("starting read loop");
// stream.set_nonblocking(true).expect("couldn't set stream to nonblocking");
let mut last_chunk = 0;
loop {
match stream.read(&mut rx_buf) {
Ok(len) => {
log::info!("got {} bytes", len);
received.extend_from_slice(&rx_buf[..len]);
rx_len += len;
if len == 0 {
log::info!("got 0-length read, concluding we're at an EOF...");
break;
}
if last_chunk == 0 {
last_chunk = len;
} else {
if len < last_chunk {
log::info!("heuristic termination of read loop");
break;
}
}
},
Err(e) => {
log::info!("quitting read loop with err {:?}", e);
break;
}
}
}
log::info!("read {} bytes", rx_len);
if let Some(start) = find_subsequence( if let Some(start) = find_subsequence(
heap_image.as_slice::<u8>(), &received,
&[0x0d, 0x0a, 0x0d, 0x0a], // this is \r\n\r\n, which indicates end of header. &[0x0d, 0x0a, 0x0d, 0x0a], // this is \r\n\r\n, which indicates end of header.
) { ) {
log::info!("header is {} bytes long", start); log::info!("header is {} bytes long", start);
let header = std::string::String::from_utf8_lossy(&heap_image.as_slice::<u8>()[..start]); let header = std::string::String::from_utf8_lossy(&received[..start]);
let lines = header.split("\r\n"); let lines = header.split("\r\n");
let mut content_length = 0; let mut content_length = 0;
for line in lines { for line in lines {
@ -355,29 +377,27 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
} }
} }
if content_length > 0 { if content_length > 0 {
let remaining_len = content_length - (init_len - start); let modals = modals::Modals::new(&env.xns).unwrap();
log::info!("fetching remaining length of {}", remaining_len); // have to add 4 to the start because the index is at the beginning of the header sequence,
match stream.read_exact(&mut heap_image.as_slice_mut::<u8>()[init_len..remaining_len]) { // not the end of the sequence.
Ok(_) => { log::info!("decoding png starting with: {:x?}", &received[start+4..start+4+64]);
log::info!("read image data remaining len {}", remaining_len); let decoder = png::Decoder::new(&received[start+4..]);
let modals = modals::Modals::new(&env.xns).unwrap(); let mut reader = decoder.read_info().expect("failed to read png info");
let decoder = png::Decoder::new(&heap_image.as_slice::<u8>()[start..start+content_length]); let mut buf = vec![0; reader.output_buffer_size()];
let mut reader = decoder.read_info().expect("failed to read png info"); let info = reader.next_frame(&mut buf).expect("failed to decode png");
let mut buf = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf).expect("failed to decode png");
let (width, _height) = (info.width, info.height); let (width, height) = (info.width, info.height);
let img = Img::new(buf, width as usize); log::info!("image is {} x {}", width, height);
log::info!("some image bytes: {:x?}", &buf[..64]);
let img = Img::new(buf, width as usize);
modals.show_image(&img).expect("show image modal failed"); modals.show_image(&img).expect("show image modal failed");
} write!(ret, "Image of {} bytes read successfully", content_length).unwrap();
Err(e) => { } else {
log::info!("couldn't read remaining image length: {:?}", e); write!(ret, "content-length was 0, no image read").unwrap();
}
}
} }
} }
xous::syscall::unmap_memory(heap_image).expect("couldn't de-allocate image memory"); // reader.consume(received.len());
} }
Err(e) => write!(ret, "Couldn't connect to {}:80: {:?}", host, e).unwrap(), Err(e) => write!(ret, "Couldn't connect to {}:80: {:?}", host, e).unwrap(),
} }

View file

@ -638,7 +638,7 @@ impl<'a> ShellCmdApi<'a> for Test {
write!(ret, "Ship mode request denied").unwrap(); write!(ret, "Ship mode request denied").unwrap();
} }
} }
#[cfg(all(feature="ditherpunk", not(any(target_os = "none", target_os = "xous"))))] #[cfg(feature="ditherpunk")]
"modals" => { "modals" => {
modals::tests::spawn_test(); modals::tests::spawn_test();
} }