| 1 |
use std::fmt; |
| 2 |
use std::str::FromStr; |
| 3 |
|
| 4 |
use 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; |
| 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)] |
| 34 |
pub struct Server(HeaderValueString); |
| 35 |
|
| 36 |
derive_header! { |
| 37 |
Server(_), |
| 38 |
name: SERVER |
| 39 |
} |
| 40 |
|
| 41 |
impl Server { |
| 42 |
/// Construct a `Server` from a static string. |
| 43 |
/// |
| 44 |
/// # Panic |
| 45 |
/// |
| 46 |
/// Panics if the static string is not a legal header value. |
| 47 |
pub fn from_static(s: &'static str) -> Server { |
| 48 |
Server(HeaderValueString::from_static(s)) |
| 49 |
} |
| 50 |
|
| 51 |
/// View this `Server` as a `&str`. |
| 52 |
pub fn as_str(&self) -> &str { |
| 53 |
self.0.as_str() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
error_type!(InvalidServer); |
| 58 |
|
| 59 |
impl FromStr for Server { |
| 60 |
type Err = InvalidServer; |
| 61 |
fn from_str(src: &str) -> Result<Self, Self::Err> { |
| 62 |
HeaderValueString::from_str(src) |
| 63 |
.map(Server) |
| 64 |
.map_err(|_| InvalidServer { _inner: () }) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl fmt::Display for Server { |
| 69 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 70 |
fmt::Display::fmt(&self.0, f) |
| 71 |
} |
| 72 |
} |