3 releases (breaking)

0.3.0 May 10, 2020
0.2.0 May 10, 2020
0.1.0 May 10, 2020

#166 in #handler

MIT license

19KB
415 lines

Proxy handler for asynchronous streams.

This library allows for writing reverse proxies in rust. It's built on top of async-std and uses async-uninet which enables unified handling of asynchronous TCP & Unix streams.

Example

Program:

let address = SocketAddr::from_str("127.0.0.1:4444").await.unwrap(); // or `unix:`
let listener = Listener::bind(&address).await.unwrap();

while let Some(stream) = listener.incoming().next().await {
    let stream = stream.unwrap();

    task::spawn(async move {
        let mut router = Router::new(stream);
        let mut router = Router::new(stream);
        router.parse_request().await.unwrap();
        router.set_request_header("Host", "jsonplaceholder.typicode.com:80"); // override header
        router.relay_request().await.unwrap();
        router.parse_response().await.unwrap();
        router.write_response_header("Status", "fast"); // override header
        router.relay_response().await.unwrap();
    })
}

Execute:

$ curl -N \
  -H "Host: typicode" \
  -H "Content-Type: application/json" \
  http://localhost:4444/posts

Dependencies

~4–13MB
~167K SLoC