From bfabb7bd2b02654e00f5b8d12541e9bec9cbbad7 Mon Sep 17 00:00:00 2001
From: Rider Linden <rider@lindenlab.com>
Date: Fri, 19 Feb 2016 11:19:50 -0800
Subject: [PATCH] MAINT-6137: Re enable pipelining by default, use new version
 of CURL (7.47) with corrections for timed out connections in pipelining.
 Minor fix for safer op retrieval.

---
 autobuild.xml                           | 14 ++++----
 indra/llcorehttp/_httplibcurl.cpp       | 43 ++++++++++++++++++-------
 indra/llcorehttp/_httpoperation.cpp     |  3 ++
 indra/llcorehttp/_httpoprequest.cpp     |  6 +++-
 indra/llcorehttp/_httppolicy.cpp        |  2 +-
 indra/newview/app_settings/settings.xml |  2 +-
 indra/newview/llappcorehttp.cpp         |  4 +--
 7 files changed, 51 insertions(+), 23 deletions(-)

diff --git a/autobuild.xml b/autobuild.xml
index 4e8d92a979f..bee2d29d9f6 100755
--- a/autobuild.xml
+++ b/autobuild.xml
@@ -212,9 +212,9 @@
             <key>archive</key>
             <map>
               <key>hash</key>
-              <string>89db4a1aa22599cf377ae49630b7b5b1</string>
+              <string>aaaedcd3b0b52b65181a64daa091fe10</string>
               <key>url</key>
-              <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/curl_3p-update-curl/rev/301717/arch/Darwin/installer/curl-7.42.1.301717-darwin-301717.tar.bz2</string>
+              <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/3p-curl/rev/311279/arch/Darwin/installer/curl-7.47.0.311279-darwin-311279.tar.bz2</string>
             </map>
             <key>name</key>
             <string>darwin</string>
@@ -224,9 +224,9 @@
             <key>archive</key>
             <map>
               <key>hash</key>
-              <string>de9e0c855ff6ee30c9e027a70bbef032</string>
+              <string>cfe7789e8f76c9781021ab1b9b4ec3c2</string>
               <key>url</key>
-              <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/curl_3p-update-curl/rev/301717/arch/Linux/installer/curl-7.42.1.301717-linux-301717.tar.bz2</string>
+              <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/3p-curl/rev/311279/arch/Linux/installer/curl-7.47.0.311279-linux-311279.tar.bz2</string>
             </map>
             <key>name</key>
             <string>linux</string>
@@ -236,16 +236,16 @@
             <key>archive</key>
             <map>
               <key>hash</key>
-              <string>98d15713de8c439b7f54cc14f2df07ac</string>
+              <string>9109ad560200701443f9b41389044c38</string>
               <key>url</key>
-              <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/curl_3p-update-curl/rev/301717/arch/CYGWIN/installer/curl-7.42.1.301717-windows-301717.tar.bz2</string>
+              <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/3p-curl/rev/311279/arch/CYGWIN/installer/curl-7.47.0.311279-windows-311279.tar.bz2</string>
             </map>
             <key>name</key>
             <string>windows</string>
           </map>
         </map>
         <key>version</key>
-        <string>7.42.1.301717</string>
+        <string>7.47.0.311279</string>
       </map>
       <key>db</key>
       <map>
diff --git a/indra/llcorehttp/_httplibcurl.cpp b/indra/llcorehttp/_httplibcurl.cpp
index d918c2ee1bd..c25e01a318b 100755
--- a/indra/llcorehttp/_httplibcurl.cpp
+++ b/indra/llcorehttp/_httplibcurl.cpp
@@ -301,7 +301,14 @@ bool HttpLibcurl::completeRequest(CURLM * multi_handle, CURL * handle, CURLcode
 {
     HttpHandle ophandle(NULL);
 
-	curl_easy_getinfo(handle, CURLINFO_PRIVATE, &ophandle);
+    CURLcode ccode(CURLE_OK);
+
+    ccode =	curl_easy_getinfo(handle, CURLINFO_PRIVATE, &ophandle);
+    if (ccode)
+    {
+        LL_WARNS(LOG_CORE) << "libcurl error: " << ccode << " Unable to retrieve operation handle from CURL handle" << LL_ENDL;
+        return false;
+    }
     HttpOpRequest::ptr_t op(HttpOpRequest::fromHandle<HttpOpRequest>(ophandle));
     
     if (!op)
@@ -343,22 +350,36 @@ bool HttpLibcurl::completeRequest(CURLM * multi_handle, CURL * handle, CURLcode
 
         if (handle)
         {
-            curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &http_status);
-            if (http_status >= 100 && http_status <= 999)
+            ccode = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &http_status);
+            if (ccode == CURLE_OK)
             {
-                char * cont_type(NULL);
-                curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &cont_type);
-                if (cont_type)
+                if (http_status >= 100 && http_status <= 999)
+                {
+                    char * cont_type(NULL);
+                    ccode = curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &cont_type);
+                    if (ccode == CURLE_OK)
+                    {
+                        if (cont_type)
+                        {
+                            op->mReplyConType = cont_type;
+                        }
+                    }
+                    else
+                    {
+                        LL_WARNS(LOG_CORE) << "CURL error:" << ccode << " Attempting to get content type." << LL_ENDL;
+                    }
+                    op->mStatus = HttpStatus(http_status);
+                }
+                else
                 {
-                    op->mReplyConType = cont_type;
+                    LL_WARNS(LOG_CORE) << "Invalid HTTP response code ("
+                        << http_status << ") received from server."
+                        << LL_ENDL;
+                    op->mStatus = HttpStatus(HttpStatus::LLCORE, HE_INVALID_HTTP_STATUS);
                 }
-                op->mStatus = HttpStatus(http_status);
             }
             else
             {
-                LL_WARNS(LOG_CORE) << "Invalid HTTP response code ("
-                    << http_status << ") received from server."
-                    << LL_ENDL;
                 op->mStatus = HttpStatus(HttpStatus::LLCORE, HE_INVALID_HTTP_STATUS);
             }
         }
diff --git a/indra/llcorehttp/_httpoperation.cpp b/indra/llcorehttp/_httpoperation.cpp
index 333f20d2815..3fc4e289105 100755
--- a/indra/llcorehttp/_httpoperation.cpp
+++ b/indra/llcorehttp/_httpoperation.cpp
@@ -179,6 +179,9 @@ HttpOperation::ptr_t HttpOperation::findByHandle(HttpHandle handle)
 {
     wptr_t weak;
 
+    if (!handle)
+        return ptr_t();
+
     {
         LLCoreInt::HttpScopedLock lock(mOpMutex);
 
diff --git a/indra/llcorehttp/_httpoprequest.cpp b/indra/llcorehttp/_httpoprequest.cpp
index 557f6207b5a..db57869a1bb 100755
--- a/indra/llcorehttp/_httpoprequest.cpp
+++ b/indra/llcorehttp/_httpoprequest.cpp
@@ -750,7 +750,11 @@ HttpStatus HttpOpRequest::prepareRequest(HttpService * service)
 		xfer_timeout *= 2L;
 	}
 	// *DEBUG:  Enable following override for timeout handling and "[curl:bugs] #1420" tests
-	// xfer_timeout = 1L;
+    //if (cpolicy.mPipelining)
+    //{
+    //    xfer_timeout = 1L;
+    //    timeout = 1L;
+    //}
 	code = curl_easy_setopt(mCurlHandle, CURLOPT_TIMEOUT, xfer_timeout);
 	check_curl_easy_code(code, CURLOPT_TIMEOUT);
 	code = curl_easy_setopt(mCurlHandle, CURLOPT_CONNECTTIMEOUT, timeout);
diff --git a/indra/llcorehttp/_httppolicy.cpp b/indra/llcorehttp/_httppolicy.cpp
index fd78a5dadce..b2709b53ecf 100755
--- a/indra/llcorehttp/_httppolicy.cpp
+++ b/indra/llcorehttp/_httppolicy.cpp
@@ -416,7 +416,7 @@ bool HttpPolicy::stageAfterCompletion(const HttpOpRequest::ptr_t &op)
 #if 0
 		if (op->mStatus == HttpStatus(HttpStatus::EXT_CURL_EASY, CURLE_OPERATION_TIMEDOUT))
 		{
-			LL_WARNS(LOG_CORE) << "HTTP request " << static_cast<HttpHandle>(op)
+			LL_WARNS(LOG_CORE) << "HTTP request " << op->getHandle()
 							   << " timed out."
 							   << LL_ENDL;
 		}
diff --git a/indra/newview/app_settings/settings.xml b/indra/newview/app_settings/settings.xml
index 46fd9a27e37..d5549013fab 100755
--- a/indra/newview/app_settings/settings.xml
+++ b/indra/newview/app_settings/settings.xml
@@ -4557,7 +4557,7 @@
       <key>Type</key>
       <string>Boolean</string>
       <key>Value</key>
-      <integer>0</integer>
+      <integer>1</integer>
     </map>
     <key>HttpRangeRequestsDisable</key>
     <map>
diff --git a/indra/newview/llappcorehttp.cpp b/indra/newview/llappcorehttp.cpp
index c13c4f982a9..7dee309a623 100755
--- a/indra/newview/llappcorehttp.cpp
+++ b/indra/newview/llappcorehttp.cpp
@@ -125,7 +125,7 @@ LLAppCoreHttp::LLAppCoreHttp()
 	  mStopHandle(LLCORE_HTTP_HANDLE_INVALID),
 	  mStopRequested(0.0),
 	  mStopped(false),
-	  mPipelined(false)
+	  mPipelined(true)
 {}
 
 
@@ -359,7 +359,7 @@ void LLAppCoreHttp::refreshSettings(bool initial)
 	static const std::string http_pipelining("HttpPipelining");
 	if (gSavedSettings.controlExists(http_pipelining))
 	{
-		// Default to false (in ctor) if absent.
+		// Default to true (in ctor) if absent.
 		bool pipelined(gSavedSettings.getBOOL(http_pipelining));
 		if (pipelined != mPipelined)
 		{
-- 
GitLab