Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 3 | #include <openssl/crypto.h> |
| 4 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 5 | #include <boost/date_time/posix_time/posix_time.hpp> |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 6 | #include <boost/url/url.hpp> |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 7 | |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 8 | #include <array> |
Ed Tanous | 74849be | 2021-02-05 09:47:47 -0800 | [diff] [blame] | 9 | #include <chrono> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 10 | #include <cstdint> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 11 | #include <ctime> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 12 | #include <functional> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 13 | #include <limits> |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 14 | #include <regex> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 15 | #include <stdexcept> |
| 16 | #include <string> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 17 | #include <string_view> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 18 | #include <tuple> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 19 | #include <type_traits> |
| 20 | #include <utility> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 21 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | namespace crow |
| 23 | { |
| 24 | namespace black_magic |
| 25 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 26 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 27 | constexpr unsigned findClosingTag(std::string_view s, unsigned p) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | { |
| 29 | return s[p] == '>' ? p : findClosingTag(s, p + 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 32 | constexpr bool isInt(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 34 | return s.substr(i, 5) == "<int>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 37 | constexpr bool isUint(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 39 | return s.substr(i, 6) == "<uint>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 42 | constexpr bool isFloat(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 44 | return s.substr(i, 7) == "<float>" || s.substr(i, 8) == "<double>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 47 | constexpr bool isStr(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 49 | return s.substr(i, 5) == "<str>" || s.substr(i, 8) == "<string>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 52 | constexpr bool isPath(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 54 | return s.substr(i, 6) == "<path>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 55 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 56 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 57 | template <typename T> |
| 58 | constexpr int getParameterTag() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 60 | if constexpr (std::is_same_v<int, T>) |
| 61 | { |
| 62 | return 1; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | } |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 64 | if constexpr (std::is_same_v<char, T>) |
| 65 | { |
| 66 | return 1; |
| 67 | } |
| 68 | if constexpr (std::is_same_v<short, T>) |
| 69 | { |
| 70 | return 1; |
| 71 | } |
| 72 | if constexpr (std::is_same_v<long, T>) |
| 73 | { |
| 74 | return 1; |
| 75 | } |
| 76 | if constexpr (std::is_same_v<long long, T>) |
| 77 | { |
| 78 | return 1; |
| 79 | } |
| 80 | if constexpr (std::is_same_v<unsigned int, T>) |
| 81 | { |
| 82 | return 2; |
| 83 | } |
| 84 | if constexpr (std::is_same_v<unsigned char, T>) |
| 85 | { |
| 86 | return 2; |
| 87 | } |
| 88 | if constexpr (std::is_same_v<unsigned short, T>) |
| 89 | { |
| 90 | return 2; |
| 91 | } |
| 92 | if constexpr (std::is_same_v<unsigned long, T>) |
| 93 | { |
| 94 | return 2; |
| 95 | } |
| 96 | if constexpr (std::is_same_v<unsigned long long, T>) |
| 97 | { |
| 98 | return 2; |
| 99 | } |
| 100 | if constexpr (std::is_same_v<double, T>) |
| 101 | { |
| 102 | return 3; |
| 103 | } |
| 104 | if constexpr (std::is_same_v<std::string, T>) |
| 105 | { |
| 106 | return 4; |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 111 | template <typename... Args> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 112 | struct computeParameterTagFromArgsList; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 113 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 114 | template <> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 115 | struct computeParameterTagFromArgsList<> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 117 | static constexpr int value = 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | template <typename Arg, typename... Args> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 121 | struct computeParameterTagFromArgsList<Arg, Args...> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 123 | static constexpr int subValue = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 124 | computeParameterTagFromArgsList<Args...>::value; |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 125 | static constexpr int value = |
| 126 | getParameterTag<typename std::decay<Arg>::type>() |
| 127 | ? subValue * 6 + getParameterTag<typename std::decay<Arg>::type>() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 128 | : subValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 131 | inline bool isParameterTagCompatible(uint64_t a, uint64_t b) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 132 | { |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 133 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 134 | if (a == 0) |
| 135 | { |
| 136 | return b == 0; |
| 137 | } |
| 138 | if (b == 0) |
| 139 | { |
| 140 | return a == 0; |
| 141 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 142 | uint64_t sa = a % 6; |
| 143 | uint64_t sb = a % 6; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | if (sa == 5) |
| 145 | { |
| 146 | sa = 4; |
| 147 | } |
| 148 | if (sb == 5) |
| 149 | { |
| 150 | sb = 4; |
| 151 | } |
| 152 | if (sa != sb) |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | return isParameterTagCompatible(a / 6, b / 6); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 159 | constexpr uint64_t getParameterTag(std::string_view s, unsigned p = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 160 | { |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 161 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 162 | if (p == s.size()) |
| 163 | { |
| 164 | return 0; |
| 165 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 166 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 167 | if (s[p] != '<') |
| 168 | { |
| 169 | return getParameterTag(s, p + 1); |
| 170 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 171 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 172 | if (isInt(s, p)) |
| 173 | { |
| 174 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 1; |
| 175 | } |
| 176 | |
| 177 | if (isUint(s, p)) |
| 178 | { |
| 179 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 2; |
| 180 | } |
| 181 | |
| 182 | if (isFloat(s, p)) |
| 183 | { |
| 184 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 3; |
| 185 | } |
| 186 | |
| 187 | if (isStr(s, p)) |
| 188 | { |
| 189 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 4; |
| 190 | } |
| 191 | |
| 192 | if (isPath(s, p)) |
| 193 | { |
| 194 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 5; |
| 195 | } |
| 196 | |
| 197 | throw std::runtime_error("invalid parameter type"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 198 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 199 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 200 | template <typename... T> |
| 201 | struct S |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 203 | template <typename U> |
| 204 | using push = S<U, T...>; |
| 205 | template <typename U> |
| 206 | using push_back = S<T..., U>; |
| 207 | template <template <typename... Args> class U> |
| 208 | using rebind = U<T...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 209 | }; |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 210 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 211 | template <typename F, typename Set> |
| 212 | struct CallHelper; |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 213 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 214 | template <typename F, typename... Args> |
| 215 | struct CallHelper<F, S<Args...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 216 | { |
| 217 | template <typename F1, typename... Args1, |
| 218 | typename = decltype(std::declval<F1>()(std::declval<Args1>()...))> |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 219 | static char test(int); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 220 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 221 | template <typename...> |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 222 | static int test(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 223 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 224 | static constexpr bool value = sizeof(test<F, Args...>(0)) == sizeof(char); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 227 | template <uint64_t N> |
| 228 | struct SingleTagToType |
| 229 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 230 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 231 | template <> |
| 232 | struct SingleTagToType<1> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 233 | { |
| 234 | using type = int64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 237 | template <> |
| 238 | struct SingleTagToType<2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 239 | { |
| 240 | using type = uint64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 243 | template <> |
| 244 | struct SingleTagToType<3> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 245 | { |
| 246 | using type = double; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 249 | template <> |
| 250 | struct SingleTagToType<4> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | { |
| 252 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 255 | template <> |
| 256 | struct SingleTagToType<5> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 257 | { |
| 258 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 261 | template <uint64_t Tag> |
| 262 | struct Arguments |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 263 | { |
| 264 | using subarguments = typename Arguments<Tag / 6>::type; |
| 265 | using type = typename subarguments::template push< |
| 266 | typename SingleTagToType<Tag % 6>::type>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 267 | }; |
| 268 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 269 | template <> |
| 270 | struct Arguments<0> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | { |
| 272 | using type = S<>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 275 | template <typename T> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 276 | struct Promote |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 277 | { |
| 278 | using type = T; |
| 279 | }; |
| 280 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 281 | template <typename T> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 282 | using PromoteT = typename Promote<T>::type; |
| 283 | |
| 284 | template <> |
| 285 | struct Promote<char> |
| 286 | { |
| 287 | using type = int64_t; |
| 288 | }; |
| 289 | template <> |
| 290 | struct Promote<short> |
| 291 | { |
| 292 | using type = int64_t; |
| 293 | }; |
| 294 | template <> |
| 295 | struct Promote<int> |
| 296 | { |
| 297 | using type = int64_t; |
| 298 | }; |
| 299 | template <> |
| 300 | struct Promote<long> |
| 301 | { |
| 302 | using type = int64_t; |
| 303 | }; |
| 304 | template <> |
| 305 | struct Promote<long long> |
| 306 | { |
| 307 | using type = int64_t; |
| 308 | }; |
| 309 | template <> |
| 310 | struct Promote<unsigned char> |
| 311 | { |
| 312 | using type = uint64_t; |
| 313 | }; |
| 314 | template <> |
| 315 | struct Promote<unsigned short> |
| 316 | { |
| 317 | using type = uint64_t; |
| 318 | }; |
| 319 | template <> |
| 320 | struct Promote<unsigned int> |
| 321 | { |
| 322 | using type = uint64_t; |
| 323 | }; |
| 324 | template <> |
| 325 | struct Promote<unsigned long> |
| 326 | { |
| 327 | using type = uint64_t; |
| 328 | }; |
| 329 | template <> |
| 330 | struct Promote<unsigned long long> |
| 331 | { |
| 332 | using type = uint64_t; |
| 333 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 334 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 335 | } // namespace black_magic |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 336 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 337 | namespace utility |
| 338 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 339 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 340 | template <typename T> |
| 341 | struct function_traits; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 342 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 343 | template <typename T> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 344 | struct function_traits : public function_traits<decltype(&T::operator())> |
| 345 | { |
| 346 | using parent_t = function_traits<decltype(&T::operator())>; |
| 347 | static const size_t arity = parent_t::arity; |
| 348 | using result_type = typename parent_t::result_type; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 349 | template <size_t i> |
| 350 | using arg = typename parent_t::template arg<i>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 351 | }; |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 352 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 353 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 354 | struct function_traits<r (ClassType::*)(Args...) const> |
| 355 | { |
| 356 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 357 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 358 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 359 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 360 | template <size_t i> |
| 361 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 364 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 365 | struct function_traits<r (ClassType::*)(Args...)> |
| 366 | { |
| 367 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 368 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 369 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 370 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 371 | template <size_t i> |
| 372 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 373 | }; |
| 374 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 375 | template <typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 376 | struct function_traits<std::function<r(Args...)>> |
| 377 | { |
| 378 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 379 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 380 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 381 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 382 | template <size_t i> |
| 383 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 384 | }; |
| 385 | |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 386 | inline std::string base64encode(const std::string_view data) |
| 387 | { |
| 388 | const std::array<char, 64> key = { |
| 389 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 390 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 391 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 392 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 393 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
| 394 | |
| 395 | size_t size = data.size(); |
| 396 | std::string ret; |
| 397 | ret.resize((size + 2) / 3 * 4); |
| 398 | auto it = ret.begin(); |
| 399 | |
| 400 | size_t i = 0; |
| 401 | while (i < size) |
| 402 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 403 | size_t keyIndex = 0; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 404 | |
| 405 | keyIndex = static_cast<size_t>(data[i] & 0xFC) >> 2; |
| 406 | *it++ = key[keyIndex]; |
| 407 | |
| 408 | if (i + 1 < size) |
| 409 | { |
| 410 | keyIndex = static_cast<size_t>(data[i] & 0x03) << 4; |
| 411 | keyIndex += static_cast<size_t>(data[i + 1] & 0xF0) >> 4; |
| 412 | *it++ = key[keyIndex]; |
| 413 | |
| 414 | if (i + 2 < size) |
| 415 | { |
| 416 | keyIndex = static_cast<size_t>(data[i + 1] & 0x0F) << 2; |
| 417 | keyIndex += static_cast<size_t>(data[i + 2] & 0xC0) >> 6; |
| 418 | *it++ = key[keyIndex]; |
| 419 | |
| 420 | keyIndex = static_cast<size_t>(data[i + 2] & 0x3F); |
| 421 | *it++ = key[keyIndex]; |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | keyIndex = static_cast<size_t>(data[i + 1] & 0x0F) << 2; |
| 426 | *it++ = key[keyIndex]; |
| 427 | *it++ = '='; |
| 428 | } |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | keyIndex = static_cast<size_t>(data[i] & 0x03) << 4; |
| 433 | *it++ = key[keyIndex]; |
| 434 | *it++ = '='; |
| 435 | *it++ = '='; |
| 436 | } |
| 437 | |
| 438 | i += 3; |
| 439 | } |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 444 | // TODO this is temporary and should be deleted once base64 is refactored out of |
| 445 | // crow |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 446 | inline bool base64Decode(const std::string_view input, std::string& output) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 447 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 448 | static const char nop = static_cast<char>(-1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 449 | // See note on encoding_data[] in above function |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 450 | static const std::array<char, 256> decodingData = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 451 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 452 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 453 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 454 | nop, 62, nop, nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, |
| 455 | 60, 61, nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, |
| 456 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 457 | 19, 20, 21, 22, 23, 24, 25, nop, nop, nop, nop, nop, nop, 26, |
| 458 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 459 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, nop, nop, nop, |
| 460 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 461 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 462 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 463 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 464 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 465 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 466 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 467 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 468 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 469 | nop, nop, nop, nop}; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 470 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 471 | size_t inputLength = input.size(); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 472 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 473 | // allocate space for output string |
| 474 | output.clear(); |
| 475 | output.reserve(((inputLength + 2) / 3) * 4); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 476 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 477 | auto getCodeValue = [](char c) { |
| 478 | auto code = static_cast<unsigned char>(c); |
| 479 | // Ensure we cannot index outside the bounds of the decoding array |
| 480 | static_assert(std::numeric_limits<decltype(code)>::max() < |
| 481 | decodingData.size()); |
| 482 | return decodingData[code]; |
| 483 | }; |
| 484 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 485 | // for each 4-bytes sequence from the input, extract 4 6-bits sequences by |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 486 | // dropping first two bits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 487 | // and regenerate into 3 8-bits sequences |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 488 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 489 | for (size_t i = 0; i < inputLength; i++) |
| 490 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 491 | char base64code0 = 0; |
| 492 | char base64code1 = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 493 | char base64code2 = 0; // initialized to 0 to suppress warnings |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 494 | char base64code3 = 0; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 495 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 496 | base64code0 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 497 | if (base64code0 == nop) |
| 498 | { // non base64 character |
| 499 | return false; |
| 500 | } |
| 501 | if (!(++i < inputLength)) |
| 502 | { // we need at least two input bytes for first |
| 503 | // byte output |
| 504 | return false; |
| 505 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 506 | base64code1 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 507 | if (base64code1 == nop) |
| 508 | { // non base64 character |
| 509 | return false; |
| 510 | } |
| 511 | output += |
| 512 | static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 513 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 514 | if (++i < inputLength) |
| 515 | { |
| 516 | char c = input[i]; |
| 517 | if (c == '=') |
| 518 | { // padding , end of input |
| 519 | return (base64code1 & 0x0f) == 0; |
| 520 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 521 | base64code2 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 522 | if (base64code2 == nop) |
| 523 | { // non base64 character |
| 524 | return false; |
| 525 | } |
| 526 | output += static_cast<char>(((base64code1 << 4) & 0xf0) | |
| 527 | ((base64code2 >> 2) & 0x0f)); |
| 528 | } |
| 529 | |
| 530 | if (++i < inputLength) |
| 531 | { |
| 532 | char c = input[i]; |
| 533 | if (c == '=') |
| 534 | { // padding , end of input |
| 535 | return (base64code2 & 0x03) == 0; |
| 536 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 537 | base64code3 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 538 | if (base64code3 == nop) |
| 539 | { // non base64 character |
| 540 | return false; |
| 541 | } |
| 542 | output += |
| 543 | static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3)); |
| 544 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 547 | return true; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 550 | namespace details |
| 551 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 552 | constexpr uint64_t maxMilliSeconds = 253402300799999; |
| 553 | constexpr uint64_t maxSeconds = 253402300799; |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 554 | inline std::string getDateTime(boost::posix_time::milliseconds timeSinceEpoch) |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 555 | { |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 556 | boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 557 | boost::posix_time::ptime time = epoch + timeSinceEpoch; |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 558 | // append zero offset to the end according to the Redfish spec for Date-Time |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 559 | return boost::posix_time::to_iso_extended_string(time) + "+00:00"; |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 560 | } |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 561 | } // namespace details |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 562 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 563 | // Returns the formatted date time string. |
| 564 | // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if |
| 565 | // the given |secondsSinceEpoch| is too large, we return the maximum supported |
| 566 | // date. This behavior is to avoid exceptions throwed by Boost. |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 567 | inline std::string getDateTimeUint(uint64_t secondsSinceEpoch) |
| 568 | { |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 569 | secondsSinceEpoch = std::min(secondsSinceEpoch, details::maxSeconds); |
| 570 | boost::posix_time::seconds boostSeconds(secondsSinceEpoch); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 571 | return details::getDateTime( |
| 572 | boost::posix_time::milliseconds(boostSeconds.total_milliseconds())); |
| 573 | } |
| 574 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 575 | // Returns the formatted date time string. |
| 576 | // Note that the maximum supported date is 9999-12-31T23:59:59.999+00:00, if |
| 577 | // the given |millisSecondsSinceEpoch| is too large, we return the maximum |
| 578 | // supported date. |
| 579 | inline std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch) |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 580 | { |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 581 | milliSecondsSinceEpoch = |
| 582 | std::min(details::maxMilliSeconds, milliSecondsSinceEpoch); |
| 583 | return details::getDateTime( |
| 584 | boost::posix_time::milliseconds(milliSecondsSinceEpoch)); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 585 | } |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 586 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 587 | inline std::string getDateTimeStdtime(std::time_t secondsSinceEpoch) |
| 588 | { |
Ed Tanous | 46666f3 | 2022-03-03 14:55:16 -0800 | [diff] [blame] | 589 | // secondsSinceEpoch >= maxSeconds |
| 590 | if constexpr (std::cmp_less_equal(details::maxSeconds, |
| 591 | std::numeric_limits<std::time_t>::max())) |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 592 | { |
Ed Tanous | 46666f3 | 2022-03-03 14:55:16 -0800 | [diff] [blame] | 593 | if (std::cmp_greater_equal(secondsSinceEpoch, details::maxSeconds)) |
| 594 | { |
| 595 | secondsSinceEpoch = details::maxSeconds; |
| 596 | } |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 597 | } |
| 598 | boost::posix_time::ptime time = |
| 599 | boost::posix_time::from_time_t(secondsSinceEpoch); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 600 | return boost::posix_time::to_iso_extended_string(time) + "+00:00"; |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Tejas Patil | 7c8c405 | 2021-06-04 17:43:14 +0530 | [diff] [blame] | 603 | /** |
| 604 | * Returns the current Date, Time & the local Time Offset |
| 605 | * infromation in a pair |
| 606 | * |
| 607 | * @param[in] None |
| 608 | * |
| 609 | * @return std::pair<std::string, std::string>, which consist |
| 610 | * of current DateTime & the TimeOffset strings respectively. |
| 611 | */ |
| 612 | inline std::pair<std::string, std::string> getDateTimeOffsetNow() |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 613 | { |
| 614 | std::time_t time = std::time(nullptr); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 615 | std::string dateTime = getDateTimeStdtime(time); |
Tejas Patil | 7c8c405 | 2021-06-04 17:43:14 +0530 | [diff] [blame] | 616 | |
| 617 | /* extract the local Time Offset value from the |
| 618 | * recevied dateTime string. |
| 619 | */ |
| 620 | std::string timeOffset("Z00:00"); |
| 621 | std::size_t lastPos = dateTime.size(); |
| 622 | std::size_t len = timeOffset.size(); |
| 623 | if (lastPos > len) |
| 624 | { |
| 625 | timeOffset = dateTime.substr(lastPos - len); |
| 626 | } |
| 627 | |
| 628 | return std::make_pair(dateTime, timeOffset); |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 631 | inline bool constantTimeStringCompare(const std::string_view a, |
| 632 | const std::string_view b) |
| 633 | { |
| 634 | // Important note, this function is ONLY constant time if the two input |
| 635 | // sizes are the same |
| 636 | if (a.size() != b.size()) |
| 637 | { |
| 638 | return false; |
| 639 | } |
| 640 | return CRYPTO_memcmp(a.data(), b.data(), a.size()) == 0; |
| 641 | } |
| 642 | |
| 643 | struct ConstantTimeCompare |
| 644 | { |
| 645 | bool operator()(const std::string_view a, const std::string_view b) const |
| 646 | { |
| 647 | return constantTimeStringCompare(a, b); |
| 648 | } |
| 649 | }; |
| 650 | |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 651 | namespace details |
| 652 | { |
| 653 | inline boost::urls::url |
| 654 | urlFromPiecesDetail(const std::initializer_list<std::string_view> args) |
| 655 | { |
| 656 | boost::urls::url url("/"); |
| 657 | for (const std::string_view& arg : args) |
| 658 | { |
| 659 | url.segments().push_back(arg); |
| 660 | } |
| 661 | return url; |
| 662 | } |
| 663 | } // namespace details |
| 664 | |
| 665 | template <typename... AV> |
| 666 | inline boost::urls::url urlFromPieces(const AV... args) |
| 667 | { |
| 668 | return details::urlFromPiecesDetail({args...}); |
| 669 | } |
| 670 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 671 | inline bool validateAndSplitUrl(std::string_view destUrl, std::string& urlProto, |
| 672 | std::string& host, std::string& port, |
| 673 | std::string& path) |
| 674 | { |
| 675 | // Validate URL using regex expression |
| 676 | // Format: <protocol>://<host>:<port>/<path> |
| 677 | // protocol: http/https |
| 678 | const std::regex urlRegex( |
| 679 | "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" |
| 680 | "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); |
| 681 | std::cmatch match; |
| 682 | if (!std::regex_match(destUrl.begin(), destUrl.end(), match, urlRegex)) |
| 683 | { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | urlProto = std::string(match[1].first, match[1].second); |
| 688 | if (urlProto == "http") |
| 689 | { |
| 690 | #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING |
| 691 | return false; |
| 692 | #endif |
| 693 | } |
| 694 | |
| 695 | host = std::string(match[2].first, match[2].second); |
| 696 | port = std::string(match[3].first, match[3].second); |
| 697 | path = std::string(match[4].first, match[4].second); |
| 698 | if (port.empty()) |
| 699 | { |
| 700 | if (urlProto == "http") |
| 701 | { |
| 702 | port = "80"; |
| 703 | } |
| 704 | else |
| 705 | { |
| 706 | port = "443"; |
| 707 | } |
| 708 | } |
| 709 | if (path.empty()) |
| 710 | { |
| 711 | path = "/"; |
| 712 | } |
| 713 | return true; |
| 714 | } |
| 715 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 716 | } // namespace utility |
| 717 | } // namespace crow |