sark_core/http/head/
flags.rs1use crate::error::{Error, Result};
2
3pub trait SeenHeaderHandler {
4 const SEEN_BIT: u16;
5 const DUPLICATE_ERR: &'static str;
6}
7
8#[derive(Clone, Copy, Default, Debug)]
9pub struct Flags(u16);
10
11impl Flags {
12 pub const SEEN_CONTENT_LENGTH: u16 = 1 << 0;
13 pub const SEEN_TRANSFER_ENCODING: u16 = 1 << 1;
14 pub const SEEN_EXPECT: u16 = 1 << 2;
15 pub const SEEN_HOST: u16 = 1 << 3;
16 pub const SEEN_CONNECTION: u16 = 1 << 4;
17
18 pub const HAS_HOST: u16 = 1 << 8;
19 pub const CONNECTION_CLOSE: u16 = 1 << 9;
20 pub const CONNECTION_KEEP_ALIVE: u16 = 1 << 10;
21
22 pub fn has(self, bit: u16) -> bool {
23 (self.0 & bit) != 0
24 }
25
26 pub fn set(&mut self, bit: u16) {
27 self.0 |= bit;
28 }
29
30 pub fn mark_seen<H: SeenHeaderHandler>(&mut self) -> Result<()> {
31 if self.has(H::SEEN_BIT) {
32 return Err(Error::BadRequest(H::DUPLICATE_ERR.into()));
33 }
34 self.set(H::SEEN_BIT);
35 Ok(())
36 }
37
38 pub fn implies_close(self, version: &[u8]) -> bool {
39 if self.has(Self::CONNECTION_CLOSE) {
40 return true;
41 }
42 if self.has(Self::CONNECTION_KEEP_ALIVE) {
43 return false;
44 }
45 version == b"HTTP/1.0"
46 }
47}