hyperx/header/common/
expect.rs

1use std::fmt;
2use std::str;
3
4use unicase;
5
6use header::{Header, RawLike};
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/// # extern crate http;
20/// use hyperx::header::{Expect, TypedHeaders};
21/// let mut headers = http::HeaderMap::new();
22/// headers.encode(&Expect::Continue);
23/// ```
24#[derive(Copy, Clone, PartialEq, Debug)]
25pub enum Expect {
26    /// The value `100-continue`.
27    Continue
28}
29
30impl Header for Expect {
31    fn header_name() -> &'static str {
32        static NAME: &'static str = "Expect";
33        NAME
34    }
35
36    fn parse_header<'a, T>(raw: &'a T) -> ::Result<Expect>
37    where T: RawLike<'a>
38    {
39        if let Some(line) = raw.one() {
40            let text = unsafe {
41                // safe because:
42                // 1. we don't actually care if it's utf8, we just want to
43                //    compare the bytes with the "case" normalized. If it's not
44                //    utf8, then the byte comparison will fail, and we'll return
45                //    None. No big deal.
46                str::from_utf8_unchecked(line)
47            };
48            if unicase::eq_ascii(text, "100-continue") {
49                Ok(Expect::Continue)
50            } else {
51                Err(::Error::Header)
52            }
53        } else {
54            Err(::Error::Header)
55        }
56    }
57
58    fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
59        f.fmt_line(self)
60    }
61}
62
63impl fmt::Display for Expect {
64    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        f.write_str("100-continue")
66    }
67}
68
69standard_header!(Expect, EXPECT);