Skip to content

Commit

Permalink
Testing updates and minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidWallOfCode committed May 19, 2020
1 parent 6f7a5df commit f81ca85
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 98 deletions.
26 changes: 26 additions & 0 deletions plugin/include/txn_box/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class Config {

~Config();

swoc::Errata load_file_glob(swoc::TextView pattern, swoc::TextView cfg_key);
swoc::Errata load_file(swoc::file::path const& cfg_path, swoc::TextView cfg_key);

/** Parse YAML from @a node to initialize @a this configuration.
*
* @param root Root node.
Expand Down Expand Up @@ -421,6 +424,29 @@ class Config {
* @see Extractor::validate
*/
swoc::Rv<ActiveType> validate(Extractor::Spec &spec);

// Configuration file tracking.
class FileInfo {
using self_type = FileInfo;
public:
using key_type = swoc::TextView;

FileInfo(key_type key) : _path(key) {}

// IntrusiveHashMap support.
static key_type key_of(self_type *self) { return self->_path; }
static bool equal(key_type lhs, key_type rhs) { return lhs == rhs; }
static size_t hash_of(key_type key) { return std::hash<std::string_view>()(key); }
static self_type *& next_ptr(self_type * self) { return self->_next; }
static self_type *& prev_ptr(self_type * self) { return self->_prev; }

protected:
key_type _path; ///< Absolute path to file.
self_type * _next = nullptr;
self_type * _prev = nullptr;
};

swoc::IntrusiveHashMap<FileInfo> _cfg_files;
};

inline Hook Config::current_hook() const { return _hook; }
Expand Down
22 changes: 18 additions & 4 deletions plugin/include/txn_box/Directive.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@

class Context;

/** Base class for directives.
*
*/
/// Base class for directives.
class Directive {
using self_type = Directive; ///< Self reference type.
friend Config;
friend Context;

public:
/// Import global value for convienence.
/// Import global value for convenience.
static constexpr swoc::TextView DO_KEY = Global::DO_KEY;

/// Generic handle for all directives.
Expand All @@ -45,6 +43,15 @@ class Directive {
*/
using InstanceLoader = std::function<swoc::Rv<Directive::Handle> (Config& cfg, YAML::Node drtv_node, swoc::TextView const& name, swoc::TextView const& arg, YAML::Node key_value)>;

/** Functor to do config level initialization.
*
* @param cfg Configuration object.
*
* This is called once per directive class when the @c Config instance is initialized. This should
* perform any initialization needed for the directive as a type, rather than as an instance used
* in the configuration. The most common use is if the directive needs space in a @c Context -
* that space must be reserved during the invocation of this functor.
*/
using CfgInitializer = std::function<swoc::Errata (Config& cfg)>;

/** Information about a directive type.
Expand Down Expand Up @@ -81,6 +88,13 @@ class Directive {
*/
virtual swoc::Errata invoke(Context &ctx) = 0;

/** Configuration initializer.
*
* @param Config& Configuration object.
* @return Errors, if any.
*
* Default implementation that does nothing. Override as needed.
*/
static swoc::Errata cfg_init(Config&) { return {}; }

protected:
Expand Down
6 changes: 6 additions & 0 deletions plugin/include/txn_box/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <swoc/Lexicon.h>
#include <swoc/bwf_fwd.h>

constexpr swoc::TextView DEBUG_TAG = "txn_box";

// Forward declares
class Config;
class Context;
Expand All @@ -31,6 +33,10 @@ template < typename ... Args > swoc::Errata Error(std::string_view const& fmt, A
return std::move(swoc::Errata().note_v(swoc::Severity::ERROR, fmt, std::forward_as_tuple(args...)));
}

template < typename ... Args > swoc::Errata Warning(std::string_view const& fmt, Args && ... args) {
return std::move(swoc::Errata().note_v(swoc::Severity::WARN, fmt, std::forward_as_tuple(args...)));
}

/// Separate a name and argument for a directive or extractor.
extern swoc::Rv<swoc::TextView> parse_arg(swoc::TextView & key);

Expand Down
46 changes: 37 additions & 9 deletions plugin/include/txn_box/ts_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ extern std::array<TSHttpHookID, std::tuple_size<Hook>::value> TS_Hook;

namespace ts {

template < typename ... Args >
void DebugMsg(swoc::TextView fmt, Args && ... args) {
swoc::LocalBufferWriter<1024> w;
auto arg_pack = std::forward_as_tuple(args...);
w.print_v(fmt, arg_pack);
if (! w.error()) {
TSDebug("txn_box", "%.*s", w.size(), w.data());
}
// Do it the hard way.
std::vector<char> buff;
buff.resize(w.extent());
swoc::FixedBufferWriter fw(buff.data(), buff.size());
fw.print_v(fmt, arg_pack);
TSDebug("txn_box", "%.*s", fw.size(), fw.data());
}

/** Hold a string allocated from TS core.
* This provides both a view of the string and clean up when destructed.
*/
Expand Down Expand Up @@ -120,14 +136,9 @@ class URL : public HeapObject {
*/
self_type & host_set(swoc::TextView const& host);

in_port_t port_get() {
return TSUrlPortGet(_buff, _loc);
}
in_port_t port_get();

self_type & port_set(in_port_t port) {
TSUrlPortSet(_buff, _loc, port);
return *this;
}
self_type & port_set(in_port_t port);

self_type & path_set(swoc::TextView path) {
TSUrlPathSet(_buff, _loc, path.data(), path.size());
Expand Down Expand Up @@ -265,8 +276,9 @@ class HttpRequest : public HttpHeader {
*/
URL url();

swoc::TextView method() const { int length; auto text = TSHttpHdrMethodGet(_buff, _loc, &length); return { text, static_cast<size_t>(length) }; }
swoc::TextView method() const;
swoc::TextView host() const;
in_port_t port() const;

/** Set the @a host for the request.
*
Expand All @@ -275,10 +287,19 @@ class HttpRequest : public HttpHeader {
*
* This will update the request as little as possible. If the URL does not contain a host
* then it is unmodified and the @c Host field is set to @a host. If the URL has a host and
* there is no @c Host field, only the URL is updated.
* there is no @c Host field, only the URL is updated. The port is not updated.
*/
bool host_set(swoc::TextView const& host);

/** Set the port.
*
* @param port Port in host order.
* @return If the port was updated.
*
* This updates the URL and @c Host field as needed, making as few changes as possible.
*/
bool port_set(in_port_t port);

bool scheme_set(swoc::TextView const& scheme);
};

Expand Down Expand Up @@ -449,6 +470,8 @@ inline bool HeapObject::is_valid() const { return _buff != nullptr && _loc != nu

inline URL::URL(TSMBuffer buff, TSMLoc loc) : super_type(buff, loc) {}

inline in_port_t URL::port_get() { return TSUrlPortGet(_buff, _loc); }

inline swoc::TextView URL::scheme() const { int length; auto text = TSUrlSchemeGet(_buff, _loc, &length); return { text, static_cast<size_t>(length) }; }

inline swoc::TextView URL::path() const { int length; auto text = TSUrlPathGet(_buff, _loc, &length); return { text, static_cast<size_t>(length) }; }
Expand All @@ -462,6 +485,11 @@ inline URL &URL::host_set(swoc::TextView const& host) {
return *this;
}

inline auto URL::port_set(in_port_t port) -> self_type & {
TSUrlPortSet(_buff, _loc, port);
return *this;
}

inline URL &URL::scheme_set(swoc::TextView const& scheme) {
if (this->is_valid()) {
TSUrlSchemeSet(_buff, _loc, scheme.data(), scheme.size());
Expand Down
25 changes: 17 additions & 8 deletions plugin/src/Features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,7 @@ Feature Ex_ua_req_host::extract(Context &ctx, Spec const&) {
FeatureView zret;
zret._direct_p = true;
if ( auto hdr { ctx.creq_hdr() } ; hdr.is_valid()) {
if ( ts::URL url { hdr.url() } ; url.is_valid()) {
zret = url.host();
if (zret.data() == nullptr) { // not in the URL, look in the HOST field.
if ( auto field { hdr.field(ts::HTTP_FIELD_HOST) } ; field.is_valid()) {
zret = field.value();
}
}
}
zret = hdr.host();
}
return zret;
}
Expand Down Expand Up @@ -451,6 +444,20 @@ ts::HttpHeader Ex_ua_req_field::hdr(Context & ctx) const {
return ctx.creq_hdr();
}
// -----
class Ex_proxy_req_field : public ExHttpField {
public:
static constexpr TextView NAME { "proxy-req-field" };

protected:
TextView const& key() const override;
ts::HttpHeader hdr(Context & ctx) const override;
};

TextView const& Ex_proxy_req_field::key() const { return NAME; }
ts::HttpHeader Ex_proxy_req_field::hdr(Context & ctx) const {
return ctx.preq_hdr();
}
// -----
class Ex_proxy_rsp_field : public ExHttpField {
public:
static constexpr TextView NAME { "proxy-rsp-field" };
Expand Down Expand Up @@ -898,6 +905,7 @@ Ex_proxy_req_host proxy_req_host;
Ex_proxy_req_path proxy_req_path;
Ex_proxy_req_url proxy_req_url;
Ex_proxy_req_scheme proxy_req_scheme;
Ex_proxy_req_field proxy_req_field;

Ex_proxy_rsp_status proxy_rsp_status;
Ex_proxy_rsp_field proxy_rsp_field;
Expand Down Expand Up @@ -946,6 +954,7 @@ Ex_remainder_feature ex_remainder_feature;
Extractor::define(Ex_proxy_req_url::NAME, &proxy_req_url);
Extractor::define(Ex_proxy_req_path::NAME, &proxy_req_path);
Extractor::define(Ex_proxy_req_scheme::NAME, &proxy_req_scheme);
Extractor::define(Ex_proxy_req_field::NAME, &proxy_req_field);

Extractor::define(Ex_proxy_rsp_status::NAME, &proxy_rsp_status);
Extractor::define(Ex_proxy_rsp_field::NAME, &proxy_rsp_field);
Expand Down
Loading

0 comments on commit f81ca85

Please sign in to comment.