-- Leo's gemini proxy
-- Connecting to typed-hole.org:1965...
-- Connected
-- Sending request
-- Meta line: 20 text/gemini
commit f1ca4253d52c8e07deb94401b0970d5075e3dbe4
Author: Julien Blanchard <julien@sideburns.eu>
Date: Mon Aug 12 17:06:14 2019 +0200
Add a 5s timeout to TCP connections
diff --git a/src/content.rs b/src/content.rs
index 5f692a3..a7234ce 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -1,7 +1,8 @@
use std::io::{Read, Write};
use native_tls::TlsConnector;
-use std::net::TcpStream;
+use std::net::{TcpStream, ToSocketAddrs};
+use std::time::Duration;
pub fn get_data(url: &url::Url) -> Result<String, String> {
let host = url.host_str().unwrap();
@@ -12,23 +13,31 @@ pub fn get_data(url: &url::Url) -> Result<String, String> {
builder.danger_accept_invalid_certs(true);
let connector = builder.build().unwrap();
- let stream = TcpStream::connect(&urlf);
+ match urlf.to_socket_addrs() {
+ Ok(mut addrs_iter) => match addrs_iter.next() {
+ Some(socket_addr) => {
+ let stream = TcpStream::connect_timeout(&socket_addr, Duration::new(5, 0));
- match stream {
- Ok(stream) => {
- let mstream = connector.connect(&host, stream);
+ match stream {
+ Ok(stream) => {
+ let mstream = connector.connect(&host, stream);
- match mstream {
- Ok(mut stream) => {
- let url = format!("{}\r\n", url);
- stream.write_all(url.as_bytes()).unwrap();
- let mut res = vec![];
- stream.read_to_end(&mut res).unwrap();
- Ok(String::from_utf8_lossy(&res).to_string())
+ match mstream {
+ Ok(mut stream) => {
+ let url = format!("{}\r\n", url);
+ stream.write_all(url.as_bytes()).unwrap();
+ let mut res = vec![];
+ stream.read_to_end(&mut res).unwrap();
+ Ok(String::from_utf8_lossy(&res).to_string())
+ }
+ Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
+ }
+ }
+ Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
}
- Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
}
- }
- Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),
+ None => Err(format!("Could not connect to {}", urlf)),
+ },
+ Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e))
}
}
---
Served by Pollux Gemini Server.
-- Response ended
-- Page fetched on Sun May 19 08:49:59 2024