CodeSampleX

示例

http 1.4.0: http::Response

已验证示例 — cargo http 1.4.0: http::Response. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过: http::Response::new and Response::default construct 200 OK…

sha256:d8aab6de7888173dfc3b8885b39bd4077913bd6366ef34d3961613c875646209

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux · alpine · musl x64 cargo

验证运行环境

环境 契约 阶段 运行日期
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-11-17

案例

HOW
目标
verify http::Response in pkg:cargo/http@1.4.0
包
符号
  • http::Response
创建时间
2026-11-17T00:17:47Z

契约

  1. http::Response::new and Response::default construct 200 OK HTTP/1.1 responses with empty headers and the provided or default body
  2. http::Response::builder configures status, HTTP version, headers, and typed extensions, and returns Err on invalid header values
  3. http::Response mutable accessors update status_mut, version_mut, headers_mut, extensions_mut, and body_mut in place
  4. http::Response::into_parts, Response::from_parts, and Response::map decompose, reconstruct, and transform response bodies while preserving metadata

文件

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • test/contract.rs

下载源代码构件 (tar.gz)

源代码

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 = "http"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a"
dependencies = [
 "bytes",
 "itoa",
]

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

[[package]]
name = "sample-http-response"
version = "1.0.0"
dependencies = [
 "http",
]
Cargo.toml
[package]
name = "sample-http-response"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
http = "=1.4.0"

[[bin]]
name = "contract"
path = "test/contract.rs"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify http::Response in pkg:cargo/http@1.4.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/http@1.4.0
Demonstrate these symbols/APIs:
  - http::Response

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:8ded6a574edb9129a1bda4cc72a896a50bf494d6013f8e055518f4451ba4f5c7","contract":["http::Response::new and Response::default construct 200 OK HTTP/1.1 responses with empty headers and the provided or default body","http::Response::builder configures status, HTTP version, headers, and typed extensions, and returns Err on invalid header values","http::Response mutable accessors update status_mut, version_mut, headers_mut, extensions_mut, and body_mut in place","http::Response::into_parts, Response::from_parts, and Response::map decompose, reconstruct, and transform response bodies while preserving metadata"],"goal":"verify http::Response in pkg:cargo/http@1.4.0","kind":"HOW","packages":["pkg:cargo/http@1.4.0"],"schemaVersion":1,"symbols":["http::Response"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"cargo","libc":"musl","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/http@1.4.0"],"schemaVersion":1,"subject":"pkg:cargo/http@1.4.0","symbols":["http::Response"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify http::Response in pkg:cargo/http@1.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/http@1.4.0"
  ],
  "symbols": [
    "http::Response"
  ]
}
src/lib.rs
//! Clean-room verification sample for http::Response.

pub use http::Response;
test/contract.rs
use http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE, LOCATION};
use http::{Response, StatusCode, Version};

#[derive(Clone, Debug, PartialEq, Eq)]
struct TraceId(u64);

fn main() {
    // 1. Response::new constructs a 200 OK HTTP/1.1 response with empty headers and the given body
    let basic = Response::new("hello world");
    assert_eq!(basic.status(), StatusCode::OK);
    assert_eq!(basic.version(), Version::HTTP_11);
    assert!(basic.headers().is_empty());
    assert_eq!(*basic.body(), "hello world");
    assert_eq!(basic.into_body(), "hello world");

    // 2. Response::default constructs a default 200 OK response when the body implements Default
    let default_resp: Response<Vec<u8>> = Response::default();
    assert_eq!(default_resp.status(), StatusCode::OK);
    assert_eq!(default_resp.version(), Version::HTTP_11);
    assert!(default_resp.body().is_empty());

    // 3. Response::builder configures status, version, headers, and typed extensions, and rejects invalid header values
    let built = Response::builder()
        .status(StatusCode::CREATED)
        .version(Version::HTTP_2)
        .header(CONTENT_TYPE, "application/json")
        .header(LOCATION, "https://example.com/items/42")
        .extension(TraceId(42))
        .body(String::from("{\"id\":42}"))
        .expect("valid response builder");

    assert_eq!(built.status(), StatusCode::CREATED);
    assert_eq!(built.version(), Version::HTTP_2);
    assert_eq!(
        built.headers().get(CONTENT_TYPE),
        Some(&HeaderValue::from_static("application/json"))
    );
    assert_eq!(
        built.headers().get(LOCATION),
        Some(&HeaderValue::from_static("https://example.com/items/42"))
    );
    assert_eq!(built.extensions().get::<TraceId>(), Some(&TraceId(42)));
    assert_eq!(built.body(), "{\"id\":42}");

    let invalid = Response::builder()
        .header("x-invalid", "bad\nvalue")
        .body(());
    assert!(invalid.is_err());

    // 4. Mutable accessors update status, version, headers, extensions, and body in place
    let mut mutable = Response::new(String::from("initial"));
    *mutable.status_mut() = StatusCode::ACCEPTED;
    *mutable.version_mut() = Version::HTTP_10;
    mutable
        .headers_mut()
        .insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
    mutable.extensions_mut().insert(TraceId(99));
    mutable.body_mut().push_str(" payload");

    assert_eq!(mutable.status(), StatusCode::ACCEPTED);
    assert_eq!(mutable.version(), Version::HTTP_10);
    assert_eq!(
        mutable.headers().get(CONTENT_TYPE),
        Some(&HeaderValue::from_static("text/plain"))
    );
    assert_eq!(mutable.extensions().get::<TraceId>(), Some(&TraceId(99)));
    assert_eq!(mutable.body(), "initial payload");

    // 5. into_parts, from_parts, and map transform response bodies while preserving metadata
    let (mut parts, body_text) = mutable.into_parts();
    assert_eq!(parts.status, StatusCode::ACCEPTED);
    parts.status = StatusCode::OK;
    let body_len = body_text.len();
    parts.headers.insert(
        CONTENT_LENGTH,
        HeaderValue::from_str(&body_len.to_string()).expect("valid content-length"),
    );

    let reconstructed = Response::from_parts(parts, body_text);
    let byte_response: Response<Vec<u8>> = reconstructed.map(String::into_bytes);

    assert_eq!(byte_response.status(), StatusCode::OK);
    assert_eq!(byte_response.version(), Version::HTTP_10);
    assert_eq!(
        byte_response.headers().get(CONTENT_LENGTH),
        Some(&HeaderValue::from_static("15"))
    );
    assert_eq!(
        byte_response.extensions().get::<TraceId>(),
        Some(&TraceId(99))
    );
    assert_eq!(byte_response.body().as_slice(), b"initial payload");
}

原始种子者

匿名