diff --git a/doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst b/doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst index 149e07bb262..5a98b258736 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 .. 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 2a2beb71566..ec39d464a41 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 00a08d4312a..77dbe183949 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 a5ff3815885..b3cd21042d2 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 1e06b2c8e44..610ec879f84 100644 --- a/plugins/header_rewrite/condition.h +++ b/plugins/header_rewrite/condition.h @@ -97,7 +97,8 @@ class Condition : public Statement 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 @@ class Condition : public Statement 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 3f7ebca116f..b535e180b13 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 1afab7767be..4788b2d0f27 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 e014914f99c..7c2b6bdfe7d 100644 --- a/plugins/header_rewrite/operator.h +++ b/plugins/header_rewrite/operator.h @@ -85,6 +85,7 @@ class OperatorHeaders : public Operator protected: std::string _header; + const char *_header_wks; }; /////////////////////////////////////////////////////////////////////////////// diff --git a/plugins/header_rewrite/operators.cc b/plugins/header_rewrite/operators.cc index 1f6f6327c5a..a45b9a79e54 100644 --- a/plugins/header_rewrite/operators.cc +++ b/plugins/header_rewrite/operators.cc @@ -645,7 +645,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 8cc5acfa838..8dd5ba8d318 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 */ /**************/