Skip to content
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

Feature: listen on unix domain sockets #935

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
153 changes: 140 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@ url = "2"
# fake_tty does not support Windows for now
fake-tty = "0.3.1"

[target.'cfg(unix)'.dev-dependencies]
# Only used to test unix sockets
curl = "0.4"

[build-dependencies]
grass = { version = "0.11", default-features = false }
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ this example, you'd need to use `-u -v`.
This uses the `--media-type` option, which sends a hint for the expected media type to the browser.
Some mobile browsers like Firefox on Android will offer to open the camera app when seeing this.

### Listen on a unix domain socket

miniserve -p /tmp/web.socket

You can test access to this socket using curl: `curl -X GET --unix-socket /tmp/web.socket http:/localhost/index.html`

## Features

- Easy to use
Expand Down
18 changes: 15 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub enum MediaType {
Video,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Interface {
Address(IpAddr),
Path(PathBuf),
}

#[derive(Parser)]
#[command(name = "miniserve", author, about, version)]
pub struct CliArgs {
Expand Down Expand Up @@ -52,7 +58,7 @@ pub struct CliArgs {
value_parser(parse_interface),
num_args(1)
)]
pub interfaces: Vec<IpAddr>,
pub interfaces: Vec<Interface>,

/// Set authentication. Currently supported formats:
/// username:password, username:sha256:hash, username:sha512:hash
Expand Down Expand Up @@ -190,8 +196,14 @@ pub struct CliArgs {
}

/// Checks whether an interface is valid, i.e. it can be parsed into an IP address
fn parse_interface(src: &str) -> Result<IpAddr, std::net::AddrParseError> {
src.parse::<IpAddr>()
fn parse_interface(src: &str) -> Result<Interface, std::net::AddrParseError> {
let first_char = src.chars().next().unwrap();

if first_char == '.' || first_char == '/' {
Ok(Interface::Path(PathBuf::from(src)))
} else {
Ok(Interface::Address(src.parse::<IpAddr>()?))
}
}

/// Parse authentication requirement
Expand Down
Loading