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