headers/common/server.rs
1use std::fmt;
2use std::str::FromStr;
3
4use crate::util::HeaderValueString;
5
6/// `Server` header, defined in [RFC7231](https://datatracker.ietf.org/doc/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/// use headers::Server;
29///
30/// let server = Server::from_static("hyper/0.12.2");
31/// ```
32#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct Server(HeaderValueString);
34
35derive_header! {
36 Server(_),
37 name: SERVER
38}
39
40impl Server {
41 /// Construct a `Server` from a static string.
42 ///
43 /// # Panic
44 ///
45 /// Panics if the static string is not a legal header value.
46 pub const fn from_static(s: &'static str) -> Server {
47 Server(HeaderValueString::from_static(s))
48 }
49
50 /// View this `Server` as a `&str`.
51 pub fn as_str(&self) -> &str {
52 self.0.as_str()
53 }
54}
55
56error_type!(InvalidServer);
57
58impl FromStr for Server {
59 type Err = InvalidServer;
60 fn from_str(src: &str) -> Result<Self, Self::Err> {
61 HeaderValueString::from_str(src)
62 .map(Server)
63 .map_err(|_| InvalidServer { _inner: () })
64 }
65}
66
67impl fmt::Display for Server {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 fmt::Display::fmt(&self.0, f)
70 }
71}