s2n_quic_core/application/server_name.rs
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use bytes::Bytes;
5
6/// ServerName holds a negotiated
7/// [Server Name Indication](https://en.wikipedia.org/wiki/Server_Name_Indication)
8/// value, encoded as UTF-8.
9///
10/// ServerName should be a valid UTF-8 string, therefore this struct can only be
11/// constructed from a `&str` or `String`.
12///
13/// ```rust
14/// # use s2n_quic_core::application::ServerName;
15/// let string: String = String::from("a valid utf-8 string");
16/// let name: ServerName = string.into();
17///
18/// let str_arr: &str = &"a valid utf-8 str array";
19/// let name: ServerName = str_arr.into();
20/// ```
21///
22/// `ServerName` serves a dual purpose:
23/// - It can be converted into [`Bytes`] which supports zero-copy slicing and
24/// reference counting.
25/// - It can be accessed as `&str` so that applications can reason about the string value.
26#[derive(Clone, PartialEq, Eq, Hash)]
27pub struct ServerName(Bytes);
28
29/// A static value for localhost
30#[allow(dead_code)] // this is used by conditional modules so don't warn
31pub(crate) static LOCALHOST: ServerName = ServerName::from_static("localhost");
32
33impl ServerName {
34 #[inline]
35 pub fn into_bytes(self) -> Bytes {
36 self.0
37 }
38
39 pub const fn from_static(s: &'static str) -> Self {
40 ServerName(Bytes::from_static(s.as_bytes()))
41 }
42
43 #[inline]
44 fn as_str(&self) -> &str {
45 // Safety: the byte array is validated as a valid UTF-8 string
46 // before creating an instance of Sni.
47 unsafe { core::str::from_utf8_unchecked(&self.0) }
48 }
49}
50
51impl From<&str> for ServerName {
52 #[inline]
53 fn from(data: &str) -> Self {
54 Self(Bytes::copy_from_slice(data.as_bytes()))
55 }
56}
57
58#[cfg(feature = "alloc")]
59impl From<alloc::string::String> for ServerName {
60 #[inline]
61 fn from(data: alloc::string::String) -> Self {
62 Self(data.into_bytes().into())
63 }
64}
65
66impl core::fmt::Debug for ServerName {
67 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
68 self.as_str().fmt(f)
69 }
70}
71
72impl core::ops::Deref for ServerName {
73 type Target = str;
74
75 #[inline]
76 fn deref(&self) -> &Self::Target {
77 self.as_str()
78 }
79}