1 /** @file
2 
3   Define the type used to store user-sensitive HTTP fields (such as cookies).
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <algorithm>
27 #include <strings.h>
28 #include <string>
29 #include <string_view>
30 #include <unordered_set>
31 
32 namespace traffic_dump
33 {
34 // A case-insensitive comparator used for comparing HTTP field names.
35 struct InsensitiveCompare {
36   bool
operatorInsensitiveCompare37   operator()(std::string_view a, std::string_view b) const
38   {
39     return strcasecmp(a.data(), b.data()) == 0;
40   }
41 };
42 
43 // A case-insensitive hash functor for HTTP field names.
44 struct StringHashByLower {
45 public:
46   size_t
operatorStringHashByLower47   operator()(std::string_view str) const
48   {
49     std::string lower;
50     std::transform(str.begin(), str.end(), lower.begin(), [](unsigned char c) -> unsigned char { return std::tolower(c); });
51     return std::hash<std::string>()(lower);
52   }
53 };
54 
55 /** The type used to store the set of user-sensitive HTTP fields, such as
56  * "Cookie" and "Set-Cookie". */
57 using sensitive_fields_t = std::unordered_set<std::string, StringHashByLower, InsensitiveCompare>;
58 
59 } // namespace traffic_dump
60