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 | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 3 | #include <bmcweb_config.h> |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 4 | #include <openssl/crypto.h> |
| 5 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 6 | #include <boost/date_time/posix_time/posix_time.hpp> |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 7 | #include <boost/url/url.hpp> |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 8 | |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 9 | #include <array> |
Ed Tanous | 74849be | 2021-02-05 09:47:47 -0800 | [diff] [blame] | 10 | #include <chrono> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | #include <cstdint> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 12 | #include <ctime> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 13 | #include <functional> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 14 | #include <limits> |
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> |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 21 | #include <variant> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | namespace crow |
| 24 | { |
| 25 | namespace black_magic |
| 26 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 27 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 28 | template <typename T> |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 29 | constexpr uint64_t getParameterTag() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 31 | if constexpr (std::is_same_v<int, T>) |
| 32 | { |
| 33 | return 1; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | } |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 35 | if constexpr (std::is_same_v<char, T>) |
| 36 | { |
| 37 | return 1; |
| 38 | } |
| 39 | if constexpr (std::is_same_v<short, T>) |
| 40 | { |
| 41 | return 1; |
| 42 | } |
| 43 | if constexpr (std::is_same_v<long, T>) |
| 44 | { |
| 45 | return 1; |
| 46 | } |
| 47 | if constexpr (std::is_same_v<long long, T>) |
| 48 | { |
| 49 | return 1; |
| 50 | } |
| 51 | if constexpr (std::is_same_v<unsigned int, T>) |
| 52 | { |
| 53 | return 2; |
| 54 | } |
| 55 | if constexpr (std::is_same_v<unsigned char, T>) |
| 56 | { |
| 57 | return 2; |
| 58 | } |
| 59 | if constexpr (std::is_same_v<unsigned short, T>) |
| 60 | { |
| 61 | return 2; |
| 62 | } |
| 63 | if constexpr (std::is_same_v<unsigned long, T>) |
| 64 | { |
| 65 | return 2; |
| 66 | } |
| 67 | if constexpr (std::is_same_v<unsigned long long, T>) |
| 68 | { |
| 69 | return 2; |
| 70 | } |
| 71 | if constexpr (std::is_same_v<double, T>) |
| 72 | { |
| 73 | return 3; |
| 74 | } |
| 75 | if constexpr (std::is_same_v<std::string, T>) |
| 76 | { |
| 77 | return 4; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 82 | template <typename... Args> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 83 | struct computeParameterTagFromArgsList; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 84 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 85 | template <> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 86 | struct computeParameterTagFromArgsList<> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 88 | static constexpr int value = 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | template <typename Arg, typename... Args> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 92 | struct computeParameterTagFromArgsList<Arg, Args...> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 94 | static constexpr int subValue = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 95 | computeParameterTagFromArgsList<Args...>::value; |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 96 | static constexpr int value = |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 97 | getParameterTag<typename std::decay<Arg>::type>() != 0 |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 98 | ? subValue * 6 + getParameterTag<typename std::decay<Arg>::type>() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 99 | : subValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 102 | inline bool isParameterTagCompatible(uint64_t a, uint64_t b) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | { |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 104 | while (true) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | { |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 106 | if (a == 0) |
| 107 | { |
| 108 | return b == 0; |
| 109 | } |
| 110 | if (b == 0) |
| 111 | { |
| 112 | return a == 0; |
| 113 | } |
| 114 | uint64_t sa = a % 6; |
| 115 | uint64_t sb = a % 6; |
| 116 | if (sa == 5) |
| 117 | { |
| 118 | sa = 4; |
| 119 | } |
| 120 | if (sb == 5) |
| 121 | { |
| 122 | sb = 4; |
| 123 | } |
| 124 | if (sa != sb) |
| 125 | { |
| 126 | return false; |
| 127 | } |
| 128 | a /= 6; |
| 129 | b /= 6; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | } |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 131 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 134 | constexpr inline uint64_t getParameterTag(std::string_view url) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 135 | { |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 136 | uint64_t tagValue = 0; |
| 137 | size_t urlSegmentIndex = std::string_view::npos; |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 138 | |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 139 | size_t paramIndex = 0; |
| 140 | |
| 141 | for (size_t urlIndex = 0; urlIndex < url.size(); urlIndex++) |
| 142 | { |
| 143 | char character = url[urlIndex]; |
| 144 | if (character == '<') |
| 145 | { |
| 146 | if (urlSegmentIndex != std::string_view::npos) |
| 147 | { |
| 148 | return 0; |
| 149 | } |
| 150 | urlSegmentIndex = urlIndex; |
| 151 | } |
| 152 | if (character == '>') |
| 153 | { |
| 154 | if (urlSegmentIndex == std::string_view::npos) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | std::string_view tag = |
| 159 | url.substr(urlSegmentIndex, urlIndex + 1 - urlSegmentIndex); |
| 160 | |
| 161 | // Note, this is a really lame way to do std::pow(6, paramIndex) |
| 162 | // std::pow doesn't work in constexpr in clang. |
| 163 | // Ideally in the future we'd move this to use a power of 2 packing |
| 164 | // (probably 8 instead of 6) so that these just become bit shifts |
| 165 | uint64_t insertIndex = 1; |
| 166 | for (size_t unused = 0; unused < paramIndex; unused++) |
| 167 | { |
| 168 | insertIndex *= 6; |
| 169 | } |
| 170 | |
| 171 | if (tag == "<int>") |
| 172 | { |
| 173 | tagValue += insertIndex * 1; |
| 174 | } |
| 175 | if (tag == "<uint>") |
| 176 | { |
| 177 | tagValue += insertIndex * 2; |
| 178 | } |
| 179 | if (tag == "<float>" || tag == "<double>") |
| 180 | { |
| 181 | tagValue += insertIndex * 3; |
| 182 | } |
| 183 | if (tag == "<str>" || tag == "<string>") |
| 184 | { |
| 185 | tagValue += insertIndex * 4; |
| 186 | } |
| 187 | if (tag == "<path>") |
| 188 | { |
| 189 | tagValue += insertIndex * 5; |
| 190 | } |
| 191 | paramIndex++; |
| 192 | urlSegmentIndex = std::string_view::npos; |
| 193 | } |
| 194 | } |
| 195 | if (urlSegmentIndex != std::string_view::npos) |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 196 | { |
| 197 | return 0; |
| 198 | } |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 199 | return tagValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 200 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 201 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 202 | template <typename... T> |
| 203 | struct S |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 205 | template <typename U> |
| 206 | using push = S<U, T...>; |
| 207 | template <typename U> |
| 208 | using push_back = S<T..., U>; |
| 209 | template <template <typename... Args> class U> |
| 210 | using rebind = U<T...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 211 | }; |
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 Set> |
| 214 | struct CallHelper; |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 215 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 216 | template <typename F, typename... Args> |
| 217 | struct CallHelper<F, S<Args...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 218 | { |
| 219 | template <typename F1, typename... Args1, |
| 220 | typename = decltype(std::declval<F1>()(std::declval<Args1>()...))> |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 221 | static char test(int); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 222 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 223 | template <typename...> |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 224 | static int test(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 225 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 226 | static constexpr bool value = sizeof(test<F, Args...>(0)) == sizeof(char); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 229 | template <uint64_t N> |
| 230 | struct SingleTagToType |
| 231 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 232 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 233 | template <> |
| 234 | struct SingleTagToType<1> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 235 | { |
| 236 | using type = int64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 239 | template <> |
| 240 | struct SingleTagToType<2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 241 | { |
| 242 | using type = uint64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 245 | template <> |
| 246 | struct SingleTagToType<3> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 247 | { |
| 248 | using type = double; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 251 | template <> |
| 252 | struct SingleTagToType<4> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 253 | { |
| 254 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 257 | template <> |
| 258 | struct SingleTagToType<5> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 259 | { |
| 260 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 263 | template <uint64_t Tag> |
| 264 | struct Arguments |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 265 | { |
| 266 | using subarguments = typename Arguments<Tag / 6>::type; |
| 267 | using type = typename subarguments::template push< |
| 268 | typename SingleTagToType<Tag % 6>::type>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 271 | template <> |
| 272 | struct Arguments<0> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | { |
| 274 | using type = S<>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 277 | template <typename T> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 278 | struct Promote |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | { |
| 280 | using type = T; |
| 281 | }; |
| 282 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 283 | template <typename T> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 284 | using PromoteT = typename Promote<T>::type; |
| 285 | |
| 286 | template <> |
| 287 | struct Promote<char> |
| 288 | { |
| 289 | using type = int64_t; |
| 290 | }; |
| 291 | template <> |
| 292 | struct Promote<short> |
| 293 | { |
| 294 | using type = int64_t; |
| 295 | }; |
| 296 | template <> |
| 297 | struct Promote<int> |
| 298 | { |
| 299 | using type = int64_t; |
| 300 | }; |
| 301 | template <> |
| 302 | struct Promote<long> |
| 303 | { |
| 304 | using type = int64_t; |
| 305 | }; |
| 306 | template <> |
| 307 | struct Promote<long long> |
| 308 | { |
| 309 | using type = int64_t; |
| 310 | }; |
| 311 | template <> |
| 312 | struct Promote<unsigned char> |
| 313 | { |
| 314 | using type = uint64_t; |
| 315 | }; |
| 316 | template <> |
| 317 | struct Promote<unsigned short> |
| 318 | { |
| 319 | using type = uint64_t; |
| 320 | }; |
| 321 | template <> |
| 322 | struct Promote<unsigned int> |
| 323 | { |
| 324 | using type = uint64_t; |
| 325 | }; |
| 326 | template <> |
| 327 | struct Promote<unsigned long> |
| 328 | { |
| 329 | using type = uint64_t; |
| 330 | }; |
| 331 | template <> |
| 332 | struct Promote<unsigned long long> |
| 333 | { |
| 334 | using type = uint64_t; |
| 335 | }; |
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 black_magic |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 338 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | namespace utility |
| 340 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 341 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 342 | template <typename T> |
| 343 | struct function_traits; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 344 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 345 | template <typename T> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 346 | struct function_traits : public function_traits<decltype(&T::operator())> |
| 347 | { |
| 348 | using parent_t = function_traits<decltype(&T::operator())>; |
| 349 | static const size_t arity = parent_t::arity; |
| 350 | using result_type = typename parent_t::result_type; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 351 | template <size_t i> |
| 352 | using arg = typename parent_t::template arg<i>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 353 | }; |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 354 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 355 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 356 | struct function_traits<r (ClassType::*)(Args...) const> |
| 357 | { |
| 358 | static const size_t arity = sizeof...(Args); |
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 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 361 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 362 | template <size_t i> |
| 363 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 366 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 367 | struct function_traits<r (ClassType::*)(Args...)> |
| 368 | { |
| 369 | static const size_t arity = sizeof...(Args); |
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 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 372 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 373 | template <size_t i> |
| 374 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 375 | }; |
| 376 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 377 | template <typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 378 | struct function_traits<std::function<r(Args...)>> |
| 379 | { |
| 380 | static const size_t arity = sizeof...(Args); |
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 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 383 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 384 | template <size_t i> |
| 385 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 388 | inline std::string base64encode(const std::string_view data) |
| 389 | { |
| 390 | const std::array<char, 64> key = { |
| 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 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 394 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 395 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
| 396 | |
| 397 | size_t size = data.size(); |
| 398 | std::string ret; |
| 399 | ret.resize((size + 2) / 3 * 4); |
| 400 | auto it = ret.begin(); |
| 401 | |
| 402 | size_t i = 0; |
| 403 | while (i < size) |
| 404 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 405 | size_t keyIndex = 0; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 406 | |
| 407 | keyIndex = static_cast<size_t>(data[i] & 0xFC) >> 2; |
| 408 | *it++ = key[keyIndex]; |
| 409 | |
| 410 | if (i + 1 < size) |
| 411 | { |
| 412 | keyIndex = static_cast<size_t>(data[i] & 0x03) << 4; |
| 413 | keyIndex += static_cast<size_t>(data[i + 1] & 0xF0) >> 4; |
| 414 | *it++ = key[keyIndex]; |
| 415 | |
| 416 | if (i + 2 < size) |
| 417 | { |
| 418 | keyIndex = static_cast<size_t>(data[i + 1] & 0x0F) << 2; |
| 419 | keyIndex += static_cast<size_t>(data[i + 2] & 0xC0) >> 6; |
| 420 | *it++ = key[keyIndex]; |
| 421 | |
| 422 | keyIndex = static_cast<size_t>(data[i + 2] & 0x3F); |
| 423 | *it++ = key[keyIndex]; |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | keyIndex = static_cast<size_t>(data[i + 1] & 0x0F) << 2; |
| 428 | *it++ = key[keyIndex]; |
| 429 | *it++ = '='; |
| 430 | } |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | keyIndex = static_cast<size_t>(data[i] & 0x03) << 4; |
| 435 | *it++ = key[keyIndex]; |
| 436 | *it++ = '='; |
| 437 | *it++ = '='; |
| 438 | } |
| 439 | |
| 440 | i += 3; |
| 441 | } |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 446 | // TODO this is temporary and should be deleted once base64 is refactored out of |
| 447 | // crow |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 448 | inline bool base64Decode(const std::string_view input, std::string& output) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 449 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 450 | static const char nop = static_cast<char>(-1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 451 | // See note on encoding_data[] in above function |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 452 | static const std::array<char, 256> decodingData = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 453 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 454 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 455 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 456 | nop, 62, nop, nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, |
| 457 | 60, 61, nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, |
| 458 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 459 | 19, 20, 21, 22, 23, 24, 25, nop, nop, nop, nop, nop, nop, 26, |
| 460 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 461 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 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, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 470 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 471 | nop, nop, nop, nop}; |
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 | size_t inputLength = input.size(); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 474 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 475 | // allocate space for output string |
| 476 | output.clear(); |
| 477 | output.reserve(((inputLength + 2) / 3) * 4); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 478 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 479 | auto getCodeValue = [](char c) { |
| 480 | auto code = static_cast<unsigned char>(c); |
| 481 | // Ensure we cannot index outside the bounds of the decoding array |
| 482 | static_assert(std::numeric_limits<decltype(code)>::max() < |
| 483 | decodingData.size()); |
| 484 | return decodingData[code]; |
| 485 | }; |
| 486 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 487 | // 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] | 488 | // dropping first two bits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 489 | // and regenerate into 3 8-bits sequences |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 490 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 491 | for (size_t i = 0; i < inputLength; i++) |
| 492 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 493 | char base64code0 = 0; |
| 494 | char base64code1 = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 495 | char base64code2 = 0; // initialized to 0 to suppress warnings |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 496 | char base64code3 = 0; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 497 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 498 | base64code0 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 499 | if (base64code0 == nop) |
| 500 | { // non base64 character |
| 501 | return false; |
| 502 | } |
| 503 | if (!(++i < inputLength)) |
| 504 | { // we need at least two input bytes for first |
| 505 | // byte output |
| 506 | return false; |
| 507 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 508 | base64code1 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 509 | if (base64code1 == nop) |
| 510 | { // non base64 character |
| 511 | return false; |
| 512 | } |
| 513 | output += |
| 514 | static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 515 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 516 | if (++i < inputLength) |
| 517 | { |
| 518 | char c = input[i]; |
| 519 | if (c == '=') |
| 520 | { // padding , end of input |
| 521 | return (base64code1 & 0x0f) == 0; |
| 522 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 523 | base64code2 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 524 | if (base64code2 == nop) |
| 525 | { // non base64 character |
| 526 | return false; |
| 527 | } |
| 528 | output += static_cast<char>(((base64code1 << 4) & 0xf0) | |
| 529 | ((base64code2 >> 2) & 0x0f)); |
| 530 | } |
| 531 | |
| 532 | if (++i < inputLength) |
| 533 | { |
| 534 | char c = input[i]; |
| 535 | if (c == '=') |
| 536 | { // padding , end of input |
| 537 | return (base64code2 & 0x03) == 0; |
| 538 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 539 | base64code3 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 540 | if (base64code3 == nop) |
| 541 | { // non base64 character |
| 542 | return false; |
| 543 | } |
| 544 | output += |
| 545 | static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3)); |
| 546 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 549 | return true; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 552 | namespace details |
| 553 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 554 | constexpr uint64_t maxMilliSeconds = 253402300799999; |
| 555 | constexpr uint64_t maxSeconds = 253402300799; |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 556 | inline std::string getDateTime(boost::posix_time::milliseconds timeSinceEpoch) |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 557 | { |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 558 | boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 559 | boost::posix_time::ptime time = epoch + timeSinceEpoch; |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 560 | // 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] | 561 | return boost::posix_time::to_iso_extended_string(time) + "+00:00"; |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 562 | } |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 563 | } // namespace details |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 564 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 565 | // Returns the formatted date time string. |
| 566 | // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if |
| 567 | // the given |secondsSinceEpoch| is too large, we return the maximum supported |
| 568 | // date. This behavior is to avoid exceptions throwed by Boost. |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 569 | inline std::string getDateTimeUint(uint64_t secondsSinceEpoch) |
| 570 | { |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 571 | secondsSinceEpoch = std::min(secondsSinceEpoch, details::maxSeconds); |
| 572 | boost::posix_time::seconds boostSeconds(secondsSinceEpoch); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 573 | return details::getDateTime( |
| 574 | boost::posix_time::milliseconds(boostSeconds.total_milliseconds())); |
| 575 | } |
| 576 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 577 | // Returns the formatted date time string. |
| 578 | // Note that the maximum supported date is 9999-12-31T23:59:59.999+00:00, if |
| 579 | // the given |millisSecondsSinceEpoch| is too large, we return the maximum |
| 580 | // supported date. |
| 581 | inline std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch) |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 582 | { |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 583 | milliSecondsSinceEpoch = |
| 584 | std::min(details::maxMilliSeconds, milliSecondsSinceEpoch); |
| 585 | return details::getDateTime( |
| 586 | boost::posix_time::milliseconds(milliSecondsSinceEpoch)); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 587 | } |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 588 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 589 | inline std::string getDateTimeStdtime(std::time_t secondsSinceEpoch) |
| 590 | { |
Ed Tanous | 46666f3 | 2022-03-03 14:55:16 -0800 | [diff] [blame] | 591 | // secondsSinceEpoch >= maxSeconds |
| 592 | if constexpr (std::cmp_less_equal(details::maxSeconds, |
| 593 | std::numeric_limits<std::time_t>::max())) |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 594 | { |
Ed Tanous | 46666f3 | 2022-03-03 14:55:16 -0800 | [diff] [blame] | 595 | if (std::cmp_greater_equal(secondsSinceEpoch, details::maxSeconds)) |
| 596 | { |
| 597 | secondsSinceEpoch = details::maxSeconds; |
| 598 | } |
Nan Zhou | 665479d | 2022-01-26 12:12:59 -0800 | [diff] [blame] | 599 | } |
| 600 | boost::posix_time::ptime time = |
| 601 | boost::posix_time::from_time_t(secondsSinceEpoch); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 602 | return boost::posix_time::to_iso_extended_string(time) + "+00:00"; |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Tejas Patil | 7c8c405 | 2021-06-04 17:43:14 +0530 | [diff] [blame] | 605 | /** |
| 606 | * Returns the current Date, Time & the local Time Offset |
| 607 | * infromation in a pair |
| 608 | * |
| 609 | * @param[in] None |
| 610 | * |
| 611 | * @return std::pair<std::string, std::string>, which consist |
| 612 | * of current DateTime & the TimeOffset strings respectively. |
| 613 | */ |
| 614 | inline std::pair<std::string, std::string> getDateTimeOffsetNow() |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 615 | { |
| 616 | std::time_t time = std::time(nullptr); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 617 | std::string dateTime = getDateTimeStdtime(time); |
Tejas Patil | 7c8c405 | 2021-06-04 17:43:14 +0530 | [diff] [blame] | 618 | |
| 619 | /* extract the local Time Offset value from the |
| 620 | * recevied dateTime string. |
| 621 | */ |
| 622 | std::string timeOffset("Z00:00"); |
| 623 | std::size_t lastPos = dateTime.size(); |
| 624 | std::size_t len = timeOffset.size(); |
| 625 | if (lastPos > len) |
| 626 | { |
| 627 | timeOffset = dateTime.substr(lastPos - len); |
| 628 | } |
| 629 | |
| 630 | return std::make_pair(dateTime, timeOffset); |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 633 | inline bool constantTimeStringCompare(const std::string_view a, |
| 634 | const std::string_view b) |
| 635 | { |
| 636 | // Important note, this function is ONLY constant time if the two input |
| 637 | // sizes are the same |
| 638 | if (a.size() != b.size()) |
| 639 | { |
| 640 | return false; |
| 641 | } |
| 642 | return CRYPTO_memcmp(a.data(), b.data(), a.size()) == 0; |
| 643 | } |
| 644 | |
| 645 | struct ConstantTimeCompare |
| 646 | { |
| 647 | bool operator()(const std::string_view a, const std::string_view b) const |
| 648 | { |
| 649 | return constantTimeStringCompare(a, b); |
| 650 | } |
| 651 | }; |
| 652 | |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 653 | namespace details |
| 654 | { |
| 655 | inline boost::urls::url |
| 656 | urlFromPiecesDetail(const std::initializer_list<std::string_view> args) |
| 657 | { |
| 658 | boost::urls::url url("/"); |
| 659 | for (const std::string_view& arg : args) |
| 660 | { |
| 661 | url.segments().push_back(arg); |
| 662 | } |
| 663 | return url; |
| 664 | } |
| 665 | } // namespace details |
| 666 | |
| 667 | template <typename... AV> |
| 668 | inline boost::urls::url urlFromPieces(const AV... args) |
| 669 | { |
| 670 | return details::urlFromPiecesDetail({args...}); |
| 671 | } |
| 672 | |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 673 | namespace details |
| 674 | { |
| 675 | |
| 676 | // std::reference_wrapper<std::string> - extracts segment to variable |
| 677 | // std::string_view - checks if segment is equal to variable |
| 678 | using UrlSegment = |
| 679 | std::variant<std::reference_wrapper<std::string>, std::string_view>; |
| 680 | |
| 681 | class UrlSegmentMatcherVisitor |
| 682 | { |
| 683 | public: |
| 684 | bool operator()(std::string& output) |
| 685 | { |
| 686 | output = std::string_view(segment.data(), segment.size()); |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | bool operator()(std::string_view expected) |
| 691 | { |
| 692 | return std::string_view(segment.data(), segment.size()) == expected; |
| 693 | } |
| 694 | |
| 695 | UrlSegmentMatcherVisitor(const boost::urls::string_value& segmentIn) : |
| 696 | segment(segmentIn) |
| 697 | {} |
| 698 | |
| 699 | private: |
| 700 | const boost::urls::string_value& segment; |
| 701 | }; |
| 702 | |
| 703 | inline bool readUrlSegments(const boost::urls::url_view& urlView, |
| 704 | std::initializer_list<UrlSegment>&& segments) |
| 705 | { |
| 706 | const boost::urls::segments_view& urlSegments = urlView.segments(); |
| 707 | |
| 708 | if (!urlSegments.is_absolute() || segments.size() != urlSegments.size()) |
| 709 | { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | boost::urls::segments_view::iterator it = urlSegments.begin(); |
| 714 | boost::urls::segments_view::iterator end = urlSegments.end(); |
| 715 | |
| 716 | for (const auto& segment : segments) |
| 717 | { |
| 718 | if (!std::visit(UrlSegmentMatcherVisitor(*it), segment)) |
| 719 | { |
| 720 | return false; |
| 721 | } |
| 722 | it++; |
| 723 | } |
| 724 | return true; |
| 725 | } |
| 726 | |
| 727 | } // namespace details |
| 728 | |
| 729 | template <typename... Args> |
| 730 | inline bool readUrlSegments(const boost::urls::url_view& urlView, |
| 731 | Args&&... args) |
| 732 | { |
| 733 | return details::readUrlSegments(urlView, {std::forward<Args>(args)...}); |
| 734 | } |
| 735 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 736 | inline std::string setProtocolDefaults(const boost::urls::url_view& url) |
| 737 | { |
| 738 | if (url.scheme() == "https") |
| 739 | { |
| 740 | return "https"; |
| 741 | } |
| 742 | if (url.scheme() == "http") |
| 743 | { |
| 744 | if (bmcwebInsecureEnableHttpPushStyleEventing) |
| 745 | { |
| 746 | return "http"; |
| 747 | } |
| 748 | return ""; |
| 749 | } |
| 750 | return ""; |
| 751 | } |
| 752 | |
| 753 | inline uint16_t setPortDefaults(const boost::urls::url_view& url) |
| 754 | { |
| 755 | uint16_t port = url.port_number(); |
| 756 | if (port != 0) |
| 757 | { |
| 758 | // user picked a port already. |
| 759 | return port; |
| 760 | } |
| 761 | |
| 762 | // If the user hasn't explicitly stated a port, pick one explicitly for them |
| 763 | // based on the protocol defaults |
| 764 | if (url.scheme() == "http") |
| 765 | { |
| 766 | return 80; |
| 767 | } |
| 768 | if (url.scheme() == "https") |
| 769 | { |
| 770 | return 443; |
| 771 | } |
| 772 | return 0; |
| 773 | } |
| 774 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 775 | inline bool validateAndSplitUrl(std::string_view destUrl, std::string& urlProto, |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 776 | std::string& host, uint16_t& port, |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 777 | std::string& path) |
| 778 | { |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 779 | boost::string_view urlBoost(destUrl.data(), destUrl.size()); |
| 780 | boost::urls::result<boost::urls::url_view> url = |
| 781 | boost::urls::parse_uri(urlBoost); |
| 782 | if (!url) |
| 783 | { |
| 784 | return false; |
| 785 | } |
| 786 | urlProto = setProtocolDefaults(url.value()); |
| 787 | if (urlProto.empty()) |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 788 | { |
| 789 | return false; |
| 790 | } |
| 791 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 792 | port = setPortDefaults(url.value()); |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 793 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 794 | host = std::string_view(url->encoded_host().data(), |
| 795 | url->encoded_host().size()); |
| 796 | |
| 797 | path = std::string_view(url->encoded_path().data(), |
| 798 | url->encoded_path().size()); |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 799 | if (path.empty()) |
| 800 | { |
| 801 | path = "/"; |
| 802 | } |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 803 | if (url->has_fragment()) |
| 804 | { |
| 805 | path += '#'; |
| 806 | path += std::string_view(url->encoded_fragment().data(), |
| 807 | url->encoded_fragment().size()); |
| 808 | } |
| 809 | |
| 810 | if (url->has_query()) |
| 811 | { |
| 812 | path += '?'; |
| 813 | path += std::string_view(url->encoded_query().data(), |
| 814 | url->encoded_query().size()); |
| 815 | } |
| 816 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 817 | return true; |
| 818 | } |
| 819 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 820 | } // namespace utility |
| 821 | } // namespace crow |