httpclient.rs 3.8 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::os::unix::io::AsRawFd;
  12. use url::Url;
  13. use smoltcp::phy::wait as phy_wait;
  14. use smoltcp::wire::{EthernetAddress, Ipv4Address, IpAddress, IpCidr};
  15. use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
  16. use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
  17. use smoltcp::time::Instant;
  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 neighbor_cache = NeighborCache::new(BTreeMap::new());
  32. let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 1024]);
  33. let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 1024]);
  34. let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
  35. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  36. let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
  37. let default_v4_gw = Ipv4Address::new(192, 168, 69, 100);
  38. let mut iface = EthernetInterfaceBuilder::new(device)
  39. .ethernet_addr(ethernet_addr)
  40. .neighbor_cache(neighbor_cache)
  41. .ip_addrs(ip_addrs)
  42. .ipv4_gateway(default_v4_gw)
  43. .finalize();
  44. let mut sockets = SocketSet::new(vec![]);
  45. let tcp_handle = sockets.add(tcp_socket);
  46. enum State { Connect, Request, Response };
  47. let mut state = State::Connect;
  48. loop {
  49. let timestamp = Instant::now();
  50. iface.poll(&mut sockets, timestamp).expect("poll error");
  51. {
  52. let mut socket = sockets.get::<TcpSocket>(tcp_handle);
  53. state = match state {
  54. State::Connect if !socket.is_active() => {
  55. debug!("connecting");
  56. let local_port = 49152 + rand::random::<u16>() % 16384;
  57. socket.connect((address, url.port().unwrap_or(80)), local_port).unwrap();
  58. State::Request
  59. }
  60. State::Request if socket.may_send() => {
  61. debug!("sending request");
  62. let http_get = "GET ".to_owned() + url.path() + " HTTP/1.1\r\n";
  63. socket.send_slice(http_get.as_ref()).expect("cannot send");
  64. let http_host = "Host: ".to_owned() + url.host_str().unwrap() + "\r\n";
  65. socket.send_slice(http_host.as_ref()).expect("cannot send");
  66. socket.send_slice(b"Connection: close\r\n").expect("cannot send");
  67. socket.send_slice(b"\r\n").expect("cannot send");
  68. State::Response
  69. }
  70. State::Response if socket.can_recv() => {
  71. socket.recv(|data| {
  72. println!("{}", str::from_utf8(data).unwrap_or("(invalid utf8)"));
  73. (data.len(), ())
  74. }).unwrap();
  75. State::Response
  76. }
  77. State::Response if !socket.may_recv() => {
  78. debug!("received complete response");
  79. break
  80. }
  81. _ => state
  82. }
  83. }
  84. phy_wait(fd, iface.poll_delay(&sockets, timestamp)).expect("wait error");
  85. }
  86. }