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 http2;
71pub mod multipart;
72mod parser;
73mod query;
74pub mod range;
75mod response;
76mod server;
77pub mod streaming;
78pub mod websocket;
79
80pub use body::{
81    AsyncChunkedStream, AsyncContentLengthStream, BodyConfig, BodyError, ChunkedReader,
82    ContentLengthReader, DEFAULT_MAX_BODY_SIZE, DEFAULT_STREAMING_THRESHOLD, StreamingBodyConfig,
83    create_chunked_stream, create_content_length_stream, parse_body, parse_body_with_consumed,
84    validate_content_length,
85};
86pub use connection::{
87    ConnectionInfo, STANDARD_HOP_BY_HOP_HEADERS, is_standard_hop_by_hop_header,
88    parse_connection_header, should_keep_alive, strip_hop_by_hop_headers,
89};
90pub use expect::{
91    CONTINUE_RESPONSE, EXPECT_100_CONTINUE, ExpectHandler, ExpectResult, FnValidator,
92    PreBodyValidator, PreBodyValidators,
93};
94pub use parser::{
95    BodyLength, Header, HeadersIter, HeadersParser, ParseError, ParseLimits, ParseStatus, Parser,
96    RequestLine, StatefulParser,
97};
98pub use query::{QueryString, percent_decode};
99pub use range::{
100    ByteRange, IfRangeResult, RangeError, RangeSpec, accept_ranges_bytes, check_if_range,
101    content_range_unsatisfiable, parse_range_header, parse_range_spec, supports_ranges,
102};
103pub use response::{ChunkedEncoder, ResponseWrite, ResponseWriter, Trailers};
104pub use server::{
105    AppServeExt, DEFAULT_DRAIN_TIMEOUT_SECS, DEFAULT_KEEP_ALIVE_TIMEOUT_SECS,
106    DEFAULT_MAX_CONNECTIONS, DEFAULT_MAX_REQUESTS_PER_CONNECTION, DEFAULT_READ_BUFFER_SIZE,
107    DEFAULT_REQUEST_TIMEOUT_SECS, ServeError, Server, ServerConfig, ServerError, ServerMetrics,
108    TcpServer, serve, serve_with_config,
109};
110
111// Re-export signal types for graceful shutdown
112pub use asupersync::signal::{GracefulOutcome, ShutdownController, ShutdownReceiver};
113pub use multipart::{
114    DEFAULT_MAX_FIELDS, DEFAULT_MAX_FILE_SIZE, DEFAULT_MAX_TOTAL_SIZE, MultipartConfig,
115    MultipartError, MultipartForm, MultipartParser, Part, UploadFile, parse_boundary,
116};
117pub use streaming::{
118    CancelAwareStream, ChunkedBytes, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_BUFFER_SIZE, FileStream,
119    StreamConfig, StreamError, StreamingResponseExt,
120};
121pub use websocket::{
122    CloseCode, DEFAULT_MAX_FRAME_SIZE, DEFAULT_MAX_MESSAGE_SIZE, Message, Opcode, WebSocket,
123    WebSocketConfig, WebSocketError, accept_key, build_accept_response, validate_upgrade_request,
124};