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

zwoop pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 37994a2273 Allow plugins to use WKS header strings (#9643)
37994a2273 is described below

commit 37994a22736834ec2d31f48e37212de63f910009
Author: Leif Hedstrom <[email protected]>
AuthorDate: Thu Jun 8 12:56:24 2023 -0600

    Allow plugins to use WKS header strings (#9643)
    
    * Allow plugins to use WKS header strings
    
    * Fixes documentation
---
 doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst    |  7 +++++++
 .../api/functions/TSMimeHdrFieldValueStringGet.en.rst          |  4 ++++
 doc/developer-guide/plugins/http-headers/mime-headers.en.rst   |  1 +
 include/ts/ts.h                                                |  1 +
 plugins/header_rewrite/condition.h                             |  8 +++++---
 plugins/header_rewrite/conditions.cc                           |  2 +-
 plugins/header_rewrite/operator.cc                             |  3 ++-
 plugins/header_rewrite/operator.h                              |  1 +
 plugins/header_rewrite/operators.cc                            |  2 +-
 src/traffic_server/InkAPI.cc                                   | 10 ++++++++++
 10 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst 
b/doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst
index 149e07bb26..5a98b25873 100644
--- a/doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst
+++ b/doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst
@@ -29,6 +29,8 @@ Synopsis
     #include <ts/ts.h>
 
 .. function:: TSMLoc TSMimeHdrFieldFind(TSMBuffer bufp, TSMLoc hdr, const char 
* name, int length)
+.. function:: const char *TSMimeHdrStringToWKS(const char *str, int length)
+
 
 Description
 ===========
@@ -42,3 +44,8 @@ comparison is done between the field name and :arg:`name`. If
 :c:func:`TSMimeHdrFieldFind` cannot find the requested field, it
 returns :c:data:`TS_NULL_MLOC`.  Release the returned :c:type:`TSMLoc`
 handle with a call to :c:func:`TSHandleMLocRelease`.
+
+The :arg:`name` argument is best specified using the pre-defined Well-Known 
strings, such as e.g.
+``TS_MIME_FIELD_CACHE_CONTROL`` and ``TS_MIME_LEN_CACHE_CONTROL``. These WK 
constants
+can also be looked up using :c:func:`TSMimeHdrStringToWKS`. If a header does
+not have a WKS, this function will return a :code:`nullptr`.
diff --git 
a/doc/developer-guide/api/functions/TSMimeHdrFieldValueStringGet.en.rst 
b/doc/developer-guide/api/functions/TSMimeHdrFieldValueStringGet.en.rst
index 2a2beb7156..ec39d464a4 100644
--- a/doc/developer-guide/api/functions/TSMimeHdrFieldValueStringGet.en.rst
+++ b/doc/developer-guide/api/functions/TSMimeHdrFieldValueStringGet.en.rst
@@ -65,6 +65,10 @@ preferred.
 value, and populated :arg:`value_len_ptr` with the length of the
 value in bytes. The returned header value is not NUL-terminated.
 
+In addition to all the predefined constants for Well-Known header strings, you 
can
+also do a lookup for a header string using :func:`TSMimeHdrStringToWKS`. If a 
lookup
+fails, this function returns a :code:`nullptr`
+
 Return Values
 =============
 
diff --git a/doc/developer-guide/plugins/http-headers/mime-headers.en.rst 
b/doc/developer-guide/plugins/http-headers/mime-headers.en.rst
index 00a08d4312..77dbe18394 100644
--- a/doc/developer-guide/plugins/http-headers/mime-headers.en.rst
+++ b/doc/developer-guide/plugins/http-headers/mime-headers.en.rst
@@ -437,3 +437,4 @@ The MIME header functions are listed below:
 -  :c:func:`TSMimeParserCreate`
 -  :c:func:`TSMimeParserDestroy`
 -  :c:func:`TSMimeHdrPrint`
+-  :c:func:`TSMimeHdrStringToWKS`
diff --git a/include/ts/ts.h b/include/ts/ts.h
index a5ff381588..b3cd21042d 100644
--- a/include/ts/ts.h
+++ b/include/ts/ts.h
@@ -1061,6 +1061,7 @@ tsapi TSReturnCode 
TSMimeHdrFieldValueUintInsert(TSMBuffer bufp, TSMLoc hdr, TSM
 tsapi TSReturnCode TSMimeHdrFieldValueDateInsert(TSMBuffer bufp, TSMLoc hdr, 
TSMLoc field, time_t value);
 
 tsapi TSReturnCode TSMimeHdrFieldValueDelete(TSMBuffer bufp, TSMLoc hdr, 
TSMLoc field, int idx);
+tsapi const char *TSMimeHdrStringToWKS(const char *str, int length);
 
 /* --------------------------------------------------------------------------
    HTTP headers */
diff --git a/plugins/header_rewrite/condition.h 
b/plugins/header_rewrite/condition.h
index 1e06b2c8e4..610ec879f8 100644
--- a/plugins/header_rewrite/condition.h
+++ b/plugins/header_rewrite/condition.h
@@ -97,7 +97,8 @@ public:
   virtual void
   set_qualifier(const std::string &q)
   {
-    _qualifier = q;
+    _qualifier_wks = TSMimeHdrStringToWKS(q.c_str(), q.length());
+    _qualifier     = q;
   }
 
   // Some getters
@@ -128,8 +129,9 @@ protected:
   virtual bool eval(const Resources &res) = 0;
 
   std::string _qualifier;
-  MatcherOps _cond_op = MATCH_EQUAL;
-  Matcher *_matcher   = nullptr;
+  const char *_qualifier_wks = nullptr;
+  MatcherOps _cond_op        = MATCH_EQUAL;
+  Matcher *_matcher          = nullptr;
 
 private:
   CondModifiers _mods = COND_NONE;
diff --git a/plugins/header_rewrite/conditions.cc 
b/plugins/header_rewrite/conditions.cc
index 3f7ebca116..b535e180b1 100644
--- a/plugins/header_rewrite/conditions.cc
+++ b/plugins/header_rewrite/conditions.cc
@@ -231,7 +231,7 @@ ConditionHeader::append_value(std::string &s, const 
Resources &res)
   if (bufp && hdr_loc) {
     TSMLoc field_loc;
 
-    field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, _qualifier.c_str(), 
_qualifier.size());
+    field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, _qualifier_wks ? 
_qualifier_wks : _qualifier.c_str(), _qualifier.size());
     TSDebug(PLUGIN_NAME, "Getting Header: %s, field_loc: %p", 
_qualifier.c_str(), field_loc);
 
     while (field_loc) {
diff --git a/plugins/header_rewrite/operator.cc 
b/plugins/header_rewrite/operator.cc
index 1afab7767b..4788b2d0f2 100644
--- a/plugins/header_rewrite/operator.cc
+++ b/plugins/header_rewrite/operator.cc
@@ -52,7 +52,8 @@ OperatorHeaders::initialize(Parser &p)
 {
   Operator::initialize(p);
 
-  _header = p.get_arg();
+  _header     = p.get_arg();
+  _header_wks = TSMimeHdrStringToWKS(_header.c_str(), _header.length());
 
   require_resources(RSRC_SERVER_RESPONSE_HEADERS);
   require_resources(RSRC_SERVER_REQUEST_HEADERS);
diff --git a/plugins/header_rewrite/operator.h 
b/plugins/header_rewrite/operator.h
index e014914f99..7c2b6bdfe7 100644
--- a/plugins/header_rewrite/operator.h
+++ b/plugins/header_rewrite/operator.h
@@ -85,6 +85,7 @@ public:
 
 protected:
   std::string _header;
+  const char *_header_wks;
 };
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/plugins/header_rewrite/operators.cc 
b/plugins/header_rewrite/operators.cc
index 0d7a7faf17..a24f4de2a4 100644
--- a/plugins/header_rewrite/operators.cc
+++ b/plugins/header_rewrite/operators.cc
@@ -651,7 +651,7 @@ OperatorSetHeader::exec(const Resources &res) const
   }
 
   if (res.bufp && res.hdr_loc) {
-    TSMLoc field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, 
_header.c_str(), _header.size());
+    TSMLoc field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, _header_wks ? 
_header_wks : _header.c_str(), _header.size());
 
     TSDebug(PLUGIN_NAME, "OperatorSetHeader::exec() invoked on %s: %s", 
_header.c_str(), value.c_str());
 
diff --git a/src/traffic_server/InkAPI.cc b/src/traffic_server/InkAPI.cc
index d62a9d2382..553f246082 100644
--- a/src/traffic_server/InkAPI.cc
+++ b/src/traffic_server/InkAPI.cc
@@ -3773,6 +3773,16 @@ TSMimeHdrFieldValueDelete(TSMBuffer bufp, TSMLoc hdr, 
TSMLoc field, int idx)
   return TS_SUCCESS;
 }
 
+const char *
+TSMimeHdrStringToWKS(const char *str, int length)
+{
+  if (length < 0) {
+    return hdrtoken_string_to_wks(str);
+  } else {
+    return hdrtoken_string_to_wks(str, length);
+  }
+}
+
 /**************/
 /* HttpParser */
 /**************/

Reply via email to