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