| 1 | use bytes::BytesMut; |
| 2 | use http::{HeaderMap, Method}; |
| 3 | use httparse::ParserConfig; |
| 4 | |
| 5 | use crate::body::DecodedLength; |
| 6 | use crate::proto::{BodyLength, MessageHead}; |
| 7 | |
| 8 | pub(crate) use self::conn::Conn; |
| 9 | pub(crate) use self::decode::Decoder; |
| 10 | pub(crate) use self::dispatch::Dispatcher; |
| 11 | pub(crate) use self::encode::{EncodedBuf, Encoder}; |
| 12 | //TODO: move out of h1::io |
| 13 | pub(crate) use self::io::MINIMUM_MAX_BUFFER_SIZE; |
| 14 | |
| 15 | mod conn; |
| 16 | mod decode; |
| 17 | pub(crate) mod dispatch; |
| 18 | mod encode; |
| 19 | mod io; |
| 20 | mod role; |
| 21 | |
| 22 | cfg_client! { |
| 23 | pub(crate) type ClientTransaction = role::Client; |
| 24 | } |
| 25 | |
| 26 | cfg_server! { |
| 27 | pub(crate) type ServerTransaction = role::Server; |
| 28 | } |
| 29 | |
| 30 | pub(crate) trait Http1Transaction { |
| 31 | type Incoming; |
| 32 | type Outgoing: Default; |
| 33 | #[cfg (feature = "tracing" )] |
| 34 | const LOG: &'static str; |
| 35 | fn parse(bytes: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<Self::Incoming>; |
| 36 | fn encode(enc: Encode<'_, Self::Outgoing>, dst: &mut Vec<u8>) -> crate::Result<Encoder>; |
| 37 | |
| 38 | fn on_error(err: &crate::Error) -> Option<MessageHead<Self::Outgoing>>; |
| 39 | |
| 40 | fn is_client() -> bool { |
| 41 | !Self::is_server() |
| 42 | } |
| 43 | |
| 44 | fn is_server() -> bool { |
| 45 | !Self::is_client() |
| 46 | } |
| 47 | |
| 48 | fn should_error_on_parse_eof() -> bool { |
| 49 | Self::is_client() |
| 50 | } |
| 51 | |
| 52 | fn should_read_first() -> bool { |
| 53 | Self::is_server() |
| 54 | } |
| 55 | |
| 56 | fn update_date() {} |
| 57 | } |
| 58 | |
| 59 | /// Result newtype for Http1Transaction::parse. |
| 60 | pub(crate) type ParseResult<T> = Result<Option<ParsedMessage<T>>, crate::error::Parse>; |
| 61 | |
| 62 | #[derive (Debug)] |
| 63 | pub(crate) struct ParsedMessage<T> { |
| 64 | head: MessageHead<T>, |
| 65 | decode: DecodedLength, |
| 66 | expect_continue: bool, |
| 67 | keep_alive: bool, |
| 68 | wants_upgrade: bool, |
| 69 | } |
| 70 | |
| 71 | pub(crate) struct ParseContext<'a> { |
| 72 | cached_headers: &'a mut Option<HeaderMap>, |
| 73 | req_method: &'a mut Option<Method>, |
| 74 | h1_parser_config: ParserConfig, |
| 75 | h1_max_headers: Option<usize>, |
| 76 | preserve_header_case: bool, |
| 77 | #[cfg (feature = "ffi" )] |
| 78 | preserve_header_order: bool, |
| 79 | h09_responses: bool, |
| 80 | #[cfg (feature = "client" )] |
| 81 | on_informational: &'a mut Option<crate::ext::OnInformational>, |
| 82 | } |
| 83 | |
| 84 | /// Passed to Http1Transaction::encode |
| 85 | pub(crate) struct Encode<'a, T> { |
| 86 | head: &'a mut MessageHead<T>, |
| 87 | body: Option<BodyLength>, |
| 88 | #[cfg (feature = "server" )] |
| 89 | keep_alive: bool, |
| 90 | req_method: &'a mut Option<Method>, |
| 91 | title_case_headers: bool, |
| 92 | #[cfg (feature = "server" )] |
| 93 | date_header: bool, |
| 94 | } |
| 95 | |
| 96 | /// Extra flags that a request "wants", like expect-continue or upgrades. |
| 97 | #[derive (Clone, Copy, Debug)] |
| 98 | struct Wants(u8); |
| 99 | |
| 100 | impl Wants { |
| 101 | const EMPTY: Wants = Wants(0b00); |
| 102 | const EXPECT: Wants = Wants(0b01); |
| 103 | const UPGRADE: Wants = Wants(0b10); |
| 104 | |
| 105 | #[must_use ] |
| 106 | fn add(self, other: Wants) -> Wants { |
| 107 | Wants(self.0 | other.0) |
| 108 | } |
| 109 | |
| 110 | fn contains(&self, other: Wants) -> bool { |
| 111 | (self.0 & other.0) == other.0 |
| 112 | } |
| 113 | } |
| 114 | |