mirror of
https://git.sr.ht/~slondr/spart
synced 2024-06-17 23:07:04 +00:00
actually handle connection errors
This commit is contained in:
parent
ce01b82533
commit
739e875117
11
src/lib.rs
11
src/lib.rs
|
@ -35,7 +35,6 @@ impl Request {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl std::fmt::Display for Request {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
write!(f, "{} {} {}\r\n{}", self.host, self.path, self.content_length, match &self.data {
|
||||
|
@ -49,9 +48,9 @@ pub fn get(r: String) -> Result<String, &'static str> {
|
|||
match Url::parse(&r) {
|
||||
Err(_) => Err("Cannot parse url"),
|
||||
Ok(url) => {
|
||||
let port = url.port_or_known_default().unwrap(); // patched version of `uri` DOES have a default
|
||||
if let Some(port) = url.port_or_known_default() { // patched version of `uri` DOES have a default
|
||||
let request = Request::from_url(url)?;
|
||||
let mut connection = std::net::TcpStream::connect(format!("{}:{}", request.host, port)).unwrap();
|
||||
if let Ok(mut connection) = std::net::TcpStream::connect(format!("{}:{}", request.host, port)) {
|
||||
let request_string = request.to_string();
|
||||
|
||||
match connection.write_all(request_string.as_bytes()) {
|
||||
|
@ -64,6 +63,12 @@ pub fn get(r: String) -> Result<String, &'static str> {
|
|||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Err("Failed to connect to host")
|
||||
}
|
||||
} else {
|
||||
Err("Could not figure out port")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user