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