latest revised test

This commit is contained in:
bunnie 2022-03-18 20:28:36 +08:00
parent 4463b909a1
commit 140f07ddc8

View file

@ -73,43 +73,37 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
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 self.dns.lookup(host) { match TcpStream::connect((host, 80)) {
Ok(ipaddr) => { Ok(mut stream) => {
log::info!("resolved {} to {:?}", host, ipaddr); log::trace!("stream open, setting timeouts");
match TcpStream::connect((IpAddr::from(ipaddr), 80)) { stream.set_read_timeout(Some(std::time::Duration::from_millis(10_000))).unwrap();
Ok(mut stream) => { stream.set_write_timeout(Some(std::time::Duration::from_millis(10_000))).unwrap();
log::trace!("stream open, setting timeouts"); log::debug!("read timeout: {:?}", stream.read_timeout().unwrap().unwrap().as_millis());
stream.set_read_timeout(Some(std::time::Duration::from_millis(10_000))).unwrap(); log::debug!("write timeout: {:?}", stream.write_timeout().unwrap().unwrap().as_millis());
stream.set_write_timeout(Some(std::time::Duration::from_millis(10_000))).unwrap(); log::info!("my socket: {:?}", stream.local_addr());
log::debug!("read timeout: {:?}", stream.read_timeout().unwrap().unwrap().as_millis()); log::info!("peer addr: {:?}", stream.peer_addr());
log::debug!("write timeout: {:?}", stream.write_timeout().unwrap().unwrap().as_millis()); log::info!("sending GET request");
log::info!("my socket: {:?}", stream.local_addr()); match write!(stream, "GET /{} HTTP/1.1\r\n", path) {
log::info!("peer addr: {:?}", stream.peer_addr()); Ok(_) => log::trace!("sent GET"),
log::info!("sending GET request"); Err(e) => {
match write!(stream, "GET /{} HTTP/1.1\r\n", path) { log::error!("GET err {:?}", e);
Ok(_) => log::trace!("sent GET"), write!(ret, "Error sending GET: {:?}", e).unwrap();
Err(e) => {
log::error!("GET err {:?}", e);
write!(ret, "Error sending GET: {:?}", e).unwrap();
}
}
write!(stream, "Host: {}\r\nAccept: */*\r\nUser-Agent: Precursor/0.9.6\r\n", host).expect("stream error");
write!(stream, "Connection: close\r\n").expect("stream error");
write!(stream, "\r\n").expect("stream error");
log::info!("fetching response....");
let mut buf = [0u8; 512];
match stream.read(&mut buf) {
Ok(len) => {
log::trace!("raw response ({}): {:?}", len, &buf[..len]);
write!(ret, "{}", std::string::String::from_utf8_lossy(&buf[..len])).unwrap();
}
Err(e) => write!(ret, "Didn't get response from host: {:?}", e).unwrap(),
}
} }
Err(e) => write!(ret, "Couldn't connect to {}:80: {:?}", host, e).unwrap(), }
write!(stream, "Host: {}\r\nAccept: */*\r\nUser-Agent: Precursor/0.9.6\r\n", host).expect("stream error");
write!(stream, "Connection: close\r\n").expect("stream error");
write!(stream, "\r\n").expect("stream error");
log::info!("fetching response....");
let mut buf = [0u8; 512];
match stream.read(&mut buf) {
Ok(len) => {
log::trace!("raw response ({}): {:?}", len, &buf[..len]);
write!(ret, "{}", std::string::String::from_utf8_lossy(&buf[..len])).unwrap();
}
Err(e) => write!(ret, "Didn't get response from host: {:?}", e).unwrap(),
} }
} }
_ => write!(ret, "Couldn't resolve {}", host).unwrap(), Err(e) => write!(ret, "Couldn't connect to {}:80: {:?}", host, e).unwrap(),
} }
} }
_ => write!(ret, "Usage: tcpget bunniefoo.com/bunnie/test.txt").unwrap(), _ => write!(ret, "Usage: tcpget bunniefoo.com/bunnie/test.txt").unwrap(),