6 releases (breaking)

0.5.0 Aug 12, 2018
0.4.1 Oct 8, 2017
0.4.0 Aug 14, 2017
0.3.0 Jan 16, 2017
0.1.0 Jan 3, 2016

#7 in HTTP client

Download history 306/week @ 2018-08-06 383/week @ 2018-08-13 442/week @ 2018-08-20 451/week @ 2018-08-27 522/week @ 2018-09-03 370/week @ 2018-09-10 845/week @ 2018-09-17 814/week @ 2018-09-24 620/week @ 2018-10-01 433/week @ 2018-10-08 469/week @ 2018-10-15 530/week @ 2018-10-22 340/week @ 2018-10-29

2,014 downloads per month
Used in 4 crates (2 directly)

MIT license

23KB
345 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 rustlang HTTP client and server tool kit. 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.

install

Add the following to your Cargo.toml file

[dependencies]
hyperlocal = "0.5"

usage

servers

A typical server can be built with hyperlocal::server::Server

extern crate hyper;
extern crate hyperlocal;
extern crate futures;

use std::fs;
use std::io;

use hyper::{header, Body, Request, Response};
use hyper::service::service_fn;

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

fn hello(_: Request<Body>) -> impl futures::Future<Item = Response<Body>, Error = io::Error> + Send {
    futures::future::ok(
        Response::builder()
            .header(header::CONTENT_TYPE, "text/plain")
            .header(header::CONTENT_LENGTH, PHRASE.len() as u64)
            .body(PHRASE.into())
            .expect("failed to create response")
    )
}

fn run() -> io::Result<()> {
    let path = "test.sock";
    if let Err(err) = fs::remove_file("test.sock") {
        if err.kind() != io::ErrorKind::NotFound {
            return Err(err);
        }
    }
    let svr = hyperlocal::server::Server::new().bind(path, || service_fn(hello))?;
    println!("Listening on unix://{path} with 1 thread.", path = path);
    svr.run()?;
    Ok(())
}

fn main() {
    if let Err(err) = run() {
        eprintln!("error starting server: {}", err)
    }
}

clients

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

Hyper's client interface makes it easy to issue typical HTTP methods like GET, POST, DELETE with factory methods, get, post, delete, ect. 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 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.

extern crate futures;
extern crate hyper;
extern crate hyperlocal;
extern crate tokio_core;

use std::io::{self, Write};

use futures::Stream;
use futures::Future;
use hyper::{Client, rt};
use hyperlocal::{Uri, UnixConnector};

fn main() {
    let client = Client::builder()
        .keep_alive(false) // without this the connection will remain open
        .build::<_, ::hyper::Body>(UnixConnector::new());
    let url = Uri::new("test.sock", "/").into();

    let work = client
        .get(url)
        .and_then(|res| {
            println!("Response: {}", res.status());
            println!("Headers: {:#?}", res.headers());

            res.into_body().for_each(|chunk| {
                io::stdout().write_all(&chunk)
                    .map_err(|e| panic!("example expects stdout is open, error={}", e))
            })
        })
        .map(|_| {
            println!("\n\nDone.");
        })
        .map_err(|err| {
            eprintln!("Error {}", err);
        });

    rt::run(work);
}

Doug Tangren (softprops) 2015-2018

Dependencies

~6MB
~103K SLoC