Skip to main content

fastapi_http/
lib.rs

1//! Zero-copy HTTP/1.1 parser.
2//!
3//! This crate provides a minimal, zero-copy HTTP parser optimized for
4//! the fastapi_rust framework. It parses directly from byte buffers
5//! without allocating for most operations.
6//!
7//! # Features
8//!
9//! - Zero-copy request parsing
10//! - HTTP/1.1 compliance (subset)
11//! - Response building with pre-allocated buffers
12//! - Request body handling (Content-Length and chunked encoding)
13//! - Query string parsing with percent-decoding
14//! - Streaming response support
15//!
16//! # Role In The System
17//!
18//! `fastapi-http` is the protocol boundary. It parses HTTP/1.1 bytes into the
19//! `fastapi-core` request/response model, manages body streaming, and provides
20//! the TCP server scaffolding used by `App::serve`. Higher-level crates build on
21//! this layer without needing to care about socket I/O details.
22//!
23//! # Example
24//!
25//! ```ignore
26//! use fastapi_http::Parser;
27//!
28//! let bytes = b"GET /path HTTP/1.1\r\nHost: example.com\r\n\r\n";
29//! let request = Parser::parse(bytes)?;
30//! ```
31
32#![deny(unsafe_code)]
33// Pedantic clippy lints allowed (style suggestions, not correctness issues)
34#![allow(clippy::uninlined_format_args)]
35#![allow(clippy::single_char_pattern)]
36#![allow(clippy::must_use_candidate)]
37#![allow(clippy::needless_pass_by_value)]
38#![allow(clippy::match_wildcard_for_single_variants)]
39#![allow(clippy::redundant_closure)]
40#![allow(clippy::single_match_else)]
41#![allow(clippy::struct_field_names)]
42#![allow(clippy::manual_strip)]
43#![allow(clippy::items_after_statements)]
44#![allow(clippy::trivially_copy_pass_by_ref)]
45#![allow(clippy::match_same_arms)]
46#![allow(clippy::needless_borrow)]
47#![allow(clippy::match_wild_err_arm)]
48#![allow(clippy::format_push_string)]
49#![allow(clippy::match_wildcard_for_single_variants)]
50#![allow(clippy::manual_contains)]
51#![allow(clippy::needless_borrows_for_generic_args)]
52#![allow(clippy::iter_without_into_iter)]
53#![allow(clippy::single_match)]
54#![allow(clippy::len_zero)]
55#![allow(clippy::len_without_is_empty)]
56#![allow(clippy::field_reassign_with_default)]
57#![allow(clippy::cast_possible_truncation)]
58#![allow(clippy::derivable_impls)]
59#![allow(clippy::manual_let_else)]
60#![allow(clippy::needless_borrows_for_generic_args)]
61#![allow(clippy::too_many_lines)]
62#![allow(clippy::trivially_copy_pass_by_ref)]
63#![allow(clippy::unused_async)]
64#![allow(clippy::used_underscore_binding)]
65#![allow(clippy::duplicated_attributes)]
66
67pub mod body;
68pub mod connection;
69pub mod expect;
70pub mod multipart;
71mod parser;
72mod query;
73pub mod range;
74mod response;
75mod server;
76pub mod streaming;
77
78pub use body::{
79    AsyncChunkedStream, AsyncContentLengthStream, BodyConfig, BodyError, ChunkedReader,
80    ContentLengthReader, DEFAULT_MAX_BODY_SIZE, DEFAULT_STREAMING_THRESHOLD, StreamingBodyConfig,
81    create_chunked_stream, create_content_length_stream, parse_body, parse_body_with_consumed,
82    validate_content_length,
83};
84pub use connection::{
85    ConnectionInfo, STANDARD_HOP_BY_HOP_HEADERS, is_standard_hop_by_hop_header,
86    parse_connection_header, should_keep_alive, strip_hop_by_hop_headers,
87};
88pub use expect::{
89    CONTINUE_RESPONSE, EXPECT_100_CONTINUE, ExpectHandler, ExpectResult, FnValidator,
90    PreBodyValidator, PreBodyValidators,
91};
92pub use parser::{
93    BodyLength, Header, HeadersIter, HeadersParser, ParseError, ParseLimits, ParseStatus, Parser,
94    RequestLine, StatefulParser,
95};
96pub use query::{QueryString, percent_decode};
97pub use range::{
98    ByteRange, IfRangeResult, RangeError, RangeSpec, accept_ranges_bytes, check_if_range,
99    content_range_unsatisfiable, parse_range_header, parse_range_spec, supports_ranges,
100};
101pub use response::{ChunkedEncoder, ResponseWrite, ResponseWriter, Trailers};
102pub use server::{
103    AppServeExt, DEFAULT_DRAIN_TIMEOUT_SECS, DEFAULT_KEEP_ALIVE_TIMEOUT_SECS,
104    DEFAULT_MAX_CONNECTIONS, DEFAULT_MAX_REQUESTS_PER_CONNECTION, DEFAULT_READ_BUFFER_SIZE,
105    DEFAULT_REQUEST_TIMEOUT_SECS, ServeError, Server, ServerConfig, ServerError, ServerMetrics,
106    TcpServer, serve, serve_with_config,
107};
108
109// Re-export signal types for graceful shutdown
110pub use asupersync::signal::{GracefulOutcome, ShutdownController, ShutdownReceiver};
111pub use multipart::{
112    DEFAULT_MAX_FIELDS, DEFAULT_MAX_FILE_SIZE, DEFAULT_MAX_TOTAL_SIZE, MultipartConfig,
113    MultipartError, MultipartForm, MultipartParser, Part, UploadFile, parse_boundary,
114};
115pub use streaming::{
116    CancelAwareStream, ChunkedBytes, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_BUFFER_SIZE, FileStream,
117    StreamConfig, StreamError, StreamingResponseExt,
118};