Skip to main content

h11/
lib.rs

1//! Sans-I/O HTTP/1.1 protocol handling.
2//!
3//! This crate parses and serializes HTTP/1.1 events without owning sockets,
4//! timers, or tasks. Applications feed bytes into a [`Connection`] with
5//! [`Connection::receive_data`], pull protocol events with
6//! [`Connection::next_event`], and serialize outbound events with
7//! [`Connection::send`].
8//!
9//! The API follows the same model as Python h11: callers drive a state machine
10//! by exchanging typed events such as [`Request`], [`Response`], [`Data`], and
11//! [`EndOfMessage`]. Invalid local usage returns [`LocalProtocolError`], while
12//! malformed peer input returns [`RemoteProtocolError`].
13//!
14//! Use the fallible constructors such as [`Request::new_http11`],
15//! [`Response::new_final_http11`], and [`Headers::new`] for public inputs.
16//! Struct fields are currently public for compatibility, but manually-built
17//! values should be validated before being sent.
18
19#![allow(
20    clippy::byte_char_slices,
21    clippy::collapsible_if,
22    clippy::for_kv_map,
23    clippy::len_zero,
24    clippy::match_like_matches_macro,
25    clippy::needless_return,
26    clippy::ptr_arg,
27    clippy::redundant_pattern_matching,
28    clippy::type_complexity,
29    clippy::unit_arg,
30    clippy::unnecessary_mut_passed,
31    clippy::unnecessary_to_owned,
32    clippy::unnecessary_unwrap,
33    clippy::useless_conversion,
34    clippy::useless_vec,
35    clippy::while_let_loop,
36    clippy::while_let_on_iterator
37)]
38mod _abnf;
39mod _connection;
40mod _events;
41mod _headers;
42mod _readers;
43mod _receivebuffer;
44mod _state;
45mod _util;
46mod _writers;
47
48pub use _connection::Connection;
49pub use _events::{ConnectionClosed, Data, EndOfMessage, Event, Request, Response};
50pub use _headers::Headers;
51pub use _state::{EventType, Role, State, Switch};
52pub use _util::{LocalProtocolError, ProtocolError, RemoteProtocolError};
53
54/// Product token for identifying this protocol implementation.
55///
56/// This is intended for callers that want a default `User-Agent` or `Server`
57/// header value. The crate never injects it automatically.
58pub const PRODUCT_ID: &str = concat!("rust-h11/", env!("CARGO_PKG_VERSION"));
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn product_id_tracks_crate_version() {
66        assert_eq!(
67            PRODUCT_ID,
68            format!("rust-h11/{}", env!("CARGO_PKG_VERSION"))
69        );
70    }
71}