From f209757ed6d0ae186297f363201547ee62ff209a Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Fri, 12 Mar 2021 12:21:02 -0800 Subject: [PATCH] Handle proxy unwraps --- src/proxy.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index 2f0e3fe..433f763 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -19,8 +19,8 @@ pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide: /// Relays the `Content-Length` and `Content-Type` header. async fn request(url: String) -> tide::Result { // Parse url into parts - let parts = Url::parse(&url).unwrap(); - let host = parts.host().unwrap().to_string(); + let parts = Url::parse(&url)?; + let host = parts.host().map(|host| host.to_string()).unwrap_or_default(); let domain = parts.domain().unwrap_or_default(); let path = format!("{}?{}", parts.path(), parts.query().unwrap_or_default()); // Build reddit-compliant user agent for Libreddit @@ -36,17 +36,17 @@ async fn request(url: String) -> tide::Result { let connector = TlsConnector::default(); // Open a TCP connection - let tcp_stream = TcpStream::connect(format!("{}:443", domain)).await.unwrap(); + let tcp_stream = TcpStream::connect(format!("{}:443", domain)).await?; // Use the connector to start the handshake process - let mut tls_stream = connector.connect(domain, tcp_stream).await.unwrap(); + let mut tls_stream = connector.connect(domain, tcp_stream).await?; // Write the aforementioned HTTP request to the stream - tls_stream.write_all(req.as_bytes()).await.unwrap(); + tls_stream.write_all(req.as_bytes()).await?; // And read the response let mut writer = Vec::new(); - io::copy(&mut tls_stream, &mut writer).await.unwrap(); + io::copy(&mut tls_stream, &mut writer).await?; // Find the delimiter which separates the body and headers match (0..writer.len()).find(|i| writer[i.to_owned()] == 10_u8 && writer[i - 2] == 10_u8) {