reinhardt_http/lib.rs
1#![warn(missing_docs)]
2//! # Reinhardt HTTP
3//!
4//! HTTP request and response handling for the Reinhardt framework.
5//!
6//! This crate provides core HTTP abstractions including typed request and response types,
7//! header handling, content negotiation, file uploads, and middleware composition.
8//!
9//! ## Quick Start
10//!
11//! ```rust
12//! use reinhardt_http::{Request, Response};
13//! use hyper::{Method, HeaderMap};
14//!
15//! // Build a request
16//! let request = Request::builder()
17//! .method(Method::GET)
18//! .uri("/api/status")
19//! .version(hyper::Version::HTTP_11)
20//! .headers(HeaderMap::new())
21//! .body(bytes::Bytes::new())
22//! .build()
23//! .unwrap();
24//!
25//! // Create a simple response
26//! let response = Response::ok().with_body("OK");
27//! ```
28//!
29//! ## Architecture
30//!
31//! Key modules in this crate:
32//!
33//! - [`request`]: Typed HTTP request wrapper with builder pattern and trusted proxy support
34//! - [`response`]: HTTP response with helpers for JSON, streaming, and error responses
35//! - [`middleware`]: Middleware trait and composition chain for request processing
36//! - [`auth_state`]: Authentication state extensions stored in request context
37//! - [`upload`]: File upload handling (in-memory and temporary file backends)
38//! - [`chunked_upload`]: Resumable chunked upload session management
39//! - [`extensions`]: Typed request extension storage
40//!
41//! ## Feature Flags
42//!
43//! | Feature | Default | Description |
44//! |---------|---------|-------------|
45//! | `parsers` | enabled | Request body parsing (JSON, Form, Multipart) |
46//! | `messages` | disabled | Flash message middleware for session-based notifications |
47//! | `full` | disabled | Enables all optional features |
48//!
49//! ## Request Construction
50//!
51//! Requests are constructed using the builder pattern for type-safe configuration:
52//!
53//! ```rust
54//! use reinhardt_http::Request;
55//! use hyper::{Method, HeaderMap};
56//!
57//! let request = Request::builder()
58//! .method(Method::POST)
59//! .uri("/api/users")
60//! .version(hyper::Version::HTTP_11)
61//! .headers(HeaderMap::new())
62//! .body(bytes::Bytes::from("request body"))
63//! .build()
64//! .unwrap();
65//! ```
66//!
67//! ## Response Creation
68//!
69//! Responses use helper methods and builder pattern:
70//!
71//! ```rust
72//! use reinhardt_http::Response;
73//!
74//! // Using helpers
75//! let response = Response::ok().with_body("Hello, World!");
76//!
77//! // With JSON
78//! let json_response = Response::ok()
79//! .with_json(&serde_json::json!({"status": "success"}))
80//! .unwrap();
81//! ```
82
83/// Authentication state tracking for requests.
84pub mod auth_state;
85/// Chunked file upload handling with progress tracking.
86pub mod chunked_upload;
87/// Request extension storage for passing data between middleware.
88pub mod extensions;
89/// Flash messages middleware for one-time notifications.
90#[cfg(feature = "messages")]
91pub mod messages_middleware;
92/// Middleware trait and handler chain.
93pub mod middleware;
94/// Ordered path parameter storage (`PathParams`).
95pub mod path_params;
96/// HTTP request type and builder.
97pub mod request;
98/// HTTP response type and builder.
99pub mod response;
100/// File upload handling and validation.
101pub mod upload;
102
103/// Response cookies for server functions to set via request extensions.
104pub mod response_cookies;
105
106pub use auth_state::AuthState;
107pub use chunked_upload::{
108 ChunkedUploadError, ChunkedUploadManager, ChunkedUploadSession, UploadProgress,
109};
110pub use extensions::{Extensions, IsActive, IsAdmin, IsAuthenticated};
111#[cfg(feature = "messages")]
112pub use messages_middleware::MessagesMiddleware;
113pub use middleware::{
114 ExcludeMiddleware, Handler, Middleware, MiddlewareChain, MiddlewareDiRegistration,
115};
116pub use path_params::PathParams;
117pub use request::{Request, RequestBuilder, TrustedProxies};
118pub use response::{Response, SafeErrorResponse, StreamBody, StreamingResponse};
119pub use response_cookies::{ResponseCookies, SharedResponseCookies};
120pub use upload::{FileUploadError, FileUploadHandler, MemoryFileUpload, TemporaryFileUpload};
121
122// Re-export error types from reinhardt-exception for consistency across the framework
123pub use reinhardt_core::exception::{Error, Result};
124
125/// A convenient type alias for view/endpoint function return types.
126///
127/// This type alias is commonly used in endpoint handlers to simplify function signatures.
128/// It wraps any type `T` (typically `Response`) with a dynamic error type that can
129/// represent various kinds of errors that might occur during request processing.
130///
131/// The `Send + Sync` bounds ensure this type is safe to use across thread boundaries,
132/// which is essential for async runtime environments.
133///
134/// # Examples
135///
136/// ```
137/// use reinhardt_http::{Response, ViewResult};
138///
139/// async fn hello_world() -> ViewResult<Response> {
140/// Ok(Response::ok().with_body("Hello, World!"))
141/// }
142///
143/// #[tokio::main]
144/// # async fn main() {
145/// let response = hello_world().await.unwrap();
146/// assert_eq!(response.status, hyper::StatusCode::OK);
147/// # }
148/// ```
149/// Result type for view handlers using reinhardt's unified Error type.
150///
151/// This type alias ensures compatibility with `UnifiedRouter::function` which requires
152/// `Future<Output = Result<Response>>` where `Result` is `reinhardt_core::exception::Result`.
153pub type ViewResult<T> = reinhardt_core::exception::Result<T>;