diff --git a/src/addresses.rs b/src/addresses.rs index 6086b3f..6365a32 100644 --- a/src/addresses.rs +++ b/src/addresses.rs @@ -33,8 +33,8 @@ fn add_addresses_to_map( let incoming_host = match incoming_uri.host() { Some(h) => h, _ => { - return Err(Error::Custom( - "could not parse host from source uri".to_string(), + return Err(Error::Addresses( + "Could not parse host from source uri".to_string(), )); } }; diff --git a/src/config.rs b/src/config.rs index cdd4c4c..63ecf07 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,7 @@ use crate::errors::Error; use serde::{Deserialize, Serialize}; use serde_json; -use std::path; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use tokio::fs; #[derive(Clone, Serialize, Deserialize, Debug)] @@ -20,8 +19,8 @@ pub struct Config { pub dangerous_self_signed_addresses: Option>, } -pub async fn from_filepath(filepath: &PathBuf) -> Result { - let config_path = match path::absolute(filepath) { +pub async fn from_filepath(filepath: &Path) -> Result { + let config_path = match filepath.canonicalize() { Ok(pb) => pb, Err(e) => return Err(Error::Io(e)), }; @@ -34,8 +33,8 @@ pub async fn from_filepath(filepath: &PathBuf) -> Result { let parent_dir = match config_path.parent() { Some(p) => p.to_path_buf(), _ => { - return Err(Error::Custom( - "parent directory of config not found".to_string(), + return Err(Error::Config( + "Parent directory of config not found".to_string(), )); } }; diff --git a/src/errors.rs b/src/errors.rs index 6fd83e2..7ee964c 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,21 +6,23 @@ use std::io::Error as IoError; #[derive(Debug)] pub enum Error { + Addresses(String), + Config(String), Io(IoError), SerdeJson(SerdeJsonError), NativeTls(NativeTlsError), Uri(InvalidUri), - Custom(String), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::Io(err) => write!(f, "{}", err), - Error::NativeTls(err) => write!(f, "{}", err), - Error::SerdeJson(err) => write!(f, "{}", err), - Error::Uri(err) => write!(f, "{}", err), - Error::Custom(err) => write!(f, "{}", err), + Error::Addresses(err) => write!(f, "[Error::Addresses] {}", err), + Error::Config(err) => write!(f, "[Error::Config] {}", err), + Error::Io(err) => write!(f, "[Error::Io] {}", err), + Error::NativeTls(err) => write!(f, "[Error::Native_tls] {}", err), + Error::SerdeJson(err) => write!(f, "[Error::Serde_json] {}", err), + Error::Uri(err) => write!(f, "[Error::Uri] {}", err), } } } diff --git a/src/main.rs b/src/main.rs index efbcfed..742a62f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,8 @@ async fn main() -> Result<(), Error> { let args = match env::args().nth(1) { Some(a) => path::PathBuf::from(a), None => { - return Err(Error::Custom( - "argument error: argv[0] config path not provided".to_string(), + return Err(Error::Config( + "Config path not provided at argv[0]".to_string(), )); } }; diff --git a/src/requests.rs b/src/requests.rs index 656ac5b..f2a3e9c 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -16,7 +16,7 @@ const FAILED_TO_PROCESS_REQUEST_ERROR: &str = "failed to process request"; fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> { let host = match uri.host() { Some(h) => h.to_string(), - _ => return Err("failed to retrieve downstream URI from upstream URI"), + _ => return Err("Failed to foward incoming URI to outgoing URI"), }; let port = match uri.port() { @@ -40,7 +40,7 @@ fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> { async fn create_tcp_stream<'a>(addr: &str) -> Result, &'a str> { match TcpStream::connect(&addr).await { Ok(client_stream) => Ok(TokioIo::new(client_stream)), - _ => Err("failed to establish tcp connection"), + _ => Err("Failed to establish tcp connection"), } } @@ -54,18 +54,18 @@ async fn create_tls_stream<'a>( let cx = match builder.build() { Ok(c) => c, - _ => return Err("failed to build TLS connection"), + _ => return Err("Failed to build TLS connection"), }; let tls_connector = tokio_native_tls::TlsConnector::from(cx); let client_stream = match TcpStream::connect(addr).await { Ok(s) => s, - _ => return Err("failed to establish TCP connection"), + _ => return Err("Failed to establish TCP connection"), }; let tls_stream = match tls_connector.connect(host, client_stream).await { Ok(s) => TokioIo::new(s), - _ => return Err("failed to establish TLS connection"), + _ => return Err("TLS connection failed"), }; Ok(tls_stream) diff --git a/src/response.rs b/src/response.rs index d744201..67133a5 100644 --- a/src/response.rs +++ b/src/response.rs @@ -6,10 +6,6 @@ use hyper::http::uri::InvalidUriParts; use hyper::{Request, Response, StatusCode, Uri}; use std::collections::HashMap; -pub type BoxedResponse = Response>; - -pub const URI_FROM_REQUEST_ERROR: &str = "unable to parse upstream URI from request"; -pub const UPSTREAM_URI_ERROR: &str = "falied to update request with upstream URI"; #[derive(Clone, Debug)] pub struct AddressParams { @@ -18,6 +14,9 @@ pub struct AddressParams { } pub type AddressMap = HashMap; +pub type BoxedResponse = Response>; + +pub const UPSTREAM_URI_ERROR: &str = "Falied to update request with upstream URI"; fn update_request_with_dest_uri( mut req: Request, diff --git a/src/service.rs b/src/service.rs index c94353d..6c71dd7 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,5 +1,4 @@ use crate::requests; -use crate::response; use crate::response::{AddressMap, BoxedResponse, build_response}; use hyper::body::Incoming; use hyper::http; @@ -9,6 +8,8 @@ use std::future::Future; use std::pin::Pin; use std::sync::Arc; +pub const URI_FROM_REQUEST_ERROR: &str = "Unnable to parse upstream URI from request"; + pub struct Svc { pub addresses: Arc, } @@ -27,7 +28,7 @@ impl Service> for Svc { return Box::pin(async move { requests::create_fallback_response( &StatusCode::BAD_REQUEST, - &response::URI_FROM_REQUEST_ERROR, + URI_FROM_REQUEST_ERROR, ) }); } @@ -40,7 +41,7 @@ impl Service> for Svc { return Box::pin(async move { requests::create_fallback_response( &StatusCode::BAD_GATEWAY, - &response::URI_FROM_REQUEST_ERROR, + URI_FROM_REQUEST_ERROR, ) }); }