trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bri...@apache.org
Subject [1/2] trafficserver git commit: Adding example remap plugin for adding headers on remap
Date Tue, 10 Feb 2015 00:22:56 GMT
Repository: trafficserver
Updated Branches:
  refs/heads/master 2dbdd9c26 -> ce74235e3


Adding example remap plugin for adding headers on remap


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/1c1a72a6
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/1c1a72a6
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/1c1a72a6

Branch: refs/heads/master
Commit: 1c1a72a69d82cc4d5ac264c54ae825a6db3b03cf
Parents: d777303
Author: Brian Geffon <briang@apache.org>
Authored: Mon Feb 9 16:16:32 2015 -0800
Committer: Brian Geffon <briang@apache.org>
Committed: Mon Feb 9 16:16:32 2015 -0800

----------------------------------------------------------------------
 example/Makefile.am                          |   2 +
 example/remap_header_add/build.txt           |  17 +++
 example/remap_header_add/remap_header_add.cc | 163 ++++++++++++++++++++++
 3 files changed, 182 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1c1a72a6/example/Makefile.am
----------------------------------------------------------------------
diff --git a/example/Makefile.am b/example/Makefile.am
index 47c1d90..db09497 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -41,6 +41,7 @@ plugins = \
   secure-link.la \
   server-transform.la \
   ssl-preaccept.la \
+  remap_header_add.la \
   ssl-sni.la \
   ssl-sni-whitelist.la \
   thread-1.la \
@@ -69,6 +70,7 @@ protocol_la_SOURCES = protocol/Protocol.c protocol/TxnSM.c
 query_remap_la_SOURCES = query-remap/query-remap.c
 remap_la_SOURCES = remap/remap.cc
 replace_header_la_SOURCES = replace-header/replace-header.c
+remap_header_add_la_SOURCES = remap_header_add/remap_header_add.cc
 response_header_1_la_SOURCES = response-header-1/response-header-1.c
 server_transform_la_SOURCES = server-transform/server-transform.c
 thread_1_la_SOURCES = thread-1/thread-1.c

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1c1a72a6/example/remap_header_add/build.txt
----------------------------------------------------------------------
diff --git a/example/remap_header_add/build.txt b/example/remap_header_add/build.txt
new file mode 100755
index 0000000..c9a87ad
--- /dev/null
+++ b/example/remap_header_add/build.txt
@@ -0,0 +1,17 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+gcc -D_FILE_OFFSET_BITS=64 -fPIC -shared -I../../include -I../../.. -I./ -g -o remap_header_add.so
remap_header_add.cc

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1c1a72a6/example/remap_header_add/remap_header_add.cc
----------------------------------------------------------------------
diff --git a/example/remap_header_add/remap_header_add.cc b/example/remap_header_add/remap_header_add.cc
new file mode 100644
index 0000000..1fad637
--- /dev/null
+++ b/example/remap_header_add/remap_header_add.cc
@@ -0,0 +1,163 @@
+/** @file
+
+  A simple remap plugin for ATS
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+  @section description
+    This is a very simple plugin: it will add headers that are specified on a remap line
+
+    Example usage:
+    map /foo http://127.0.0.1/ @plugin=remap_header_add.so @pparam=foo:"x" @pparam=@test:"c"
@pparam=a:"b"
+
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ts/ts.h"
+#include "ts/remap.h"
+
+struct remap_line {
+  int argc;
+  char **argv; // store the originals
+
+  int nvc; // the number of name value pairs, should be argc - 2.
+  char **name; // at load we will parse out the name and values.
+  char **val;
+};
+
+#define TAG "headeradd_remap"
+#define NOWARN_UNUSED __attribute__ ((unused))
+#define EXTERN extern "C"
+
+EXTERN void ParseArgIntoNv(const char *arg, char **n, char **v) {
+  const char *colon_pos = strchr(arg, ':');
+  if (colon_pos == NULL) {
+    *n = NULL;
+    *v = NULL;
+    TSDebug(TAG, "No name value pair since it was malformed");
+    return;
+  }
+
+  size_t name_len = colon_pos - arg;
+  *n = (char *) TSmalloc(name_len + 1);
+  memcpy(*n, arg, colon_pos - arg);
+  (*n)[name_len] = '\0';
+
+  size_t val_len = strlen(colon_pos + 1); // skip past the ':'
+
+  // check to see if the value is quoted.
+  if (val_len > 1 && colon_pos[1] == '"' && colon_pos[val_len] == '"')
{
+    colon_pos++; // advance past the first quote
+    val_len -= 2; // don't include the trailing quote
+  }
+
+  *v = (char *) TSmalloc(val_len + 1);
+  memcpy(*v, colon_pos + 1, val_len);
+  (*v)[val_len] = '\0';
+
+  TSDebug(TAG, "\t name_len=%d, val_len=%d, %s=%s", name_len, val_len, *n, *v);
+}
+
+TSReturnCode TSRemapInit(NOWARN_UNUSED TSRemapInterface *api_info, NOWARN_UNUSED char *errbuf,
NOWARN_UNUSED int errbuf_size) {
+  return TS_SUCCESS;
+}
+
+TSReturnCode TSRemapNewInstance(int argc, char* argv[], void** ih, char* errbuf, int errbuf_size)
{
+  remap_line *rl = NULL;
+
+  TSDebug(TAG, "TSRemapNewInstance()");
+
+  if (!argv || !ih) {
+    TSError(TAG ": Unable to load plugin because missing argv or ih.");
+    return TS_ERROR;
+  }
+
+  // print all arguments for this particular remapping
+
+  rl = (remap_line*) TSmalloc(sizeof(remap_line));
+  rl->argc = argc;
+  rl->argv = argv;
+  rl->nvc = argc - 2; // the first two are the remap from and to
+  if (rl->nvc) {
+    rl->name = (char**) TSmalloc(sizeof(char *) * rl->nvc);
+    rl->val = (char**) TSmalloc(sizeof(char *) * rl->nvc);
+  }
+
+  TSDebug(TAG, "NewInstance:");
+  for (int i = 2; i < argc; i++) {
+    ParseArgIntoNv(argv[i], &rl->name[i-2], &rl->val[i-2]);
+  }
+
+  *ih = (void *) rl;
+
+  return TS_SUCCESS;
+}
+
+void TSRemapDeleteInstance(void* ih)
+{
+  TSDebug(TAG, "deleting instance %p", ih);
+
+  if (ih) {
+    remap_line *rl = (remap_line*)ih;
+    for (int i = 0; i < rl->nvc; ++i)
+    {
+      TSfree(rl->name[i]);
+      TSfree(rl->val[i]);
+    }
+
+    TSfree(rl->name);
+    TSfree(rl->val);
+  }
+}
+
+TSRemapStatus TSRemapDoRemap(void* ih, NOWARN_UNUSED TSHttpTxn txn, NOWARN_UNUSED TSRemapRequestInfo
*rri) {
+  remap_line *rl = (remap_line *) ih;
+
+  if (!rl || !rri) {
+    TSError(TAG ": rl or rri is null.");
+    return TSREMAP_NO_REMAP;
+  }
+
+  TSDebug(TAG, "TSRemapDoRemap:");
+
+  TSMBuffer req_bufp;
+  TSMLoc req_loc;
+  if (TSHttpTxnClientReqGet(txn, &req_bufp, &req_loc) != TS_SUCCESS) {
+    TSError(TAG ": Error while retrieving client request header");
+    return TSREMAP_NO_REMAP;
+  }
+
+  for (int i = 0; i < rl->nvc; ++i) {
+    TSDebug(TAG, "Attaching header \"%s\" with value \"%s\".", rl->name[i], rl->val[i]);
+
+    TSMLoc field_loc;
+    if (TSMimeHdrFieldCreate(req_bufp, req_loc, &field_loc) == TS_SUCCESS) {
+        TSMimeHdrFieldNameSet(req_bufp, req_loc, field_loc, rl->name[i], strlen(rl->name[i]));
+        TSMimeHdrFieldAppend(req_bufp, req_loc, field_loc);
+        TSMimeHdrFieldValueStringInsert(req_bufp, req_loc, field_loc, 0,  rl->val[i],
strlen(rl->val[i]));
+    } else {
+      TSError(TAG ": Failure on TSMimeHdrFieldCreate");
+    }
+  }
+  return TSREMAP_NO_REMAP;
+}


Mime
View raw message