Skip to content

Return an error if path is invalid #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::io::{self, AsyncWriteExt as _};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let url = Uri::new("/tmp/hyperlocal.sock", "/").into();
let url = Uri::new("/tmp/hyperlocal.sock", "/").unwrap().into();

let client: Client<UnixConnector, Full<Bytes>> = Client::unix();

Expand Down
17 changes: 9 additions & 8 deletions src/uri.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hyper::Uri as HyperUri;
use hyper::{http::uri::InvalidUri, Uri as HyperUri};
use std::path::Path;

/// A convenience type that can be used to construct Unix Domain Socket URIs
Expand All @@ -10,7 +10,7 @@ use std::path::Path;
/// use hyper::Uri as HyperUri;
/// use hyperlocal::Uri;
///
/// let uri: HyperUri = Uri::new("/tmp/hyperlocal.sock", "/").into();
/// let uri: HyperUri = Uri::new("/tmp/hyperlocal.sock", "/").unwrap().into();
/// ```
#[derive(Debug, Clone)]
pub struct Uri {
Expand All @@ -20,17 +20,18 @@ pub struct Uri {
impl Uri {
/// Create a new `[Uri]` from a socket address and a path
///
/// # Panics
/// Will panic if path is not absolute and/or a malformed path string.
/// # Errors
///
/// Returns an error if path is not absolute and/or a malformed path string.
pub fn new(
socket: impl AsRef<Path>,
path: &str,
) -> Self {
) -> Result<Self, InvalidUri> {
let host = hex::encode(socket.as_ref().to_string_lossy().as_bytes());
let host_str = format!("unix://{host}:0{path}");
let hyper_uri: HyperUri = host_str.parse().unwrap();
let hyper_uri: HyperUri = host_str.parse()?;

Self { hyper_uri }
Ok(Self { hyper_uri })
}
}

Expand All @@ -47,7 +48,7 @@ mod tests {

#[test]
fn test_unix_uri_into_hyper_uri() {
let unix: HyperUri = Uri::new("foo.sock", "/").into();
let unix: HyperUri = Uri::new("foo.sock", "/").unwrap().into();
let expected: HyperUri = "unix://666f6f2e736f636b:0/".parse().unwrap();
assert_eq!(unix, expected);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/server_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn test_server_client() -> Result<(), Box<dyn Error + Send + Sync>> {

let client: Client<UnixConnector, Full<Bytes>> = Client::unix();

let url = Uri::new(path, "/").into();
let url = Uri::new(path, "/").unwrap().into();

let mut response = client.get(url).await?;
let mut bytes = Vec::default();
Expand Down