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