String Operations to deal with POST Body

This commit is contained in:
Tina_Azure
2023-04-07 22:30:24 +02:00
parent 80f377c048
commit 88167c4f95

View File

@ -1,6 +1,8 @@
#include <string>
#include <list>
#include <map>
#include <iostream>
#include <regex>
#include <vector>
#include <pqxx/pqxx>
#include "crow.h"
@ -15,6 +17,8 @@ using namespace jed_utils::cpp;
* Utility Manager
*/
namespace Utilities {
const static std::map<std::string, std::string> HTML_URL_CODES = {{"%20", " "}, {"%21", "!"}, {"%22", "\""}, {"%23", "#"}, {"%24", "$"}, {"%25", "%"}, {"%26", "&"}, {"%27", "'"}, {"%28", "("}, {"%29", ")"}, {"%2A", "*"}, {"%2B", "+"}, {"%2C", ","}, {"%2D", "-"}, {"%2E", "."}, {"%2F", "/"}, {"%30", "0"}, {"%31", "1"}, {"%32", "2"}, {"%33", "3"}, {"%34", "4"}, {"%35", "5"}, {"%36", "6"}, {"%37", "7"}, {"%38", "8"}, {"%39", "9"}, {"%3A", ":"}, {"%3B", ";"}, {"%3C", "<"}, {"%3D", "="}, {"%3E", ">"}, {"%3F", "?"}, {"%40", "@"}, {"%5B", "["}, {"%5C", "\\"}, {"%5D", "]"}, {"%5E", "^"}, {"%5F", "_"}, {"%60", "`"}, {"%7B", "{"}, {"%7C", "|"}, {"%7D", "}"}, {"%7E", "~"}, {"%7F", " "}, {"%80", ""}, {"%82", ""}, {"%83", "ƒ"}, {"%84", ""}, {"%85", ""}, {"%86", ""}, {"%87", ""}, {"%88", "ˆ"}, {"%89", ""}, {"%8A", "Š"}, {"%8B", ""}, {"%8C", "Œ"}, {"%8E", "Ž"}, {"%91", ""}, {"%92", ""}, {"%93", ""}, {"%94", ""}, {"%95", ""}, {"%96", ""}, {"%97", ""}, {"%98", "˜"}, {"%99", ""}, {"%9A", "š"}, {"%9B", ""}, {"%9C", "œ"}, {"%9E", "ž"}, {"%9F", "Ÿ"}, {"%A1", "¡"}, {"%A2", "¢"}, {"%A3", "£"}, {"%A4", "¤"}, {"%A5", "¥"}, {"%A6", "¦"}, {"%A7", "§"}, {"%A8", "¨"}, {"%A9", "©"}, {"%AA", "ª"}, {"%AB", "«"}, {"%AC", "¬"}, {"%AE", "®"}, {"%AF", "¯"}, {"%B0", "°"}, {"%B1", "±"}, {"%B2", "²"}, {"%B3", "³"}, {"%B4", "´"}, {"%B5", "µ"}, {"%B6", ""}, {"%B7", "·"}, {"%B8", "¸"}, {"%B9", "¹"}, {"%BA", "º"}, {"%BB", "»"}, {"%BC", "¼"}, {"%BD", "½"}, {"%BE", "¾"}, {"%BF", "¿"}, {"%C0", "À"}, {"%C1", "Á"}, {"%C2", "Â"}, {"%C3", "Ã"}, {"%C4", "Ä"}, {"%C5", "Å"}, {"%C6", "Æ"}, {"%C7", "Ç"}, {"%C8", "È"}, {"%C9", "É"}, {"%CA", "Ê"}, {"%CB", "Ë"}, {"%CC", "Ì"}, {"%CD", "Í"}, {"%CE", "Î"}, {"%CF", "Ï"}, {"%D0", "Ð"}, {"%D1", "Ñ"}, {"%D2", "Ò"}, {"%D3", "Ó"}, {"%D4", "Ô"}, {"%D5", "Õ"}, {"%D6", "Ö"}, {"%D7", "×"}, {"%D8", "Ø"}, {"%D9", "Ù"}, {"%DA", "Ú"}, {"%DB", "Û"}, {"%DC", "Ü"}, {"%DD", "Ý"}, {"%DE", "Þ"}, {"%DF", "ß"}, {"%E0", "à"}, {"%E1", "á"}, {"%E2", "â"}, {"%E3", "ã"}, {"%E4", "ä"}, {"%E5", "å"}, {"%E6", "æ"}, {"%E7", "ç"}, {"%E8", "è"}, {"%E9", "é"}, {"%EA", "ê"}, {"%EB", "ë"}, {"%EC", "ì"}, {"%ED", "í"}, {"%EE", "î"}, {"%EF", "ï"}, {"%F0", "ð"}, {"%F1", "ñ"}, {"%F2", "ò"}, {"%F3", "ó"}, {"%F4", "ô"}, {"%F5", "õ"}, {"%F6", "ö"}, {"%F7", "÷"}, {"%F8", "ø"}, {"%F9", "ù"}, {"%FA", "ú"}, {"%FB", "û"}, {"%FC", "ü"}, {"%FD", "ý"}, {"%FE", "þ"}, {"%FF", "ÿ"}};
/*
* Struct representing the configuration file
*/
@ -33,13 +37,49 @@ namespace Utilities {
}
};
/*
* Takes String and cuts from the start-up to the length of valueName
*/
std::string extractSingleValueFromRequestBody(std::string responseBody, std::string valueName){
std::string extractSingleValueFromRequestBody(std::string responseBody, std::string valueName) {
return responseBody.substr(valueName.length() + 1, responseBody.length());
}
/*
* Takes string to split it into a vector based on a given delimiter
*/
std::vector<std::string> splitStringIntoVector(std::string stringToSplit, char delimiter) {
std::vector<std::string> splitVector;
std::istringstream stringStream(stringToSplit);
while (!stringStream.eof()) {
std::string splitResult;
std::getline( stringStream, splitResult, delimiter);
splitVector.push_back(splitResult);
}
return splitVector;
}
/*
* replaces a string with another string within a string
*/
void replaceString(std::string &stringToProcess, std::string from, std::string to) {
int stringPosition = stringToProcess.find(from);
while (stringPosition != std::string::npos) {
stringToProcess.replace(stringToProcess.find(from), std::string(from).size(), to);
stringPosition = stringToProcess.find(from);
}
}
/*
* decodes html coded chars
*/
void decodeString(std::string &stringToDecode) {
replaceString(stringToDecode, "+", " ");
for(auto it = HTML_URL_CODES.begin(); it != HTML_URL_CODES.end(); ++it) {
replaceString(stringToDecode, it->first, it->second);
}
}
/*
* Sends an HTML Email using CPP-SMTPClient-library
* return 0 at success, 1 at client fail, 2 at critical fail.