Commit 3f212ab6 authored by jan.koester's avatar jan.koester
Browse files

complete codetable

parent c9f135ed
Loading
Loading
Loading
Loading
+47 −8
Changes for src/http.cpp: 47 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -1438,22 +1438,61 @@ const char *libhttppp::HttpForm::getContentType(){
}

void libhttppp::HttpForm::urlEncode(const std::string &in,std::string &out){
  const char *codetable[][2]={
    {" " ,"+"},
    {"%","%25"},
    {"&","%26"},
    {"=","%3D}"},
    {"@","%40"}
  std::string_view codetable[]={
    // ----------------------------------------------------
    // 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

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

    // ----------------------------------------------------
    // 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
    {}
  };

  for (size_t i = 0; i < in.length(); ++i) {
    bool encoded = false;

    // Correct size calculation for the inner loop
    for (size_t ii = 0; ii < sizeof(codetable) / sizeof(codetable[0]); ++ii) {
    for (size_t ii = 0; !codetable[ii].empty(); ++ii) {

      // Correct check for single-character match
      if (in[i] == codetable[ii][0][0]) {
      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'