pingora_http/
case_header_name.rs

1// Copyright 2024 Cloudflare, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::*;
16use bytes::Bytes;
17use http::header;
18
19#[derive(Debug, Clone)]
20pub struct CaseHeaderName(Bytes);
21
22impl CaseHeaderName {
23    pub fn new(name: String) -> Self {
24        CaseHeaderName(name.into())
25    }
26}
27
28impl CaseHeaderName {
29    pub fn as_slice(&self) -> &[u8] {
30        &self.0
31    }
32
33    pub fn from_slice(buf: &[u8]) -> Self {
34        CaseHeaderName(Bytes::copy_from_slice(buf))
35    }
36}
37
38/// A trait that converts into case-sensitive header names.
39pub trait IntoCaseHeaderName {
40    fn into_case_header_name(self) -> CaseHeaderName;
41}
42
43impl IntoCaseHeaderName for CaseHeaderName {
44    fn into_case_header_name(self) -> CaseHeaderName {
45        self
46    }
47}
48
49impl IntoCaseHeaderName for String {
50    fn into_case_header_name(self) -> CaseHeaderName {
51        CaseHeaderName(self.into())
52    }
53}
54
55impl IntoCaseHeaderName for &'static str {
56    fn into_case_header_name(self) -> CaseHeaderName {
57        CaseHeaderName(self.into())
58    }
59}
60
61impl IntoCaseHeaderName for HeaderName {
62    fn into_case_header_name(self) -> CaseHeaderName {
63        CaseHeaderName(titled_header_name(&self))
64    }
65}
66
67impl IntoCaseHeaderName for &HeaderName {
68    fn into_case_header_name(self) -> CaseHeaderName {
69        CaseHeaderName(titled_header_name(self))
70    }
71}
72
73impl IntoCaseHeaderName for Bytes {
74    fn into_case_header_name(self) -> CaseHeaderName {
75        CaseHeaderName(self)
76    }
77}
78
79fn titled_header_name(header_name: &HeaderName) -> Bytes {
80    titled_header_name_str(header_name).map_or_else(
81        || Bytes::copy_from_slice(header_name.as_str().as_bytes()),
82        |s| Bytes::from_static(s.as_bytes()),
83    )
84}
85
86pub(crate) fn titled_header_name_str(header_name: &HeaderName) -> Option<&'static str> {
87    Some(match *header_name {
88        header::AGE => "Age",
89        header::CACHE_CONTROL => "Cache-Control",
90        header::CONNECTION => "Connection",
91        header::CONTENT_TYPE => "Content-Type",
92        header::CONTENT_ENCODING => "Content-Encoding",
93        header::CONTENT_LENGTH => "Content-Length",
94        header::DATE => "Date",
95        header::TRANSFER_ENCODING => "Transfer-Encoding",
96        header::HOST => "Host",
97        header::SERVER => "Server",
98        header::SET_COOKIE => "Set-Cookie",
99        // TODO: add more const header here to map to their titled case
100        // TODO: automatically upper case the first letter?
101        _ => {
102            return None;
103        }
104    })
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn test_case_header_name() {
113        assert_eq!("FoO".into_case_header_name().as_slice(), b"FoO");
114        assert_eq!("FoO".to_string().into_case_header_name().as_slice(), b"FoO");
115        assert_eq!(header::SERVER.into_case_header_name().as_slice(), b"Server");
116    }
117}