/
main.rs
40 lines (34 loc) · 1.28 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::env;
fn main() {
match smoke_test() {
Ok(()) => println!("Did not hang! Success"),
Err(err) => {
println!("Did not hang! Error: {}", err);
std::process::exit(1);
}
}
}
fn smoke_test() -> Result<(), Box<dyn std::error::Error>> {
let url = format!(
"http://{}",
env::args().skip(1).next().expect("No URL provided")
);
println!("Fetching {}", url);
let response = minreq::get(&url)
.with_timeout(40)
.with_max_redirects(10)
.send()?;
println!("HTTP status code: {}", response.status_code);
// Print headers
// Note: this is not a multimap, so duplicate values for the same header will be lost
for (header_name, value) in response.headers.iter() {
println!("Header: {}: {:?}", header_name, value);
}
// This retains the whole body in memory, but tests show that RAM is plentiful, so I didn't bother optimizing.
// Converting to a string lets us exercise encoding conversion routines.
let text = response.as_str()?;
// Print the first 8k chars of the body to get an idea of what we've downloaded, ignore the rest.
let first_8k_chars_of_body: String = text.chars().take(8192).collect();
println!("{}", first_8k_chars_of_body);
Ok(())
}