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