Skip to content

Commit

Permalink
Allow plugins to use WKS header strings
Browse files Browse the repository at this point in the history
  • Loading branch information
zwoop committed Apr 25, 2023
1 parent efda39e commit 3f5babe
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 6 deletions.
6 changes: 6 additions & 0 deletions doc/developer-guide/api/functions/TSMimeHdrFieldFind.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========
Expand All @@ -42,3 +44,7 @@ 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`.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ 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-Kown header strings, you can
also do a lookup for a header string using :func:`TSMimeHdrStringToWKS`.

Return Values
=============

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,4 @@ The MIME header functions are listed below:
- :c:func:`TSMimeParserCreate`
- :c:func:`TSMimeParserDestroy`
- :c:func:`TSMimeHdrPrint`
- :c:func:`TSMimeHdrStringToWKS`
1 change: 1 addition & 0 deletions include/ts/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
8 changes: 5 additions & 3 deletions plugins/header_rewrite/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion plugins/header_rewrite/conditions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion plugins/header_rewrite/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions plugins/header_rewrite/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class OperatorHeaders : public Operator

protected:
std::string _header;
const char *_header_wks;
};

///////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion plugins/header_rewrite/operators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
10 changes: 10 additions & 0 deletions src/traffic_server/InkAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3786,6 +3786,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 */
/**************/
Expand Down

0 comments on commit 3f5babe

Please sign in to comment.