2 releases

0.3.15 Feb 4, 2023
0.3.14 Oct 1, 2022

#271 in HTTP client

Download history 235/week @ 2023-11-20 197/week @ 2023-11-27 198/week @ 2023-12-04 237/week @ 2023-12-11 282/week @ 2023-12-18 283/week @ 2023-12-25 315/week @ 2024-01-01 252/week @ 2024-01-08 395/week @ 2024-01-15 198/week @ 2024-01-22 396/week @ 2024-01-29 197/week @ 2024-02-05 255/week @ 2024-02-12 313/week @ 2024-02-19 283/week @ 2024-02-26 314/week @ 2024-03-04

1,187 downloads per month
Used in 16 crates (2 directly)

MIT license

745KB
17K SLoC

H2 for WebAssembly

A Tokio aware, HTTP/2 client & server implementation for Rust. Compiled to WebAssembly.

Features

  • Client and server HTTP/2 implementation.
  • Implements the full HTTP/2 specification.
  • Passes h2spec.
  • Focus on performance and correctness.
  • Built on Tokio.

Non goals

This crate is intended to only be an implementation of the HTTP/2 specification. It does not handle:

  • Managing TCP connections
  • HTTP 1.0 upgrade
  • TLS
  • Any feature not described by the HTTP/2 specification.

This crate is now used by hyper, which will provide all of these features.

Usage

To use h2, first add this to your Cargo.toml:

[dependencies]
h2_wasi = "0.3"

Next, add this to your crate:

extern crate h2;

use h2::server::Connection;

fn main() {
    // ...
}

lib.rs:

An asynchronous, HTTP/2 server and client implementation.

This library implements the HTTP/2 specification. The implementation is asynchronous, using futures as the basis for the API. The implementation is also decoupled from TCP or TLS details. The user must handle ALPN and HTTP/1.1 upgrades themselves.

Getting started

Add the following to your Cargo.toml file:

[dependencies]
h2 = "0.3"

Layout

The crate is split into client and server modules. Types that are common to both clients and servers are located at the root of the crate.

See module level documentation for more details on how to use h2.

Handshake

Both the client and the server require a connection to already be in a state ready to start the HTTP/2 handshake. This library does not provide facilities to do this.

There are three ways to reach an appropriate state to start the HTTP/2 handshake.

  • Opening an HTTP/1.1 connection and performing an upgrade.
  • Opening a connection with TLS and use ALPN to negotiate the protocol.
  • Open a connection with prior knowledge, i.e. both the client and the server assume that the connection is immediately ready to start the HTTP/2 handshake once opened.

Once the connection is ready to start the HTTP/2 handshake, it can be passed to server::handshake or client::handshake. At this point, the library will start the handshake process, which consists of:

  • The client sends the connection preface (a predefined sequence of 24 octets).
  • Both the client and the server sending a SETTINGS frame.

See the Starting HTTP/2 in the specification for more details.

Flow control

Flow control is a fundamental feature of HTTP/2. The h2 library exposes flow control to the user.

An HTTP/2 client or server may not send unlimited data to the peer. When a stream is initiated, both the client and the server are provided with an initial window size for that stream. A window size is the number of bytes the endpoint can send to the peer. At any point in time, the peer may increase this window size by sending a WINDOW_UPDATE frame. Once a client or server has sent data filling the window for a stream, no further data may be sent on that stream until the peer increases the window.

There is also a connection level window governing data sent across all streams.

Managing flow control for inbound data is done through FlowControl. Managing flow control for outbound data is done through SendStream. See the struct level documentation for those two types for more details.

Dependencies

~5–16MB
~147K SLoC