<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[117022] trunk/base/src/pextlib1.0/curl.c</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="https://trac.macports.org/changeset/117022">117022</a></dd>
<dt>Author</dt> <dd>cal@macports.org</dd>
<dt>Date</dt> <dd>2014-02-12 15:55:31 -0800 (Wed, 12 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>pextlib1.0/curl.c: Use only a single cURL handle

 - This change makes this code no longer thread safe, which is not a problem
   because we're not using it from different threads anyway.
 - The change also makes cURL re-use connections with HTTP Keep-Alive, speeding
   up downloading binary packages and their checksum. For binary-only
   installations of small packages the installation is often fast enough to
   re-use the same connection for the next port, too.
 - This creates a (delibarate) memory leak, since the curl handle is never
   cleaned up.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbasesrcpextlib10curlc">trunk/base/src/pextlib1.0/curl.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbasesrcpextlib10curlc"></a>
<div class="modfile"><h4>Modified: trunk/base/src/pextlib1.0/curl.c (117021 => 117022)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/base/src/pextlib1.0/curl.c        2014-02-12 23:36:40 UTC (rev 117021)
+++ trunk/base/src/pextlib1.0/curl.c        2014-02-12 23:55:31 UTC (rev 117022)
</span><span class="lines">@@ -63,6 +63,13 @@
</span><span class="cx"> #pragma mark Definitions
</span><span class="cx"> 
</span><span class="cx"> /* ------------------------------------------------------------------------- **
</span><ins>+ * Global cURL handle
+ * ------------------------------------------------------------------------- */
+/* we use a single global handle rather than creating and destroying handles to
+ * take advantage of HTTP pipelining, especially to the packages servers. */
+static CURL* theHandle = NULL;
+
+/* ------------------------------------------------------------------------- **
</ins><span class="cx">  * Prototypes
</span><span class="cx">  * ------------------------------------------------------------------------- */
</span><span class="cx"> int SetResultFromCurlErrorCode(Tcl_Interp* interp, CURLcode inErrorCode);
</span><span class="lines">@@ -115,7 +122,6 @@
</span><span class="cx"> CurlFetchCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
</span><span class="cx"> {
</span><span class="cx">         int theResult = TCL_OK;
</span><del>-        CURL* theHandle = NULL;
</del><span class="cx">         FILE* theFile = NULL;
</span><span class="cx">         bool performFailed = false;
</span><span class="cx">         char theErrorString[CURL_ERROR_SIZE];
</span><span class="lines">@@ -221,7 +227,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* Open the file */
</span><del>-                theFile = fopen( theFilePath, &quot;w&quot; );
</del><ins>+                theFile = fopen(theFilePath, &quot;w&quot;);
</ins><span class="cx">                 if (theFile == NULL) {
</span><span class="cx">                         Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE);
</span><span class="cx">                         theResult = TCL_ERROR;
</span><span class="lines">@@ -229,7 +235,12 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* Create the CURL handle */
</span><del>-                theHandle = curl_easy_init();
</del><ins>+                if (theHandle == NULL) {
+                        /* Re-use existing handle if theHandle isn't NULL */
+                        theHandle = curl_easy_init();
+                }
+                /* If we're re-using a handle, the previous call did ensure to reset it
+                 * to the default state using curl_easy_reset(3) */
</ins><span class="cx"> 
</span><span class="cx">                 /* Setup the handle */
</span><span class="cx">                 theCurlCode = curl_easy_setopt(theHandle, CURLOPT_URL, theURL);
</span><span class="lines">@@ -388,7 +399,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* close the file */
</span><del>-                (void) fclose( theFile );
</del><ins>+                (void) fclose(theFile);
</ins><span class="cx">                 theFile = NULL;
</span><span class="cx"> 
</span><span class="cx"> #if LIBCURL_VERSION_NUM == 0x070d01 /* work around broken Tiger version of cURL */
</span><span class="lines">@@ -405,21 +416,21 @@
</span><span class="cx">                                 theResult = TCL_ERROR;
</span><span class="cx">                                 break;
</span><span class="cx">                         }
</span><del>-                        theFile = fopen( theFilePath, &quot;r&quot;);
</del><ins>+                        theFile = fopen(theFilePath, &quot;r&quot;);
</ins><span class="cx">                         if (theFile == NULL) {
</span><span class="cx">                                 Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE);
</span><span class="cx">                                 theResult = TCL_ERROR;
</span><span class="cx">                                 break;
</span><span class="cx">                         }
</span><del>-                        if ( (p = fgets(buf, BUFSIZ, theFile)) != NULL) {
</del><ins>+                        if ((p = fgets(buf, BUFSIZ, theFile)) != NULL) {
</ins><span class="cx">                                 /* skip stray header escaping into output */
</span><span class="cx">                                 if (strncmp(p, &quot;Last-Modified:&quot;, 14) != 0)
</span><span class="cx">                                         rewind(theFile);
</span><span class="cx">                         }
</span><del>-                        while ( (size = fread(buf, 1, BUFSIZ, theFile)) &gt; 0) {
</del><ins>+                        while ((size = fread(buf, 1, BUFSIZ, theFile)) &gt; 0) {
</ins><span class="cx">                                 fwrite(buf, 1, size, fp);
</span><span class="cx">                         }
</span><del>-                        (void) fclose( theFile );
</del><ins>+                        (void) fclose(theFile);
</ins><span class="cx">                         theFile = NULL;
</span><span class="cx">                         fclose(fp);
</span><span class="cx">                         if (rename(tmp, theFilePath) != 0) {
</span><span class="lines">@@ -450,10 +461,6 @@
</span><span class="cx">                                 (effectiveURL == NULL || theCurlCode != CURLE_OK) ? &quot;&quot; : effectiveURL,
</span><span class="cx">                                 0);
</span><span class="cx">                 }
</span><del>-
-                /* clean up */
-                curl_easy_cleanup( theHandle );
-                theHandle = NULL;
</del><span class="cx">         } while (0);
</span><span class="cx"> 
</span><span class="cx">         if (performFailed) {
</span><span class="lines">@@ -461,11 +468,12 @@
</span><span class="cx">                 theResult = TCL_ERROR;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        /* reset the connection */
</ins><span class="cx">         if (theHandle != NULL) {
</span><del>-                curl_easy_cleanup( theHandle );
</del><ins>+                curl_easy_reset(theHandle);
</ins><span class="cx">         }
</span><span class="cx">         if (theFile != NULL) {
</span><del>-                fclose( theFile );
</del><ins>+                fclose(theFile);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return theResult;
</span><span class="lines">@@ -511,7 +519,7 @@
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         optioncrsr++;
</span><del>-                }
</del><ins>+                }
</ins><span class="cx"> 
</span><span class="cx">                 if (optioncrsr &lt;= lastoption) {
</span><span class="cx">                         /* something went wrong */
</span><span class="lines">@@ -535,7 +543,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* Open the file (dev/null) */
</span><del>-                theFile = fopen( &quot;/dev/null&quot;, &quot;a&quot; );
</del><ins>+                theFile = fopen(&quot;/dev/null&quot;, &quot;a&quot;);
</ins><span class="cx">                 if (theFile == NULL) {
</span><span class="cx">                         Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE);
</span><span class="cx">                         theResult = TCL_ERROR;
</span><span class="lines">@@ -543,7 +551,12 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* Create the CURL handle */
</span><del>-                theHandle = curl_easy_init();
</del><ins>+                if (theHandle == NULL) {
+                        /* Re-use existing handle if theHandle isn't NULL */
+                        theHandle = curl_easy_init();
+                }
+                /* If we're re-using a handle, the previous call did ensure to reset it
+                 * to the default state using curl_easy_reset(3) */
</ins><span class="cx"> 
</span><span class="cx">                 /* Setup the handle */
</span><span class="cx">                 theCurlCode = curl_easy_setopt(theHandle, CURLOPT_URL, theURL);
</span><span class="lines">@@ -656,7 +669,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* close the file */
</span><del>-                (void) fclose( theFile );
</del><ins>+                (void) fclose(theFile);
</ins><span class="cx">                 theFile = NULL;
</span><span class="cx"> 
</span><span class="cx">                 /* check everything went fine */
</span><span class="lines">@@ -676,10 +689,6 @@
</span><span class="cx">                                 break;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        /* clean up */
-                        curl_easy_cleanup( theHandle );
-                        theHandle = NULL;
-
</del><span class="cx">                         /* compare this with the date provided by user */
</span><span class="cx">                         if (theModDate &lt; -1) {
</span><span class="cx">                                 Tcl_SetResult(interp, &quot;Couldn't get resource modification date&quot;, TCL_STATIC);
</span><span class="lines">@@ -695,8 +704,9 @@
</span><span class="cx">                 }
</span><span class="cx">         } while (0);
</span><span class="cx"> 
</span><ins>+        /* reset the connection */
</ins><span class="cx">         if (theHandle != NULL) {
</span><del>-                curl_easy_cleanup(theHandle);
</del><ins>+                curl_easy_reset(theHandle);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (theFile != NULL) {
</span><span class="lines">@@ -763,7 +773,7 @@
</span><span class="cx">                 theURL = Tcl_GetString(objv[objc - 1]);
</span><span class="cx"> 
</span><span class="cx">                 /* Open the file (dev/null) */
</span><del>-                theFile = fopen( &quot;/dev/null&quot;, &quot;a&quot; );
</del><ins>+                theFile = fopen(&quot;/dev/null&quot;, &quot;a&quot;);
</ins><span class="cx">                 if (theFile == NULL) {
</span><span class="cx">                         Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE);
</span><span class="cx">                         theResult = TCL_ERROR;
</span><span class="lines">@@ -771,7 +781,12 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* Create the CURL handle */
</span><del>-                theHandle = curl_easy_init();
</del><ins>+                if (theHandle == NULL) {
+                        /* Re-use existing handle if theHandle isn't NULL */
+                        theHandle = curl_easy_init();
+                }
+                /* If we're re-using a handle, the previous call did ensure to reset it
+                 * to the default state using curl_easy_reset(3) */
</ins><span class="cx"> 
</span><span class="cx">                 /* Setup the handle */
</span><span class="cx">                 theCurlCode = curl_easy_setopt(theHandle, CURLOPT_URL, theURL);
</span><span class="lines">@@ -879,7 +894,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* close the file */
</span><del>-                (void) fclose( theFile );
</del><ins>+                (void) fclose(theFile);
</ins><span class="cx">                 theFile = NULL;
</span><span class="cx"> 
</span><span class="cx">                 theFileSize = 0.0;
</span><span class="lines">@@ -891,17 +906,14 @@
</span><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                /* clean up */
-                curl_easy_cleanup( theHandle );
-                theHandle = NULL;
-
</del><span class="cx">                 (void) snprintf(theSizeString, sizeof(theSizeString),
</span><span class="cx">                         &quot;%.0f&quot;, theFileSize);
</span><span class="cx">                 Tcl_SetResult(interp, theSizeString, TCL_VOLATILE);
</span><span class="cx">         } while (0);
</span><span class="cx"> 
</span><ins>+        /* reset the connection */
</ins><span class="cx">         if (theHandle != NULL) {
</span><del>-                curl_easy_cleanup(theHandle);
</del><ins>+                curl_easy_reset(theHandle);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (theFile != NULL) {
</span><span class="lines">@@ -943,7 +955,7 @@
</span><span class="cx">                 /* Retrieve the post data - it's before the url */
</span><span class="cx">                 thePostData = Tcl_GetString(objv[objc - 2]);
</span><span class="cx">                 /* Open the file (dev/null) */
</span><del>-                theFile = fopen( &quot;/dev/null&quot;, &quot;a&quot; );
</del><ins>+                theFile = fopen(&quot;/dev/null&quot;, &quot;a&quot;);
</ins><span class="cx">                 if (theFile == NULL) {
</span><span class="cx">                         Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE);
</span><span class="cx">                         theResult = TCL_ERROR;
</span><span class="lines">@@ -951,7 +963,12 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* Create the CURL handle */
</span><del>-                theHandle = curl_easy_init();
</del><ins>+                if (theHandle == NULL) {
+                        /* Re-use existing handle if theHandle isn't NULL */
+                        theHandle = curl_easy_init();
+                }
+                /* If we're re-using a handle, the previous call did ensure to reset it
+                 * to the default state using curl_easy_reset(3) */
</ins><span class="cx"> 
</span><span class="cx">                 /* Setup the handle */
</span><span class="cx">                 theCurlCode = curl_easy_setopt(theHandle, CURLOPT_URL, theURL);
</span><span class="lines">@@ -1045,16 +1062,13 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* close the file */
</span><del>-                (void) fclose( theFile );
</del><ins>+                (void) fclose(theFile);
</ins><span class="cx">                 theFile = NULL;
</span><del>-
-                /* clean up */
-                curl_easy_cleanup( theHandle );
-                theHandle = NULL;
</del><span class="cx">         } while (0);
</span><span class="cx"> 
</span><ins>+        /* reset the connection */
</ins><span class="cx">         if (theHandle != NULL) {
</span><del>-                curl_easy_cleanup(theHandle);
</del><ins>+                curl_easy_reset(theHandle);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (theFile != NULL) {
</span></span></pre>
</div>
</div>

</body>
</html>