Crate async_h1

source ·
Expand description

Streaming async HTTP 1.1 parser.

At its core HTTP is a stateful RPC protocol, where a client and server communicate with one another by encoding and decoding messages between them.

  • client encodes HTTP requests, and decodes HTTP responses.
  • server decodes HTTP requests, and encodes HTTP responses.

A client always starts the HTTP connection. The lifetime of an HTTP connection looks like this:

1. encode            2. decode
        \            /
        -> request  ->
 client                server
        <- response <-
        /            \
4. decode            3. encode

See also async-tls, async-std.


HTTP client

use async_std::net::TcpStream;
use http_types::{Method, Request, Url};

async fn main() -> http_types::Result<()> {
    let stream = TcpStream::connect("").await?;

    let peer_addr = stream.peer_addr()?;
    println!("connecting to {}", peer_addr);
    let url = Url::parse(&format!("http://{}/foo", peer_addr))?;

    let req = Request::new(Method::Get, url);
    let res = async_h1::connect(stream.clone(), req).await?;
    println!("{:?}", res);


HTTP Server

use async_std::net::{TcpStream, TcpListener};
use async_std::prelude::*;
use async_std::task;
use http_types::{Response, StatusCode};

async fn main() -> http_types::Result<()> {
    // Open up a TCP connection and create a URL.
    let listener = TcpListener::bind(("", 8080)).await?;
    let addr = format!("http://{}", listener.local_addr()?);
    println!("listening on {}", addr);

    // For each incoming TCP connection, spawn a task and call `accept`.
    let mut incoming = listener.incoming();
    while let Some(stream) = {
        let stream = stream?;
        task::spawn(async {
            if let Err(err) = accept(stream).await {
                eprintln!("{}", err);

// Take a TCP stream, and convert it into sequential HTTP request / response pairs.
async fn accept(stream: TcpStream) -> http_types::Result<()> {
    println!("starting new connection from {}", stream.peer_addr()?);
    async_h1::accept(stream.clone(), |_req| async move {
        let mut res = Response::new(StatusCode::Ok);
        res.insert_header("Content-Type", "text/plain");



  • Process HTTP connections on the client.
  • Process HTTP connections on the server.


  • like ready! but early-returns the Poll<Result> early in all situations other than Ready(Ok(0))