Crate http_kit

Crate http_kit 

Source
Expand description

A flexible and ergonomic HTTP toolkit for Rust.

This crate provides high-level abstractions for HTTP operations while maintaining performance and type safety. It’s designed to be no-std compatible with optional standard library features.

§Features

  • Type-safe HTTP primitives - Request, Response, Headers, and Body types with strong type checking
  • Streaming support - Efficient handling of large payloads through streaming interfaces
  • Body transformations - Convert between different body formats (JSON, form data, files) with zero-copy when possible
  • Middleware system - Extensible middleware architecture for request/response processing
  • Async/await ready - Built on top of futures-lite for async I/O operations

§Optional Features

  • json - JSON serialization/deserialization via serde_json (enabled by default)
  • form - Form data handling via serde_urlencoded (enabled by default)
  • fs - File upload support with MIME type detection
  • mime - MIME type parsing and manipulation
  • http_body - Implementation of http_body traits
  • std - Enable standard library support (enabled by default)

§Examples

§Basic Request/Response Handling

use http_kit::{Request, Response, Result, Body};

async fn echo_handler(mut request: Request) -> Result<Response> {
    let body = std::mem::replace(request.body_mut(), Body::empty());
    Ok(Response::new(body))
}

let mut request = Request::new(Body::from_bytes("Hello, world!"));
let response = echo_handler(request).await?;

§JSON Handling

use http_kit::{Request, Response, Result, Body};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct User {
    name: String,
    email: String,
}

async fn create_user(mut request: Request) -> Result<Response> {
    let user: User = request.body_mut().into_json().await?;
    // Process user...
    let response_body = Body::from_json(&user)?;
    Ok(Response::new(response_body))
}

§Middleware Usage

use http_kit::{Request, Response, Result, Middleware, Endpoint, Body, Error};
use http_kit::middleware::MiddlewareError;

struct LoggingMiddleware;

impl Middleware for LoggingMiddleware {
    type Error = Error;
    async fn handle<E: Endpoint>(&mut self, request: &mut Request, mut next: E) -> Result<Response, MiddlewareError<E::Error, Self::Error>> {
        println!("Request: {} {}", request.method(), request.uri());
        let response = next.respond(request).await.map_err(MiddlewareError::Endpoint)?;
        println!("Response: {}", response.status());
        Ok(response)
    }
}

Re-exports§

pub use error::Error;
pub use error::HttpError;
pub use error::Result;
pub use error::ResultExt;
pub use cookie;

Modules§

endpoint
HTTP endpoint abstraction for request handling.
error
Error types and utilities.
header
HTTP header types
method
The HTTP request method
middleware
Middleware functionality for HTTP request and response processing.
sse
Server-Sent Events (SSE) implementation module.
uri
URI component of request and response lines
utils
Utility types and functions for HTTP operations.
version
HTTP version

Macros§

http_error
Defines a zero-sized [HttpError] type that renders as a static message.
http_error_fmt
Defines a zero-sized type that implements [HttpError] with a custom formatter.

Structs§

Body
Flexible HTTP body that can represent data in various forms.
Extensions
A type map of protocol extensions.
Method
The Request Method (VERB)
StatusCode
An HTTP status code (status-code in RFC 9110 et al.).
Uri
The URI component of a request.
Version
Represents a version of the HTTP spec.

Enums§

BodyError
Error type for body operations.

Traits§

Endpoint
A trait for types that can handle HTTP requests and generate responses.
Middleware
Trait for implementing middleware that can process HTTP requests and responses.

Type Aliases§

Request
A type alias for HTTP requests with a custom Body type.
Response
A type alias for HTTP responses with a custom Body type.