hyper

repository·master·Indexed 12 days ago

https://github.com/hyperium/hyper

A fast, correct, and asynchronous HTTP implementation for Rust supporting HTTP/1 and HTTP/2. Version 1.11.0 provides low-level Client and Server APIs, serving as a foundational building block for other Rust web frameworks. It features a shift toward connection-level APIs in 1.0, an unstable C API for FFI, and a design that avoids implicit runtime dependencies by requiring explicit Executor and Timer implementations.

Tokens
22.3K
Snippets
57
Records
99
Agent score
96%

What's inside hyper

  1. Overview of hyper

    master
    hyper is a high-performance, asynchronous HTTP library for Rust that supports both HTTP/1 and HTTP/2. It provides both Client and Server APIs and is designed as a low-level building block for other libraries and applications. It is highly tested for correctness and widely used in production environments.
  2. Understand the hyper 1.0 architectural shift to Connection-level APIs

    master

    In hyper 1.0, the primary API focus has shifted from high-level Client and Server abstractions to lower-level Connection types. The previous high-level hyper::Client and hyper::Server helpers have been removed from the core crate and moved to hyper-util.

    Developers should now interact directly with connection management via version-specific modules. This design provides more flexibility and stability, especially as the project prepares for HTTP/3 support which requires different transport abstractions than the standard AsyncRead + AsyncWrite model.

  3. Choosing the right HTTP library

    master

    hyper is a low-level library. Depending on your use case, you may prefer higher-level abstractions built on top of it:

    • For a convenient HTTP client: Use reqwest.
    • For an HTTP server: Use axum or warp (which uses a functional approach). Both are built on top of hyper.
  4. Use hyper-util for experimental or non-stable features

    master
    The hyper-util crate contains useful components that do not yet meet hyper's stability promise. While hyper is highly stable, hyper-util is intended for experimentation and providing utilities that might eventually be promoted into the core hyper crate. Use hyper-util when you need features that are not yet part of the stable public API.
  5. Identify hyper's scope and non-goals

    master

    To choose the right tool for your project, note that the following features are explicitly out of scope for hyper and should be handled by other libraries:

    • TLS: hyper does not bundle TLS. It is designed to allow users to bring their own TLS implementation.
    • Routing: hyper does not provide HTTP routing capabilities.
    • Cookies: hyper does not manage HTTP cookies.
    • Non-HTTP Protocols: Protocols like WebSockets are not implemented in hyper. While hyper can be used to facilitate a protocol upgrade, the actual implementation of the next protocol should be handled by a separate library.
  6. Understand the architecture and layers of hyper

    master

    hyper is designed as a layered library where the user provides their own runtime and IO. hyper operates on top of the provided IO and returns Futures that the user polls using their chosen runtime.

    The architecture consists of three main layers:

    1. Protocol Codecs: Dedicated codecs for major HTTP versions designed for correctness and speed. These may be implemented as sub-crates to allow users to build custom connection management or customize encoding/decoding.
    2. Connection State Management: This layer enforces HTTP protocol correctness, such as enforcing content-length, managing frame order/flags, handling connection-terminating headers (e.g., Connection: close), and processing connection-level frames in HTTP/2 and HTTP/3.
    3. HTTP Role and Version Abstraction (Public API): The primary interface for users. It focuses on sending and receiving http::Requests and http::Responses via a client or server Connection interface, abstracting away version-specific framing. Version-specific functionality can be accessed via http::Extensions.
  7. Understand hyper's stability and MSRV promises

    master

    hyper follows semantic versioning and provides the following stability guarantees:

    • Major Version Stability: Major versions of hyper are promised to be stable for 3 years. Breaking changes are deferred until after this period.
    • Minor Versions: New features are released frequently in minor versions.
    • Minimum Supported Rust Version (MSRV): hyper supports Rust versions that are at least 6 months old. If a new Rust feature is required, hyper will not adopt it until at least 6 months after the Rust version release. hyper only requires new Rust versions as a minor release (1.x), never as a patch (1.x.y).
  8. Dependency guidelines for `hyper-util`

    master

    The hyper-util crate is intended to be more flexible but is explicitly less stable than the core hyper crate.

    Best Practices:

    • Avoid publicly exposing hyper-util in your own library's public API.
    • If your library depends on a trait from hyper-util (e.g., for a high-level Client or Server), it is recommended to define your own abstraction and provide an internal adapter for your users. This prevents your users from being forced to depend on an unstable crate.
  9. Check code style with rustfmt

    master

    hyper uses the default configuration of rustfmt. Note that cargo fmt --all is not supported in this repository. Instead, use the platform-specific commands below to check the formatting of all .rs files using the 2021 edition.

    # Mac or Linux
    rustfmt --check --edition 2021 $(git ls-files '*.rs')
    
    # Powershell
    Get-ChildItem . -Filter "*.rs" -Recurse | foreach { rustfmt --check --edition 2021 $_.FullName }
  10. Build the hyper C API using cargo

    master

    To compile the C API as part of the Rust library, use cargo rustc with the required unstable flag and specific features enabled. You must specify --crate-type cdylib to produce a dynamic library suitable for C consumption.

    RUSTFLAGS="--cfg hyper_unstable_ffi" cargo rustc --features client,http1,http2,ffi --crate-type cdylib
  11. Build and install the hyper shared library with cargo-c

    master

    If you are using cargo-c, you can build and install a shared library version of the C API using the cbuild command. Ensure you include the ffi feature and the unstable configuration flag.

    RUSTFLAGS="--cfg hyper_unstable_ffi" cargo cbuild --features client,http1,http2,ffi --release
  12. Run hyper examples

    master

    If you have cloned the hyper repository locally, you can run any of the provided examples using cargo run. You must enable the full feature to ensure all dependencies required by the examples are available.

    cargo run --example {example_name} --features="full"