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