Skip to content

Instantly share code, notes, and snippets.

@vi

vi/hyperstdioclient.crs

Last active Jan 12, 2019
Embed
What would you like to do?
Rust Hyper low-level client demo - make request to stdout, read reply from stdin; also similar server demo
#!/usr/bin/env run-cargo-script
//! ```cargo
//! [dependencies]
//! hyper = { version = "0.12.20", default-features = false }
//! tokio-stdin-stdout = "0.1.5"
//! readwrite = {version="0.1.1", features=["tokio"]}
//! futures = "0.1.25"
//! tokio = "0.1.14"
//! ```
extern crate hyper;
extern crate futures;
extern crate tokio;
extern crate tokio_stdin_stdout;
extern crate readwrite;
use hyper::client::conn as cc;
use readwrite::ReadWriteAsync;
use tokio_stdin_stdout as tstdio;
use futures::future::Future;
use tokio::runtime::current_thread;
use hyper::{Request,Body};
fn main() -> Result<(), Box<std::error::Error>> {
let mut rt = current_thread::Builder::new().build()?;
let io = ReadWriteAsync(
tstdio::stdin(0).make_sendable(),
tstdio::stdout(0).make_sendable(),
);
let hs = cc::handshake(io);
let (mut sr, conn) = rt.block_on(hs)?;
rt.spawn(conn.map_err(|e|eprintln!("{}", e)));
let rq = Request::builder().uri("/lol").body(Body::empty())?;
let resp = rt.block_on(sr.send_request(rq))?;
eprintln!("{:#?}", resp);
Ok(())
}
/*
$ socat exec:./hyperstdioclient.crs tcp:example.com:80
Compiling hyperstdioclient v0.1.0 (/home/vi/.cargo/script-cache/file-hyperstdioclient-001f89356bb0c5d4)
Finished release [optimized] target(s) in 10.20s
Response {
status: 400,
version: HTTP/1.1,
headers: {
"content-type": "text/html",
"content-length": "349",
"connection": "close",
"date": "Sat, 12 Jan 2019 22:00:14 GMT",
"server": "ECSF (dca/24FA)"
},
body: Body
}
*/
#!/usr/bin/env run-cargo-script
//! ```cargo
//! [dependencies]
//! hyper = { version = "0.12.20", default-features = false }
//! tokio-stdin-stdout = "0.1.5"
//! readwrite = {version="0.1.1", features=["tokio"]}
//! futures = "0.1.25"
//! tokio = "0.1.14"
//! ```
extern crate hyper;
extern crate tokio_stdin_stdout;
extern crate readwrite;
extern crate tokio;
extern crate futures;
use hyper::server::conn as sc;
use readwrite::ReadWriteAsync;
use tokio_stdin_stdout as tstdio;
use futures::future::Future;
use tokio::runtime::current_thread;
use hyper::{Request,Body,Response};
use hyper::service::service_fn;
fn main() -> Result<(), Box<std::error::Error>> {
let mut rt = current_thread::Builder::new().build()?;
let io = ReadWriteAsync(
tstdio::stdin(0).make_sendable(),
tstdio::stdout(0).make_sendable(),
);
let mut h = sc::Http::new();
h.http1_only(true);
h.keep_alive(false);
//h.with_executor(rt);
let service = service_fn(|req: Request<Body>| {
eprintln!("request: {:#?}", req);
use futures::stream::Stream;
req.into_body().concat2().and_then(|whole_body| {
eprintln!("whole body: {:?}", whole_body);
futures::future::ok::<_,hyper::Error>(
Response::new(Body::from("Hello World\n"))
)
})
});
rt.block_on(h.serve_connection(io, service))?;
Ok(())
}
/*
$ ./hyperstdioserver.crs
> POST /qqq HTTP/1.1
> Host: lol
> Content-Length: 3
>
< request: Request {
< method: POST,
< uri: /qqq,
< version: HTTP/1.1,
< headers: {
< "host": "lol",
< "content-length": "3"
< },
< body: Body
< }
> 12345
< whole body: b"123"
< HTTP/1.1 200 OK
< content-length: 12
< date: Sat, 12 Jan 2019 23:02:53 GMT
<
< Hello World
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.