This is an automated email from the ASF dual-hosted git repository.

bcall pushed a commit to branch 9.1.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/9.1.x by this push:
     new 20c857a78 [9.1.x] Change Content-Type to text/plain if xdebug modifies 
response body (#9248)
20c857a78 is described below

commit 20c857a785da93fa0e3263597207b5ef35b65b7c
Author: Masakazu Kitajo <mas...@apache.org>
AuthorDate: Tue Dec 13 18:54:25 2022 -0700

    [9.1.x] Change Content-Type to text/plain if xdebug modifies response body 
(#9248)
    
    * Change Content-Type to text/plain if xdebug modifies response body
    
    Co-authored-by: Bryan Call <bc...@apache.org>
    (cherry picked from commit 635528a707e80f918b89703865ccc1507f1b576e)
    
    * Fix indentation
    
    (cherry picked from commit 34623437d5dab624f7c4793be4f9765a6296ee46)
---
 plugins/xdebug/xdebug.cc                           | 24 ++++++++++++++
 .../gold_tests/pluginTest/xdebug/x_remap/out.gold  | 37 ++++++++++++++++------
 2 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/plugins/xdebug/xdebug.cc b/plugins/xdebug/xdebug.cc
index f0b1d307a..895491f25 100644
--- a/plugins/xdebug/xdebug.cc
+++ b/plugins/xdebug/xdebug.cc
@@ -376,6 +376,29 @@ InjectRemapHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc 
hdr)
   }
 }
 
+static void
+InjectOriginalContentTypeHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc hdr)
+{
+  TSMLoc ct_field = TSMimeHdrFieldFind(buffer, hdr, 
TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE);
+  if (TS_NULL_MLOC != ct_field) {
+    int original_content_type_len     = 0;
+    const char *original_content_type = TSMimeHdrFieldValueStringGet(buffer, 
hdr, ct_field, -1, &original_content_type_len);
+    if (original_content_type != nullptr) {
+      TSMLoc dst = FindOrMakeHdrField(buffer, hdr, "X-Original-Content-Type", 
lengthof("X-Original-Content-Type"));
+      TSReleaseAssert(TS_NULL_MLOC != dst);
+      TSReleaseAssert(TSMimeHdrFieldValueStringInsert(buffer, hdr, dst, -1 /* 
idx */, original_content_type,
+                                                      
original_content_type_len) == TS_SUCCESS);
+    }
+  } else {
+    if (TSMimeHdrFieldCreateNamed(buffer, hdr, TS_MIME_FIELD_CONTENT_TYPE, 
TS_MIME_LEN_CONTENT_TYPE, &ct_field) == TS_SUCCESS) {
+      TSReleaseAssert(TSMimeHdrFieldAppend(buffer, hdr, ct_field) == 
TS_SUCCESS);
+    }
+  }
+
+  TSMimeHdrFieldValuesClear(buffer, hdr, ct_field);
+  TSReleaseAssert(TSMimeHdrFieldValueStringSet(buffer, hdr, ct_field, -1, 
"text/plain", lengthof("text/plain")) == TS_SUCCESS);
+}
+
 static void
 InjectTxnUuidHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc hdr)
 {
@@ -491,6 +514,7 @@ XInjectResponseHeaders(TSCont /* contp */, TSEvent event, 
void *edata)
   }
 
   if (xheaders & XHEADER_X_PROBE_HEADERS) {
+    InjectOriginalContentTypeHeader(txn, buffer, hdr);
     BodyBuilder *data = AuxDataMgr::data(txn).body_builder.get();
     TSDebug("xdebug_transform", "XInjectResponseHeaders(): client resp header 
ready");
     if (data == nullptr) {
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold 
b/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold
index f9f4ebef2..7cd12e1ec 100644
--- a/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold
@@ -6,6 +6,7 @@ Cache-Control: no-store
 Content-Type: text/html
 Content-Language: en
 X-Remap: from=Not-Found, to=Not-Found
+X-Original-Content-Type: text/html; charset=utf-8
 Content-Length: 391
 
 <HTML>
@@ -33,6 +34,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://one/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -66,7 +68,8 @@ X-Remap: from=http://one/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://one/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://one/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -80,6 +83,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -113,7 +117,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -127,6 +132,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://three[0-9]+/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -160,7 +166,8 @@ X-Remap: from=http://three[0-9]+/, 
to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://three[0-9]+/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://three[0-9]+/, 
to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -174,6 +181,7 @@ Age: ``
 Transfer-Encoding: chunked
 Connection: close
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -208,7 +216,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Age' : '``
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -222,6 +231,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -257,7 +267,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -271,6 +282,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -304,7 +316,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -318,6 +331,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -353,7 +367,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -367,6 +382,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -402,7 +418,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }
@@ -416,6 +433,7 @@ Transfer-Encoding: chunked
 Connection: close
 Server: ATS/``
 X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+Content-Type: text/plain
 
 ``
 {'xDebugProbeAt' : '``
@@ -449,7 +467,8 @@ X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
        'Transfer-Encoding' : 'chunked',
        'Connection' : 'close',
        'Server' : 'ATS/``
-       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/'
+       'X-Remap' : 'from=http://two/, to=http://127.0.0.1:SERVER_PORT/',
+       'Content-Type' : 'text/plain'
        }}
    ]
 }

Reply via email to