Skip to content

pimalaya/http

Repository files navigation

HTTP flows Documentation Matrix

I/O-free Rust library to send HTTP requests, based on stream-flows.

Design

This library does not perform any I/O. Instead, it exposes I/O-free, composable and iterable state machines so called flows.

A flow emits I/O requests, which should be processed by I/O handlers. A flow is considered terminated when it does not emit I/O requests anymore.

HTTP flows emit stream I/O requests defined by the stream-flows crate, so any I/O stream handler should work.

Examples

See complete examples at ./examples.

Send HTTPS request via rustls synchronously

use std::{net::TcpStream, sync::Arc};

use http_flows::{flows::Send, Request};
use rustls::{ClientConfig, ClientConnection, StreamOwned};
use rustls_platform_verifier::ConfigVerifierExt;
use stream_flows::handlers::std::Handler;

// build TLS stream
let domain = "github.com"
let config = ClientConfig::with_platform_verifier();
let server_name = domain.to_string().try_into().unwrap();
let conn = ClientConnection::new(Arc::new(config), server_name).unwrap();
let tcp = TcpStream::connect((domain, 443)).unwrap();
let tls = StreamOwned::new(conn, tcp);

// send request send receive response
let request = Request::new("GET", "/", "1.0").body("");
let mut send = Send::new(request);
let response = loop {
    match send.next() {
        Ok(response) => break response,
        Err(io) => Handler::handle(&mut stream, &mut send, io).unwrap(),
    }
};

See complete example at ./examples/send.rs.

cargo run --example send

Sponsoring

nlnet

Special thanks to the NLnet foundation and the European Commission that helped the project to receive financial support from various programs:

If you appreciate the project, feel free to donate using one of the following providers:

GitHub Ko-fi Buy Me a Coffee Liberapay thanks.dev PayPal