| Crates.io | reinhardt-http |
| lib.rs | reinhardt-http |
| version | 0.1.0-alpha.1 |
| created_at | 2026-01-23 04:34:44.961879+00 |
| updated_at | 2026-01-23 04:34:44.961879+00 |
| description | HTTP primitives, request and response handling for Reinhardt |
| homepage | |
| repository | https://github.com/kent8192/reinhardt-rs |
| max_upload_size | |
| id | 2063428 |
| size | 247,454 |
HTTP request and response handling for the Reinhardt framework
Core HTTP abstractions for the Reinhardt framework. Provides comprehensive request and response types, header handling, cookie management, content negotiation, and streaming support with a Django/DRF-inspired API design.
path_params) and query string parsing (query_params)is_secure)remote_addr)Extensions)Request::builder() - Start building.method() - Set HTTP method.uri() - Set URI (with automatic query parsing).version() - Set HTTP version (defaults to HTTP/1.1).headers() - Set headers.header() - Set single header.body() - Set request body.secure() - Set HTTPS flag.remote_addr() - Set remote address.build() - Finalize constructionparsers feature)
Response::ok() - 200 OKResponse::created() - 201 CreatedResponse::no_content() - 204 No ContentResponse::bad_request() - 400 Bad RequestResponse::unauthorized() - 401 UnauthorizedResponse::forbidden() - 403 ForbiddenResponse::not_found() - 404 Not FoundResponse::gone() - 410 GoneResponse::internal_server_error() - 500 Internal Server ErrorResponse::permanent_redirect(url) - 301 Moved PermanentlyResponse::temporary_redirect(url) - 302 FoundResponse::temporary_redirect_preserve_method(url) - 307 Temporary Redirect.with_body(data) - Set response body (bytes or string).with_header(name, value) - Add single header.with_typed_header(header) - Add typed header.with_json(data) - Serialize data to JSON and set Content-Type.with_location(url) - Set Location header (for redirects).with_stop_chain(bool) - Control middleware chain executionstop_chain flagStream)request.extensions.insert::<T>(value) - Store typed datarequest.extensions.get::<T>() - Retrieve typed dataArc<Mutex<TypeMap>>reinhardt_exception::Error and Result for consistent error handlingAdd reinhardt to your Cargo.toml:
[dependencies]
reinhardt = "0.1.0-alpha.1"
# Or use a preset with parsers support:
# reinhardt = { version = "0.1.0-alpha.1", features = ["standard"] } # Recommended
# reinhardt = { version = "0.1.0-alpha.1", features = ["full"] } # All features
Note: HTTP types are available through the main reinhardt crate, which provides a unified interface to all framework components.
use reinhardt::http::Request;
use hyper::Method;
use bytes::Bytes;
// Using builder pattern
let request = Request::builder()
.method(Method::POST)
.uri("/api/users?page=1")
.body(Bytes::from(r#"{"name": "Alice"}"#))
.build()
.unwrap();
assert_eq!(request.method, Method::POST);
assert_eq!(request.path(), "/api/users");
assert_eq!(request.query_params.get("page"), Some(&"1".to_string()));
use reinhardt::http::Request;
use hyper::Method;
let mut request = Request::builder()
.method(Method::GET)
.uri("/api/users/123?sort=name&order=asc")
.build()
.unwrap();
// Access query parameters
assert_eq!(request.query_params.get("sort"), Some(&"sort".to_string()));
assert_eq!(request.query_params.get("order"), Some(&"asc".to_string()));
// Add path parameters (typically done by router)
request.path_params.insert("id".to_string(), "123".to_string());
assert_eq!(request.path_params.get("id"), Some(&"123".to_string()));
use reinhardt::http::Request;
use hyper::Method;
#[derive(Clone)]
struct UserId(i64);
let mut request = Request::builder()
.method(Method::GET)
.uri("/api/profile")
.build()
.unwrap();
// Store typed data in extensions
request.extensions.insert(UserId(42));
// Retrieve typed data
let user_id = request.extensions.get::<UserId>().unwrap();
assert_eq!(user_id.0, 42);
use reinhardt::http::Response;
// Success responses
let response = Response::ok()
.with_body("Success");
assert_eq!(response.status, hyper::StatusCode::OK);
let response = Response::created()
.with_json(&serde_json::json!({
"id": 123,
"name": "Alice"
}))
.unwrap();
assert_eq!(response.status, hyper::StatusCode::CREATED);
assert_eq!(
response.headers.get("content-type").unwrap(),
"application/json"
);
// Error responses
let response = Response::bad_request()
.with_body("Invalid input");
assert_eq!(response.status, hyper::StatusCode::BAD_REQUEST);
let response = Response::not_found()
.with_body("Resource not found");
assert_eq!(response.status, hyper::StatusCode::NOT_FOUND);
use reinhardt::http::Response;
// Permanent redirect (301)
let response = Response::permanent_redirect("/new-location");
assert_eq!(response.status, hyper::StatusCode::MOVED_PERMANENTLY);
assert_eq!(
response.headers.get("location").unwrap().to_str().unwrap(),
"/new-location"
);
// Temporary redirect (302)
let response = Response::temporary_redirect("/login");
assert_eq!(response.status, hyper::StatusCode::FOUND);
// Temporary redirect preserving method (307)
let response = Response::temporary_redirect_preserve_method("/users/123");
assert_eq!(response.status, hyper::StatusCode::TEMPORARY_REDIRECT);
use reinhardt::http::Response;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct User {
id: i64,
name: String,
}
let user = User {
id: 1,
name: "Alice".to_string(),
};
let response = Response::ok()
.with_json(&user)
.unwrap();
// Automatically sets Content-Type: application/json
assert_eq!(
response.headers.get("content-type").unwrap(),
"application/json"
);
use reinhardt::http::Response;
// Stop middleware chain (useful for authentication, rate limiting)
let response = Response::unauthorized()
.with_body("Authentication required")
.with_stop_chain(true);
// This response will stop further middleware execution
assert!(response.should_stop_chain());
use reinhardt::http::StreamingResponse;
use futures::stream::{self, StreamExt};
use bytes::Bytes;
use hyper::StatusCode;
let data = vec![
Bytes::from("chunk1"),
Bytes::from("chunk2"),
Bytes::from("chunk3"),
];
let stream = stream::iter(data.into_iter().map(Ok));
// Create streaming response (default status: 200 OK)
let response = StreamingResponse::new(Box::pin(stream))
.status(StatusCode::OK)
.media_type("text/plain");
// Or use with_status for custom status code
let response = StreamingResponse::with_status(
Box::pin(stream),
StatusCode::OK,
)
.media_type("text/plain");
// Use for large files, server-sent events, etc.
Fields:
method: Method - HTTP method (GET, POST, etc.)uri: Uri - Request URIversion: Version - HTTP versionheaders: HeaderMap - HTTP headerspath_params: HashMap<String, String> - Path parameters from URL routingquery_params: HashMap<String, String> - Query string parametersis_secure: bool - Whether request is over HTTPSremote_addr: Option<SocketAddr> - Client's remote addressextensions: Extensions - Type-safe extension storageMethods:
Request::builder() - Create builder.path() - Get URI path without query.body() - Get request body as Option<&Bytes>.json::<T>() - Parse body as JSON (requires parsers feature).post() - Parse POST data (form/JSON, requires parsers feature).data() - Get parsed data from body.set_di_context::<T>() - Set DI context for type T.get_di_context::<T>() - Get DI context for type T.decoded_query_params() - Get URL-decoded query parameters.get_accepted_languages() - Parse Accept-Language header.get_preferred_language() - Get user's preferred language.is_secure() - Check if request is over HTTPS.scheme() - Get URI scheme.build_absolute_uri() - Build absolute URI from requestFields:
status: StatusCode - HTTP status codeheaders: HeaderMap - HTTP headersbody: Bytes - Response bodyConstructor Methods:
Response::new(status) - Create with status codeResponse::ok() - 200 OKResponse::created() - 201 CreatedResponse::no_content() - 204 No ContentResponse::bad_request() - 400 Bad RequestResponse::unauthorized() - 401 UnauthorizedResponse::forbidden() - 403 ForbiddenResponse::not_found() - 404 Not FoundResponse::gone() - 410 GoneResponse::internal_server_error() - 500 Internal Server ErrorResponse::permanent_redirect(url) - 301 Moved PermanentlyResponse::temporary_redirect(url) - 302 FoundResponse::temporary_redirect_preserve_method(url) - 307 Temporary RedirectBuilder Methods:
.with_body(data) - Set body (bytes or string).with_header(name, value) - Add header.with_typed_header(header) - Add typed header.with_json(data) - Serialize to JSON.with_location(url) - Set Location header.with_stop_chain(bool) - Control middleware chain.should_stop_chain() - Check if chain should stopMethods:
.insert::<T>(value) - Store typed value.get::<T>() - Retrieve typed value (returns Option<T>).remove::<T>() - Remove typed valueparsers - Enable request body parsing (JSON, form data, multipart)
parse_json(), parse_form() methods to Requestreinhardt-parsers cratehyper - HTTP types (Method, Uri, StatusCode, HeaderMap, Version)bytes - Efficient byte buffer handlingfutures - Stream support for streaming responsesserde - Serialization support (with serde_json for JSON)reinhardt-exception - Error handlingreinhardt-parsers - Request body parsing (optional, with parsers feature)The crate includes comprehensive unit tests and doctests covering:
Run tests with:
cargo test
cargo test --features parsers # With parsers support
Licensed under either of Apache License, Version 2.0 or MIT license at your option.