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


##########
plugins/compress/compress.cc:
##########
@@ -811,6 +810,167 @@ add_vary_header_to_client_response(TSHttpTxn txnp)
   TSHandleMLocRelease(resp_buf, TS_NULL_MLOC, resp_loc);
 }
 
+// A 304 carries the origin's strong ETag, which the cache would merge over 
the weakened ETag of a
+// stored compressed copy. Keep it weak when both name the same entity and the 
stored copy is
+// encoded; an identity copy's ETag is the origin's own, so it may 
legitimately become strong.
+static void
+keep_cached_etag_weak(TSHttpTxn txnp)
+{
+  TSMBuffer srv_buf;
+  TSMLoc    srv_loc;
+
+  if (TS_SUCCESS != TSHttpTxnServerRespGet(txnp, &srv_buf, &srv_loc)) {
+    return;
+  }
+  ts::PostScript srv_defer([&]() -> void { TSHandleMLocRelease(srv_buf, 
TS_NULL_MLOC, srv_loc); });
+
+  if (TSHttpHdrStatusGet(srv_buf, srv_loc) != TS_HTTP_STATUS_NOT_MODIFIED) {
+    return;
+  }
+
+  TSMBuffer cached_buf;
+  TSMLoc    cached_loc;
+
+  if (TS_SUCCESS != TSHttpTxnCachedRespGet(txnp, &cached_buf, &cached_loc)) {
+    return;
+  }
+  ts::PostScript cached_defer([&]() -> void { TSHandleMLocRelease(cached_buf, 
TS_NULL_MLOC, cached_loc); });
+
+  TSMLoc cached_ce = TSMimeHdrFieldFind(cached_buf, cached_loc, 
TS_MIME_FIELD_CONTENT_ENCODING, TS_MIME_LEN_CONTENT_ENCODING);
+
+  if (cached_ce == TS_NULL_MLOC) {
+    return;
+  }
+  TSHandleMLocRelease(cached_buf, cached_loc, cached_ce);
+
+  TSMLoc srv_etag = TSMimeHdrFieldFind(srv_buf, srv_loc, TS_MIME_FIELD_ETAG, 
TS_MIME_LEN_ETAG);
+
+  if (srv_etag == TS_NULL_MLOC) {
+    return;
+  }
+  ts::PostScript srv_etag_defer([&]() -> void { TSHandleMLocRelease(srv_buf, 
srv_loc, srv_etag); });
+
+  TSMLoc cached_etag = TSMimeHdrFieldFind(cached_buf, cached_loc, 
TS_MIME_FIELD_ETAG, TS_MIME_LEN_ETAG);
+
+  if (cached_etag == TS_NULL_MLOC) {
+    return;
+  }
+  ts::PostScript cached_etag_defer([&]() -> void { 
TSHandleMLocRelease(cached_buf, cached_loc, cached_etag); });
+
+  int              srv_len;
+  const char      *srv_str = TSMimeHdrFieldValueStringGet(srv_buf, srv_loc, 
srv_etag, -1, &srv_len);
+  int              cached_len;
+  const char      *cached_str = TSMimeHdrFieldValueStringGet(cached_buf, 
cached_loc, cached_etag, -1, &cached_len);
+  std::string_view srv_value{srv_str, static_cast<size_t>(srv_len)};
+  std::string_view cached_value{cached_str, static_cast<size_t>(cached_len)};
+
+  if (cached_value.starts_with("W/") && cached_value.substr(2) == srv_value) {
+    TSMimeHdrFieldValueStringSet(srv_buf, srv_loc, srv_etag, -1, 
cached_value.data(), cached_value.size());
+  }
+}
+
+// No transform runs on a bodyless 304, so it goes out with the origin's or 
the cached identity
+// copy's strong ETag. A 304 must carry the ETag its 200 would (RFC 9110 
15.4.5), and this
+// request's 200 would have been compressed, so weaken it to match.
+static void
+weaken_client_not_modified_etag(TSHttpTxn txnp)
+{
+  TSMBuffer resp_buf;
+  TSMLoc    resp_loc;
+
+  if (TS_SUCCESS != TSHttpTxnClientRespGet(txnp, &resp_buf, &resp_loc)) {
+    return;
+  }
+  ts::PostScript resp_defer([&]() -> void { TSHandleMLocRelease(resp_buf, 
TS_NULL_MLOC, resp_loc); });
+
+  if (TSHttpHdrStatusGet(resp_buf, resp_loc) == TS_HTTP_STATUS_NOT_MODIFIED) {
+    etag_header(resp_buf, resp_loc);
+  }
+}
+
+static int
+not_modified_etag_plugin(TSCont contp, TSEvent event, void *edata)
+{
+  TSHttpTxn txnp = static_cast<TSHttpTxn>(edata);
+
+  switch (event) {
+  case TS_EVENT_HTTP_SEND_RESPONSE_HDR:
+    weaken_client_not_modified_etag(txnp);
+    break;
+
+  case TS_EVENT_HTTP_TXN_CLOSE:
+    TSContDestroy(contp);
+    break;
+
+  default:
+    fatal("compress 304 ETag unknown event");
+  }
+
+  TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
+
+  return 0;
+}
+
+static void
+remove_field_with_dups(TSMBuffer bufp, TSMLoc hdr_loc, const char *name, int 
name_len)
+{
+  TSMLoc field = TSMimeHdrFieldFind(bufp, hdr_loc, name, name_len);
+
+  while (field != TS_NULL_MLOC) {
+    TSMLoc next_dup = TSMimeHdrFieldNextDup(bufp, hdr_loc, field);
+
+    TSMimeHdrFieldDestroy(bufp, hdr_loc, field);
+    TSHandleMLocRelease(bufp, hdr_loc, field);
+    field = next_dup;
+  }
+}
+
+// With nothing stored, an origin 304 leaves no representation to tell whether 
this request's 200
+// would have been compressed. A weak validator likely came from a compressed 
copy, so fetch the 200
+// instead; ATS still answers the client's conditional from it, and the 304 is 
weakened if needed.
+static void
+unconditionalize_weak_validation(TSHttpTxn txnp)
+{
+  TSMBuffer cached_buf;
+  TSMLoc    cached_loc;
+
+  if (TS_SUCCESS == TSHttpTxnCachedRespGet(txnp, &cached_buf, &cached_loc)) {
+    TSHandleMLocRelease(cached_buf, TS_NULL_MLOC, cached_loc);
+    return;
+  }
+
+  TSMBuffer req_buf;
+  TSMLoc    req_loc;
+
+  if (TS_SUCCESS != TSHttpTxnServerReqGet(txnp, &req_buf, &req_loc)) {
+    return;
+  }
+  ts::PostScript req_defer([&]() -> void { TSHandleMLocRelease(req_buf, 
TS_NULL_MLOC, req_loc); });
+
+  TSMLoc inm = TSMimeHdrFieldFind(req_buf, req_loc, 
TS_MIME_FIELD_IF_NONE_MATCH, TS_MIME_LEN_IF_NONE_MATCH);
+
+  if (inm == TS_NULL_MLOC) {
+    return;
+  }
+
+  bool      has_weak = false;
+  int const nvalues  = TSMimeHdrFieldValuesCount(req_buf, req_loc, inm);
+
+  for (int i = 0; i < nvalues && !has_weak; i++) {
+    int         len;
+    const char *str = TSMimeHdrFieldValueStringGet(req_buf, req_loc, inm, i, 
&len);
+
+    has_weak = str != nullptr && std::string_view{str, 
static_cast<size_t>(len)}.starts_with("W/");
+  }
+  TSHandleMLocRelease(req_buf, req_loc, inm);
+
+  if (has_weak) {
+    debug("no stored response to judge a weak validator against, fetching 
unconditionally");
+    remove_field_with_dups(req_buf, req_loc, TS_MIME_FIELD_IF_NONE_MATCH, 
TS_MIME_LEN_IF_NONE_MATCH);
+    remove_field_with_dups(req_buf, req_loc, TS_MIME_FIELD_IF_MODIFIED_SINCE, 
TS_MIME_LEN_IF_MODIFIED_SINCE);
+  }

Review Comment:
   This also strips the precondition from cache-skipped POST requests: the 
plugin accepts POST as compressible at `compress.cc:587-592`, and ATS invokes 
the cache-lookup-complete hook even when lookup is skipped 
(`HttpTransact.cc:2292-2300`). A matching `If-None-Match` on POST is supposed 
to prevent the origin operation; fetching unconditionally can perform the 
mutation before ATS evaluates the returned response. Restrict this workaround 
to GET and add a conditional POST regression case.



-- 
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