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 | c867a83 | 2022-03-10 14:17:00 -0800 | [diff] [blame] | 5 | #include <boost/callable_traits.hpp> |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 6 | #include <boost/url/parse.hpp> |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 7 | #include <boost/url/url.hpp> |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 8 | #include <boost/url/url_view.hpp> |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 9 | #include <boost/url/url_view_base.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 | { |
Ed Tanous | 47488a9 | 2023-06-26 18:19:33 -0700 | [diff] [blame] | 30 | namespace utility |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 32 | |
Ed Tanous | 9de65b3 | 2024-03-27 13:34:40 -0700 | [diff] [blame] | 33 | constexpr uint64_t getParameterTag(std::string_view url) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | { |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 35 | uint64_t tagValue = 0; |
| 36 | size_t urlSegmentIndex = std::string_view::npos; |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 37 | |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 38 | for (size_t urlIndex = 0; urlIndex < url.size(); urlIndex++) |
| 39 | { |
| 40 | char character = url[urlIndex]; |
| 41 | if (character == '<') |
| 42 | { |
| 43 | if (urlSegmentIndex != std::string_view::npos) |
| 44 | { |
| 45 | return 0; |
| 46 | } |
| 47 | urlSegmentIndex = urlIndex; |
| 48 | } |
| 49 | if (character == '>') |
| 50 | { |
| 51 | if (urlSegmentIndex == std::string_view::npos) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 55 | std::string_view tag = |
| 56 | url.substr(urlSegmentIndex, urlIndex + 1 - urlSegmentIndex); |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 57 | |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 58 | if (tag == "<str>" || tag == "<string>") |
| 59 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 60 | tagValue++; |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 61 | } |
| 62 | if (tag == "<path>") |
| 63 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 64 | tagValue++; |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 65 | } |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 66 | urlSegmentIndex = std::string_view::npos; |
| 67 | } |
| 68 | } |
| 69 | if (urlSegmentIndex != std::string_view::npos) |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 70 | { |
| 71 | return 0; |
| 72 | } |
Ed Tanous | 1c30e50 | 2022-03-08 18:02:24 -0800 | [diff] [blame] | 73 | return tagValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 74 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 75 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 76 | class Base64Encoder |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 77 | { |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 78 | char overflow1 = '\0'; |
| 79 | char overflow2 = '\0'; |
| 80 | uint8_t overflowCount = 0; |
| 81 | |
| 82 | constexpr static std::array<char, 64> key = { |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 83 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 84 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 85 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 86 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 87 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
| 88 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 89 | // Takes 3 ascii chars, and encodes them as 4 base64 chars |
| 90 | static void encodeTriple(char first, char second, char third, |
| 91 | std::string& output) |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 92 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 93 | size_t keyIndex = 0; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 94 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 95 | keyIndex = static_cast<size_t>(first & 0xFC) >> 2; |
| 96 | output += key[keyIndex]; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 97 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 98 | keyIndex = static_cast<size_t>(first & 0x03) << 4; |
| 99 | keyIndex += static_cast<size_t>(second & 0xF0) >> 4; |
| 100 | output += key[keyIndex]; |
| 101 | |
| 102 | keyIndex = static_cast<size_t>(second & 0x0F) << 2; |
| 103 | keyIndex += static_cast<size_t>(third & 0xC0) >> 6; |
| 104 | output += key[keyIndex]; |
| 105 | |
| 106 | keyIndex = static_cast<size_t>(third & 0x3F); |
| 107 | output += key[keyIndex]; |
| 108 | } |
| 109 | |
| 110 | public: |
| 111 | // Accepts a partial string to encode, and writes the encoded characters to |
| 112 | // the output stream. requires subsequently calling finalize to complete |
| 113 | // stream. |
| 114 | void encode(std::string_view data, std::string& output) |
| 115 | { |
| 116 | // Encode the last round of overflow chars first |
| 117 | if (overflowCount == 2) |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 118 | { |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 119 | if (!data.empty()) |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 120 | { |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 121 | encodeTriple(overflow1, overflow2, data[0], output); |
| 122 | overflowCount = 0; |
| 123 | data.remove_prefix(1); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 124 | } |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 125 | } |
| 126 | else if (overflowCount == 1) |
| 127 | { |
| 128 | if (data.size() >= 2) |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 129 | { |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 130 | encodeTriple(overflow1, data[0], data[1], output); |
| 131 | overflowCount = 0; |
| 132 | data.remove_prefix(2); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 133 | } |
| 134 | } |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 135 | |
| 136 | while (data.size() >= 3) |
| 137 | { |
| 138 | encodeTriple(data[0], data[1], data[2], output); |
| 139 | data.remove_prefix(3); |
| 140 | } |
| 141 | |
| 142 | if (!data.empty() && overflowCount == 0) |
| 143 | { |
| 144 | overflow1 = data[0]; |
| 145 | overflowCount++; |
| 146 | data.remove_prefix(1); |
| 147 | } |
| 148 | |
| 149 | if (!data.empty() && overflowCount == 1) |
| 150 | { |
| 151 | overflow2 = data[0]; |
| 152 | overflowCount++; |
| 153 | data.remove_prefix(1); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Completes a base64 output, by writing any MOD(3) characters to the |
| 158 | // output, as well as any required trailing = |
| 159 | void finalize(std::string& output) |
| 160 | { |
| 161 | if (overflowCount == 0) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | size_t keyIndex = static_cast<size_t>(overflow1 & 0xFC) >> 2; |
| 166 | output += key[keyIndex]; |
| 167 | |
| 168 | keyIndex = static_cast<size_t>(overflow1 & 0x03) << 4; |
| 169 | if (overflowCount == 2) |
| 170 | { |
| 171 | keyIndex += static_cast<size_t>(overflow2 & 0xF0) >> 4; |
| 172 | output += key[keyIndex]; |
| 173 | keyIndex = static_cast<size_t>(overflow2 & 0x0F) << 2; |
| 174 | output += key[keyIndex]; |
| 175 | } |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 176 | else |
| 177 | { |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 178 | output += key[keyIndex]; |
| 179 | output += '='; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 180 | } |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 181 | output += '='; |
| 182 | overflowCount = 0; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 185 | // Returns the required output buffer in characters for an input of size |
| 186 | // inputSize |
| 187 | static size_t constexpr encodedSize(size_t inputSize) |
| 188 | { |
| 189 | // Base64 encodes 3 character blocks as 4 character blocks |
| 190 | // With a possibility of 2 trailing = characters |
| 191 | return (inputSize + 2) / 3 * 4; |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | inline std::string base64encode(std::string_view data) |
| 196 | { |
| 197 | // Encodes a 3 character stream into a 4 character stream |
| 198 | std::string out; |
| 199 | Base64Encoder base64; |
| 200 | out.reserve(Base64Encoder::encodedSize(data.size())); |
| 201 | base64.encode(data, out); |
| 202 | base64.finalize(out); |
| 203 | return out; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 204 | } |
| 205 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 206 | // TODO this is temporary and should be deleted once base64 is refactored out of |
| 207 | // crow |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 208 | inline bool base64Decode(std::string_view input, std::string& output) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 209 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 210 | static const char nop = static_cast<char>(-1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 211 | // See note on encoding_data[] in above function |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 212 | static const std::array<char, 256> decodingData = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 213 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 214 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 215 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 216 | nop, 62, nop, nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, |
| 217 | 60, 61, nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, |
| 218 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 219 | 19, 20, 21, 22, 23, 24, 25, nop, nop, nop, nop, nop, nop, 26, |
| 220 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 221 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, nop, nop, nop, |
| 222 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 223 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 224 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 225 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 226 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 227 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 228 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 229 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 230 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 231 | nop, nop, nop, nop}; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 232 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 233 | size_t inputLength = input.size(); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 234 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 235 | // allocate space for output string |
| 236 | output.clear(); |
| 237 | output.reserve(((inputLength + 2) / 3) * 4); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 238 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 239 | auto getCodeValue = [](char c) { |
| 240 | auto code = static_cast<unsigned char>(c); |
| 241 | // Ensure we cannot index outside the bounds of the decoding array |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 242 | static_assert( |
| 243 | std::numeric_limits<decltype(code)>::max() < decodingData.size()); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 244 | return decodingData[code]; |
| 245 | }; |
| 246 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 247 | // 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] | 248 | // dropping first two bits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 249 | // and regenerate into 3 8-bits sequences |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 250 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | for (size_t i = 0; i < inputLength; i++) |
| 252 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 253 | char base64code0 = 0; |
| 254 | char base64code1 = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 255 | char base64code2 = 0; // initialized to 0 to suppress warnings |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 256 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 257 | base64code0 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 258 | if (base64code0 == nop) |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 259 | { |
| 260 | // non base64 character |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | if (!(++i < inputLength)) |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 264 | { |
| 265 | // we need at least two input bytes for first byte output |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 268 | base64code1 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | if (base64code1 == nop) |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 270 | { |
| 271 | // non base64 character |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | output += |
| 275 | static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 276 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 277 | if (++i < inputLength) |
| 278 | { |
| 279 | char c = input[i]; |
| 280 | if (c == '=') |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 281 | { |
| 282 | // padding , end of input |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 | return (base64code1 & 0x0f) == 0; |
| 284 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 285 | base64code2 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 286 | if (base64code2 == nop) |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 287 | { |
| 288 | // non base64 character |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | return false; |
| 290 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 291 | output += static_cast<char>( |
| 292 | ((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if (++i < inputLength) |
| 296 | { |
| 297 | char c = input[i]; |
| 298 | if (c == '=') |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 299 | { |
| 300 | // padding , end of input |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 301 | return (base64code2 & 0x03) == 0; |
| 302 | } |
Ed Tanous | f8fe53e | 2022-06-30 15:55:45 -0700 | [diff] [blame] | 303 | char base64code3 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 304 | if (base64code3 == nop) |
Myung Bae | 41868c6 | 2024-10-09 16:36:49 -0700 | [diff] [blame] | 305 | { |
| 306 | // non base64 character |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 307 | return false; |
| 308 | } |
| 309 | output += |
| 310 | static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3)); |
| 311 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 314 | return true; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 317 | namespace details |
| 318 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 319 | inline boost::urls::url appendUrlPieces( |
| 320 | boost::urls::url& url, const std::initializer_list<std::string_view> args) |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 321 | { |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 322 | for (std::string_view arg : args) |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 323 | { |
| 324 | url.segments().push_back(arg); |
| 325 | } |
| 326 | return url; |
| 327 | } |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 328 | |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 329 | } // namespace details |
| 330 | |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 331 | class OrMorePaths |
| 332 | {}; |
| 333 | |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 334 | template <typename... AV> |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 335 | inline void appendUrlPieces(boost::urls::url& url, const AV... args) |
| 336 | { |
| 337 | details::appendUrlPieces(url, {args...}); |
| 338 | } |
| 339 | |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 340 | namespace details |
| 341 | { |
| 342 | |
| 343 | // std::reference_wrapper<std::string> - extracts segment to variable |
| 344 | // std::string_view - checks if segment is equal to variable |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 345 | using UrlSegment = std::variant<std::reference_wrapper<std::string>, |
| 346 | std::string_view, OrMorePaths>; |
| 347 | |
| 348 | enum class UrlParseResult |
| 349 | { |
| 350 | Continue, |
| 351 | Fail, |
| 352 | Done, |
| 353 | }; |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 354 | |
| 355 | class UrlSegmentMatcherVisitor |
| 356 | { |
| 357 | public: |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 358 | UrlParseResult operator()(std::string& output) |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 359 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 360 | output = segment; |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 361 | return UrlParseResult::Continue; |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 364 | UrlParseResult operator()(std::string_view expected) |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 365 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 366 | if (segment == expected) |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 367 | { |
| 368 | return UrlParseResult::Continue; |
| 369 | } |
| 370 | return UrlParseResult::Fail; |
| 371 | } |
| 372 | |
| 373 | UrlParseResult operator()(OrMorePaths /*unused*/) |
| 374 | { |
| 375 | return UrlParseResult::Done; |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 378 | explicit UrlSegmentMatcherVisitor(std::string_view segmentIn) : |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 379 | segment(segmentIn) |
| 380 | {} |
| 381 | |
| 382 | private: |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 383 | std::string_view segment; |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 384 | }; |
| 385 | |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 386 | inline bool readUrlSegments(const boost::urls::url_view_base& url, |
Ed Tanous | 5be2b14 | 2024-03-27 15:27:04 -0700 | [diff] [blame] | 387 | std::initializer_list<UrlSegment> segments) |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 388 | { |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 389 | const boost::urls::segments_view& urlSegments = url.segments(); |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 390 | |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 391 | if (!urlSegments.is_absolute()) |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 392 | { |
| 393 | return false; |
| 394 | } |
| 395 | |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 396 | boost::urls::segments_view::const_iterator it = urlSegments.begin(); |
| 397 | boost::urls::segments_view::const_iterator end = urlSegments.end(); |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 398 | |
| 399 | for (const auto& segment : segments) |
| 400 | { |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 401 | if (it == end) |
| 402 | { |
| 403 | // If the request ends with an "any" path, this was successful |
| 404 | return std::holds_alternative<OrMorePaths>(segment); |
| 405 | } |
| 406 | UrlParseResult res = std::visit(UrlSegmentMatcherVisitor(*it), segment); |
| 407 | if (res == UrlParseResult::Done) |
| 408 | { |
| 409 | return true; |
| 410 | } |
| 411 | if (res == UrlParseResult::Fail) |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 412 | { |
| 413 | return false; |
| 414 | } |
| 415 | it++; |
| 416 | } |
Carson Labrado | 4c30e22 | 2022-06-24 22:16:00 +0000 | [diff] [blame] | 417 | |
| 418 | // There will be an empty segment at the end if the URI ends with a "/" |
| 419 | // e.g. /redfish/v1/Chassis/ |
| 420 | if ((it != end) && urlSegments.back().empty()) |
| 421 | { |
| 422 | it++; |
| 423 | } |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 424 | return it == end; |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | } // namespace details |
| 428 | |
| 429 | template <typename... Args> |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 430 | inline bool readUrlSegments(const boost::urls::url_view_base& url, |
| 431 | Args&&... args) |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 432 | { |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 433 | return details::readUrlSegments(url, {std::forward<Args>(args)...}); |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 436 | inline boost::urls::url |
| 437 | replaceUrlSegment(const boost::urls::url_view_base& urlView, |
| 438 | const uint replaceLoc, std::string_view newSegment) |
Carson Labrado | 1c0bb5c | 2022-05-18 00:12:52 +0000 | [diff] [blame] | 439 | { |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 440 | const boost::urls::segments_view& urlSegments = urlView.segments(); |
Carson Labrado | 1c0bb5c | 2022-05-18 00:12:52 +0000 | [diff] [blame] | 441 | boost::urls::url url("/"); |
| 442 | |
| 443 | if (!urlSegments.is_absolute()) |
| 444 | { |
| 445 | return url; |
| 446 | } |
| 447 | |
| 448 | boost::urls::segments_view::iterator it = urlSegments.begin(); |
| 449 | boost::urls::segments_view::iterator end = urlSegments.end(); |
| 450 | |
| 451 | for (uint idx = 0; it != end; it++, idx++) |
| 452 | { |
| 453 | if (idx == replaceLoc) |
| 454 | { |
| 455 | url.segments().push_back(newSegment); |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | url.segments().push_back(*it); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return url; |
| 464 | } |
| 465 | |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 466 | inline void setProtocolDefaults(boost::urls::url& url, |
| 467 | std::string_view protocol) |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 468 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 469 | if (url.has_scheme()) |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 470 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 471 | return; |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 472 | } |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 473 | if (protocol == "Redfish" || protocol.empty()) |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 474 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 475 | if (url.port_number() == 443) |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 476 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 477 | url.set_scheme("https"); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 478 | } |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 479 | if (url.port_number() == 80) |
| 480 | { |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 481 | if constexpr (BMCWEB_INSECURE_PUSH_STYLE_NOTIFICATION) |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 482 | { |
| 483 | url.set_scheme("http"); |
| 484 | } |
| 485 | } |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 486 | } |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 487 | else if (protocol == "SNMPv2c") |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 488 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 489 | url.set_scheme("snmp"); |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 490 | } |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 493 | inline void setPortDefaults(boost::urls::url& url) |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 494 | { |
| 495 | uint16_t port = url.port_number(); |
| 496 | if (port != 0) |
| 497 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 498 | return; |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // If the user hasn't explicitly stated a port, pick one explicitly for them |
| 502 | // based on the protocol defaults |
| 503 | if (url.scheme() == "http") |
| 504 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 505 | url.set_port_number(80); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 506 | } |
| 507 | if (url.scheme() == "https") |
| 508 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 509 | url.set_port_number(443); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 510 | } |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 511 | if (url.scheme() == "snmp") |
| 512 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 513 | url.set_port_number(162); |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 514 | } |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 517 | } // namespace utility |
| 518 | } // namespace crow |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 519 | |
| 520 | namespace nlohmann |
| 521 | { |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 522 | template <std::derived_from<boost::urls::url_view_base> URL> |
| 523 | struct adl_serializer<URL> |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 524 | { |
| 525 | // NOLINTNEXTLINE(readability-identifier-naming) |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 526 | static void to_json(json& j, const URL& url) |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 527 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 528 | j = url.buffer(); |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 529 | } |
| 530 | }; |
| 531 | } // namespace nlohmann |