rama_http_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 rama_http_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 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
56rama_utils::macros::error::static_str_error! {
57 #[doc = "server is not valid"]
58 pub struct InvalidServer;
59}
60
61impl FromStr for Server {
62 type Err = InvalidServer;
63 fn from_str(src: &str) -> Result<Self, Self::Err> {
64 HeaderValueString::from_str(src)
65 .map(Server)
66 .map_err(|_| InvalidServer)
67 }
68}
69
70impl fmt::Display for Server {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 fmt::Display::fmt(&self.0, f)
73 }
74}