Copilot commented on code in PR #13415:
URL: https://github.com/apache/trafficserver/pull/13415#discussion_r3717481798


##########
plugins/header_rewrite/operators.cc:
##########
@@ -58,11 +66,25 @@ handleFetchEvents(TSCont cont, TSEvent event, void *edata)
 
       TSHttpHdrTypeSet(hdr_buf, hdr_loc, TS_HTTP_TYPE_RESPONSE);
       if (TSHttpHdrParseResp(parser, hdr_buf, hdr_loc, &data_start, data_end) 
== TS_PARSE_DONE) {
-        size_t body_len = data_end - data_start;
-        char  *body     = static_cast<char *>(TSmalloc(body_len + 1));
+        char  *content_type = nullptr;
+        size_t body_len     = data_end - data_start;
+        char  *body         = static_cast<char *>(TSmalloc(body_len + 1));
+
+        if (!fetch_data->content_type.empty()) {
+          content_type = TSstrdup(fetch_data->content_type.c_str());
+        } else if (TSMLoc field_loc = TSMimeHdrFieldFind(hdr_buf, hdr_loc, 
TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE);
+                   field_loc != TS_NULL_MLOC) {
+          int         value_len = 0;
+          const char *value     = TSMimeHdrFieldValueStringGet(hdr_buf, 
hdr_loc, field_loc, -1, &value_len);
+
+          if (value != nullptr && value_len > 0) {
+            content_type = TSstrndup(value, value_len);
+          }
+          TSHandleMLocRelease(hdr_buf, hdr_loc, field_loc);
+        }

Review Comment:
   TSMimeHdrFieldFind/TSMimeHdrFieldValueStringGet expect a MIME header 
locator, but here `hdr_loc` is an HTTP header locator created by 
TSHttpHdrCreate(). This will fail to find the fetched Content-Type (and may 
mis-handle the buffer). Retrieve the MIME header via TSHttpHdrMimeGet() and use 
that locator for the lookup/release.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to