bneradt commented on code in PR #12463: URL: https://github.com/apache/trafficserver/pull/12463#discussion_r2298407583
########## plugins/xdebug/xdebug_transforms.cc: ########## @@ -82,11 +84,107 @@ getPostBodyFullJson(TSHttpTxn txn) return output.str(); } +/** JSON-escape the given input stream. */ +static inline void +write_json_escaped(TSIOBuffer output_buffer, const char *data, int64_t len, int64_t &written) +{ + for (int64_t i = 0; i < len; ++i) { + unsigned char c = static_cast<unsigned char>(data[i]); + switch (c) { + case '"': { + const char *s = "\\\""; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + case '\\': { + const char *s = "\\\\"; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + case '\b': { + const char *s = "\\b"; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + case '\f': { + const char *s = "\\f"; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + case '\n': { + const char *s = "\\n"; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + case '\r': { + const char *s = "\\r"; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + case '\t': { + const char *s = "\\t"; + TSIOBufferWrite(output_buffer, s, 2); + written += 2; + break; + } + default: + if (c < 0x20) { + char esc[6]; + esc[0] = '\\'; + esc[1] = 'u'; + esc[2] = '0'; + esc[3] = '0'; + static const char hex[] = "0123456789abcdef"; + esc[4] = hex[(c >> 4) & 0x0F]; + esc[5] = hex[c & 0x0F]; + TSIOBufferWrite(output_buffer, esc, 6); + written += 6; + } else { + TSIOBufferWrite(output_buffer, reinterpret_cast<const char *>(&data[i]), 1); + written += 1; + } + } + } +} + +static inline void +write_hex(TSIOBuffer output_buffer, const char *src, int64_t len, int64_t &written) +{ + // Convert each byte to two hex characters + static const char hex_chars[] = "0123456789abcdef"; + + // Process in chunks to keep stack usage reasonable + const int64_t CHUNK = 1024; // 1KB of raw -> 2KB hex + int64_t idx = 0; + + while (idx < len) { + int64_t take = (len - idx > CHUNK) ? CHUNK : (len - idx); Review Comment: Agreed. -- 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-unsubscr...@trafficserver.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org