trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [trafficserver] branch master updated: X-Debug: log-headers
Date Wed, 07 Feb 2018 22:35:24 GMT
This is an automated email from the ASF dual-hosted git repository.

paziz 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 8defb3c  X-Debug: log-headers
8defb3c is described below

commit 8defb3cafe957f0ae39d297db4362f8fd57a63e0
Author: Aaron Canary <acanary@oath.com>
AuthorDate: Wed Feb 7 12:09:49 2018 -0600

    X-Debug: log-headers
    
    This tag will make the plugin output all headers to be logged for all ClientRequest, ServerRequest,
ServerResponse and ClientResponse.
---
 plugins/xdebug/xdebug.cc | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/plugins/xdebug/xdebug.cc b/plugins/xdebug/xdebug.cc
index 223043d..82ddef9 100644
--- a/plugins/xdebug/xdebug.cc
+++ b/plugins/xdebug/xdebug.cc
@@ -17,14 +17,18 @@
  */
 
 #include <cstdlib>
+#include <stdio.h>
 #include <cstdio>
 #include <strings.h>
+#include <sstream>
 #include <cstring>
 #include <getopt.h>
 
 #include "ts/ts.h"
 #include "ts/ink_defs.h"
 
+#define DEBUG_TAG_LOG_HEADERS "xdebug.headers"
+
 static struct {
   const char *str;
   int len;
@@ -36,6 +40,7 @@ enum {
   XHEADER_X_CACHE          = 0x0010u,
   XHEADER_X_GENERATION     = 0x0020u,
   XHEADER_X_TRANSACTION_ID = 0x0040u,
+  XHEADER_X_DUMP_HEADERS   = 0x0080u,
 };
 
 static int XArgIndex             = 0;
@@ -252,6 +257,44 @@ InjectTxnUuidHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc hdr)
   }
 }
 
+///////////////////////////////////////////////////////////////////////////
+// Dump a header on stderr, useful together with TSDebug().
+void
+log_headers(TSHttpTxn txn, TSMBuffer bufp, TSMLoc hdr_loc, const char *msg_type)
+{
+  TSIOBuffer output_buffer;
+  TSIOBufferReader reader;
+  TSIOBufferBlock block;
+  const char *block_start;
+  int64_t block_avail;
+
+  std::stringstream ss;
+  ss << "TxnID:" << TSHttpTxnIdGet(txn) << " " << msg_type <<
" Headers are...";
+
+  output_buffer = TSIOBufferCreate();
+  reader        = TSIOBufferReaderAlloc(output_buffer);
+
+  /* This will print  just MIMEFields and not the http request line */
+  TSMimeHdrPrint(bufp, hdr_loc, output_buffer);
+
+  /* We need to loop over all the buffer blocks, there can be more than 1 */
+  block = TSIOBufferReaderStart(reader);
+  do {
+    block_start = TSIOBufferBlockReadStart(block, reader, &block_avail);
+    if (block_avail > 0) {
+      ss << "\n" << std::string(block_start, static_cast<int>(block_avail));
+    }
+    TSIOBufferReaderConsume(reader, block_avail);
+    block = TSIOBufferReaderStart(reader);
+  } while (block && block_avail != 0);
+
+  /* Free up the TSIOBuffer that we used to print out the header */
+  TSIOBufferReaderFree(reader);
+  TSIOBufferDestroy(output_buffer);
+
+  TSDebug(DEBUG_TAG_LOG_HEADERS, "%s", ss.str().c_str());
+}
+
 static int
 XInjectResponseHeaders(TSCont /* contp */, TSEvent event, void *edata)
 {
@@ -291,6 +334,10 @@ XInjectResponseHeaders(TSCont /* contp */, TSEvent event, void *edata)
     InjectTxnUuidHeader(txn, buffer, hdr);
   }
 
+  if (xheaders & XHEADER_X_DUMP_HEADERS) {
+    log_headers(txn, buffer, hdr, "ClientResponse");
+  }
+
 done:
   TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
   return TS_EVENT_NONE;
@@ -347,6 +394,45 @@ XScanRequestHeaders(TSCont /* contp */, TSEvent event, void *edata)
       } else if (header_field_eq("diags", value, vsize)) {
         // Enable diagnostics for DebugTxn()'s only
         TSHttpTxnDebugSet(txn, 1);
+      } else if (header_field_eq("log-headers", value, vsize) && TSIsDebugTagSet(DEBUG_TAG_LOG_HEADERS))
{
+        xheaders |= XHEADER_X_DUMP_HEADERS;
+        log_headers(txn, buffer, hdr, "ClientRequest");
+
+        // dump on server request
+        auto send_req_dump = [](TSCont /* contp */, TSEvent event, void *edata) -> int
{
+          TSHttpTxn txn = (TSHttpTxn)edata;
+          TSMBuffer buffer;
+          TSMLoc hdr;
+          if (TSHttpTxnServerReqGet(txn, &buffer, &hdr) == TS_SUCCESS) {
+            // re-add header "X-Debug: log-headers", but only once
+            TSMLoc dst = TSMimeHdrFieldFind(buffer, hdr, xDebugHeader.str, xDebugHeader.len);
+            if (dst == TS_NULL_MLOC) {
+              if (TSMimeHdrFieldCreateNamed(buffer, hdr, xDebugHeader.str, xDebugHeader.len,
&dst) == TS_SUCCESS) {
+                TSReleaseAssert(TSMimeHdrFieldAppend(buffer, hdr, dst) == TS_SUCCESS);
+                TSReleaseAssert(TSMimeHdrFieldValueStringInsert(buffer, hdr, dst, 0 /* idx
*/, "log-headers",
+                                                                lengthof("log-headers"))
== TS_SUCCESS);
+                log_headers(txn, buffer, hdr, "ServerRequest");
+              }
+            }
+          }
+          TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
+          return TS_EVENT_NONE;
+        };
+        TSHttpTxnHookAdd(txn, TS_HTTP_SEND_REQUEST_HDR_HOOK, TSContCreate(send_req_dump,
nullptr));
+
+        // dump on server response
+        auto read_resp_dump = [](TSCont /* contp */, TSEvent event, void *edata) -> int
{
+          TSHttpTxn txn = (TSHttpTxn)edata;
+          TSMBuffer buffer;
+          TSMLoc hdr;
+          if (TSHttpTxnServerRespGet(txn, &buffer, &hdr) == TS_SUCCESS) {
+            log_headers(txn, buffer, hdr, "ServerResponse");
+          }
+          TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
+          return TS_EVENT_NONE;
+        };
+        TSHttpTxnHookAdd(txn, TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(read_resp_dump,
nullptr));
+
       } else {
         TSDebug("xdebug", "ignoring unrecognized debug tag '%.*s'", vsize, value);
       }

-- 
To stop receiving notification emails like this one, please contact
paziz@apache.org.

Mime
View raw message