1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::fmt;

use header;
use header::shared;

use mime;

/// The `Accept` header.
///
/// The `Accept` header is used to tell a server which content-types the client
/// is capable of using. It can be a comma-separated list of `Mime`s, and the
/// priority can be indicated with a `q` parameter.
///
/// Example:
///
/// ```
/// # use hyper::header::Headers;
/// # use hyper::header::common::Accept;
/// # use hyper::header::shared::qitem;
/// use hyper::mime::Mime;
/// use hyper::mime::TopLevel::Text;
/// use hyper::mime::SubLevel::{Html, Xml};
/// # let mut headers = Headers::new();
/// headers.set(Accept(vec![
///     qitem(Mime(Text, Html, vec![])),
///     qitem(Mime(Text, Xml, vec![])) ]));
/// ```
#[derive(Clone, PartialEq, Show)]
pub struct Accept(pub Vec<shared::QualityItem<mime::Mime>>);

deref!(Accept => Vec<shared::QualityItem<mime::Mime>>);

impl header::Header for Accept {
    fn header_name(_: Option<Accept>) -> &'static str {
        "Accept"
    }

    fn parse_header(raw: &[Vec<u8>]) -> Option<Accept> {
        // TODO: Return */* if no value is given.
        shared::from_comma_delimited(raw).map(Accept)
    }
}

impl header::HeaderFormat for Accept {
    fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        shared::fmt_comma_delimited(fmt, &self[])
    }
}

bench_header!(bench, Accept, { vec![b"text/plain; q=0.5, text/html".to_vec()] });

#[test]
fn test_parse_header_no_quality() {
    let a: Accept = header::Header::parse_header([b"text/plain; charset=utf-8".to_vec()].as_slice()).unwrap();
    let b = Accept(vec![
        shared::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 1f32},
    ]);
    assert_eq!(a, b);
}

#[test]
fn test_parse_header_with_quality() {
    let a: Accept = header::Header::parse_header([b"text/plain; charset=utf-8; q=0.5".to_vec()].as_slice()).unwrap();
    let b = Accept(vec![
        shared::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 0.5f32},
    ]);
    assert_eq!(a, b);
}