You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "zwoop (via GitHub)" <gi...@apache.org> on 2025/09/23 19:03:14 UTC

[PR] HRW: Support \r, \n and \t as ctrl chars in values [trafficserver]

zwoop opened a new pull request, #12528:
URL: https://github.com/apache/trafficserver/pull/12528

   This allows to have \r\n when calling e.g. set-body. I'm considering this a bug in header_rewrite.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] HRW: Support \r, \n and \t as ctrl chars in values [trafficserver]

Posted by "zwoop (via GitHub)" <gi...@apache.org>.
zwoop merged PR #12528:
URL: https://github.com/apache/trafficserver/pull/12528


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] HRW: Support \r, \n and \t as ctrl chars in values [trafficserver]

Posted by "cmcfarlen (via GitHub)" <gi...@apache.org>.
cmcfarlen commented on PR #12528:
URL: https://github.com/apache/trafficserver/pull/12528#issuecomment-3377908247

   Added to milestone 10.1.1 via #12547 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] HRW: Support \r, \n and \t as ctrl chars in values [trafficserver]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #12528:
URL: https://github.com/apache/trafficserver/pull/12528#discussion_r2388414519


##########
plugins/header_rewrite/parser.cc:
##########
@@ -67,12 +68,26 @@ Parser::parse_line(const std::string &original_line)
         extracting_token = false;
       }
     } else if ((state != PARSER_IN_REGEX) && (line[i] == '\\')) {
-      // Escaping
+      // Escaping - convert escape sequences to control characters
       if (!extracting_token) {
         extracting_token = true;
         cur_token_start  = i;
       }
-      line.erase(i, 1);
+
+      // Check if next character forms an escape sequence we want to convert
+      if (i + 1 < line.size()) {
+        constexpr std::string_view controls{"trn", 3};
+        constexpr char             mapped_ctrls[] = "\t\r\n";
+
+        if (auto pos = controls.find(line[i + 1]); pos != std::string_view::npos) {
+          line[i] = mapped_ctrls[pos];
+          line.erase(i + 1, 1);
+        } else {
+          line.erase(i, 1);
+        }

Review Comment:
   The `line.erase()` operations are inefficient as they shift all subsequent characters. For large strings with many escape sequences, this could cause O(n²) behavior. Consider building the result string incrementally or using a single pass with a write pointer to avoid repeated memory moves.



##########
plugins/header_rewrite/parser.cc:
##########
@@ -67,12 +68,26 @@ Parser::parse_line(const std::string &original_line)
         extracting_token = false;
       }
     } else if ((state != PARSER_IN_REGEX) && (line[i] == '\\')) {
-      // Escaping
+      // Escaping - convert escape sequences to control characters
       if (!extracting_token) {
         extracting_token = true;
         cur_token_start  = i;
       }
-      line.erase(i, 1);
+
+      // Check if next character forms an escape sequence we want to convert
+      if (i + 1 < line.size()) {
+        constexpr std::string_view controls{"trn", 3};
+        constexpr char             mapped_ctrls[] = "\t\r\n";
+
+        if (auto pos = controls.find(line[i + 1]); pos != std::string_view::npos) {
+          line[i] = mapped_ctrls[pos];
+          line.erase(i + 1, 1);
+        } else {

Review Comment:
   The mapping between `controls` and `mapped_ctrls` is implicit and error-prone. The order dependency between 'trn' and '\t\r\n' could lead to bugs if modified separately. Consider using a more explicit mapping structure like `std::array<std::pair<char, char>, 3>` or document this dependency clearly.
   ```suggestion
           constexpr std::array<std::pair<char, char>, 3> escape_map{{
             {'t', '\t'},
             {'r', '\r'},
             {'n', '\n'},
           }};
   
           bool mapped = false;
           for (const auto &pair : escape_map) {
             if (line[i + 1] == pair.first) {
               line[i] = pair.second;
               line.erase(i + 1, 1);
               mapped = true;
               break;
             }
           }
           if (!mapped) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org