Skip to content

Commit

Permalink
Change Content-Type to text/plain if xdebug modifies response body (#…
Browse files Browse the repository at this point in the history
…9246)

* Change Content-Type to text/plain if xdebug modifies response body

Co-authored-by: Bryan Call <bcall@apache.org>

* Fix indentation

Co-authored-by: Bryan Call <bcall@apache.org>
  • Loading branch information
maskit and bryancall committed Dec 15, 2022
1 parent 3531972 commit eb5efe1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
24 changes: 24 additions & 0 deletions plugins/xdebug/xdebug.cc
Expand Up @@ -400,6 +400,29 @@ InjectEffectiveURLHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc hdr)
TSfree(strval.ptr);
}

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)
{
Expand Down Expand Up @@ -519,6 +542,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) {
Expand Down
37 changes: 28 additions & 9 deletions tests/gold_tests/pluginTest/xdebug/x_remap/out.gold
Expand Up @@ -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>
Expand Down Expand Up @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand All @@ -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' : '``
Expand Down Expand Up @@ -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'
}}
]
}
Expand Down

0 comments on commit eb5efe1

Please sign in to comment.