17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_UTILITIES__URL_DECODE 18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_UTILITIES__URL_DECODE 38 static std::unordered_map<std::string, std::string>
splitAndDecode(
const std::string_view & data,
39 bool validateUtf8 =
true,
40 bool throwOnError =
false) {
41 std::unordered_map<std::string, std::string> decodedParameters;
43 const auto parameters =
split(data);
45 for (
auto iter = parameters.begin(); iter != parameters.end(); ++iter) {
46 auto decodedKey =
decode(iter->first, validateUtf8, throwOnError);
47 auto decodedValue =
decode(iter->second, validateUtf8, throwOnError);
49 decodedParameters.insert(std::make_pair(std::move(decodedKey), std::move(decodedValue)));
52 return decodedParameters;
64 static std::unordered_map<std::string_view, std::string_view>
split(
const std::string_view data) {
65 std::unordered_map<std::string_view, std::string_view> keysAndValues;
68 for (
auto parameter : parameters) {
71 if (keyAndValue.size() == 1) {
73 keysAndValues.emplace(keyAndValue[0],
"");
74 }
else if (keyAndValue.size() != 2) {
77 keysAndValues.emplace(keyAndValue[0], keyAndValue[1]);
106 static std::string
decode(
const std::string_view input,
bool validateUtf8 =
true,
bool throwOnError =
false) {
110 while (index < input.length()) {
111 switch (input[index]) {
119 std::string thisOutput;
121 while (input[index] ==
'%') {
124 if (index == input.length()) {
133 const size_t x = index;
136 if (index == input.length()) {
146 std::string_view v = input.substr(x, 2);
148 fromString(b, v, 16);
149 thisOutput += (char) b;
158 while (offset != (
int) thisOutput.length()) {
174 output += thisOutput;
181 output += input[index];
200 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_UTILITIES__URL_DECODE static std::unordered_map< std::string_view, std::string_view > split(const std::string_view data)
Split the URL encoded parameters into a map of string views.
Definition: UrlDecode.hpp:64
static std::unordered_map< std::string, std::string > splitAndDecode(const std::string_view &data, bool validateUtf8=true, bool throwOnError=false)
Split the URL encoded parameters then decode each one.
Definition: UrlDecode.hpp:38
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
static std::string decode(const std::string_view input, bool validateUtf8=true, bool throwOnError=false)
Decode the percent encoded input string into a UTF-8 string, according to the following rules...
Definition: UrlDecode.hpp:106
static std::vector< std::string_view > split(const std::string_view &input, const std::regex &delimiter, bool returnDelimiters=false, bool compress=true)
Split the input string on each of the occurrences of the specified delimiter regular expression...
Definition: Strings.hpp:1240
Base class of network exceptions.
Definition: NetworkExceptions.hpp:28
Components and utilities working on network data transmission.
static char32_t getNextUtf8Safe(const std::string_view &text, int &offset)
Get the next code point from the UTF-8 string view (validating version).
Definition: Character.hpp:198
Balau exceptions for network operations.
Utility for splitting and decoding URL encoded data.
Definition: UrlDecode.hpp:30