Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
CompuIves committed Sep 4, 2024
2 parents d254744 + aa7bb12 commit a579ece
Show file tree
Hide file tree
Showing 15 changed files with 477 additions and 323 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
with:
rust-version: ${{ matrix.rust }}
- name: Test
run: cargo test
run: cargo test --all-features

publish-docs:
if: github.ref == 'refs/heads/main'
Expand All @@ -97,7 +97,7 @@ jobs:
- uses: actions/checkout@v2
- name: Generate Docs
run: |
cargo doc --no-deps
cargo doc --no-deps --all-features
echo "<meta http-equiv=refresh content=0;url=`echo ${{ github.repository }} | cut -d / -f 2 | tr '-' '_'`/index.html>" > target/doc/index.html
- name: Publish
uses: peaceiris/actions-gh-pages@v3
Expand All @@ -118,4 +118,4 @@ jobs:
shell: bash
run: cargo publish --token ${{ env.CRATES_TOKEN }}
env:
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}
5 changes: 0 additions & 5 deletions .rustfmt.toml

This file was deleted.

53 changes: 31 additions & 22 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
# 0.9.1

- Export UnixStream for clients [#60](https://github.com/softprops/hyperlocal/pull/60) via [@onalante-msft](https://github.com/onalante-msft)
- Adding support for UnixStream to take advantage of vectored writes [#63](https://github.com/softprops/hyperlocal/pull/63) via [@craftytrickster](https://github.com/craftytrickster)

# 0.9.0

- upgrade to hyper 1.1 [#65](https://github.com/softprops/hyperlocal/pull/65) via [iamjpotts](https://github.com/iamjpotts)

# 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
- 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
- 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)
- 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`
- 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)
- 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
- 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 =
Expand All @@ -42,18 +51,18 @@ let uri: hyper:Uri =
"/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

- `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
- 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

- upgraded to hyper 0.9 and transitively url 1.0

# 0.1.0

Expand Down
46 changes: 37 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
[package]
name = "hyperlocal"
version = "0.8.0"
version = "0.9.1"
authors = ["softprops <d.tangren@gmail.com>"]
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"
edition = "2021"

[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"
http-body-util = { version = "0.1", optional = true }
hyper = "1.3"
hyper-util = { version = "0.1.2", optional = true }
tokio = { version = "1.35", default-features = false, features = ["net"] }
tower-service = { version = "0.3", optional = true }
pin-project-lite = "0.2"

[dev-dependencies]
tokio = { version = "1.0", features = ["rt-multi-thread", "net", "macros", "io-std", "io-util"] }
thiserror = "1.0"
tokio = { version = "1.35", features = [
"io-std",
"io-util",
"macros",
"rt-multi-thread",
] }

[features]
client = []
server = []
default = ["client", "server"]
client = [
"http-body-util",
"hyper/client",
"hyper/http1",
"hyper-util/client-legacy",
"hyper-util/http1",
"hyper-util/tokio",
"tower-service",
]
server = ["hyper/http1", "hyper/server", "hyper-util/tokio"]

[[example]]
name = "client"
required-features = ["client"]

[[example]]
name = "server"
required-features = ["server"]

[[test]]
name = "server_client"
required-features = ["client", "server"]
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015-2020 Doug Tangren
Copyright (c) 2015-2024 Doug Tangren

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -17,4 +17,4 @@ 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.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 15 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div align="center">
🔌 ✨
🔌 ✨

</div>

<h1 align="center">
Expand Down Expand Up @@ -40,55 +41,28 @@ want to limit access to the current host, in which case, opening and exposing tc
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"
hyperlocal = "0.9"
```

## 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<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?;
A typical server can be built by creating a `tokio::net::UnixListener` and accepting connections in a loop using
`hyper::service::service_fn` to create a request/response processing function, and connecting the `UnixStream` to it
using `hyper::server::conn::http1::Builder::new().serve_connection()`.

Ok(())
}
`hyperlocal` provides an extension trait `UnixListenerExt` with an implementation of this.

```

To test that your server is working you can use an out of the box tool like `curl`
An example is at [examples/server.rs](./examples/server.rs), runnable via `cargo run --example server`

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
Expand All @@ -98,38 +72,18 @@ 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.
`hyperlocal` also provides bindings for writing unix domain socket based HTTP clients the `Client` interface from the
`hyper-utils` crate.
Configure your `Hyper` client using `hyper::Client::builder()`.
An example is at [examples/client.rs](./examples/client.rs), runnable via `cargo run --example client`
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`.
methods, `get`, `post`, `delete`, etc. These require an argument that can be transformed 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<dyn Error + Send + Sync>> {
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
Doug Tangren (softprops) 2015-2024
17 changes: 11 additions & 6 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use hyper::{body::HttpBody, Client};
use hyperlocal::{UnixClientExt, Uri};
use http_body_util::{BodyExt, Full};
use hyper::body::Bytes;
use hyper_util::client::legacy::Client;
use hyperlocal::{UnixClientExt, UnixConnector, Uri};
use std::error::Error;
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 client = Client::unix();
let client: Client<UnixConnector, Full<Bytes>> = 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?;
while let Some(frame_result) = response.frame().await {
let frame = frame_result?;

if let Some(segment) = frame.data_ref() {
io::stdout().write_all(segment.iter().as_slice()).await?;
}
}

Ok(())
Expand Down
32 changes: 20 additions & 12 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use 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.";
use hyper::Response;
use tokio::net::UnixListener;

use hyperlocal::UnixListenerExt;

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

// Adapted from https://hyper.rs/guides/1/server/hello-world/
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let path = Path::new("/tmp/hyperlocal.sock");
Expand All @@ -15,13 +16,20 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
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)))
}))
});
let listener = UnixListener::bind(path)?;

println!("Listening for connections at {}.", path.display());

listener
.serve(|| {
println!("Accepted connection.");

Server::bind_unix(path)?.serve(make_service).await?;
|_request| async {
let body = PHRASE.to_string();
Ok::<_, hyper::Error>(Response::new(body))
}
})
.await?;

Ok(())
}
Loading

0 comments on commit a579ece

Please sign in to comment.