You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2014/03/29 00:31:28 UTC

[06/13] git commit: TS-2675: metalink: Reuse TSCacheWrite() vs. TSTransformCreate() continuation

TS-2675: metalink: Reuse TSCacheWrite() vs. TSTransformCreate() continuation


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

Branch: refs/heads/master
Commit: d25b00c2f82701f8de51cc96d941049c2f63b739
Parents: 441c1c5
Author: Jack Bates <ja...@nottheoilrig.com>
Authored: Thu Mar 13 11:57:17 2014 -0700
Committer: James Peach <jp...@apache.org>
Committed: Fri Mar 28 16:23:54 2014 -0700

----------------------------------------------------------------------
 plugins/experimental/metalink/metalink.cc | 166 +++++++++++++------------
 1 file changed, 87 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d25b00c2/plugins/experimental/metalink/metalink.cc
----------------------------------------------------------------------
diff --git a/plugins/experimental/metalink/metalink.cc b/plugins/experimental/metalink/metalink.cc
index 238bbee..77f62dc 100644
--- a/plugins/experimental/metalink/metalink.cc
+++ b/plugins/experimental/metalink/metalink.cc
@@ -49,16 +49,20 @@
  *
  * [wiki page]  https://cwiki.apache.org/confluence/display/TS/Metalink */
 
-/* TSVConnWrite() data: Store the request URL */
+/* TSCacheWrite() and TSVConnWrite() data: Write the digest to the cache and
+ * store the request URL at that key */
 
 typedef struct {
+  TSHttpTxn txnp;
+
+  TSCacheKey key;
+
   TSVConn connp;
   TSIOBuffer cache_bufp;
 
 } WriteData;
 
-/* TSTransformCreate() and TSCacheWrite() data: Compute the SHA-256 digest of
- * the content and write it to the cache */
+/* TSTransformCreate() data: Compute the SHA-256 digest of the content */
 
 typedef struct {
   TSHttpTxn txnp;
@@ -70,8 +74,6 @@ typedef struct {
   /* Message digest handle */
   SHA256_CTX c;
 
-  TSCacheKey key;
-
 } TransformData;
 
 /* TSCacheRead() and TSVConnRead() data: Check the "Location: ..." and
@@ -102,43 +104,7 @@ typedef struct {
 
 /* Implement TS_HTTP_READ_RESPONSE_HDR_HOOK to implement a null transform */
 
-/* Store the request URL */
-
-static int
-write_vconn_write_complete(TSCont contp, void * /* edata ATS_UNUSED */)
-{
-  WriteData *data = (WriteData *) TSContDataGet(contp);
-  TSContDestroy(contp);
-
-  /* The object is not committed to the cache until the vconnection is closed.
-   * When all the data has been transferred, the user (contp) must do a
-   * TSVConnClose() */
-  TSVConnClose(data->connp);
-
-  TSIOBufferDestroy(data->cache_bufp);
-  TSfree(data);
-
-  return 0;
-}
-
-/* TSVConnWrite() handler: Store the request URL */
-
-static int
-write_handler(TSCont contp, TSEvent event, void *edata)
-{
-  switch (event) {
-  case TS_EVENT_VCONN_WRITE_COMPLETE:
-    return write_vconn_write_complete(contp, edata);
-
-  default:
-    TSAssert(!"Unexpected event");
-  }
-
-  return 0;
-}
-
-/* Compute the SHA-256 digest of the content, write it to the cache and store
- * the request URL at that key */
+/* Write the digest to the cache and store the request URL at that key */
 
 static int
 cache_open_write(TSCont contp, void *edata)
@@ -151,22 +117,26 @@ cache_open_write(TSCont contp, void *edata)
   const char *value;
   int length;
 
-  TransformData *transform_data = (TransformData *) TSContDataGet(contp);
-  TSContDestroy(contp);
+  WriteData *data = (WriteData *) TSContDataGet(contp);
+  data->connp = (TSVConn) edata;
 
-  TSCacheKeyDestroy(transform_data->key);
+  TSCacheKeyDestroy(data->key);
 
-  if (TSHttpTxnClientReqGet(transform_data->txnp, &req_bufp, &hdr_loc) != TS_SUCCESS) {
+  if (TSHttpTxnClientReqGet(data->txnp, &req_bufp, &hdr_loc) != TS_SUCCESS) {
     TSError("Couldn't retrieve client request header");
 
-    TSfree(transform_data);
+    TSContDestroy(contp);
+
+    TSfree(data);
 
     return 0;
   }
 
-  TSfree(transform_data);
-
   if (TSHttpHdrUrlGet(req_bufp, hdr_loc, &url_loc) != TS_SUCCESS) {
+    TSContDestroy(contp);
+
+    TSfree(data);
+
     TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, hdr_loc);
 
     return 0;
@@ -174,6 +144,10 @@ cache_open_write(TSCont contp, void *edata)
 
   value = TSUrlStringGet(req_bufp, url_loc, &length);
   if (!value) {
+    TSContDestroy(contp);
+
+    TSfree(data);
+
     TSHandleMLocRelease(req_bufp, hdr_loc, url_loc);
     TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, hdr_loc);
 
@@ -185,20 +159,13 @@ cache_open_write(TSCont contp, void *edata)
 
   /* Store the request URL */
 
-  WriteData *write_data = (WriteData *) TSmalloc(sizeof(WriteData));
-  write_data->connp = (TSVConn) edata;
-
-  /* Can't reuse TSTransformCreate() continuation because it already implements
-   * TS_EVENT_VCONN_WRITE_COMPLETE */
-  contp = TSContCreate(write_handler, NULL);
-  TSContDataSet(contp, write_data);
-
-  write_data->cache_bufp = TSIOBufferCreate();
-  TSIOBufferReader readerp = TSIOBufferReaderAlloc(write_data->cache_bufp);
+  data->cache_bufp = TSIOBufferCreate();
+  TSIOBufferReader readerp = TSIOBufferReaderAlloc(data->cache_bufp);
 
-  int nbytes = TSIOBufferWrite(write_data->cache_bufp, value, length);
+  int nbytes = TSIOBufferWrite(data->cache_bufp, value, length);
 
-  TSVConnWrite(write_data->connp, contp, readerp, nbytes);
+  /* Reuse the TSCacheWrite() continuation */
+  TSVConnWrite(data->connp, contp, readerp, nbytes);
 
   return 0;
 }
@@ -208,7 +175,7 @@ cache_open_write(TSCont contp, void *edata)
 static int
 cache_open_write_failed(TSCont contp, void * /* edata ATS_UNUSED */)
 {
-  TransformData *data = (TransformData *) TSContDataGet(contp);
+  WriteData *data = (WriteData *) TSContDataGet(contp);
   TSContDestroy(contp);
 
   TSCacheKeyDestroy(data->key);
@@ -217,6 +184,46 @@ cache_open_write_failed(TSCont contp, void * /* edata ATS_UNUSED */)
   return 0;
 }
 
+static int
+write_vconn_write_complete(TSCont contp, void * /* edata ATS_UNUSED */)
+{
+  WriteData *data = (WriteData *) TSContDataGet(contp);
+  TSContDestroy(contp);
+
+  /* The object is not committed to the cache until the vconnection is closed.
+   * When all the data has been transferred, the user (contp) must do a
+   * TSVConnClose() */
+  TSVConnClose(data->connp);
+
+  TSIOBufferDestroy(data->cache_bufp);
+  TSfree(data);
+
+  return 0;
+}
+
+/* TSCacheWrite() and TSVConnWrite() handler: Write the digest to the cache and
+ * store the request URL at that key */
+
+static int
+write_handler(TSCont contp, TSEvent event, void *edata)
+{
+  switch (event) {
+  case TS_EVENT_CACHE_OPEN_WRITE:
+    return cache_open_write(contp, edata);
+
+  case TS_EVENT_CACHE_OPEN_WRITE_FAILED:
+    return cache_open_write_failed(contp, edata);
+
+  case TS_EVENT_VCONN_WRITE_COMPLETE:
+    return write_vconn_write_complete(contp, edata);
+
+  default:
+    TSAssert(!"Unexpected event");
+  }
+
+  return 0;
+}
+
 /* Copy content from the input buffer to the output buffer without modification
  * while at the same time feeding it to the message digest */
 
@@ -335,40 +342,41 @@ transform_vconn_write_complete(TSCont contp, void * /* edata ATS_UNUSED */)
 {
   char digest[32];
 
-  TransformData *data = (TransformData *) TSContDataGet(contp);
+  TransformData *transform_data = (TransformData *) TSContDataGet(contp);
+  TSContDestroy(contp);
 
-  TSIOBufferDestroy(data->output_bufp);
+  TSIOBufferDestroy(transform_data->output_bufp);
 
-  SHA256_Final((unsigned char *) digest, &data->c);
+  SHA256_Final((unsigned char *) digest, &transform_data->c);
 
-  data->key = TSCacheKeyCreate();
-  if (TSCacheKeyDigestSet(data->key, digest, sizeof(digest)) != TS_SUCCESS) {
-    TSContDestroy(contp);
+  WriteData *write_data = (WriteData *) TSmalloc(sizeof(WriteData));
+  write_data->txnp = transform_data->txnp;
 
-    TSCacheKeyDestroy(data->key);
-    TSfree(data);
+  TSfree(transform_data);
+
+  write_data->key = TSCacheKeyCreate();
+  if (TSCacheKeyDigestSet(write_data->key, digest, sizeof(digest)) != TS_SUCCESS) {
+
+    TSCacheKeyDestroy(write_data->key);
+    TSfree(write_data);
 
     return 0;
   }
 
-  /* Reuse the TSTransformCreate() continuation */
-  TSCacheWrite(contp, data->key);
+  contp = TSContCreate(write_handler, NULL);
+  TSContDataSet(contp, write_data);
+
+  TSCacheWrite(contp, write_data->key);
 
   return 0;
 }
 
-/* TSTransformCreate() and TSCacheWrite() handler */
+/* TSTransformCreate() handler: Compute the SHA-256 digest of the content */
 
 static int
 transform_handler(TSCont contp, TSEvent event, void *edata)
 {
   switch (event) {
-  case TS_EVENT_CACHE_OPEN_WRITE:
-    return cache_open_write(contp, edata);
-
-  case TS_EVENT_CACHE_OPEN_WRITE_FAILED:
-    return cache_open_write_failed(contp, edata);
-
   case TS_EVENT_IMMEDIATE:
   case TS_EVENT_VCONN_WRITE_READY:
     return vconn_write_ready(contp, edata);