some thinking around passing the form

This commit is contained in:
Blue 2023-12-14 19:17:28 -03:00
parent 3fe6d25448
commit 0c50cfa639
Signed by: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 149 additions and 0 deletions

View file

@ -1,9 +1,11 @@
set(HEADER
helpers.h
formdecode.h
)
set(SOURCES
helpers.cpp
formdecode.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

46
utils/formdecode.cpp Normal file
View file

@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "formdecode.h"
#include <sstream>
std::map<std::string, std::string> urlDecodeAndParse(const std::string_view& encoded) {
std::map<std::string, std::string> result;
std::istringstream iss(std::string(encoded.begin(), encoded.end()));
std::string pair;
while (std::getline(iss, pair, '&')) {
size_t equalsPos = pair.find('=');
if (equalsPos != std::string::npos)
result.emplace(
urlDecode(pair.substr(0, equalsPos)),
urlDecode(pair.substr(equalsPos + 1))
);
}
return result;
}
std::string urlDecode(const std::string_view& encoded) {
std::ostringstream decoded;
for (size_t i = 0; i < encoded.length(); ++i) {
if (encoded[i] == '%') {
if (i + 2 < encoded.length()) {
std::string hexStr(encoded.substr(i + 1, 2));
char hexValue = static_cast<char>(std::stoul(hexStr, nullptr, 16));
decoded.put(hexValue);
i += 2;
} else {
decoded.put(encoded[i]);
}
} else if (encoded[i] == '+') {
decoded.put(' ');
} else {
decoded.put(encoded[i]);
}
}
return decoded.str();
}

11
utils/formdecode.h Normal file
View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <map>
#include <string>
#include <string_view>
std::map<std::string, std::string> urlDecodeAndParse(const std::string_view& encoded);
std::string urlDecode(const std::string_view& encoded);