8 releases (breaking)

✓ Uses Rust 2018 edition

new 0.7.0 Mar 22, 2020
0.6.0 Sep 23, 2018
0.5.0 Aug 12, 2018
0.4.1 Oct 8, 2017
0.1.0 Jan 3, 2016

#5 in HTTP client

Download history 3948/week @ 2019-12-02 4636/week @ 2019-12-09 3441/week @ 2019-12-16 1427/week @ 2019-12-23 1271/week @ 2019-12-30 3605/week @ 2020-01-06 3043/week @ 2020-01-13 2963/week @ 2020-01-20 4245/week @ 2020-01-27 3706/week @ 2020-02-03 3456/week @ 2020-02-10 3485/week @ 2020-02-17 3992/week @ 2020-02-24 3434/week @ 2020-03-02 3770/week @ 2020-03-09 3412/week @ 2020-03-16

13,813 downloads per month
Used in 14 crates (7 directly)

MIT license

14KB
206 lines

hyperlocal Build Status Coverage Status crates.io docs.rs Master API docs

Hyper client and server bindings for Unix domain sockets

Hyper is a rock solid Rust HTTP client and server toolkit. Unix domain sockets provide a mechanism for host-local interprocess communication. hyperlocal builds on and complements Hyper's interfaces for building Unix domain socket HTTP clients and servers.

This is useful for exposing simple HTTP interfaces for your Unix daemons in cases where you want to limit access to the current host, in which case, opening and exposing tcp ports is not needed. Examples of Unix daemons that provide this kind of host local interface include Docker, a process container manager.

Installation

Add the following to your Cargo.toml file

[dependencies]
hyperlocal = "0.7"

Usage

Servers

A typical server can be built with hyperlocal::server::UnixServerExt.

use std::{error::Error, fs, path::Path};

use hyper::{
    service::{make_service_fn, service_fn},
    Body, Response, Server,
};
use hyperlocal::UnixServerExt;

const PHRASE: &'static str = "It's a Unix system. I know this.";

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

    if path.exists() {
        fs::remove_file(path)?;
    }

    let make_service = make_service_fn(|_| async {
        Ok::<_, hyper::Error>(service_fn(|_req| async {
            Ok::<_, hyper::Error>(Response::new(Body::from(PHRASE)))
        }))
    });

    Server::bind_unix(path)?.serve(make_service).await?;

    Ok(())
}

Clients

You can communicate over HTTP with Unix domain socket servers using Hyper's Client interface. Configure your Hyper client using Client::builder().

Hyper's client interface makes it easy to issue typical HTTP methods like GET, POST, DELETE with factory methods, get, post, delete, etc. These require an argument that can be tranformed into a hyper::Uri. Since Unix domain sockets aren't represented with hostnames that resolve to ip addresses coupled with network ports, your standard URL string won't do. Instead, use a hyperlocal::Uri, which represents both file path to the domain socket and the resource URI path and query string.

use std::error::Error;
use std::path::Path;

use futures_util::stream::TryStreamExt;
use hyper::{Body, Client};
use hyperlocal::{Uri, UnixClientExt};

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

    let client = Client::unix();

    let response_body = client.get(url).await?.into_body;
    
    let bytes = response_body
        .try_fold(Vec::default(), |mut buf, bytes| async {
            buf.extend(bytes);
            Ok(buf)
        })
        .await?;

    println!("{}", String::from_utf8(bytes)?);

    Ok(())
}

Doug Tangren (softprops) 2015-2018

Dependencies

~6MB
~117K SLoC