You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2014/04/10 14:54:23 UTC

[3/6] git commit: TS-2692 Add an API to Get the current redirection retry count

TS-2692 Add an API to Get the current redirection retry count


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

Branch: refs/heads/master
Commit: 98c581919958096b7b8a1bfff036bdeff12b7d30
Parents: 6cec1a2
Author: Leif Hedstrom <zw...@apache.org>
Authored: Fri Apr 4 11:55:02 2014 -0600
Committer: Leif Hedstrom <zw...@apache.org>
Committed: Thu Apr 10 06:50:21 2014 -0600

----------------------------------------------------------------------
 plugins/experimental/escalate/escalate.cc | 49 ++++++++++++++------------
 proxy/InkAPI.cc                           | 12 ++++++-
 proxy/api/ts/ts.h                         | 43 ++++++++++++++++++++++
 proxy/http/HttpSM.h                       |  2 +-
 4 files changed, 81 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/98c58191/plugins/experimental/escalate/escalate.cc
----------------------------------------------------------------------
diff --git a/plugins/experimental/escalate/escalate.cc b/plugins/experimental/escalate/escalate.cc
index d57003a..05a8234 100644
--- a/plugins/experimental/escalate/escalate.cc
+++ b/plugins/experimental/escalate/escalate.cc
@@ -66,42 +66,45 @@ EscalateResponse(TSCont cont, TSEvent event, void* edata)
   TSMBuffer        response;
   TSMLoc           resp_hdr;
 
-  TSDebug(PLUGIN_NAME, "Recieved event %d", (int)event);
   TSReleaseAssert(event == TS_EVENT_HTTP_READ_RESPONSE_HDR);
 
   // First, we need the server response ...
   if (TS_SUCCESS == TSHttpTxnServerRespGet(txn, &response, &resp_hdr)) {
-    // Next, the respose status ...
-    TSHttpStatus status = TSHttpHdrStatusGet(response, resp_hdr);
+    int tries = TSRedirectRetriesGet(txn);
 
-    // If we have an escalation URL for this response code, set the redirection URL and force it
-    // to be followed.
-    EscalationState::hostmap_type::iterator entry = es->hostmap.find((unsigned)status);
+    TSDebug(PLUGIN_NAME, "This is try %d", tries);
+    if (0 == tries) { // ToDo: Future support for more than one retry-URL
+      // Next, the response status ...
+      TSHttpStatus status = TSHttpHdrStatusGet(response, resp_hdr);
 
-    if (entry != es->hostmap.end()) {
-      TSMBuffer request;
-      TSMLoc    req_hdr;
+      // If we have an escalation URL for this response code, set the redirection URL and force it
+      // to be followed.
+      EscalationState::hostmap_type::iterator entry = es->hostmap.find((unsigned)status);
 
-      TSDebug(PLUGIN_NAME, "found an escalation entry for HTTP status %u", (unsigned)status);
-      if (TS_SUCCESS == TSHttpTxnClientReqGet(txn, &request, &req_hdr)) {
-        TSMLoc url;
+      if (entry != es->hostmap.end()) {
+        TSMBuffer request;
+        TSMLoc    req_hdr;
 
-        if (TS_SUCCESS == TSHttpHdrUrlGet(request, req_hdr, &url)) {
-          char* url_str;
-          int url_len;
+        TSDebug(PLUGIN_NAME, "Found an entry for HTTP status %u", (unsigned)status);
+        if (TS_SUCCESS == TSHttpTxnClientReqGet(txn, &request, &req_hdr)) {
+          TSMLoc url;
 
-          // Update the request URL with the new Host to try.
-          TSUrlHostSet(request, url, entry->second.c_str(), entry->second.size());
-          url_str = TSUrlStringGet(request, url, &url_len);
+          if (TS_SUCCESS == TSHttpHdrUrlGet(request, req_hdr, &url)) {
+            char* url_str;
+            int url_len;
 
-          TSDebug(PLUGIN_NAME, "Setting new URL to %.*s", url_len, url_str);
-          TSRedirectUrlSet(txn, url_str, url_len); // Transfers ownership
-        }
-        // Release the response MLoc
+            // Update the request URL with the new Host to try.
+            TSUrlHostSet(request, url, entry->second.c_str(), entry->second.size());
+            url_str = TSUrlStringGet(request, url, &url_len);
+
+            TSDebug(PLUGIN_NAME, "Setting new URL to %.*s", url_len, url_str);
+            TSRedirectUrlSet(txn, url_str, url_len); // Transfers ownership
+          }
+          // Release the request MLoc
         TSHandleMLocRelease(request, TS_NULL_MLOC, req_hdr);
+        }
       }
     }
-
     // Release the response MLoc
     TSHandleMLocRelease(response, TS_NULL_MLOC, resp_hdr);
   }

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/98c58191/proxy/InkAPI.cc
----------------------------------------------------------------------
diff --git a/proxy/InkAPI.cc b/proxy/InkAPI.cc
index e4a0b9c..92c37fc 100644
--- a/proxy/InkAPI.cc
+++ b/proxy/InkAPI.cc
@@ -7134,7 +7134,7 @@ TSRedirectUrlSet(TSHttpTxn txnp, const char* url, const int url_len)
     sm->redirect_url_len = 0;
   }
 
-  sm->redirect_url = url;
+  sm->redirect_url = (char*)url;
   sm->redirect_url_len = url_len;
   sm->enable_redirection = true;
 }
@@ -7150,6 +7150,16 @@ TSRedirectUrlGet(TSHttpTxn txnp, int *url_len_ptr)
   return sm->redirect_url;
 }
 
+int
+TSRedirectRetriesGet(TSHttpTxn txnp)
+{
+  sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
+
+  HttpSM *sm = (HttpSM*)txnp;
+
+  return sm->redirection_tries;
+}
+
 char*
 TSFetchRespGet(TSHttpTxn txnp, int *length)
 {

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/98c58191/proxy/api/ts/ts.h
----------------------------------------------------------------------
diff --git a/proxy/api/ts/ts.h b/proxy/api/ts/ts.h
index 99098da..eaafda1 100644
--- a/proxy/api/ts/ts.h
+++ b/proxy/api/ts/ts.h
@@ -2227,9 +2227,52 @@ extern "C"
 
   tsapi TSReturnCode TSHttpTxnConfigFind(const char* name, int length, TSOverridableConfigKey* conf, TSRecordDataType* type);
 
+  /**
+     This API informs the core to try to follow redirections (e.g. 301 responses.
+     The new URL would be provided in the standard Location header.
+
+     @param txnp the transaction pointer
+     @param on   turn this on or off (0 or 1)
+
+     @return @c TS_SUCCESS if it succeeded
+  */
   tsapi TSReturnCode TSHttpTxnFollowRedirect(TSHttpTxn txnp, int on);
+  /**
+     This is a generalization of the TSHttpTxnFollowRedirect(), but gives finer
+     control over the behavior. Instead of using the Location: header for the new
+     destination, this API takes the new URL as a parameter. Calling this API
+     transfers the ownership of the URL from the plugin to the core, so you must
+     make sure it is heap allocated, and that you do not free it.
+
+     Calling this API implicitly also enables the "Follow Redirect" feature, so
+     there is no rason to call TSHttpTxnFollowRedirect() as well.
+
+     @param txnp the transaction pointer
+     @param url  a heap allocated string with the URL
+     @param url_len the length of the URL
+  */
   tsapi void TSRedirectUrlSet(TSHttpTxn txnp, const char* url, const int url_len);
+  /**
+     Return the current (if set) redirection URL string. This is still owned by the
+     core, and must not be free'd.
+
+     @param txnp the transaction pointer
+     @param url_len_ptr a pointer to where the URL length is to be stored
+
+     @return the url string
+  */
   tsapi const char* TSRedirectUrlGet(TSHttpTxn txnp, int* url_len_ptr);
+  /**
+     Return the number of redirection retries we have done. This starts off
+     at zero, and can be used to select different URLs based on which attempt this
+     is. This can be useful for example when providing a list of URLs to try, and
+     do so in order until one succeeds.
+
+     @param txnp the transaction pointer
+
+     @return the redirect try count
+  */
+  tsapi int TSRedirectRetriesGet(TSHttpTxn txnp);
 
   /* Get current HTTP connection stats */
   tsapi int TSHttpCurrentClientConnectionsGet(void);

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/98c58191/proxy/http/HttpSM.h
----------------------------------------------------------------------
diff --git a/proxy/http/HttpSM.h b/proxy/http/HttpSM.h
index a04742d..f0117c0 100644
--- a/proxy/http/HttpSM.h
+++ b/proxy/http/HttpSM.h
@@ -270,7 +270,7 @@ public:
 
   //YTS Team, yamsat Plugin
   bool enable_redirection;      //To check if redirection is enabled
-  const char *redirect_url;     //url for force redirect (provide users a functionality to redirect to another url when needed)
+  char *redirect_url;     //url for force redirect (provide users a functionality to redirect to another url when needed)
   int redirect_url_len;
   int redirection_tries;        //To monitor number of redirections
   int64_t transfered_bytes;     //Added to calculate POST data