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 23 cfg_client! { 24 pub(crate) type ClientTransaction = role::Client; 25 } 26 27 cfg_server! { 28 pub(crate) type ServerTransaction = role::Server; 29 } 30 31 pub(crate) trait Http1Transaction { 32 type Incoming; 33 type Outgoing: Default; 34 const LOG: &'static str; parse(bytes: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<Self::Incoming>35 fn parse(bytes: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<Self::Incoming>; encode(enc: Encode<'_, Self::Outgoing>, dst: &mut Vec<u8>) -> crate::Result<Encoder>36 fn encode(enc: Encode<'_, Self::Outgoing>, dst: &mut Vec<u8>) -> crate::Result<Encoder>; 37 on_error(err: &crate::Error) -> Option<MessageHead<Self::Outgoing>>38 fn on_error(err: &crate::Error) -> Option<MessageHead<Self::Outgoing>>; 39 is_client() -> bool40 fn is_client() -> bool { 41 !Self::is_server() 42 } 43 is_server() -> bool44 fn is_server() -> bool { 45 !Self::is_client() 46 } 47 should_error_on_parse_eof() -> bool48 fn should_error_on_parse_eof() -> bool { 49 Self::is_client() 50 } 51 should_read_first() -> bool52 fn should_read_first() -> bool { 53 Self::is_server() 54 } 55 update_date()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 preserve_header_case: bool, 76 h09_responses: bool, 77 } 78 79 /// Passed to Http1Transaction::encode 80 pub(crate) struct Encode<'a, T> { 81 head: &'a mut MessageHead<T>, 82 body: Option<BodyLength>, 83 #[cfg(feature = "server")] 84 keep_alive: bool, 85 req_method: &'a mut Option<Method>, 86 title_case_headers: bool, 87 } 88 89 /// Extra flags that a request "wants", like expect-continue or upgrades. 90 #[derive(Clone, Copy, Debug)] 91 struct Wants(u8); 92 93 impl Wants { 94 const EMPTY: Wants = Wants(0b00); 95 const EXPECT: Wants = Wants(0b01); 96 const UPGRADE: Wants = Wants(0b10); 97 98 #[must_use] add(self, other: Wants) -> Wants99 fn add(self, other: Wants) -> Wants { 100 Wants(self.0 | other.0) 101 } 102 contains(&self, other: Wants) -> bool103 fn contains(&self, other: Wants) -> bool { 104 (self.0 & other.0) == other.0 105 } 106 } 107