-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
42 lines (36 loc) · 1.33 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
41
42
use std::env;
use std::time::Duration;
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 = attohttpc::get(&url)
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(30))
.send()?;
println!("HTTP status code: {}", response.status());
// Print headers
for header_name in response.headers().keys() {
for value in response.headers().get_all(header_name).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.text()?;
// 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(())
}