Skip to main content
Be Bitwise
RustCapstone: CLI Network Tool

Making HTTP Requests

At the heart of the checker: fire HTTP requests, time them, capture what came back.

The CheckResult Type

rust
#[derive(Debug)]
pub struct CheckResult {
pub url: String,
pub status: Option<u16>,
pub response_time_ms: u128,
pub error: Option<String>,
}

Synchronous Checker

Using standard library TCP:

rust
use std::net::{TcpStream, ToSocketAddrs};
use std::io::{Read, Write};
use std::time::Instant;
pub fn check_url(url: &str, timeout_secs: u64) -> CheckResult {
let start = Instant::now();
let parsed = match parse_url(url) {
Some(u) => u,
None => return CheckResult {
url: url.to_string(),
status: None,
response_time_ms: start.elapsed().as_millis(),
error: Some("Invalid URL".to_string()),
},
};
let timeout = std::time::Duration::from_secs(timeout_secs);
let fail = |msg: String| CheckResult {
url: url.to_string(),
status: None,
response_time_ms: start.elapsed().as_millis(),
error: Some(msg),
};
// "example.com:80" is a hostname, not a SocketAddr, so resolve it
// with DNS first and connect to the first address that comes back.
let addr = match (parsed.host.as_str(), parsed.port).to_socket_addrs() {
Ok(mut addrs) => match addrs.next() {
Some(a) => a,
None => return fail("No address for host".to_string()),
},
Err(e) => return fail(format!("DNS lookup failed: {}", e)),
};
let mut stream = match TcpStream::connect_timeout(&addr, timeout) {
Ok(s) => s,
Err(e) => return fail(format!("Connection failed: {}", e)),
};
// connect_timeout only covers the handshake; a server that accepts
// and then goes quiet would otherwise block read_to_string forever.
let _ = stream.set_read_timeout(Some(timeout));
let _ = stream.set_write_timeout(Some(timeout));
let request = format!(
"GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
parsed.path, parsed.host
);
let _ = stream.write_all(request.as_bytes());
let mut response = String::new();
let _ = stream.read_to_string(&mut response);
let status = extract_status_code(&response);
let elapsed = start.elapsed().as_millis();
CheckResult {
url: url.to_string(),
status,
response_time_ms: elapsed,
error: None,
}
}

parse_url is the one from module 5, so it only accepts http:// URLs; there's no TLS here, and an https:// URL comes back as "Invalid URL". connect_timeout wants a SocketAddr, which is an IP and port, so the host name has to go through to_socket_addrs (a DNS lookup) first. Calling .parse() on "example.com:80" fails, and unwrapping that would panic on every real URL.

Key Concept

Instant::now() and elapsed() give you monotonic, sub-millisecond timing. TcpStream::connect_timeout keeps the tool from sitting forever on a black-holed IP. Both matter when responsiveness is the very thing you're measuring.

Concurrent Checking

Fan out across threads to hit URLs in parallel:

rust
use std::thread;
pub fn check_urls(urls: &[String], timeout: u64) -> Vec<CheckResult> {
let handles: Vec<_> = urls
.iter()
.map(|url| {
let url = url.clone();
thread::spawn(move || check_url(&url, timeout))
})
.collect();
handles
.into_iter()
.map(|h| h.join().unwrap())
.collect()
}

Status Code Extraction

rust
fn extract_status_code(response: &str) -> Option<u16> {
let first_line = response.lines().next()?;
let parts: Vec<&str> = first_line.split_whitespace().collect();
if parts.len() >= 2 {
parts[1].parse().ok()
} else {
None
}
}

Retry Logic

Networks lie. Wrap the call in a retry loop:

rust
pub fn check_url_with_retry(url: &str, timeout: u64, retries: u32) -> CheckResult {
for attempt in 0..=retries {
let result = check_url(url, timeout);
if result.error.is_none() || attempt == retries {
return result;
}
std::thread::sleep(std::time::Duration::from_millis(500));
}
unreachable!()
}

Iterators and closures collapse the fan-out/fan-in pattern into a few lines, which is the whole sales pitch for the functional style here.