Advertisement
Guest User

filter file

a guest
Feb 18th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. // NOLINT(namespace-envoy)
  2. #include <map>
  3. #include <numeric>
  4. #include <regex>
  5. #include <set>
  6. #include <string>
  7. #include <unordered_map>
  8.  
  9. #include "proxy_wasm_intrinsics.h"
  10.  
  11. // TrafficDirection is a mirror of envoy xDS traffic direction.
  12. // As defined in istio/proxy/extensions/common/context.h
  13. enum class TrafficDirection : int64_t {
  14.     Unspecified = 0,
  15.     Inbound = 1,
  16.     Outbound = 2,
  17. };
  18.  
  19. // Retrieves the traffic direction from the configuration context.
  20. TrafficDirection getTrafficDirection() {
  21.     int64_t direction;
  22.     if (getValue({"listener_direction"}, &direction)) {
  23.         return static_cast<TrafficDirection>(direction);
  24.     }
  25.     return TrafficDirection::Unspecified;
  26. }
  27.  
  28. class BidiRootContext : public RootContext {
  29.  public:
  30.     explicit BidiRootContext(uint32_t id, std::string_view root_id)
  31.         : RootContext(id, root_id) {
  32.         std::string workload_name;
  33.         if (getValue({"node", "metadata", "WORKLOAD_NAME"}, &workload_name)) {
  34.             workload_name_ = workload_name;
  35.             LOG_WARN("Initialized workload_name: " + workload_name_);
  36.         } else {
  37.             LOG_WARN("Failed to set workload name");
  38.         }
  39.     }
  40.     bool onConfigure(size_t /* configuration_size */) override;
  41.  
  42.     std::string_view getWorkloadName() { return workload_name_; }
  43.  
  44.  private:
  45.     std::string workload_name_;
  46. };
  47.  
  48. class BidiContext : public Context {
  49.  public:
  50.     explicit BidiContext(uint32_t id, RootContext *root)
  51.         : Context(id, root),
  52.           root_(static_cast<BidiRootContext *>(static_cast<void *>(root))) {
  53.         direction_ = getTrafficDirection();
  54.     }
  55.  
  56.     FilterHeadersStatus onRequestHeaders(uint32_t headers,
  57.                                          bool end_of_stream) override;
  58.     void onRequestHeadersInbound();
  59.     void onRequestHeadersOutbound();
  60.     FilterHeadersStatus onResponseHeaders(uint32_t headers,
  61.                                           bool end_of_stream) override;
  62.     void onResponseHeadersInbound();
  63.     void onResponseHeadersOutbound();
  64.     void print_headers(WasmHeaderMapType type);
  65.  
  66.  private:
  67.     BidiRootContext *root_;
  68.     TrafficDirection direction_;
  69. };
  70.  
  71. static RegisterContextFactory
  72.     register_BidiContext(CONTEXT_FACTORY(BidiContext),
  73.                          ROOT_FACTORY(BidiRootContext), "bidi_root_id");
  74.  
  75. bool BidiRootContext::onConfigure(size_t) { return true; }
  76.  
  77. void BidiContext::print_headers(WasmHeaderMapType type) {
  78.     if (type == WasmHeaderMapType::RequestHeaders) {
  79.         auto result = getRequestHeaderPairs();
  80.         auto pairs = result->pairs();
  81.         LOG_WARN("Request headers: " + toString(pairs.size()));
  82.         for (auto &p : pairs) {
  83.             LOG_WARN(std::string(p.first) + " -> " + std::string(p.second));
  84.         }
  85.     } else if (type == WasmHeaderMapType::ResponseHeaders) {
  86.         auto result = getResponseHeaderPairs();
  87.         auto pairs = result->pairs();
  88.         LOG_WARN("Response headers: " + toString(pairs.size()));
  89.         for (auto &p : pairs) {
  90.             LOG_WARN(std::string(p.first) + " -> " + std::string(p.second));
  91.         }
  92.     }
  93. }
  94.  
  95. FilterHeadersStatus BidiContext::onRequestHeaders(uint32_t, bool) {
  96.     if (direction_ == TrafficDirection::Inbound) {
  97.         onRequestHeadersInbound();
  98.     } else if (direction_ == TrafficDirection::Outbound) {
  99.         onRequestHeadersOutbound();
  100.     } else {
  101.       LOG_WARN("Unknown direction.");
  102.     }
  103.     print_headers(WasmHeaderMapType::RequestHeaders);
  104.     return FilterHeadersStatus::Continue;
  105. }
  106.  
  107. void BidiContext::onRequestHeadersInbound() {
  108.     LOG_WARN("Traversed.");
  109. }
  110.  
  111. void BidiContext::onRequestHeadersOutbound() {
  112.     LOG_WARN("Traversed.");
  113. }
  114.  
  115. FilterHeadersStatus BidiContext::onResponseHeaders(uint32_t, bool) {
  116.     if (direction_ == TrafficDirection::Inbound) {
  117.         onResponseHeadersInbound();
  118.     } else if (direction_ == TrafficDirection::Outbound) {
  119.         onResponseHeadersOutbound();
  120.     } else {
  121.       LOG_WARN("Unknown direction.");
  122.     }
  123.     print_headers(WasmHeaderMapType::ResponseHeaders);
  124.     return FilterHeadersStatus::Continue;
  125. }
  126. void BidiContext::onResponseHeadersInbound() {
  127.     LOG_WARN("Traversed.");
  128. }
  129. void BidiContext::onResponseHeadersOutbound() {
  130.     LOG_WARN("Traversed.");
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement