cogo_http/header/common/expect.rs
1use std::fmt;
2use std::str;
3
4use unicase::UniCase;
5
6use crate::header::{Header, HeaderFormat};
7
8/// The `Expect` header.
9///
10/// > The "Expect" header field in a request indicates a certain set of
11/// > behaviors (expectations) that need to be supported by the server in
12/// > order to properly handle this request. The only such expectation
13/// > defined by this specification is 100-continue.
14/// >
15/// > Expect = "100-continue"
16///
17/// # Example
18/// ```
19/// use cogo_http::header::{Headers, Expect};
20/// let mut headers = Headers::new();
21/// headers.set(Expect::Continue);
22/// ```
23#[derive(Copy, Clone, PartialEq, Debug)]
24pub enum Expect {
25 /// The value `100-continue`.
26 Continue
27}
28
29const EXPECT_CONTINUE: UniCase<&'static str> = UniCase("100-continue");
30
31impl Header for Expect {
32 fn header_name() -> &'static str {
33 "Expect"
34 }
35
36 fn parse_header(raw: &[Vec<u8>]) -> crate::Result<Expect> {
37 if raw.len() == 1 {
38 let text = unsafe {
39 // safe because:
40 // 1. we just checked raw.len == 1
41 // 2. we don't actually care if it's utf8, we just want to
42 // compare the bytes with the "case" normalized. If it's not
43 // utf8, then the byte comparison will fail, and we'll return
44 // None. No big deal.
45 str::from_utf8_unchecked(raw.get_unchecked(0))
46 };
47 if UniCase(text) == EXPECT_CONTINUE {
48 Ok(Expect::Continue)
49 } else {
50 Err(crate::Error::Header)
51 }
52 } else {
53 Err(crate::Error::Header)
54 }
55 }
56}
57
58impl HeaderFormat for Expect {
59 fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 fmt::Display::fmt(self, f)
61 }
62}
63
64impl fmt::Display for Expect {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 f.write_str("100-continue")
67 }
68}