Hi all,

This is a proposal to add a new virtual method to the CPP API to get access
to TSRemapRequestInfo.

We're working on a plugin that changes the origin URL on demand, and the
CPP API is a little bit confusing around which URL you need to modify to
accomplish this, specially if you want to leave the client URL as it is.

The change should be minimal and should not require any existent plugin
modifications.

I'm attaching a patch that I wrote this morning to accomplish that. If
people are +1 on this change, I can submit a pull request.

Cheers,
David
From 7157372f49744e70dc70ac945eae715659f06a8a Mon Sep 17 00:00:00 2001
From: David Calavera <david.calav...@gmail.com>
Date: Thu, 27 Feb 2020 11:26:30 -0800
Subject: [PATCH] [CPPAPI] Provide access to TSRemapRequestInfo in
 RemapPlugins.

Add a new virtual method that gives you more control over
how a remap happens in a RemapPlugin.

Signed-off-by: David Calavera <david.calav...@gmail.com>
---
 include/tscpp/api/RemapPlugin.h | 20 ++++++++++++++++++++
 src/tscpp/api/RemapPlugin.cc    |  7 ++-----
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/include/tscpp/api/RemapPlugin.h b/include/tscpp/api/RemapPlugin.h
index 44e550ead..e69cbb6ee 100644
--- a/include/tscpp/api/RemapPlugin.h
+++ b/include/tscpp/api/RemapPlugin.h
@@ -25,6 +25,7 @@
 #include "tscpp/api/Transaction.h"
 #include "tscpp/api/Url.h"
 #include "tscpp/api/utils.h"
+#include "ts/remap.h"
 
 namespace atscppapi
 {
@@ -49,6 +50,25 @@ public:
     RESULT_DID_REMAP_STOP,
   };
 
+  /**
+   * Invoked when a request matches the remap.config line - it gives you access to the remap information
+   * if you want to have more control over how the remap happens.
+   *
+   * @param transaction Transaction
+   * @param rri The remap information in the remap.config line.
+   *
+   * @return Result of the remap - will dictate further processing by the system.
+   */
+  virtual Result
+  remapTransaction(Transaction &transaction, TSRemapRequestInfo *rri)
+  {
+    Url map_from_url(rri->requestBufp, rri->mapFromUrl), map_to_url(rri->requestBufp, rri->mapToUrl);
+    bool redirect              = false;
+    RemapPlugin::Result result = doRemap(map_from_url, map_to_url, transaction, redirect);
+    rri->redirect              = redirect ? 1 : 0;
+    return result;
+  }
+
   /**
    * Invoked when a request matches the remap.config line - implementation should perform the
    * remap. The client's URL is in the transaction and that's where it should be modified.
diff --git a/src/tscpp/api/RemapPlugin.cc b/src/tscpp/api/RemapPlugin.cc
index 7be742ca0..2e9bb1e63 100644
--- a/src/tscpp/api/RemapPlugin.cc
+++ b/src/tscpp/api/RemapPlugin.cc
@@ -30,12 +30,9 @@ using namespace atscppapi;
 TSRemapStatus
 TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
 {
-  RemapPlugin *remap_plugin = static_cast<RemapPlugin *>(ih);
-  Url map_from_url(rri->requestBufp, rri->mapFromUrl), map_to_url(rri->requestBufp, rri->mapToUrl);
+  RemapPlugin *remap_plugin  = static_cast<RemapPlugin *>(ih);
   Transaction &transaction   = utils::internal::getTransaction(rh);
-  bool redirect              = false;
-  RemapPlugin::Result result = remap_plugin->doRemap(map_from_url, map_to_url, transaction, redirect);
-  rri->redirect              = redirect ? 1 : 0;
+  RemapPlugin::Result result = remap_plugin->remapTransaction(transaction, rri);
   switch (result) {
   case RemapPlugin::RESULT_ERROR:
     return TSREMAP_ERROR;
-- 
2.20.1

Reply via email to