1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Shared HTTP types and extension traits for clients and servers.
//!
//! This crate provides common HTTP functionality built on the popular [`http`] crate,
//! including flexible body handling, unified error types, and ergonomic extension traits
//! for working with HTTP requests and responses.
//!
//! # Core Types
//!
//! - [`HttpRequest`] and [`HttpResponse`] - Type aliases for requests and responses with [`HttpBody`]
//! - [`HttpRequestBuilder`] - Builder for constructing HTTP requests with a fluent API
//! - [`HttpResponseBuilder`] - Builder for constructing HTTP responses with a fluent API
//! - [`HttpBody`] - Flexible body type supporting text, binary, JSON, and streaming content
//! - [`HttpBodyBuilder`] - Builder for creating HTTP bodies with memory pool optimization
//! - [`HttpError`] - Unified error type with automatic backtraces and recovery classification
//! - [`RequestHandler`] - Trait for HTTP middleware and request processing pipelines
//!
//! # Extension Traits
//!
//! The crate provides extension traits that add convenience methods to standard HTTP types:
//!
//! - [`StatusExt`] - Status code validation and recovery classification
//! - [`RequestExt`] - Extensions for HTTP requests
//! - [`ResponseExt`] - Response recovery classification with `Retry-After` support
//! - [`HttpRequestExt`] - Request cloning with body support
//! - [`HeaderMapExt`] - Header value extraction and parsing
//! - [`HeaderValueExt`] - Construction of [`HeaderValue`][http::HeaderValue] from [`Bytes`][bytes::Bytes]
//! - [`ExtensionsExt`] - Extensions for [`Extensions`][http::Extensions] to extract URL template labels
//!
//! # Quick Start
//!
//! Here's a complete example showing how to create an HTTP client, build a request,
//! and validate the response:
//!
//! ```rust
//! # use http_extensions::{
//! # HttpRequestBuilder, HttpResponseBuilder, HttpBodyBuilder, HttpRequestBuilderExt,
//! # FakeHandler, StatusExt, Result,
//! # };
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! // Create a body builder for constructing request/response bodies
//! let body_builder = HttpBodyBuilder::new_fake();
//!
//! // Create a fake handler that returns a successful response
//! // (This uses the `test-util` feature for testing; similar workflow applies to real clients)
//! let handler = FakeHandler::from(
//! HttpResponseBuilder::new(&body_builder)
//! .status(200)
//! .header("Content-Type", "application/json")
//! .text(r#"{"message": "Success"}"#)
//! .build()?,
//! );
//!
//! // Build and send an HTTP request using the handler
//! let response = handler
//! .request_builder()
//! .get("https://api.example.com/data")
//! .header("Authorization", "Bearer token")
//! .fetch()
//! .await?;
//!
//! // Validate that the response succeeded (returns error for `4xx/5xx` status codes)
//! let validated_response = response.ensure_success()?;
//!
//! println!("response status: {}", validated_response.status());
//! # Ok(())
//! # }
//! ```
//!
//! **Note**: This example uses the `test-util` feature to create a `FakeHandler` for testing.
//! In production code, you would use a real HTTP client that implements the
//! [`RequestHandler`] trait, but the workflow remains the same: build requests with
//! [`HttpRequestBuilder`], send them through a handler, and validate responses with
//! [`StatusExt::ensure_success`].
//!
//! # Integration with the HTTP Ecosystem
//!
//! This crate builds on the popular [`http`] crate rather than inventing new types:
//!
//! - Uses [`http::Request`] and [`http::Response`] as base types
//! - Reuses [`http::Method`], [`http::StatusCode`], and [`http::HeaderMap`]
//! - Implements standard traits like [`http_body::Body`] for ecosystem compatibility
//! - Works seamlessly with other Rust HTTP libraries
//!
//! # Examples
//!
//! ## Validating Response Status
//! ```rust
//! # use http_extensions::{StatusExt, HttpResponse, HttpResponseBuilder, HttpError};
//! # let response: HttpResponse = HttpResponseBuilder::new_fake().status(200).build().unwrap();
//! // Check if the response succeeded and return an error if not
//! let validated_response = response.ensure_success()?;
//! # Ok::<(), HttpError>(())
//! ```
//!
//! ## Creating Request Bodies
//! ```rust
//! # use http_extensions::HttpBodyBuilder;
//! # let builder = HttpBodyBuilder::new_fake();
//! // Create different body types
//! let text_body = builder.text("Hello, world!");
//! let binary_body = builder.slice(&[1, 2, 3, 4]);
//! let empty_body = builder.empty();
//! ```
//!
//! ## Building HTTP Requests
//! ```rust
//! # use http_extensions::{HttpRequestBuilder, HttpBodyBuilder};
//! # let body_builder = HttpBodyBuilder::new_fake();
//! let request = HttpRequestBuilder::new(&body_builder)
//! .get("https://api.example.com/data")
//! .text("Hello World")
//! .build()
//! .unwrap();
//! ```
//!
//! ## Building HTTP Responses
//! ```rust
//! # use http_extensions::{HttpResponseBuilder, HttpBodyBuilder};
//! # let body_builder = HttpBodyBuilder::new_fake();
//! let response = HttpResponseBuilder::new(&body_builder)
//! .status(200)
//! .header("Content-Type", "text/plain")
//! .body(body_builder.text("Success"))
//! .build()
//! .unwrap();
//! ```
//!
//! ## Building Middleware with `RequestHandler`
//! ```rust
//! # use http_extensions::{HttpRequest, HttpResponse, RequestHandler, Result};
//! # use layered::Service;
//! struct LoggingMiddleware<S> {
//! inner: S,
//! }
//!
//! impl<S: RequestHandler> Service<HttpRequest> for LoggingMiddleware<S> {
//! type Out = Result<HttpResponse>;
//!
//! async fn execute(&self, request: HttpRequest) -> Self::Out {
//! println!("Processing request to: {}", request.uri());
//! let response = self.inner.execute(request).await?;
//! println!("Response status: {}", response.status());
//! Ok(response)
//! }
//! }
//! ```
//!
//! ## Testing with `FakeHandler`
//!
//! The `FakeHandler` type (available with the `test-util` feature) lets you mock HTTP responses
//! for testing without making actual network requests. This is useful for unit testing code
//! that depends on HTTP clients.
//!
//! # Features
//!
//! - `json` - Enables JSON serialization/deserialization support via `Json` type
//! - `test-util` - Enables fake implementations for testing
//!
//! # Memory Management
//!
//! Bodies created through [`HttpBodyBuilder`] use memory pools from [`bytesbuf`] to
//! reduce allocation overhead. When body data is consumed, memory is automatically recycled
//! for future requests. This makes the crate particularly efficient for high-throughput scenarios.
use ;
/// Specialized HTTP request that uses the [`HttpBody`] type for the body.
///
/// This is a type alias for [`Request<HttpBody>`].
pub type HttpRequest = ;
/// Specialized HTTP response that uses the [`HttpBody`] type for the body.
///
/// This is a type alias for [`Response<HttpBody>`].
pub type HttpResponse = ;
pub use ;
pub use ;
pub use ;
pub use RequestHandler;
pub use HttpRequestBuilderExt;
pub use ;
pub use UrlTemplateLabel;
pub use HttpResponseBuilder;
pub
pub use HttpRequestBuilder;
pub use FakeHandler;
pub