shared_http_body
A Rust library for creating shareable HTTP bodies that can be cloned and consumed by multiple tasks.
Overview
shared_http_body provides SharedBody, which allows you to create an HTTP body that can be cloned and shared across multiple consumers. All clones share the same underlying body state, so clones created at the same time will see the same frames, while clones created after partial consumption will only see the remaining frames.
Quick Example
use SharedBodyExt;
use ;
use Frame;
use Bytes;
use stream;
async
Key Features
- Cloneable HTTP bodies: Create multiple consumers from a single body
- Thread-safe:
Send+Sync- clones can be moved across threads - Efficient sharing: Frames are cloned only when consumed by each clone
- Works with any
Unpinbody: Compatible with most HTTP body types - Preserves HTTP semantics: Maintains
size_hint()andis_end_stream()behavior - Convenient extension trait: Use
.into_shared()on any body viaSharedBodyExt
Use Cases
Common scenarios where cloning HTTP bodies is useful:
- Dark Forwarding — Clone the body to send it to a secondary destination for testing in production or shadow traffic analysis.
- Request Retries — Keep a clone of the request body for retry attempts on failure.
- Request/Response Logging — Clone the body to log it while still forwarding to the handler.
Requirements
- The underlying body must implement
Unpin - Body data type (
Body::Data) must implementClone - Body error type (
Body::Error) must implementClone - For thread safety, the body and its data/error types must be
Send+Sync
How It Works
SharedBody uses futures_util::future::Shared internally to share the state of frame consumption across clones. Each frame is wrapped in a ClonableFrame that can clone both data frames and trailer frames. This allows multiple consumers to independently consume the same body without interference.
License
Licensed under the Apache License, Version 2.0.