headers_ext/common/server.rs
1use std::fmt;
2use std::str::FromStr;
3
4use util::HeaderValueString;
5
6/// `Server` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.2)
7///
8/// The `Server` header field contains information about the software
9/// used by the origin server to handle the request, which is often used
10/// by clients to help identify the scope of reported interoperability
11/// problems, to work around or tailor requests to avoid particular
12/// server limitations, and for analytics regarding server or operating
13/// system use. An origin server MAY generate a Server field in its
14/// responses.
15///
16/// # ABNF
17///
18/// ```text
19/// Server = product *( RWS ( product / comment ) )
20/// ```
21///
22/// # Example values
23/// * `CERN/3.0 libwww/2.17`
24///
25/// # Example
26///
27/// ```
28/// # extern crate headers_ext as headers;
29/// use headers::Server;
30///
31/// let server = Server::from_static("hyper/0.12.2");
32/// ```
33#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Header)]
34pub struct Server(HeaderValueString);
35
36impl Server {
37 /// Construct a `Server` from a static string.
38 ///
39 /// # Panic
40 ///
41 /// Panics if the static string is not a legal header value.
42 pub fn from_static(s: &'static str) -> Server {
43 Server(HeaderValueString::from_static(s))
44 }
45
46 /// View this `Server` as a `&str`.
47 pub fn as_str(&self) -> &str {
48 self.0.as_str()
49 }
50}
51
52error_type!(InvalidServer);
53
54impl FromStr for Server {
55 type Err = InvalidServer;
56 fn from_str(src: &str) -> Result<Self, Self::Err> {
57 HeaderValueString::from_str(src)
58 .map(Server)
59 .map_err(|_| InvalidServer { _inner: () })
60 }
61}
62
63impl fmt::Display for Server {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 fmt::Display::fmt(&self.0, f)
66 }
67}
68