CodeSampleX

Beispiel

http-body-util 0.1.5: Handle http-body-util Collected body aggregation, to_bytes conversion, trailer inspection order, and Infallible error boxing

Verifiziertes Beispiel für cargo http-body-util 0.1.5: Handle http-body-util Collected body aggregation, to_bytes conversion, trailer inspection order, and…

sha256:c9718cb574b223b501bc0543170c23d73dfe9db56bda6c876822d6dfe6c1b005

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
3
Signaturschlüssel, die es gebaut haben
3
Deklarierte Umgebung rust linux x64 rust rust cargo

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

Fall

HOW
Ziel
Handle http-body-util Collected body aggregation, to_bytes conversion, trailer inspection order, and Infallible error boxing
Pakete
Symbole
  • http_body_util::BodyExt::collect
  • http_body_util::Collected::to_bytes
  • http_body_util::Collected::aggregate
  • http_body_util::Collected::trailers
  • http_body_util::Full
  • http_body_util::Empty
  • http_body_util::Limited
Umgebung
rust
Erstellt
2026-08-17T02:33:40Z

Contract

  1. Collected::aggregate().chunk() yields only the first frame chunk on multi-frame bodies, not the entire contiguous payload
  2. Collected::to_bytes() consumes Collected by value and concatenates all data frames into a single Bytes buffer
  3. Collected::trailers() borrows Collected and must be inspected before to_bytes() consumes the instance
  4. Full and Empty bodies have Infallible error types requiring map_err to construct heterogeneous BoxBody with dynamic error types
  5. Limited returns a boxed LengthLimitError when accumulated body bytes exceed the configured limit

Dateien

  • .gitignore
  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs
  • tests/contract.rs

Quellartefakt herunterladen (tar.gz)

Quelltext

.gitignore
/target
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "bytes"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"

[[package]]
name = "futures-core"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e"

[[package]]
name = "futures-macro"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "futures-task"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd"

[[package]]
name = "futures-util"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc"
dependencies = [
 "futures-core",
 "futures-macro",
 "futures-task",
 "pin-project-lite",
 "slab",
]

[[package]]
name = "http"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
dependencies = [
 "bytes",
 "itoa",
]

[[package]]
name = "http-body"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c"
dependencies = [
 "bytes",
 "http",
]

[[package]]
name = "http-body-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c"
dependencies = [
 "bytes",
 "futures-core",
 "http",
 "http-body",
 "pin-project-lite",
]

[[package]]
name = "http_body_util_contract"
version = "0.1.0"
dependencies = [
 "bytes",
 "futures-util",
 "http",
 "http-body",
 "http-body-util",
 "tokio",
]

[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"

[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "slab"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"

[[package]]
name = "syn"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "tokio"
version = "1.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed"
dependencies = [
 "pin-project-lite",
 "tokio-macros",
]

[[package]]
name = "tokio-macros"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
Cargo.toml
[package]
name = "http_body_util_contract"
version = "0.1.0"
edition = "2021"

[dependencies]
bytes = "1.12.1"
futures-util = "0.3.34"
http = "1.5.0"
http-body = "1.1.0"
http-body-util = "0.1.5"
tokio = { version = "1.53.1", features = ["rt", "macros"] }
NOTES.md
# Implementation Notes: http-body-util

## Search Result
`search_known_solution` returned `NO_SAFE_MATCH` / no relevant matching sample for `http-body-util` in the cargo ecosystem (closest alternatives were unrelated Python httpx and JS undici samples).

## What a naive model would write
A naive model upgrading from Hyper 0.14 assumes `collected.aggregate().chunk()` returns the entire contiguous body payload or attempts to inspect `collected.trailers()` after calling `collected.to_bytes()`.

## How it fails
Calling `collected.aggregate().chunk()` on a multi-frame streaming body compiles cleanly with a green build but silently truncates the payload to only the first frame chunk (e.g. 7 bytes instead of 13 bytes), causing partial reads or JSON parsing errors downstream.
csx.json
{"case":{"caseId":"case:sha256:b17d2d3d7ebb3fd1aa58c9c4b859f3f63b1324f2c4c721cc03f2d7b09a76231a","contract":["Collected::aggregate().chunk() yields only the first frame chunk on multi-frame bodies, not the entire contiguous payload","Collected::to_bytes() consumes Collected by value and concatenates all data frames into a single Bytes buffer","Collected::trailers() borrows Collected and must be inspected before to_bytes() consumes the instance","Full and Empty bodies have Infallible error types requiring map_err to construct heterogeneous BoxBody with dynamic error types","Limited returns a boxed LengthLimitError when accumulated body bytes exceed the configured limit"],"goal":"Handle http-body-util Collected body aggregation, to_bytes conversion, trailer inspection order, and Infallible error boxing","kind":"HOW","packages":["pkg:cargo/http-body-util@0.1.5"],"schemaVersion":1,"symbols":["http_body_util::BodyExt::collect","http_body_util::Collected::to_bytes","http_body_util::Collected::aggregate","http_body_util::Collected::trailers","http_body_util::Full","http_body_util::Empty","http_body_util::Limited"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/http-body-util@0.1.5"],"schemaVersion":1,"symbols":["http_body_util::BodyExt::collect","http_body_util::Collected::to_bytes","http_body_util::Collected::aggregate","http_body_util::Collected::trailers","http_body_util::Full","http_body_util::Empty","http_body_util::Limited"],"verifierAdapter":"cargo@1"}
src/lib.rs
//! Verification of http-body-util API behaviors and contracts.
tests/contract.rs
use bytes::{Buf, Bytes};
use http::HeaderMap;
use http_body::Frame;
use http_body_util::{
    combinators::BoxBody, BodyExt, Empty, Full, LengthLimitError, Limited, StreamBody,
};
use std::convert::Infallible;
use std::error::Error as StdError;

#[tokio::test]
async fn test_aggregate_buf_chunk_does_not_return_full_payload_for_multiframe_body() {
    // Multi-frame streaming body (common in chunked HTTP transfers)
    let chunk1 = Bytes::from_static(b"Hello, ");
    let chunk2 = Bytes::from_static(b"world!");
    let stream = futures_util::stream::iter(vec![
        Ok::<_, Infallible>(Frame::data(chunk1)),
        Ok::<_, Infallible>(Frame::data(chunk2)),
    ]);
    let body = StreamBody::new(stream);

    // BodyExt::collect aggregates all body frames into Collected<Bytes>
    let collected = body.collect().await.expect("collect should succeed");

    // TRAP: Calling collected.aggregate() returns an `impl Buf`.
    // Naive models assume Buf::chunk(&self) returns the entire contiguous body (&[u8]).
    // In reality, chunk() returns ONLY the first frame chunk!
    let mut agg = collected.aggregate();
    assert_eq!(agg.remaining(), 13, "Total remaining payload is 13 bytes");
    assert_eq!(
        agg.chunk(),
        b"Hello, ",
        "Buf::chunk() only exposes the first chunk (7 bytes), NOT the full 13 bytes"
    );
    assert_ne!(
        agg.chunk().len(),
        agg.remaining(),
        "chunk().len() must not equal remaining() on multi-frame bodies"
    );

    // To read the entire payload through Buf, callers must consume it across chunks
    let full_copy = agg.copy_to_bytes(agg.remaining());
    assert_eq!(full_copy, &b"Hello, world!"[..]);
}

#[tokio::test]
async fn test_collected_to_bytes_returns_contiguous_bytes_and_consumes_collected() {
    let stream = futures_util::stream::iter(vec![
        Ok::<_, Infallible>(Frame::data(Bytes::from_static(b"chunk1_"))),
        Ok::<_, Infallible>(Frame::data(Bytes::from_static(b"chunk2"))),
    ]);
    let collected = StreamBody::new(stream).collect().await.unwrap();

    // to_bytes(self) consumes the Collected instance and concatenates all data frames
    let total_bytes: Bytes = collected.to_bytes();
    assert_eq!(total_bytes, Bytes::from_static(b"chunk1_chunk2"));
}

#[tokio::test]
async fn test_collected_trailers_must_be_inspected_before_to_bytes_consumes_it() {
    let mut headers = HeaderMap::new();
    headers.insert("x-trailer-key", "trailer-val".parse().unwrap());

    let stream = futures_util::stream::iter(vec![
        Ok::<_, Infallible>(Frame::data(Bytes::from_static(b"body-data"))),
        Ok::<_, Infallible>(Frame::trailers(headers)),
    ]);
    let collected = StreamBody::new(stream).collect().await.unwrap();

    // trailers(&self) borrows Collected; must be inspected before to_bytes(self) moves it
    let trailer_val = collected
        .trailers()
        .and_then(|t| t.get("x-trailer-key"))
        .map(|v| v.to_str().unwrap().to_string());
    assert_eq!(trailer_val.as_deref(), Some("trailer-val"));

    // Now consume Collected to get contiguous data bytes (trailers are excluded from bytes)
    let body_bytes = collected.to_bytes();
    assert_eq!(body_bytes, Bytes::from_static(b"body-data"));
}

#[tokio::test]
async fn test_full_and_empty_error_type_is_infallible_requiring_map_err_to_box() {
    let full: Full<Bytes> = Full::new(Bytes::from_static(b"payload"));
    let empty: Empty<Bytes> = Empty::new();

    // Default error type of Full/Empty is Infallible
    let _boxed_infallible: BoxBody<Bytes, Infallible> = full.boxed();

    // To unify with standard dynamic error traits Box<dyn StdError + Send + Sync>,
    // callers must map the Infallible error explicitly before boxing:
    let boxed_dyn: BoxBody<Bytes, Box<dyn StdError + Send + Sync>> =
        empty.map_err(|e: Infallible| -> Box<dyn StdError + Send + Sync> { match e {} }).boxed();

    let collected = boxed_dyn.collect().await.unwrap();
    assert_eq!(collected.to_bytes(), Bytes::new());
}

#[tokio::test]
async fn test_limited_body_returns_boxed_length_limit_error() {
    let payload = Bytes::from_static(b"payload_that_is_too_long");
    let limited = Limited::new(Full::new(payload), 5);

    let res = limited.collect().await;
    assert!(res.is_err(), "Exceeding limit must return an Err");

    let err = res.unwrap_err();
    assert!(
        err.downcast_ref::<LengthLimitError>().is_some(),
        "Limited error must downcast to LengthLimitError"
    );
}

Ursprungs-Seeder

csx-seed