blob: e19e25858ff3790d7dc2a512bc5a3477e63d445b [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7045c8d2017-04-03 10:04:37 -07003#pragma once
Ed Tanous1abe55e2018-09-05 08:30:59 -07004
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "bmcweb_config.h"
6
Ed Tanouse5dd4992025-02-17 12:33:40 -08007#include "str_utility.hpp"
8
Ed Tanousd7857202025-01-28 15:32:26 -08009#include <sys/types.h>
10
11#include <boost/url/segments_view.hpp>
Ed Tanouseae855c2021-10-26 11:26:02 -070012#include <boost/url/url.hpp>
Ed Tanous4a7fbef2024-04-06 16:03:49 -070013#include <boost/url/url_view_base.hpp>
Myung Bae8873f322025-03-17 18:24:13 -050014#include <nlohmann/adl_serializer.hpp>
Ed Tanous71f2db72022-05-25 12:28:09 -070015#include <nlohmann/json.hpp>
Nan Zhou1d8782e2021-11-29 22:23:18 -080016
Ed Tanous9ea15c32022-01-04 14:18:22 -080017#include <array>
Ed Tanous80d2ef32025-02-04 09:29:02 -080018#include <bit>
Ed Tanousd7857202025-01-28 15:32:26 -080019#include <concepts>
Ed Tanousc715ec22022-03-10 15:38:01 -080020#include <cstddef>
Ed Tanous7045c8d2017-04-03 10:04:37 -070021#include <cstdint>
Ed Tanous9ea15c32022-01-04 14:18:22 -080022#include <ctime>
Ed Tanous7045c8d2017-04-03 10:04:37 -070023#include <functional>
Ed Tanousd7857202025-01-28 15:32:26 -080024#include <initializer_list>
Ed Tanous9ea15c32022-01-04 14:18:22 -080025#include <limits>
Ed Tanous7045c8d2017-04-03 10:04:37 -070026#include <string>
Ed Tanous9ea15c32022-01-04 14:18:22 -080027#include <string_view>
Ed Tanous9ea15c32022-01-04 14:18:22 -080028#include <type_traits>
29#include <utility>
Szymon Dompkeca1600c2022-03-03 14:42:52 +010030#include <variant>
Ed Tanous7045c8d2017-04-03 10:04:37 -070031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032namespace crow
33{
Ed Tanous47488a92023-06-26 18:19:33 -070034namespace utility
Ed Tanous1abe55e2018-09-05 08:30:59 -070035{
Ed Tanous7045c8d2017-04-03 10:04:37 -070036
Ed Tanous9de65b32024-03-27 13:34:40 -070037constexpr uint64_t getParameterTag(std::string_view url)
Ed Tanous1abe55e2018-09-05 08:30:59 -070038{
Ed Tanous1c30e502022-03-08 18:02:24 -080039 uint64_t tagValue = 0;
40 size_t urlSegmentIndex = std::string_view::npos;
Ed Tanousb00dcc22021-02-23 12:52:50 -080041
Ed Tanous1c30e502022-03-08 18:02:24 -080042 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 Williamsbd79bce2024-08-16 15:22:20 -040059 std::string_view tag =
60 url.substr(urlSegmentIndex, urlIndex + 1 - urlSegmentIndex);
Ed Tanous1c30e502022-03-08 18:02:24 -080061
Ed Tanous1c30e502022-03-08 18:02:24 -080062 if (tag == "<str>" || tag == "<string>")
63 {
Ed Tanousd9e89df2024-03-27 14:08:59 -070064 tagValue++;
Ed Tanous1c30e502022-03-08 18:02:24 -080065 }
66 if (tag == "<path>")
67 {
Ed Tanousd9e89df2024-03-27 14:08:59 -070068 tagValue++;
Ed Tanous1c30e502022-03-08 18:02:24 -080069 }
Ed Tanous1c30e502022-03-08 18:02:24 -080070 urlSegmentIndex = std::string_view::npos;
71 }
72 }
73 if (urlSegmentIndex != std::string_view::npos)
Ed Tanous988403c2020-08-24 11:29:49 -070074 {
75 return 0;
76 }
Ed Tanous1c30e502022-03-08 18:02:24 -080077 return tagValue;
Ed Tanous7045c8d2017-04-03 10:04:37 -070078}
Ed Tanous7045c8d2017-04-03 10:04:37 -070079
Ed Tanous80d2ef32025-02-04 09:29:02 -080080constexpr 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
87static constexpr char nop = static_cast<char>(-1);
88constexpr 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 Tanousee192c02023-12-13 10:49:58 -0800112class Base64Encoder
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600113{
Ed Tanousee192c02023-12-13 10:49:58 -0800114 char overflow1 = '\0';
115 char overflow2 = '\0';
116 uint8_t overflowCount = 0;
117
Ed Tanousee192c02023-12-13 10:49:58 -0800118 // 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 Kobylakd830ff52021-01-27 14:15:27 -0600121 {
Ed Tanous543f4402022-01-06 13:12:53 -0800122 size_t keyIndex = 0;
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600123
Ed Tanousee192c02023-12-13 10:49:58 -0800124 keyIndex = static_cast<size_t>(first & 0xFC) >> 2;
Ed Tanous80d2ef32025-02-04 09:29:02 -0800125 output += base64key[keyIndex];
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600126
Ed Tanousee192c02023-12-13 10:49:58 -0800127 keyIndex = static_cast<size_t>(first & 0x03) << 4;
128 keyIndex += static_cast<size_t>(second & 0xF0) >> 4;
Ed Tanous80d2ef32025-02-04 09:29:02 -0800129 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800130
131 keyIndex = static_cast<size_t>(second & 0x0F) << 2;
132 keyIndex += static_cast<size_t>(third & 0xC0) >> 6;
Ed Tanous80d2ef32025-02-04 09:29:02 -0800133 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800134
135 keyIndex = static_cast<size_t>(third & 0x3F);
Ed Tanous80d2ef32025-02-04 09:29:02 -0800136 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800137 }
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 Kobylakd830ff52021-01-27 14:15:27 -0600147 {
Ed Tanousee192c02023-12-13 10:49:58 -0800148 if (!data.empty())
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600149 {
Ed Tanousee192c02023-12-13 10:49:58 -0800150 encodeTriple(overflow1, overflow2, data[0], output);
151 overflowCount = 0;
152 data.remove_prefix(1);
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600153 }
Ed Tanousee192c02023-12-13 10:49:58 -0800154 }
155 else if (overflowCount == 1)
156 {
157 if (data.size() >= 2)
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600158 {
Ed Tanousee192c02023-12-13 10:49:58 -0800159 encodeTriple(overflow1, data[0], data[1], output);
160 overflowCount = 0;
161 data.remove_prefix(2);
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600162 }
163 }
Ed Tanousee192c02023-12-13 10:49:58 -0800164
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 Tanous80d2ef32025-02-04 09:29:02 -0800195 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800196
197 keyIndex = static_cast<size_t>(overflow1 & 0x03) << 4;
198 if (overflowCount == 2)
199 {
200 keyIndex += static_cast<size_t>(overflow2 & 0xF0) >> 4;
Ed Tanous80d2ef32025-02-04 09:29:02 -0800201 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800202 keyIndex = static_cast<size_t>(overflow2 & 0x0F) << 2;
Ed Tanous80d2ef32025-02-04 09:29:02 -0800203 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800204 }
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600205 else
206 {
Ed Tanous80d2ef32025-02-04 09:29:02 -0800207 output += base64key[keyIndex];
Ed Tanousee192c02023-12-13 10:49:58 -0800208 output += '=';
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600209 }
Ed Tanousee192c02023-12-13 10:49:58 -0800210 output += '=';
211 overflowCount = 0;
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600212 }
213
Ed Tanousee192c02023-12-13 10:49:58 -0800214 // 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
224inline 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 Kobylakd830ff52021-01-27 14:15:27 -0600233}
234
Ed Tanous80d2ef32025-02-04 09:29:02 -0800235template <bool urlsafe = false>
Ed Tanous26ccae32023-02-16 10:28:44 -0800236inline bool base64Decode(std::string_view input, std::string& output)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 size_t inputLength = input.size();
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +0100239
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 // allocate space for output string
241 output.clear();
242 output.reserve(((inputLength + 2) / 3) * 4);
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +0100243
Ed Tanous80d2ef32025-02-04 09:29:02 -0800244 static constexpr auto decodingData = getDecodeTable(urlsafe);
245
Jonathan Doman5beaf842020-08-14 11:23:33 -0700246 auto getCodeValue = [](char c) {
247 auto code = static_cast<unsigned char>(c);
248 // Ensure we cannot index outside the bounds of the decoding array
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400249 static_assert(
250 std::numeric_limits<decltype(code)>::max() < decodingData.size());
Jonathan Doman5beaf842020-08-14 11:23:33 -0700251 return decodingData[code];
252 };
253
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 // for each 4-bytes sequence from the input, extract 4 6-bits sequences by
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500255 // dropping first two bits
Ed Tanous1abe55e2018-09-05 08:30:59 -0700256 // and regenerate into 3 8-bits sequences
James Feist5a806642020-07-31 16:40:33 +0000257
Ed Tanous1abe55e2018-09-05 08:30:59 -0700258 for (size_t i = 0; i < inputLength; i++)
259 {
Ed Tanous543f4402022-01-06 13:12:53 -0800260 char base64code0 = 0;
261 char base64code1 = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 char base64code2 = 0; // initialized to 0 to suppress warnings
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +0100263
Jonathan Doman5beaf842020-08-14 11:23:33 -0700264 base64code0 = getCodeValue(input[i]);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700265 if (base64code0 == nop)
Myung Bae41868c62024-10-09 16:36:49 -0700266 {
267 // non base64 character
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268 return false;
269 }
270 if (!(++i < inputLength))
Myung Bae41868c62024-10-09 16:36:49 -0700271 {
272 // we need at least two input bytes for first byte output
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 return false;
274 }
Jonathan Doman5beaf842020-08-14 11:23:33 -0700275 base64code1 = getCodeValue(input[i]);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700276 if (base64code1 == nop)
Myung Bae41868c62024-10-09 16:36:49 -0700277 {
278 // non base64 character
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 return false;
280 }
281 output +=
282 static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3));
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +0100283
Ed Tanous1abe55e2018-09-05 08:30:59 -0700284 if (++i < inputLength)
285 {
286 char c = input[i];
287 if (c == '=')
Myung Bae41868c62024-10-09 16:36:49 -0700288 {
289 // padding , end of input
Ed Tanous1abe55e2018-09-05 08:30:59 -0700290 return (base64code1 & 0x0f) == 0;
291 }
Jonathan Doman5beaf842020-08-14 11:23:33 -0700292 base64code2 = getCodeValue(input[i]);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700293 if (base64code2 == nop)
Myung Bae41868c62024-10-09 16:36:49 -0700294 {
295 // non base64 character
Ed Tanous1abe55e2018-09-05 08:30:59 -0700296 return false;
297 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400298 output += static_cast<char>(
299 ((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700300 }
301
302 if (++i < inputLength)
303 {
304 char c = input[i];
305 if (c == '=')
Myung Bae41868c62024-10-09 16:36:49 -0700306 {
307 // padding , end of input
Ed Tanous1abe55e2018-09-05 08:30:59 -0700308 return (base64code2 & 0x03) == 0;
309 }
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700310 char base64code3 = getCodeValue(input[i]);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700311 if (base64code3 == nop)
Myung Bae41868c62024-10-09 16:36:49 -0700312 {
313 // non base64 character
Ed Tanous1abe55e2018-09-05 08:30:59 -0700314 return false;
315 }
316 output +=
317 static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3));
318 }
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +0100319 }
320
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 return true;
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +0100322}
323
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700324class OrMorePaths
325{};
326
Ed Tanouseae855c2021-10-26 11:26:02 -0700327template <typename... AV>
Ed Tanousdaadfb22024-12-20 09:25:54 -0800328inline void appendUrlPieces(boost::urls::url& url, AV&&... args)
Willy Tuc6bcedc2022-09-27 05:36:59 +0000329{
Ed Tanousdaadfb22024-12-20 09:25:54 -0800330 // Unclear the correct fix here.
331 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
332 for (const std::string_view arg : {args...})
333 {
334 url.segments().push_back(arg);
335 }
Willy Tuc6bcedc2022-09-27 05:36:59 +0000336}
337
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100338namespace details
339{
340
341// std::reference_wrapper<std::string> - extracts segment to variable
342// std::string_view - checks if segment is equal to variable
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700343using UrlSegment = std::variant<std::reference_wrapper<std::string>,
344 std::string_view, OrMorePaths>;
345
346enum class UrlParseResult
347{
348 Continue,
349 Fail,
350 Done,
351};
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100352
353class UrlSegmentMatcherVisitor
354{
355 public:
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700356 UrlParseResult operator()(std::string& output)
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100357 {
Ed Tanous079360a2022-06-29 10:05:19 -0700358 output = segment;
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700359 return UrlParseResult::Continue;
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100360 }
361
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700362 UrlParseResult operator()(std::string_view expected)
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100363 {
Ed Tanous079360a2022-06-29 10:05:19 -0700364 if (segment == expected)
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700365 {
366 return UrlParseResult::Continue;
367 }
368 return UrlParseResult::Fail;
369 }
370
371 UrlParseResult operator()(OrMorePaths /*unused*/)
372 {
373 return UrlParseResult::Done;
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100374 }
375
Ed Tanous079360a2022-06-29 10:05:19 -0700376 explicit UrlSegmentMatcherVisitor(std::string_view segmentIn) :
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100377 segment(segmentIn)
378 {}
379
380 private:
Ed Tanous079360a2022-06-29 10:05:19 -0700381 std::string_view segment;
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100382};
383
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700384inline bool readUrlSegments(const boost::urls::url_view_base& url,
Ed Tanous5be2b142024-03-27 15:27:04 -0700385 std::initializer_list<UrlSegment> segments)
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100386{
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700387 const boost::urls::segments_view& urlSegments = url.segments();
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100388
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700389 if (!urlSegments.is_absolute())
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100390 {
391 return false;
392 }
393
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700394 boost::urls::segments_view::const_iterator it = urlSegments.begin();
395 boost::urls::segments_view::const_iterator end = urlSegments.end();
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100396
Ed Tanouse5dd4992025-02-17 12:33:40 -0800397 std::string fragment = url.fragment();
398 std::vector<std::string> fragmentParts;
399 bmcweb::split(fragmentParts, fragment, '/');
400 auto fragIt = fragmentParts.begin();
401 auto fragEnd = fragmentParts.end();
402
403 // Url fragments start with a /, so we need to skip the first empty string
404 if (fragIt != fragEnd)
405 {
406 if (fragIt->empty())
407 {
408 fragIt++;
409 }
410 }
411
412 // There will be an empty segment at the end if the URI ends with a "/"
413 // e.g. /redfish/v1/Chassis/
414 if ((it != end) && urlSegments.back().empty())
415 {
416 end--;
417 }
418
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100419 for (const auto& segment : segments)
420 {
Ed Tanouse5dd4992025-02-17 12:33:40 -0800421 UrlParseResult res = UrlParseResult::Fail;
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700422 if (it == end)
423 {
Ed Tanouse5dd4992025-02-17 12:33:40 -0800424 if (fragIt == fragEnd)
425 {
426 return std::holds_alternative<OrMorePaths>(segment);
427 }
428 res = std::visit(UrlSegmentMatcherVisitor(*fragIt), segment);
429 fragIt++;
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700430 }
Ed Tanouse5dd4992025-02-17 12:33:40 -0800431 else
432 {
433 res = std::visit(UrlSegmentMatcherVisitor(*it), segment);
434 it++;
435 }
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700436 if (res == UrlParseResult::Done)
437 {
438 return true;
439 }
440 if (res == UrlParseResult::Fail)
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100441 {
442 return false;
443 }
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100444 }
Carson Labrado4c30e222022-06-24 22:16:00 +0000445
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700446 return it == end;
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100447}
448
449} // namespace details
450
451template <typename... Args>
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700452inline bool readUrlSegments(const boost::urls::url_view_base& url,
453 Args&&... args)
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100454{
Ed Tanous39662a32023-02-06 15:09:46 -0800455 return details::readUrlSegments(url, {std::forward<Args>(args)...});
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100456}
457
Patrick Williams504af5a2025-02-03 14:29:03 -0500458inline boost::urls::url replaceUrlSegment(
459 const boost::urls::url_view_base& urlView, const uint replaceLoc,
460 std::string_view newSegment)
Carson Labrado1c0bb5c2022-05-18 00:12:52 +0000461{
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700462 const boost::urls::segments_view& urlSegments = urlView.segments();
Carson Labrado1c0bb5c2022-05-18 00:12:52 +0000463 boost::urls::url url("/");
464
465 if (!urlSegments.is_absolute())
466 {
467 return url;
468 }
469
470 boost::urls::segments_view::iterator it = urlSegments.begin();
471 boost::urls::segments_view::iterator end = urlSegments.end();
472
473 for (uint idx = 0; it != end; it++, idx++)
474 {
475 if (idx == replaceLoc)
476 {
477 url.segments().push_back(newSegment);
478 }
479 else
480 {
481 url.segments().push_back(*it);
482 }
483 }
484
485 return url;
486}
487
Ed Tanousa716aa72023-08-01 11:35:53 -0700488inline void setProtocolDefaults(boost::urls::url& url,
489 std::string_view protocol)
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800490{
Ed Tanousa716aa72023-08-01 11:35:53 -0700491 if (url.has_scheme())
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800492 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700493 return;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800494 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700495 if (protocol == "Redfish" || protocol.empty())
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800496 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700497 if (url.port_number() == 443)
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800498 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700499 url.set_scheme("https");
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800500 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700501 if (url.port_number() == 80)
502 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700503 if constexpr (BMCWEB_INSECURE_PUSH_STYLE_NOTIFICATION)
Ed Tanousa716aa72023-08-01 11:35:53 -0700504 {
505 url.set_scheme("http");
506 }
507 }
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800508 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700509 else if (protocol == "SNMPv2c")
Chicago Duan3d307082020-11-26 14:12:12 +0800510 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700511 url.set_scheme("snmp");
Chicago Duan3d307082020-11-26 14:12:12 +0800512 }
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800513}
514
Ed Tanousa716aa72023-08-01 11:35:53 -0700515inline void setPortDefaults(boost::urls::url& url)
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800516{
517 uint16_t port = url.port_number();
518 if (port != 0)
519 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700520 return;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800521 }
522
523 // If the user hasn't explicitly stated a port, pick one explicitly for them
524 // based on the protocol defaults
525 if (url.scheme() == "http")
526 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700527 url.set_port_number(80);
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800528 }
529 if (url.scheme() == "https")
530 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700531 url.set_port_number(443);
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800532 }
Chicago Duan3d307082020-11-26 14:12:12 +0800533 if (url.scheme() == "snmp")
534 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700535 url.set_port_number(162);
Chicago Duan3d307082020-11-26 14:12:12 +0800536 }
Ed Tanous11baefe2022-02-09 12:14:12 -0800537}
538
Ed Tanous1abe55e2018-09-05 08:30:59 -0700539} // namespace utility
540} // namespace crow
Ed Tanous71f2db72022-05-25 12:28:09 -0700541
542namespace nlohmann
543{
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700544template <std::derived_from<boost::urls::url_view_base> URL>
545struct adl_serializer<URL>
Ed Tanous71f2db72022-05-25 12:28:09 -0700546{
547 // NOLINTNEXTLINE(readability-identifier-naming)
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700548 static void to_json(json& j, const URL& url)
Ed Tanous71f2db72022-05-25 12:28:09 -0700549 {
Ed Tanous079360a2022-06-29 10:05:19 -0700550 j = url.buffer();
Ed Tanous71f2db72022-05-25 12:28:09 -0700551 }
552};
553} // namespace nlohmann