trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject [trafficserver] branch master updated: TS-4976: Regularize plugins - remap
Date Thu, 27 Apr 2017 02:38:03 GMT
This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

The following commit(s) were added to refs/heads/master by this push:
       new  5b56bf0   TS-4976: Regularize plugins - remap
5b56bf0 is described below

commit 5b56bf014bcfd3e35145cbb05867f6cb389f1dcb
Author: Alan M. Carroll <amc@apache.org>
AuthorDate: Sat Apr 22 12:58:30 2017 -0500

    TS-4976: Regularize plugins - remap
---
 example/remap/remap.cc | 102 +++++++++++++++++--------------------------------
 1 file changed, 35 insertions(+), 67 deletions(-)

diff --git a/example/remap/remap.cc b/example/remap/remap.cc
index ceb4aef..fa43937 100644
--- a/example/remap/remap.cc
+++ b/example/remap/remap.cc
@@ -34,19 +34,14 @@
 #include <cstring>
 #include <cstdarg>
 #include <cerrno>
-#include <pwd.h>
 #include <pthread.h>
-#include <unistd.h>
-#include <dlfcn.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/resource.h>
 
 #include "ts/ink_defs.h"
 #include "ts/ts.h"
 #include "ts/remap.h"
 
+#define PLUGIN_NAME "remap"
+
 class remap_entry
 {
 public:
@@ -137,102 +132,78 @@ store_my_error_message(TSReturnCode retcode, char *err_msg_buf, int
buf_size, co
   return retcode; /* error code here */
 }
 
-void
-TSPluginInit(int argc ATS_UNUSED, const char *argv[] ATS_UNUSED)
-{
-  TSPluginRegistrationInfo info;
-  info.plugin_name   = (char *)"remap_plugin";
-  info.vendor_name   = (char *)"Apache";
-  info.support_email = (char *)"";
-
-  if (TSPluginRegister(&info) != TS_SUCCESS) {
-    TSError("[remap] Plugin registration failed.");
-  }
-  TSDebug("debug-remap", "TSPluginInit: Remap plugin started");
-}
-
 // Plugin initialization code. Called immediately after dlopen() Only once!
 // Can perform internal initialization. For example, pthread_.... initialization.
 /* ------------------------- TSRemapInit ---------------------------------- */
 TSReturnCode
 TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size)
 {
-  fprintf(stderr, "Remap Plugin: TSRemapInit()\n");
+  TSDebug(PLUGIN_NAME, "enter");
 
   if (!plugin_init_counter) {
     if (unlikely(!api_info)) {
-      return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "[TSRemapInit] - Invalid
TSRemapInterface argument");
+      return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "Invalid TSRemapInterface
argument");
     }
     if (unlikely(api_info->size < sizeof(TSRemapInterface))) {
       return store_my_error_message(TS_ERROR, errbuf, errbuf_size,
-                                    "[TSRemapInit] - Incorrect size of TSRemapInterface structure
%d. Should be at least %d bytes",
+                                    "Incorrect size of TSRemapInterface structure %d. Should
be at least %d bytes",
                                     (int)api_info->size, (int)sizeof(TSRemapInterface));
     }
     if (unlikely(api_info->tsremap_version < TSREMAP_VERSION)) {
-      return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "[TSRemapInit] - Incorrect
API version %d.%d",
-                                    (api_info->tsremap_version >> 16), (api_info->tsremap_version
& 0xffff));
+      return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "Incorrect API version
%d.%d", (api_info->tsremap_version >> 16),
+                                    (api_info->tsremap_version & 0xffff));
     }
 
     if (pthread_mutex_init(&remap_plugin_global_mutex, nullptr) ||
         pthread_mutex_init(&remap_entry::mutex, nullptr)) { /* pthread_mutex_init - always
returns 0. :) - impossible error */
-      return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "[TSRemapInit] - Mutex
initialization error");
+      return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "Mutex initialization
error");
     }
     plugin_init_counter++;
   }
   return TS_SUCCESS; /* success */
 }
 
-// Plugin shutdown
-// Optional function.
-/* -------------------------- TSRemapDone --------------------------------- */
-void
-TSRemapDone(void)
-{
-  fprintf(stderr, "Remap Plugin: TSRemapDone()\n");
-}
-
+/* ------------------------ TSRemapNewInstance --------------------------- */
 // Plugin new instance for new remapping rule.
 // This function can be called multiple times (depends on remap.config)
-/* ------------------------ TSRemapNewInstance --------------------------- */
 TSReturnCode
 TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int errbuf_size)
 {
   remap_entry *ri;
   int i;
 
-  fprintf(stderr, "Remap Plugin: TSRemapNewInstance()\n");
+  TSDebug(PLUGIN_NAME, "enter"); // Debug output automatically includes the file, line #,
and function.
 
   if (argc < 2) {
-    return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "[TSRemapNewInstance] -
Incorrect number of arguments - %d", argc);
+    return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "Incorrect number of arguments
- %d", argc);
   }
   if (!argv || !ih) {
-    return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "[TSRemapNewInstance] -
Invalid argument(s)");
+    return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "Invalid argument(s)");
   }
   // print all arguments for this particular remapping
   for (i = 0; i < argc; i++) {
-    fprintf(stderr, "[TSRemapNewInstance] - argv[%d] = \"%s\"\n", i, argv[i]);
+    TSDebug(PLUGIN_NAME, "[%s] - argv[%d] = \"%s\"\n", __func__, i, argv[i]);
   }
 
   ri = new remap_entry(argc, argv);
 
   if (!ri) {
-    return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "[TSRemapNewInstance] -
Can't create remap_entry class");
+    return store_my_error_message(TS_ERROR, errbuf, errbuf_size, "Can't create remap_entry
class");
   }
 
   remap_entry::add_to_list(ri);
 
-  *ih = (void *)ri;
+  *ih = ri;
 
   return TS_SUCCESS;
 }
-
 /* ---------------------- TSRemapDeleteInstance -------------------------- */
 void
 TSRemapDeleteInstance(void *ih)
 {
   remap_entry *ri = (remap_entry *)ih;
 
-  fprintf(stderr, "Remap Plugin: TSRemapDeleteInstance()\n");
+  TSDebug(PLUGIN_NAME, "enter");
 
   remap_entry::remove_from_list(ri);
 
@@ -254,53 +225,53 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
     ++processing_counter; // one more function call (in real life use mutex to protect this
counter)
 
   remap_entry *ri = (remap_entry *)ih;
-  fprintf(stderr, "Remap Plugin: TSRemapDoRemap()\n");
+  TSDebug(PLUGIN_NAME, "enter");
 
   if (!ri || !rri) {
     return TSREMAP_NO_REMAP; /* TS must remap this request */
   }
 
-  fprintf(stderr, "[TSRemapDoRemap] From: \"%s\"  To: \"%s\"\n", ri->argv[0], ri->argv[1]);
+  TSDebug(PLUGIN_NAME, "From: \"%s\"  To: \"%s\"\n", ri->argv[0], ri->argv[1]);
 
   temp = TSUrlHostGet(rri->requestBufp, rri->requestUrl, &len);
-  fprintf(stderr, "[TSRemapDoRemap] Request Host(%d): \"%.*s\"\n", len, len, temp);
+  TSDebug(PLUGIN_NAME, "Request Host(%d): \"%.*s\"\n", len, len, temp);
 
   temp = TSUrlHostGet(rri->requestBufp, rri->mapToUrl, &len);
-  fprintf(stderr, "[TSRemapDoRemap] Remap To Host: \"%.*s\"\n", len, temp);
+  TSDebug(PLUGIN_NAME, "Remap To Host: \"%.*s\"\n", len, temp);
 
   temp = TSUrlHostGet(rri->requestBufp, rri->mapFromUrl, &len);
-  fprintf(stderr, "[TSRemapDoRemap] Remap From Host: \"%.*s\"\n", len, temp);
+  TSDebug(PLUGIN_NAME, "Remap From Host: \"%.*s\"\n", len, temp);
 
-  fprintf(stderr, "[TSRemapDoRemap] Request Port: %d\n", TSUrlPortGet(rri->requestBufp,
rri->requestUrl));
-  fprintf(stderr, "[TSRemapDoRemap] Remap From Port: %d\n", TSUrlPortGet(rri->requestBufp,
rri->mapFromUrl));
-  fprintf(stderr, "[TSRemapDoRemap] Remap To Port: %d\n", TSUrlPortGet(rri->requestBufp,
rri->mapToUrl));
+  TSDebug(PLUGIN_NAME, "Request Port: %d\n", TSUrlPortGet(rri->requestBufp, rri->requestUrl));
+  TSDebug(PLUGIN_NAME, "Remap From Port: %d\n", TSUrlPortGet(rri->requestBufp, rri->mapFromUrl));
+  TSDebug(PLUGIN_NAME, "Remap To Port: %d\n", TSUrlPortGet(rri->requestBufp, rri->mapToUrl));
 
   temp = TSUrlPathGet(rri->requestBufp, rri->requestUrl, &len);
-  fprintf(stderr, "[TSRemapDoRemap] Request Path: \"%.*s\"\n", len, temp);
+  TSDebug(PLUGIN_NAME, "Request Path: \"%.*s\"\n", len, temp);
 
   temp = TSUrlPathGet(rri->requestBufp, rri->mapFromUrl, &len);
-  fprintf(stderr, "[TSRemapDoRemap] Remap From Path: \"%.*s\"\n", len, temp);
+  TSDebug(PLUGIN_NAME, "Remap From Path: \"%.*s\"\n", len, temp);
 
   temp = TSUrlPathGet(rri->requestBufp, rri->mapToUrl, &len);
-  fprintf(stderr, "[TSRemapDoRemap] Remap To Path: \"%.*s\"\n", len, temp);
+  TSDebug(PLUGIN_NAME, "Remap To Path: \"%.*s\"\n", len, temp);
 
   // InkAPI usage case
   const char *value;
 
   if ((cfield = TSMimeHdrFieldFind(rri->requestBufp, rri->requestHdrp, TS_MIME_FIELD_DATE,
-1)) != TS_NULL_MLOC) {
-    fprintf(stderr, "We have \"Date\" header in request\n");
+    TSDebug(PLUGIN_NAME, "We have \"Date\" header in request\n");
     value = TSMimeHdrFieldValueStringGet(rri->requestBufp, rri->requestHdrp, cfield,
-1, nullptr);
-    fprintf(stderr, "Header value: %s\n", value);
+    TSDebug(PLUGIN_NAME, "Header value: %s\n", value);
   }
   if ((cfield = TSMimeHdrFieldFind(rri->requestBufp, rri->requestHdrp, "MyHeader",
sizeof("MyHeader") - 1)) != TS_NULL_MLOC) {
-    fprintf(stderr, "We have \"MyHeader\" header in request\n");
+    TSDebug(PLUGIN_NAME, "We have \"MyHeader\" header in request\n");
     value = TSMimeHdrFieldValueStringGet(rri->requestBufp, rri->requestHdrp, cfield,
-1, nullptr);
-    fprintf(stderr, "Header value: %s\n", value);
+    TSDebug(PLUGIN_NAME, "Header value: %s\n", value);
   }
 
   // How to store plugin private arguments inside Traffic Server request processing block.
   if (TSHttpArgIndexReserve("remap_example", "Example remap plugin", &arg_index) == TS_SUCCESS)
{
-    fprintf(stderr, "[TSRemapDoRemap] Save processing counter %lu inside request processing
block\n", _processing_counter);
+    TSDebug(PLUGIN_NAME, "Save processing counter %lu inside request processing block\n",
_processing_counter);
     TSHttpTxnArgSet((TSHttpTxn)rh, arg_index, (void *)_processing_counter); // save counter
   }
   // How to cancel request processing and return error message to the client
@@ -353,12 +324,9 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
 void
 TSRemapOSResponse(void *ih ATS_UNUSED, TSHttpTxn rh, int os_response_type)
 {
-  int request_id = -1;
   void *data     = TSHttpTxnArgGet((TSHttpTxn)rh, arg_index); // read counter (we store it
in TSRemapDoRemap function call)
+  int request_id = data ? static_cast<int *>(data)[0] : -1;
 
-  if (data) {
-    request_id = *((int *)data);
-  }
-  fprintf(stderr, "[TSRemapOSResponse] Read processing counter %d from request processing
block\n", request_id);
-  fprintf(stderr, "[TSRemapOSResponse] OS response status: %d\n", os_response_type);
+  TSDebug(PLUGIN_NAME, "Read processing counter %d from request processing block\n", request_id);
+  TSDebug(PLUGIN_NAME, "OS response status: %d\n", os_response_type);
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <commits@trafficserver.apache.org>'].

Mime
View raw message