cogo_http/header/common/
cookie.rs1use crate::header::{Header, HeaderFormat};
2use std::fmt::{self, Display};
3use std::str::from_utf8;
4
5#[derive(Clone, PartialEq, Debug)]
31pub struct Cookie(pub Vec<String>);
32
33__hyper__deref!(Cookie => Vec<String>);
34
35impl Header for Cookie {
36 fn header_name() -> &'static str {
37 "Cookie"
38 }
39
40 fn parse_header(raw: &[Vec<u8>]) -> crate::Result<Cookie> {
41 let mut cookies = Vec::with_capacity(raw.len());
42 for cookies_raw in raw.iter() {
43 let cookies_str = r#try!(from_utf8(&cookies_raw[..]));
44 for cookie_str in cookies_str.split(';') {
45 cookies.push(cookie_str.trim().to_owned())
46 }
47 }
48
49 if !cookies.is_empty() {
50 Ok(Cookie(cookies))
51 } else {
52 Err(crate::Error::Header)
53 }
54 }
55}
56
57impl HeaderFormat for Cookie {
58 fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 let cookies = &self.0;
60 for (i, cookie) in cookies.iter().enumerate() {
61 if i != 0 {
62 r#try!(f.write_str("; "));
63 }
64 r#try!(Display::fmt(&cookie, f));
65 }
66 Ok(())
67 }
68}
69
70bench_header!(bench, Cookie, { vec![b"foo=bar; baz=quux".to_vec()] });