proxygen
SSLContextSelectionMisc.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <string>
19 #include <folly/hash/Hash.h>
20 #include <folly/String.h>
21 
22 namespace wangle {
23 
24 enum class CertCrypto {
27 };
28 
29 struct dn_char_traits : public std::char_traits<char> {
30  static bool eq(char c1, char c2) {
31  return ::tolower(c1) == ::tolower(c2);
32  }
33 
34  static bool ne(char c1, char c2) {
35  return ::tolower(c1) != ::tolower(c2);
36  }
37 
38  static bool lt(char c1, char c2) {
39  return ::tolower(c1) < ::tolower(c2);
40  }
41 
42  static int compare(const char* s1, const char* s2, size_t n) {
43  while (n--) {
44  if(::tolower(*s1) < ::tolower(*s2) ) {
45  return -1;
46  }
47  if(::tolower(*s1) > ::tolower(*s2) ) {
48  return 1;
49  }
50  ++s1;
51  ++s2;
52  }
53  return 0;
54  }
55 
56  static const char* find(const char* s, size_t n, char a) {
57  char la = ::tolower(a);
58  while (n--) {
59  if(::tolower(*s) == la) {
60  return s;
61  } else {
62  ++s;
63  }
64  }
65  return nullptr;
66  }
67 };
68 
69 // Case insensitive string
70 typedef std::basic_string<char, dn_char_traits> DNString;
71 
72 struct SSLContextKey {
73  DNString dnString;
75 
76  explicit SSLContextKey(DNString dns,
78  : dnString(dns), certCrypto(crypto) {}
79 
80  bool operator==(const SSLContextKey& rhs) const {
81  return dnString == rhs.dnString && certCrypto == rhs.certCrypto;
82  }
83 };
84 
86  size_t operator()(const SSLContextKey& sslContextKey) const noexcept {
87  std::string lowercase(sslContextKey.dnString.data(),
88  sslContextKey.dnString.size());
89  folly::toLowerAscii(lowercase);
91  lowercase, static_cast<int>(sslContextKey.certCrypto));
92  }
93 };
94 
95 } // namespace wangle
std::basic_string< char, dn_char_traits > DNString
static bool eq(char c1, char c2)
requires E e noexcept(noexcept(s.error(std::move(e))))
static bool lt(char c1, char c2)
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
size_t hash_combine(const T &t, const Ts &...ts) noexcept(noexcept(hash_combine_generic(StdHasher{}, t, ts...)))
Definition: Hash.h:669
static const char * find(const char *s, size_t n, char a)
char a
static bool ne(char c1, char c2)
bool operator==(const SSLContextKey &rhs) const
size_t operator()(const SSLContextKey &sslContextKey) const noexcept
void toLowerAscii(char *str, size_t length)
Definition: String.cpp:601
static int compare(const char *s1, const char *s2, size_t n)
const char * string
Definition: Conv.cpp:212
static set< string > s
crypto
Definition: CMakeCache.txt:597
SSLContextKey(DNString dns, CertCrypto crypto=CertCrypto::BEST_AVAILABLE)