hyperlocal-0.8.0/.cargo_vcs_info.json0000644000000001121377245006000132370ustar { "git": { "sha1": "5ee680e3dace68aad9ae11269c9592e410d76216" } } hyperlocal-0.8.0/.github/workflows/main.yml010064400017510000164000000057031377245002700171210ustar 00000000000000name: Main on: workflow_dispatch: push: paths-ignore: - '*.md' branches: - main tags: - '**' pull_request: paths-ignore: - '*.md' branches: - main env: CARGO_TERM_COLOR: always jobs: codestyle: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 with: path: | ~/.cargo/registry ~/.cargo/git target key: ${{ runner.os }}-nightly-cargo-${{ hashFiles('Cargo.lock') }} - name: Set up Rust uses: hecrj/setup-rust-action@v1 with: components: rustfmt rust-version: nightly - run: cargo fmt --all -- --check lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 with: path: | ~/.cargo/registry ~/.cargo/git target key: ${{ runner.os }}-stable-cargo-${{ hashFiles('Cargo.lock') }} - name: Set up Rust uses: hecrj/setup-rust-action@v1 with: components: clippy - run: cargo clippy --all-targets --all-features -- -D clippy::all compile: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 with: path: | ~/.cargo/registry ~/.cargo/git target key: ${{ runner.os }}-stable-cargo-${{ hashFiles('Cargo.lock') }} - name: Set up Rust uses: hecrj/setup-rust-action@v1 - run: cargo check --all test: needs: [codestyle, lint, compile] strategy: matrix: os: [ubuntu-latest, macOS-latest] rust: [stable] runs-on: ${{ matrix.os }} steps: - name: Checkout uses: actions/checkout@v2 - name: Setup Rust uses: hecrj/setup-rust-action@v1 with: rust-version: ${{ matrix.rust }} - name: Test run: cargo test publish-docs: if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest needs: [test] steps: - name: Set up Rust uses: hecrj/setup-rust-action@v1 - uses: actions/checkout@v2 - name: Generate Docs run: | cargo doc --no-deps echo "" > target/doc/index.html - name: Publish uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./target/doc publish-crate: if: startsWith(github.ref, 'refs/tags/') needs: [test] runs-on: ubuntu-latest steps: - name: Set up Rust uses: hecrj/setup-rust-action@v1 - uses: actions/checkout@v1 - name: Publish if: env.CRATES_TOKEN shell: bash run: cargo publish --token ${{ env.CRATES_TOKEN }} env: CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}hyperlocal-0.8.0/.gitignore010064400017510000164000000000221377245002700140320ustar 00000000000000target Cargo.lock hyperlocal-0.8.0/.rustfmt.toml010064400017510000164000000001621377245002700145260ustar 00000000000000version = "Two" use_field_init_shorthand = true merge_imports = true wrap_comments = true use_try_shorthand = truehyperlocal-0.8.0/CHANGELOG.md010064400017510000164000000042071377245002700136640ustar 00000000000000# 0.8.0 * upgrade to tokio 1.0 and hyper 0.14 [#44](https://github.com/softprops/hyperlocal/pull/44) * port ci from Travis CI to GitHub Actions * `main` is the new default GitHub branch # 0.7.0 * reimplement server for `std::future` (`async`/`await`) * upgrade to tokio 0.2 * add `SocketIncoming` interface # 0.6.0 * upgrade to hyper 0.13 * upgrade hex to 0.3 [#15](https://github.com/softprops/hyperlocal/pull/15) * move from tokio-core to tokio 0.1 [#16](https://github.com/softprops/hyperlocal/pull/16) * don't explicitly block on unix socket connection [#18](https://github.com/softprops/hyperlocal/pull/18) * provide a more flexible set of Server interfaces and to align more closely with those of hyper's default server bindings [#19](https://github.com/softprops/hyperlocal/pull/19) You'll want to use `hyperlocal::server::Server` where you would have used `hyperlocal::server::Http` in the past and use `hyperlocal::server::Http` for a lower level interfaces that give you more control over "driving" your server. # 0.5.0 * upgrade to hyper 0.12 [#11](https://github.com/softprops/hyperlocal/pull/11) * expose the [SocketAddr](https://doc.rust-lang.org/std/os/unix/net/struct.SocketAddr.html) servers listen on with `Server#local_addr` # 0.4.1 * implement Clone for `UnixConnector` [@letmutx](https://github.com/softprops/hyperlocal/pull/7) # 0.4.0 * refactor for async hyper * `hyperlocal::DomainUrl` is now `hyperlocal::Uri` the semantics are the same but the name now matches hyper's new name can can be lifted into hypers type ```rust let uri: hyper:Uri = hyperlocal::Uri( "path/to/server.sock", "/foo/bar?baz=boom" ).into(); ``` * `hyperlocal::UnitSocketConnector` is now just `hyperlocal::UnixConnector` to be more inline with the naming conventions behind`hyper::HttpConnector` and `hyper_tls::HttpsConnector` * `hyperlocal::UnixSocketServer` is now `hyperlocal::server::Http` to be more inline with hyper naming conventions # 0.3.0 * enable using unix_socket from stdlib. [#4](https://github.com/softprops/hyperlocal/pull/4) * upgrade to hyper 0.10 # 0.2.0 * upgraded to hyper 0.9 and transitively url 1.0 # 0.1.0 Initial release hyperlocal-0.8.0/Cargo.lock0000644000000262561377245006000112330ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bytes" version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "bytes" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72" [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "futures-channel" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b7109687aa4e177ef6fe84553af6280ef2778bdb7783ba44c9dc3399110fe64" dependencies = [ "futures-core", ] [[package]] name = "futures-core" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748" [[package]] name = "futures-macro" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77408a692f1f97bcc61dc001d752e00643408fbc922e4d634c655df50d595556" dependencies = [ "proc-macro-hack", "proc-macro2", "quote", "syn", ] [[package]] name = "futures-task" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c554eb5bf48b2426c4771ab68c6b14468b6e76cc90996f528c3338d761a4d0d" dependencies = [ "once_cell", ] [[package]] name = "futures-util" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d304cff4a7b99cfb7986f7d43fbe93d175e72e704a8860787cc95e9ffd85cbd2" dependencies = [ "futures-core", "futures-macro", "futures-task", "pin-project", "pin-utils", "proc-macro-hack", "proc-macro-nested", "slab", ] [[package]] name = "hermit-abi" version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" dependencies = [ "libc", ] [[package]] name = "hex" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" [[package]] name = "http" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84129d298a6d57d246960ff8eb831ca4af3f96d29e2e28848dae275408658e26" dependencies = [ "bytes 0.5.6", "fnv", "itoa", ] [[package]] name = "http-body" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2861bd27ee074e5ee891e8b539837a9430012e249d7f0ca2d795650f579c1994" dependencies = [ "bytes 1.0.0", "http", ] [[package]] name = "httparse" version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" [[package]] name = "httpdate" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" [[package]] name = "hyper" version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7b980c7bc75203b968f06374cbde00bf1818e02e156b8e5b6ccf440fb53b6d0" dependencies = [ "bytes 1.0.0", "futures-channel", "futures-core", "futures-util", "http", "http-body", "httparse", "httpdate", "itoa", "pin-project", "socket2", "tokio", "tower-service", "tracing", "want", ] [[package]] name = "hyperlocal" version = "0.8.0" dependencies = [ "futures-util", "hex", "hyper", "pin-project", "tokio", ] [[package]] name = "itoa" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" [[package]] name = "log" version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ "cfg-if 0.1.10", ] [[package]] name = "memchr" version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] name = "mio" version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f33bc887064ef1fd66020c9adfc45bb9f33d75a42096c81e7c56c65b75dd1a8b" dependencies = [ "libc", "log", "miow", "ntapi", "winapi", ] [[package]] name = "miow" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" dependencies = [ "socket2", "winapi", ] [[package]] name = "ntapi" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" dependencies = [ "winapi", ] [[package]] name = "num_cpus" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" dependencies = [ "hermit-abi", "libc", ] [[package]] name = "once_cell" version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" [[package]] name = "pin-project" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "pin-project-lite" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c" [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "proc-macro-hack" version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro-nested" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" [[package]] name = "proc-macro2" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ "unicode-xid", ] [[package]] name = "quote" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" dependencies = [ "proc-macro2", ] [[package]] name = "slab" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" [[package]] name = "socket2" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" dependencies = [ "cfg-if 1.0.0", "libc", "winapi", ] [[package]] name = "syn" version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9802ddde94170d186eeee5005b798d9c159fa970403f1be19976d0cfb939b72" dependencies = [ "proc-macro2", "quote", "unicode-xid", ] [[package]] name = "tokio" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd" dependencies = [ "autocfg", "bytes 1.0.0", "libc", "memchr", "mio", "num_cpus", "pin-project-lite", "tokio-macros", ] [[package]] name = "tokio-macros" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42517d2975ca3114b22a16192634e8241dc5cc1f130be194645970cc1c371494" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "tower-service" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" [[package]] name = "tracing" version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3" dependencies = [ "cfg-if 1.0.0", "pin-project-lite", "tracing-core", ] [[package]] name = "tracing-core" version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" dependencies = [ "lazy_static", ] [[package]] name = "try-lock" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "unicode-xid" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] name = "want" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" dependencies = [ "log", "try-lock", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" hyperlocal-0.8.0/Cargo.toml0000644000000025101377245006000112410ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "hyperlocal" version = "0.8.0" authors = ["softprops "] description = "Hyper bindings for Unix domain sockets" homepage = "https://github.com/softprops/hyperlocal" readme = "README.md" keywords = ["hyper", "unix", "sockets", "http"] license = "MIT" repository = "https://github.com/softprops/hyperlocal" [dependencies.futures-util] version = "0.3" [dependencies.hex] version = "0.4" [dependencies.hyper] version = "0.14" features = ["server", "client", "http1", "runtime"] [dependencies.pin-project] version = "1.0" [dependencies.tokio] version = "1.0" features = ["rt-multi-thread", "net"] [dev-dependencies.tokio] version = "1.0" features = ["rt-multi-thread", "net", "macros", "io-std", "io-util"] [features] client = [] default = ["client", "server"] server = [] hyperlocal-0.8.0/Cargo.toml.orig010064400017510000164000000013721377245002700147420ustar 00000000000000[package] name = "hyperlocal" version = "0.8.0" authors = ["softprops "] description = "Hyper bindings for Unix domain sockets" homepage = "https://github.com/softprops/hyperlocal" repository = "https://github.com/softprops/hyperlocal" keywords = ["hyper", "unix", "sockets", "http"] license = "MIT" readme = "README.md" edition = "2018" [dependencies] hex = "0.4" hyper = { version = "0.14", features = ["server", "client", "http1", "runtime"] } tokio = { version = "1.0", features = ["rt-multi-thread", "net"] } pin-project = "1.0" futures-util = "0.3" [dev-dependencies] tokio = { version = "1.0", features = ["rt-multi-thread", "net", "macros", "io-std", "io-util"] } [features] client = [] server = [] default = ["client", "server"] hyperlocal-0.8.0/LICENSE010064400017510000164000000020441377245002700130550ustar 00000000000000Copyright (c) 2015-2020 Doug Tangren Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.hyperlocal-0.8.0/README.md010064400017510000164000000077551377245002700133450ustar 00000000000000
🔌 ✨

hyperlocal

Hyper client and server bindings for Unix domain sockets


Hyper is a rock solid [Rust](https://www.rust-lang.org/) HTTP client and server toolkit. [Unix domain sockets](https://en.wikipedia.org/wiki/Unix_domain_socket) 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](https://docs.docker.com/engine/misc/), a process container manager. ## Installation Add the following to your `Cargo.toml` file ```toml [dependencies] hyperlocal = "0.8" ``` ## Usage ### Servers A typical server can be built with `hyperlocal::server::UnixServerExt`. ```rust use std::{error::Error, fs, path::Path}; use hyper::{ service::{make_service_fn, service_fn}, Body, Response, Server, }; use hyperlocal::UnixServerExt; const PHRASE: &str = "It's a Unix system. I know this."; #[tokio::main] async fn main() -> Result<(), Box> { 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(()) } ``` To test that your server is working you can use an out of the box tool like `curl` ```sh $ curl --unix-socket /tmp/hyperlocal.sock localhost It's a Unix system. I know this. ``` ### Clients `hyperlocal` also provides bindings for writing unix domain socket based HTTP clients using `Hyper`'s native `Client` interface. Configure your `Hyper` client using `hyper::Client::builder()`. Hyper's client interface makes it easy to send 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 over the counter 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. ```rust use std::error::Error; use hyper::{body::HttpBody, Client}; use hyperlocal::{UnixClientExt, Uri}; use tokio::io::{self, AsyncWriteExt as _}; #[tokio::main] async fn main() -> Result<(), Box> { let url = Uri::new("/tmp/hyperlocal.sock", "/").into(); let client = Client::unix(); let mut response = client.get(url).await?; while let Some(next) = response.data().await { let chunk = next?; io::stdout().write_all(&chunk).await?; } Ok(()) } ``` Doug Tangren (softprops) 2015-2020 hyperlocal-0.8.0/examples/client.rs010064400017510000164000000007731377245002700155210ustar 00000000000000use hyper::{body::HttpBody, Client}; use hyperlocal::{UnixClientExt, Uri}; use std::error::Error; use tokio::io::{self, AsyncWriteExt as _}; #[tokio::main] async fn main() -> Result<(), Box> { let url = Uri::new("/tmp/hyperlocal.sock", "/").into(); let client = Client::unix(); let mut response = client.get(url).await?; while let Some(next) = response.data().await { let chunk = next?; io::stdout().write_all(&chunk).await?; } Ok(()) } hyperlocal-0.8.0/examples/server.rs010064400017510000164000000012451377245002700155440ustar 00000000000000use hyper::{ service::{make_service_fn, service_fn}, Body, Response, Server, }; use hyperlocal::UnixServerExt; use std::{error::Error, fs, path::Path}; const PHRASE: &str = "It's a Unix system. I know this."; #[tokio::main] async fn main() -> Result<(), Box> { 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(()) } hyperlocal-0.8.0/rustfmt.toml010064400017510000164000000005331377245002700144520ustar 00000000000000# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#fn_args_layout fn_args_layout = "Vertical" # https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#merge_imports merge_imports = true # https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#format_code_in_doc_comments format_code_in_doc_comments = truehyperlocal-0.8.0/src/client.rs010064400017510000164000000074731377245002700144760ustar 00000000000000use futures_util::future::BoxFuture; use hex::FromHex; use hyper::{ client::connect::{Connected, Connection}, service::Service, Body, Client, Uri, }; use pin_project::pin_project; use std::{ io, path::{Path, PathBuf}, pin::Pin, task::{Context, Poll}, }; use tokio::io::ReadBuf; #[pin_project] #[derive(Debug)] pub struct UnixStream { #[pin] unix_stream: tokio::net::UnixStream, } impl UnixStream { async fn connect

(path: P) -> std::io::Result where P: AsRef, { let unix_stream = tokio::net::UnixStream::connect(path).await?; Ok(Self { unix_stream }) } } impl tokio::io::AsyncWrite for UnixStream { fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { self.project().unix_stream.poll_write(cx, buf) } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { self.project().unix_stream.poll_flush(cx) } fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { self.project().unix_stream.poll_shutdown(cx) } } impl tokio::io::AsyncRead for UnixStream { fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll> { self.project().unix_stream.poll_read(cx, buf) } } /// the `[UnixConnector]` can be used to construct a `[hyper::Client]` which can /// speak to a unix domain socket. /// /// # Example /// ``` /// use hyper::{Client, Body}; /// use hyperlocal::UnixConnector; /// /// let connector = UnixConnector; /// let client: Client = Client::builder().build(connector); /// ``` /// /// # Note /// If you don't need access to the low-level `[hyper::Client]` builder /// interface, consider using the `[UnixClientExt]` trait instead. #[derive(Clone, Copy, Debug, Default)] pub struct UnixConnector; impl Unpin for UnixConnector {} impl Service for UnixConnector { type Response = UnixStream; type Error = std::io::Error; type Future = BoxFuture<'static, Result>; fn call(&mut self, req: Uri) -> Self::Future { let fut = async move { let path = parse_socket_path(req)?; UnixStream::connect(path).await }; Box::pin(fut) } fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } } impl Connection for UnixStream { fn connected(&self) -> Connected { Connected::new() } } fn parse_socket_path(uri: Uri) -> Result { if uri.scheme_str() != Some("unix") { return Err(io::Error::new( io::ErrorKind::InvalidInput, "invalid URL, scheme must be unix", )); } if let Some(host) = uri.host() { let bytes = Vec::from_hex(host).map_err(|_| { io::Error::new( io::ErrorKind::InvalidInput, "invalid URL, host must be a hex-encoded path", ) })?; Ok(PathBuf::from(String::from_utf8_lossy(&bytes).into_owned())) } else { Err(io::Error::new( io::ErrorKind::InvalidInput, "invalid URL, host must be present", )) } } /// Extention trait for constructing a hyper HTTP client over a Unix domain /// socket. pub trait UnixClientExt { /// Construct a client which speaks HTTP over a Unix domain socket /// /// # Example /// ``` /// use hyper::Client; /// use hyperlocal::UnixClientExt; /// /// let client = Client::unix(); /// ``` fn unix() -> Client { Client::builder().build(UnixConnector) } } impl UnixClientExt for Client {} hyperlocal-0.8.0/src/lib.rs010064400017510000164000000020201377245002700137450ustar 00000000000000#![deny( missing_debug_implementations, unreachable_pub, rust_2018_idioms, missing_docs )] //! `hyperlocal` provides [Hyper](http://github.com/hyperium/hyper) bindings //! for [Unix domain sockets](https://github.com/tokio-rs/tokio/tree/master/tokio-net/src/uds/). //! //! See the [`UnixClientExt`] docs for //! how to configure clients. //! //! See the //! [`UnixServerExt`] docs for how to //! configure servers. //! //! The [`UnixConnector`] can be used in the [`hyper::Client`] builder //! interface, if required. //! //! # Features //! //! - Client- enables the client extension trait and connector. *Enabled by //! default*. //! //! - Server- enables the server extension trait. *Enabled by default*. #[cfg(feature = "client")] mod client; #[cfg(feature = "client")] pub use client::{UnixClientExt, UnixConnector}; #[cfg(feature = "server")] mod server; #[cfg(feature = "server")] pub use server::UnixServerExt; mod uri; pub use uri::Uri; #[cfg(feature = "server")] pub use crate::server::conn::SocketIncoming; hyperlocal-0.8.0/src/server.rs010064400017510000164000000053321377245002700145160ustar 00000000000000use std::{io, path::Path}; use hyper::server::{Builder, Server}; use conn::SocketIncoming; pub(crate) mod conn { use futures_util::ready; use hyper::server::accept::Accept; use pin_project::pin_project; use std::{ io, path::Path, pin::Pin, task::{Context, Poll}, }; use tokio::net::{UnixListener, UnixStream}; /// A stream of connections from binding to a socket. #[pin_project] #[derive(Debug)] pub struct SocketIncoming { listener: UnixListener, } impl SocketIncoming { /// Creates a new `SocketIncoming` binding to provided socket path. pub fn bind(path: impl AsRef) -> Result { let listener = UnixListener::bind(path)?; Ok(Self { listener }) } /// Creates a new `SocketIncoming` from Tokio's `UnixListener` /// /// ```rust,ignore /// let socket = SocketIncoming::from_listener(unix_listener); /// let server = Server::builder(socket).serve(service); /// ``` pub fn from_listener(listener: UnixListener) -> Self { Self { listener } } } impl Accept for SocketIncoming { type Conn = UnixStream; type Error = io::Error; fn poll_accept( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll>> { let conn = ready!(self.listener.poll_accept(cx))?.0; Poll::Ready(Some(Ok(conn))) } } impl From for SocketIncoming { fn from(listener: UnixListener) -> Self { Self::from_listener(listener) } } } /// Extension trait for provisioning a hyper HTTP server over a Unix domain /// socket. /// /// # Example /// /// ```rust /// use hyper::{Server, Body, Response, service::{make_service_fn, service_fn}}; /// use hyperlocal::UnixServerExt; /// /// # async { /// let make_service = make_service_fn(|_| async { /// Ok::<_, hyper::Error>(service_fn(|_req| async { /// Ok::<_, hyper::Error>(Response::new(Body::from("It works!"))) /// })) /// }); /// /// Server::bind_unix("/tmp/hyperlocal.sock")?.serve(make_service).await?; /// # Ok::<_, Box>(()) /// # }; /// ``` pub trait UnixServerExt { /// Convenience method for constructing a Server listening on a Unix socket. fn bind_unix(path: impl AsRef) -> Result, io::Error>; } impl UnixServerExt for Server { fn bind_unix(path: impl AsRef) -> Result, io::Error> { let incoming = SocketIncoming::bind(path)?; Ok(Server::builder(incoming)) } } hyperlocal-0.8.0/src/uri.rs010064400017510000164000000022421377245002700140040ustar 00000000000000use hyper::Uri as HyperUri; use std::path::Path; /// A convenience type that can be used to construct Unix Domain Socket URIs /// /// This type implements `Into`. /// /// # Example /// ``` /// use hyperlocal::Uri; /// use hyper::Uri as HyperUri; /// /// let uri: HyperUri = Uri::new("/tmp/hyperlocal.sock", "/").into(); /// ``` #[derive(Debug)] pub struct Uri { hyper_uri: HyperUri, } impl Uri { /// Create a new `[Uri]` from a socket address and a path pub fn new(socket: impl AsRef, path: &str) -> Self { let host = hex::encode(socket.as_ref().to_string_lossy().as_bytes()); let host_str = format!("unix://{}:0{}", host, path); let hyper_uri: HyperUri = host_str.parse().unwrap(); Self { hyper_uri } } } impl From for HyperUri { fn from(uri: Uri) -> Self { uri.hyper_uri } } #[cfg(test)] mod tests { use super::Uri; use hyper::Uri as HyperUri; #[test] fn test_unix_uri_into_hyper_uri() { let unix: HyperUri = Uri::new("foo.sock", "/").into(); let expected: HyperUri = "unix://666f6f2e736f636b:0/".parse().unwrap(); assert_eq!(unix, expected); } } hyperlocal-0.8.0/tests/server_client.rs010064400017510000164000000024041377245002700164240ustar 00000000000000use std::{error::Error, fs, path::Path}; use hyper::{ body::HttpBody, service::{make_service_fn, service_fn}, Body, Client, Response, Server, }; use hyperlocal::{UnixClientExt, UnixServerExt, Uri}; #[tokio::test] async fn test_server_client() -> Result<(), Box> { 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("It works!"))) })) }); let (tx, rx) = tokio::sync::oneshot::channel::<()>(); let server = Server::bind_unix("/tmp/hyperlocal.sock")? .serve(make_service) .with_graceful_shutdown(async { rx.await.unwrap() }); tokio::spawn(async move { server.await.unwrap() }); let client = Client::unix(); let url = Uri::new(path, "/").into(); let mut response = client.get(url).await?; let mut bytes = Vec::default(); while let Some(next) = response.data().await { let chunk = next?; bytes.extend(chunk); } let string = String::from_utf8(bytes)?; tx.send(()).unwrap(); assert_eq!(string, "It works!"); Ok(()) }