hyper
repository·master·Indexed 12 days ago
https://github.com/hyperium/hyperA 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.
What's inside hyper
- 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.
Understand the hyper 1.0 architectural shift to Connection-level APIs
masterIn hyper 1.0, the primary API focus has shifted from high-level
ClientandServerabstractions to lower-levelConnectiontypes. The previous high-levelhyper::Clientandhyper::Serverhelpers have been removed from the core crate and moved tohyper-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 + AsyncWritemodel.Choosing the right HTTP library
masterUse hyper-util for experimental or non-stable features
masterThehyper-utilcrate contains useful components that do not yet meet hyper's stability promise. Whilehyperis highly stable,hyper-utilis intended for experimentation and providing utilities that might eventually be promoted into the corehypercrate. Usehyper-utilwhen you need features that are not yet part of the stable public API.Identify hyper's scope and non-goals
masterTo 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.
Understand the architecture and layers of hyper
masterhyper 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:
- 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.
- 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. - HTTP Role and Version Abstraction (Public API): The primary interface for users. It focuses on sending and receiving
http::Requests andhttp::Responses via a client or serverConnectioninterface, abstracting away version-specific framing. Version-specific functionality can be accessed viahttp::Extensions.
Understand hyper's stability and MSRV promises
masterhyper 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).
Dependency guidelines for `hyper-util`
masterThe
hyper-utilcrate is intended to be more flexible but is explicitly less stable than the corehypercrate.Best Practices:
- Avoid publicly exposing
hyper-utilin 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.
- Avoid publicly exposing
Check code style with rustfmt
masterhyper uses the default configuration of
rustfmt. Note thatcargo fmt --allis not supported in this repository. Instead, use the platform-specific commands below to check the formatting of all.rsfiles 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 }Build the hyper C API using cargo
masterTo compile the C API as part of the Rust library, use
cargo rustcwith the required unstable flag and specific features enabled. You must specify--crate-type cdylibto produce a dynamic library suitable for C consumption.RUSTFLAGS="--cfg hyper_unstable_ffi" cargo rustc --features client,http1,http2,ffi --crate-type cdylibBuild and install the hyper shared library with cargo-c
masterIf you are using
cargo-c, you can build and install a shared library version of the C API using thecbuildcommand. Ensure you include theffifeature and the unstable configuration flag.RUSTFLAGS="--cfg hyper_unstable_ffi" cargo cbuild --features client,http1,http2,ffi --releaseRun hyper examples
masterIf you have cloned the
hyperrepository locally, you can run any of the provided examples usingcargo run. You must enable thefullfeature to ensure all dependencies required by the examples are available.cargo run --example {example_name} --features="full"