Loading src/http.cpp +53 −78 Changes for src/http.cpp: 53 added lines, 78 removed lines. Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ *******************************************************************************/ #include <algorithm> #include <array> #include <sstream> #include <charconv> #include <cstdio> Loading Loading @@ -3397,16 +3398,14 @@ const std::string& libhttppp::HttpHeader::HeaderData::Values::getvalue(){ int libhttppp::HttpHeader::HeaderData::Values::getIntvalue(){ std::istringstream iss(_value); int size; iss >> size; int size = 0; std::from_chars(_value.data(), _value.data() + _value.size(), size); return size; } size_t libhttppp::HttpHeader::HeaderData::Values::getSizetValue(){ std::istringstream iss(_value); size_t size; iss >> size; size_t size = 0; std::from_chars(_value.data(), _value.data() + _value.size(), size); return size; } Loading @@ -3417,16 +3416,12 @@ libhttppp::HttpHeader::HeaderData::Values& libhttppp::HttpHeader::HeaderData::Va } libhttppp::HttpHeader::HeaderData::Values &libhttppp::HttpHeader::HeaderData::Values::operator=(size_t val){ std::stringstream ss; ss << val; *this=ss.str(); *this = std::to_string(val); return *this; } libhttppp::HttpHeader::HeaderData::Values &libhttppp::HttpHeader::HeaderData::Values::operator=(int val){ std::stringstream ss; ss << val; *this=ss.str(); *this = std::to_string(val); return *this; } Loading Loading @@ -3760,11 +3755,11 @@ void libhttppp::HttpResponse::sendChunk(netplus::con &curconnection, const char if (len == 0 || !data) return; std::stringstream sizeLine; sizeLine << std::hex << len << "\r\n"; std::string prefix = sizeLine.str(); curconnection.SendData.append(prefix.data(), prefix.size()); char sizeLine[2 * sizeof(size_t) + 2 + 1]; auto conv = std::to_chars(sizeLine, sizeLine + sizeof(sizeLine), len, 16); conv.ptr[0] = '\r'; conv.ptr[1] = '\n'; curconnection.SendData.append(sizeLine, static_cast<size_t>(conv.ptr - sizeLine) + 2); curconnection.SendData.append(data, len); curconnection.SendData.append("\r\n", 2); } Loading Loading @@ -4757,76 +4752,59 @@ void libhttppp::HttpForm::parse(libhttppp::HttpRequest &request){ } void libhttppp::HttpForm::urlEncode(const std::string &in, std::string &out) { // Note: Use 'static const' to define the map once if the function is called repeatedly. static const std::map<char, std::string_view> codetable = { // ... (All your encoding pairs) ... // ---------------------------------------------------- // Indexed directly by (unsigned char) instead of a std::map<char,...> -- // O(1) array lookup with no pointer-chasing tree traversal per input byte. static const std::array<std::string_view, 256> codetable = [] { std::array<std::string_view, 256> t{}; // Unsafe/Form-Encoded Characters (Commonly Replaced) // ---------------------------------------------------- {' ', "+"}, t[static_cast<unsigned char>(' ')] = "+"; // ---------------------------------------------------- // General Delimiters (Must be encoded if used literally) // ---------------------------------------------------- {'#', "%23"}, {'%', "%25"}, {'&', "%26"}, {'+', "%2B"}, {'/', "%2F"}, {':', "%3A"}, {';', "%3B"}, {'=', "%3D"}, {'?', "%3F"}, {'@', "%40"}, // ---------------------------------------------------- t[static_cast<unsigned char>('#')] = "%23"; t[static_cast<unsigned char>('%')] = "%25"; t[static_cast<unsigned char>('&')] = "%26"; t[static_cast<unsigned char>('+')] = "%2B"; t[static_cast<unsigned char>('/')] = "%2F"; t[static_cast<unsigned char>(':')] = "%3A"; t[static_cast<unsigned char>(';')] = "%3B"; t[static_cast<unsigned char>('=')] = "%3D"; t[static_cast<unsigned char>('?')] = "%3F"; t[static_cast<unsigned char>('@')] = "%40"; // Sub-Delimiters (Must be encoded if used literally) // ---------------------------------------------------- {'$', "%24"}, {',', "%2C"}, {'(', "%28"}, {')', "%29"}, {'*', "%2A"}, {'\'', "%27"}, // ---------------------------------------------------- t[static_cast<unsigned char>('$')] = "%24"; t[static_cast<unsigned char>(',')] = "%2C"; t[static_cast<unsigned char>('(')] = "%28"; t[static_cast<unsigned char>(')')] = "%29"; t[static_cast<unsigned char>('*')] = "%2A"; t[static_cast<unsigned char>('\'')] = "%27"; // Other Unsafe/Control Characters // ---------------------------------------------------- {'"', "%22"}, {'<', "%3C"}, {'>', "%3E"}, {'[', "%5B"}, {']', "%5D"}, {'^', "%5E"}, {'`', "%60"}, {'{', "%7B"}, {'|', "%7C"}, {'}', "%7D"}, }; t[static_cast<unsigned char>('"')] = "%22"; t[static_cast<unsigned char>('<')] = "%3C"; t[static_cast<unsigned char>('>')] = "%3E"; t[static_cast<unsigned char>('[')] = "%5B"; t[static_cast<unsigned char>(']')] = "%5D"; t[static_cast<unsigned char>('^')] = "%5E"; t[static_cast<unsigned char>('`')] = "%60"; t[static_cast<unsigned char>('{')] = "%7B"; t[static_cast<unsigned char>('|')] = "%7C"; t[static_cast<unsigned char>('}')] = "%7D"; return t; }(); // Clear the output string and reserve space out.clear(); out.reserve(in.length() * 3); // Reserve space for potential growth for (char c : in) { // Use map::find to efficiently search for the character 'c' auto it = codetable.find(c); if (it != codetable.end()) { // Character found in the map const std::string_view& encoded_value = it->second; // --- CORRECTION HERE --- // Append the ENTIRE encoded string_view (it->second) // if you need the debug print: std::cout << encoded_value; out += encoded_value; } else { // Character not found (unreserved), append it as is const std::string_view &encoded = codetable[static_cast<unsigned char>(c)]; if (!encoded.empty()) out += encoded; else out += c; } } } void libhttppp::HttpForm::urlDecode(const std::string &in,std::string &out){ for (auto i = in.begin(), nd = in.end(); i < nd; ++i) Loading Loading @@ -5137,10 +5115,7 @@ void libhttppp::HttpCookie::setcookie(libhttppp::HttpResponse &curresp, dat->push_back(buf); } if(maxage>=0){ std::stringstream ss; ss << "max-age="; ss << maxage; dat->push_back(ss.str()); dat->push_back("max-age=" + std::to_string(maxage)); } if(!path.empty()){ buf="path="; Loading Loading
src/http.cpp +53 −78 Changes for src/http.cpp: 53 added lines, 78 removed lines. Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ *******************************************************************************/ #include <algorithm> #include <array> #include <sstream> #include <charconv> #include <cstdio> Loading Loading @@ -3397,16 +3398,14 @@ const std::string& libhttppp::HttpHeader::HeaderData::Values::getvalue(){ int libhttppp::HttpHeader::HeaderData::Values::getIntvalue(){ std::istringstream iss(_value); int size; iss >> size; int size = 0; std::from_chars(_value.data(), _value.data() + _value.size(), size); return size; } size_t libhttppp::HttpHeader::HeaderData::Values::getSizetValue(){ std::istringstream iss(_value); size_t size; iss >> size; size_t size = 0; std::from_chars(_value.data(), _value.data() + _value.size(), size); return size; } Loading @@ -3417,16 +3416,12 @@ libhttppp::HttpHeader::HeaderData::Values& libhttppp::HttpHeader::HeaderData::Va } libhttppp::HttpHeader::HeaderData::Values &libhttppp::HttpHeader::HeaderData::Values::operator=(size_t val){ std::stringstream ss; ss << val; *this=ss.str(); *this = std::to_string(val); return *this; } libhttppp::HttpHeader::HeaderData::Values &libhttppp::HttpHeader::HeaderData::Values::operator=(int val){ std::stringstream ss; ss << val; *this=ss.str(); *this = std::to_string(val); return *this; } Loading Loading @@ -3760,11 +3755,11 @@ void libhttppp::HttpResponse::sendChunk(netplus::con &curconnection, const char if (len == 0 || !data) return; std::stringstream sizeLine; sizeLine << std::hex << len << "\r\n"; std::string prefix = sizeLine.str(); curconnection.SendData.append(prefix.data(), prefix.size()); char sizeLine[2 * sizeof(size_t) + 2 + 1]; auto conv = std::to_chars(sizeLine, sizeLine + sizeof(sizeLine), len, 16); conv.ptr[0] = '\r'; conv.ptr[1] = '\n'; curconnection.SendData.append(sizeLine, static_cast<size_t>(conv.ptr - sizeLine) + 2); curconnection.SendData.append(data, len); curconnection.SendData.append("\r\n", 2); } Loading Loading @@ -4757,76 +4752,59 @@ void libhttppp::HttpForm::parse(libhttppp::HttpRequest &request){ } void libhttppp::HttpForm::urlEncode(const std::string &in, std::string &out) { // Note: Use 'static const' to define the map once if the function is called repeatedly. static const std::map<char, std::string_view> codetable = { // ... (All your encoding pairs) ... // ---------------------------------------------------- // Indexed directly by (unsigned char) instead of a std::map<char,...> -- // O(1) array lookup with no pointer-chasing tree traversal per input byte. static const std::array<std::string_view, 256> codetable = [] { std::array<std::string_view, 256> t{}; // Unsafe/Form-Encoded Characters (Commonly Replaced) // ---------------------------------------------------- {' ', "+"}, t[static_cast<unsigned char>(' ')] = "+"; // ---------------------------------------------------- // General Delimiters (Must be encoded if used literally) // ---------------------------------------------------- {'#', "%23"}, {'%', "%25"}, {'&', "%26"}, {'+', "%2B"}, {'/', "%2F"}, {':', "%3A"}, {';', "%3B"}, {'=', "%3D"}, {'?', "%3F"}, {'@', "%40"}, // ---------------------------------------------------- t[static_cast<unsigned char>('#')] = "%23"; t[static_cast<unsigned char>('%')] = "%25"; t[static_cast<unsigned char>('&')] = "%26"; t[static_cast<unsigned char>('+')] = "%2B"; t[static_cast<unsigned char>('/')] = "%2F"; t[static_cast<unsigned char>(':')] = "%3A"; t[static_cast<unsigned char>(';')] = "%3B"; t[static_cast<unsigned char>('=')] = "%3D"; t[static_cast<unsigned char>('?')] = "%3F"; t[static_cast<unsigned char>('@')] = "%40"; // Sub-Delimiters (Must be encoded if used literally) // ---------------------------------------------------- {'$', "%24"}, {',', "%2C"}, {'(', "%28"}, {')', "%29"}, {'*', "%2A"}, {'\'', "%27"}, // ---------------------------------------------------- t[static_cast<unsigned char>('$')] = "%24"; t[static_cast<unsigned char>(',')] = "%2C"; t[static_cast<unsigned char>('(')] = "%28"; t[static_cast<unsigned char>(')')] = "%29"; t[static_cast<unsigned char>('*')] = "%2A"; t[static_cast<unsigned char>('\'')] = "%27"; // Other Unsafe/Control Characters // ---------------------------------------------------- {'"', "%22"}, {'<', "%3C"}, {'>', "%3E"}, {'[', "%5B"}, {']', "%5D"}, {'^', "%5E"}, {'`', "%60"}, {'{', "%7B"}, {'|', "%7C"}, {'}', "%7D"}, }; t[static_cast<unsigned char>('"')] = "%22"; t[static_cast<unsigned char>('<')] = "%3C"; t[static_cast<unsigned char>('>')] = "%3E"; t[static_cast<unsigned char>('[')] = "%5B"; t[static_cast<unsigned char>(']')] = "%5D"; t[static_cast<unsigned char>('^')] = "%5E"; t[static_cast<unsigned char>('`')] = "%60"; t[static_cast<unsigned char>('{')] = "%7B"; t[static_cast<unsigned char>('|')] = "%7C"; t[static_cast<unsigned char>('}')] = "%7D"; return t; }(); // Clear the output string and reserve space out.clear(); out.reserve(in.length() * 3); // Reserve space for potential growth for (char c : in) { // Use map::find to efficiently search for the character 'c' auto it = codetable.find(c); if (it != codetable.end()) { // Character found in the map const std::string_view& encoded_value = it->second; // --- CORRECTION HERE --- // Append the ENTIRE encoded string_view (it->second) // if you need the debug print: std::cout << encoded_value; out += encoded_value; } else { // Character not found (unreserved), append it as is const std::string_view &encoded = codetable[static_cast<unsigned char>(c)]; if (!encoded.empty()) out += encoded; else out += c; } } } void libhttppp::HttpForm::urlDecode(const std::string &in,std::string &out){ for (auto i = in.begin(), nd = in.end(); i < nd; ++i) Loading Loading @@ -5137,10 +5115,7 @@ void libhttppp::HttpCookie::setcookie(libhttppp::HttpResponse &curresp, dat->push_back(buf); } if(maxage>=0){ std::stringstream ss; ss << "max-age="; ss << maxage; dat->push_back(ss.str()); dat->push_back("max-age=" + std::to_string(maxage)); } if(!path.empty()){ buf="path="; Loading