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