httpclient.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #[macro_use]
  2. extern crate log;
  3. extern crate env_logger;
  4. extern crate getopts;
  5. extern crate rand;
  6. extern crate url;
  7. extern crate smoltcp;
  8. mod utils;
  9. use std::str::{self, FromStr};
  10. use std::collections::BTreeMap;
  11. use std::time::Instant;
  12. use std::os::unix::io::AsRawFd;
  13. use url::Url;
  14. use smoltcp::phy::wait as phy_wait;
  15. use smoltcp::wire::{EthernetAddress, Ipv4Address, IpAddress, IpCidr};
  16. use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
  17. use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
  18. fn main() {
  19. utils::setup_logging("");
  20. let (mut opts, mut free) = utils::create_options();
  21. utils::add_tap_options(&mut opts, &mut free);
  22. utils::add_middleware_options(&mut opts, &mut free);
  23. free.push("ADDRESS");
  24. free.push("URL");
  25. let mut matches = utils::parse_options(&opts, free);
  26. let device = utils::parse_tap_options(&mut matches);
  27. let fd = device.as_raw_fd();
  28. let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
  29. let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
  30. let url = Url::parse(&matches.free[1]).expect("invalid url format");
  31. let startup_time = Instant::now();
  32. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  33. let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 1024]);
  34. let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 1024]);
  35. let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
  36. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  37. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
  38. let default_v4_gw = Ipv4Address::new(192, 168, 69, 100);
  39. let mut iface = EthernetInterfaceBuilder::new(device)
  40. .ethernet_addr(ethernet_addr)
  41. .neighbor_cache(neighbor_cache)
  42. .ip_addrs(ip_addrs)
  43. .ipv4_gateway(default_v4_gw)
  44. .finalize();
  45. let mut sockets = SocketSet::new(vec![]);
  46. let tcp_handle = sockets.add(tcp_socket);
  47. enum State { Connect, Request, Response };
  48. let mut state = State::Connect;
  49. loop {
  50. {
  51. let mut socket = sockets.get::<TcpSocket>(tcp_handle);
  52. state = match state {
  53. State::Connect if !socket.is_active() => {
  54. debug!("connecting");
  55. let local_port = 49152 + rand::random::<u16>() % 16384;
  56. socket.connect((address, url.port().unwrap_or(80)), local_port).unwrap();
  57. State::Request
  58. }
  59. State::Request if socket.may_send() => {
  60. debug!("sending request");
  61. let http_get = "GET ".to_owned() + url.path() + " HTTP/1.1\r\n";
  62. socket.send_slice(http_get.as_ref()).expect("cannot send");
  63. let http_host = "Host: ".to_owned() + url.host_str().unwrap() + "\r\n";
  64. socket.send_slice(http_host.as_ref()).expect("cannot send");
  65. socket.send_slice(b"Connection: close\r\n").expect("cannot send");
  66. socket.send_slice(b"\r\n").expect("cannot send");
  67. State::Response
  68. }
  69. State::Response if socket.can_recv() => {
  70. socket.recv(|data| {
  71. println!("{}", str::from_utf8(data).unwrap_or("(invalid utf8)"));
  72. (data.len(), ())
  73. }).unwrap();
  74. State::Response
  75. }
  76. State::Response if !socket.may_recv() => {
  77. debug!("received complete response");
  78. break
  79. }
  80. _ => state
  81. }
  82. }
  83. let timestamp = utils::millis_since(startup_time);
  84. let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
  85. phy_wait(fd, poll_at.map(|at| at.saturating_sub(timestamp))).expect("wait error");
  86. }
  87. }