Commit 8d32dd10 authored by jan.koester's avatar jan.koester
Browse files

test

parent 31281fdb
Loading
Loading
Loading
Loading
+51 −44
Changes for src/http.cpp: 51 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ctype.h>
#include <cstring>
#include <iterator>
#include <map>

#include "config.h"

@@ -1438,70 +1439,73 @@ const char *libhttppp::HttpForm::getContentType(){
}

void libhttppp::HttpForm::urlEncode(const std::string &in, std::string &out) {
  std::string_view codetable[]={
    // 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) ...
        // ----------------------------------------------------
        // Unsafe/Form-Encoded Characters (Commonly Replaced)
        // ----------------------------------------------------
    {" ",  "+"},     // Space is commonly encoded as '+' in form data (application/x-www-form-urlencoded)
        {' ',  "+"},

        // ----------------------------------------------------
        // General Delimiters (Must be encoded if used literally)
        // ----------------------------------------------------
    {"#",  "%23"},   // Hash (fragment identifier)
    {"%",  "%25"},   // Percent sign (used for encoding itself)
    {"&",  "%26"},   // Ampersand (parameter separator)
    {"+",  "%2B"},   // Plus sign (reserved for space replacement, must be encoded if literal)
    {"/",  "%2F"},   // Forward slash (path delimiter)
    {":",  "%3A"},   // Colon (scheme/port)
    {";",  "%3B"},   // Semicolon (parameter separator)
    {"=",  "%3D"},   // Equals sign (key-value separator)
    {"?",  "%3F"},   // Question mark (start of query string)
    {"@",  "%40"},   // At symbol
        {'#',  "%23"},
        {'%',  "%25"},
        {'&',  "%26"},
        {'+',  "%2B"},
        {'/',  "%2F"},
        {':',  "%3A"},
        {';',  "%3B"},
        {'=',  "%3D"},
        {'?',  "%3F"},
        {'@',  "%40"},

        // ----------------------------------------------------
        // Sub-Delimiters (Must be encoded if used literally)
        // ----------------------------------------------------
    {"$",  "%24"},   // Dollar sign
    {",",  "%2C"},   // Comma
    {"(",  "%28"},   // Open parenthesis
    {")",  "%29"},   // Close parenthesis
    {"*",  "%2A"},   // Asterisk
    {"'",  "%27"},   // Single quote
        {'$',  "%24"},
        {',',  "%2C"},
        {'(',  "%28"},
        {')',  "%29"},
        {'*',  "%2A"},
        {'\'', "%27"},

        // ----------------------------------------------------
        // Other Unsafe/Control Characters
        // ----------------------------------------------------
    {"\"", "%22"},   // Double quote
    {"<",  "%3C"},   // Less than
    {">",  "%3E"},   // Greater than
    {"[",  "%5B"},   // Open bracket
    {"]",  "%5D"},   // Close bracket
    {"^",  "%5E"},   // Caret/Circumflex
    {"`",  "%60"},   // Grave accent
    {"{",  "%7B"},   // Open curly brace
    {"|",  "%7C"},   // Pipe/Vertical bar
    {"}",  "%7D"},   // Close curly brace
    // Terminator
    {}
        {'"', "%22"},
        {'<',  "%3C"},
        {'>',  "%3E"},
        {'[',  "%5B"},
        {']',  "%5D"},
        {'^',  "%5E"},
        {'`',  "%60"},
        {'{',  "%7B"},
        {'|',  "%7C"},
        {'}',  "%7D"},
    };

  for (size_t i = 0; i < in.length(); ++i) {
    bool encoded = false;
    // Clear the output string and reserve space
    out.clear();
    out.reserve(in.length() * 3); // Reserve space for potential growth

    // Correct size calculation for the inner loop
    for (size_t ii = 0; !codetable[ii].empty(); ++ii) {
    for (char c : in) {
        // Use map::find to efficiently search for the character 'c'
        auto it = codetable.find(c);

      // Correct check for single-character match
      if (in[i] == codetable[ii][0]) {
        out += codetable[ii][1];
        encoded = true; // Flag that we handled the character
        break;          // Stop checking other entries and move to the next 'i'
      }
    }
        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;

    // If we didn't find a match in the table, append the character as is
    if (!encoded) {
      out += in[i];
        } else {
            // Character not found (unreserved), append it as is
            out += c;
        }
    }
}
@@ -1524,6 +1528,9 @@ void libhttppp::HttpForm::urlDecode(const std::string &in,std::string &out){
                throw e;
            }
            break;
            case '+':
                out.push_back(' ');
            break;
            default:
                out.push_back(c);
        }