From cd55655592b7518451d78b1ef229bce3316af420 Mon Sep 17 00:00:00 2001
From: Rider Linden <none@none>
Date: Thu, 30 Apr 2015 13:12:09 -0700
Subject: [PATCH] Crash logger changes to LLCore::Http

---
 indra/llcrashlogger/CMakeLists.txt    |   2 +
 indra/llcrashlogger/llcrashlogger.cpp | 125 ++++++++++++++++++++------
 indra/llcrashlogger/llcrashlogger.h   |  10 +++
 indra/llmessage/llcorehttputil.cpp    |  39 +-------
 indra/llmessage/llcorehttputil.h      |  57 ++++++++++--
 indra/mac_crash_logger/CMakeLists.txt |   6 ++
 indra/win_crash_logger/CMakeLists.txt |   6 ++
 7 files changed, 173 insertions(+), 72 deletions(-)

diff --git a/indra/llcrashlogger/CMakeLists.txt b/indra/llcrashlogger/CMakeLists.txt
index ba4e34d92b0..c41e61c4976 100755
--- a/indra/llcrashlogger/CMakeLists.txt
+++ b/indra/llcrashlogger/CMakeLists.txt
@@ -3,6 +3,7 @@
 project(llcrashlogger)
 
 include(00-Common)
+include(LLCoreHttp)
 include(LLCommon)
 include(LLMath)
 include(LLMessage)
@@ -10,6 +11,7 @@ include(LLVFS)
 include(LLXML)
 
 include_directories(
+	${LLCOREHTTP_INCLUDE_DIRS}
     ${LLCOMMON_INCLUDE_DIRS}
     ${LLMATH_INCLUDE_DIRS}
     ${LLMESSAGE_INCLUDE_DIRS}
diff --git a/indra/llcrashlogger/llcrashlogger.cpp b/indra/llcrashlogger/llcrashlogger.cpp
index 7a97c16ea70..ec794ac4781 100755
--- a/indra/llcrashlogger/llcrashlogger.cpp
+++ b/indra/llcrashlogger/llcrashlogger.cpp
@@ -40,38 +40,45 @@
 #include "lldir.h"
 #include "llfile.h"
 #include "llsdserialize.h"
-#include "lliopipe.h"
-#include "llpumpio.h"
-#include "llhttpclient.h"
 #include "llsdserialize.h"
 #include "llproxy.h"
- 
-LLPumpIO* gServicePump = NULL;
+#include "llcorehttputil.h"
+#include "llhttpsdhandler.h"
+#include "httpcommon.h"
+#include "httpresponse.h"
+
+#include <curl/curl.h>
+#include <openssl/crypto.h>
+
 BOOL gBreak = false;
 BOOL gSent = false;
 
-class LLCrashLoggerResponder : public LLHTTPClient::Responder
+int LLCrashLogger::ssl_mutex_count = 0;
+LLCoreInt::HttpMutex ** LLCrashLogger::ssl_mutex_list = NULL;
+
+class LLCrashLoggerHandler : public LLHttpSDHandler
 {
-	LOG_CLASS(LLCrashLoggerResponder);
+    LOG_CLASS(LLCrashLoggerHandler);
 public:
-	LLCrashLoggerResponder() 
-	{
-	}
+    LLCrashLoggerHandler() {}
 
 protected:
-	virtual void httpFailure()
-	{
-		LL_WARNS() << dumpResponse() << LL_ENDL;
-		gBreak = true;
-	}
+    virtual void onSuccess(LLCore::HttpResponse * response, const LLSD &content);
+    virtual void onFailure(LLCore::HttpResponse * response, LLCore::HttpStatus status);
 
-	virtual void httpSuccess()
-	{
-		gBreak = true;
-		gSent = true;
-	}
 };
 
+void LLCrashLoggerHandler::onSuccess(LLCore::HttpResponse * response, const LLSD &content)
+{
+    gBreak = true;
+    gSent = true;
+}
+
+void LLCrashLoggerHandler::onFailure(LLCore::HttpResponse * response, LLCore::HttpStatus status)
+{
+    gBreak = true;
+}
+
 LLCrashLogger::LLCrashLogger() :
 	mCrashBehavior(CRASH_BEHAVIOR_ALWAYS_SEND),
 	mCrashInPreviousExec(false),
@@ -389,14 +396,20 @@ bool LLCrashLogger::saveCrashBehaviorSetting(S32 crash_behavior)
 
 bool LLCrashLogger::runCrashLogPost(std::string host, LLSD data, std::string msg, int retries, int timeout)
 {
+    LLCore::HttpRequest httpRequest;
+
 	gBreak = false;
 	for(int i = 0; i < retries; ++i)
 	{
 		updateApplication(llformat("%s, try %d...", msg.c_str(), i+1));
-		LLHTTPClient::post(host, data, new LLCrashLoggerResponder(), timeout);
-		while(!gBreak)
+
+        LLCoreHttpUtil::requestPostWithLLSD(&httpRequest, LLCore::HttpRequest::DEFAULT_POLICY_ID, 0,
+            host, data, NULL, NULL, new LLCrashLoggerHandler);
+
+        while(!gBreak)
 		{
 			updateApplication(); // No new message, just pump the IO
+            httpRequest.update(0L);
 		}
 		if(gSent)
 		{
@@ -510,8 +523,6 @@ bool LLCrashLogger::sendCrashLogs()
 
 void LLCrashLogger::updateApplication(const std::string& message)
 {
-	gServicePump->pump();
-    gServicePump->callback();
 	if (!message.empty()) LL_INFOS() << message << LL_ENDL;
 }
 
@@ -576,16 +587,74 @@ bool LLCrashLogger::init()
 		return false;
 	}
     
-	gServicePump = new LLPumpIO(gAPRPoolp);
-	gServicePump->prime(gAPRPoolp);
-	LLHTTPClient::setPump(*gServicePump);
- 	
+    init_curl();
+    LLCore::HttpRequest::createService();
+    LLCore::HttpRequest::startThread();
+
 	return true;
 }
 
 // For cleanup code common to all platforms.
 void LLCrashLogger::commonCleanup()
 {
+    term_curl();
 	LLError::logToFile("");   //close crashreport.log
 	LLProxy::cleanupClass();
 }
+
+void LLCrashLogger::init_curl()
+{
+    curl_global_init(CURL_GLOBAL_ALL);
+
+    ssl_mutex_count = CRYPTO_num_locks();
+    if (ssl_mutex_count > 0)
+    {
+        ssl_mutex_list = new LLCoreInt::HttpMutex *[ssl_mutex_count];
+
+        for (int i(0); i < ssl_mutex_count; ++i)
+        {
+            ssl_mutex_list[i] = new LLCoreInt::HttpMutex;
+        }
+
+        CRYPTO_set_locking_callback(ssl_locking_callback);
+        CRYPTO_set_id_callback(ssl_thread_id_callback);
+    }
+}
+
+
+void LLCrashLogger::term_curl()
+{
+    CRYPTO_set_locking_callback(NULL);
+    for (int i(0); i < ssl_mutex_count; ++i)
+    {
+        delete ssl_mutex_list[i];
+    }
+    delete[] ssl_mutex_list;
+}
+
+
+unsigned long LLCrashLogger::ssl_thread_id_callback(void)
+{
+#if LL_WINDOWS
+    return (unsigned long)GetCurrentThread();
+#else
+    return (unsigned long)pthread_self();
+#endif
+}
+
+
+void LLCrashLogger::ssl_locking_callback(int mode, int type, const char * /* file */, int /* line */)
+{
+    if (type >= 0 && type < ssl_mutex_count)
+    {
+        if (mode & CRYPTO_LOCK)
+        {
+            ssl_mutex_list[type]->lock();
+        }
+        else
+        {
+            ssl_mutex_list[type]->unlock();
+        }
+    }
+}
+
diff --git a/indra/llcrashlogger/llcrashlogger.h b/indra/llcrashlogger/llcrashlogger.h
index a06bf1d6ac2..f5383daefc1 100755
--- a/indra/llcrashlogger/llcrashlogger.h
+++ b/indra/llcrashlogger/llcrashlogger.h
@@ -34,6 +34,7 @@
 #include "llsd.h"
 #include "llcontrol.h"
 #include "llcrashlock.h"
+#include "_mutex.h"
 
 // Crash reporter behavior
 const S32 CRASH_BEHAVIOR_ASK = 0;
@@ -66,6 +67,11 @@ class LLCrashLogger : public LLApp
 	bool readMinidump(std::string minidump_path);
 
 protected:
+    static void init_curl();
+    static void term_curl();
+    static unsigned long ssl_thread_id_callback(void);
+    static void ssl_locking_callback(int mode, int type, const char * file, int line);
+
 	S32 mCrashBehavior;
 	BOOL mCrashInPreviousExec;
 	std::map<std::string, std::string> mFileMap;
@@ -78,6 +84,10 @@ class LLCrashLogger : public LLApp
 	LLSD mDebugLog;
 	bool mSentCrashLogs;
     LLCrashLock mKeyMaster;
+
+    static int ssl_mutex_count;
+    static LLCoreInt::HttpMutex ** ssl_mutex_list;
+
 };
 
 #endif //LLCRASHLOGGER_H
diff --git a/indra/llmessage/llcorehttputil.cpp b/indra/llmessage/llcorehttputil.cpp
index a3226ee2c3a..1f9d4d15cd6 100644
--- a/indra/llmessage/llcorehttputil.cpp
+++ b/indra/llmessage/llcorehttputil.cpp
@@ -90,18 +90,7 @@ HttpHandle requestPostWithLLSD(HttpRequest * request,
     return handle;
 }
 
-HttpHandle requestPostWithLLSD(HttpRequest::ptr_t & request,
-    HttpRequest::policy_t policy_id,
-    HttpRequest::priority_t priority,
-    const std::string & url,
-    const LLSD & body,
-    HttpOptions::ptr_t & options,
-    HttpHeaders::ptr_t & headers,
-    HttpHandler * handler)
-{
-    return requestPostWithLLSD(request.get(), policy_id, priority,
-        url, body, options.get(), headers.get(), handler);
-}
+
 
 HttpHandle requestPutWithLLSD(HttpRequest * request,
     HttpRequest::policy_t policy_id,
@@ -129,19 +118,6 @@ HttpHandle requestPutWithLLSD(HttpRequest * request,
     return handle;
 }
 
-HttpHandle requestPutWithLLSD(HttpRequest::ptr_t & request,
-    HttpRequest::policy_t policy_id,
-    HttpRequest::priority_t priority,
-    const std::string & url,
-    const LLSD & body,
-    HttpOptions::ptr_t & options,
-    HttpHeaders::ptr_t & headers,
-    HttpHandler * handler)
-{
-    return requestPutWithLLSD(request.get(), policy_id, priority,
-        url, body, options.get(), headers.get(), handler);
-}
-
 HttpHandle requestPatchWithLLSD(HttpRequest * request,
     HttpRequest::policy_t policy_id,
     HttpRequest::priority_t priority,
@@ -168,19 +144,6 @@ HttpHandle requestPatchWithLLSD(HttpRequest * request,
     return handle;
 }
 
-HttpHandle requestPatchWithLLSD(HttpRequest::ptr_t & request,
-    HttpRequest::policy_t policy_id,
-    HttpRequest::priority_t priority,
-    const std::string & url,
-    const LLSD & body,
-    HttpOptions::ptr_t & options,
-    HttpHeaders::ptr_t & headers,
-    HttpHandler * handler)
-{
-    return requestPatchWithLLSD(request.get(), policy_id, priority,
-        url, body, options.get(), headers.get(), handler);
-}
-
 
 std::string responseToString(LLCore::HttpResponse * response)
 {
diff --git a/indra/llmessage/llcorehttputil.h b/indra/llmessage/llcorehttputil.h
index 1e2b50e1891..77b91634929 100644
--- a/indra/llmessage/llcorehttputil.h
+++ b/indra/llmessage/llcorehttputil.h
@@ -112,14 +112,30 @@ LLCore::HttpHandle requestPostWithLLSD(LLCore::HttpRequest * request,
 									   LLCore::HttpHeaders * headers,
 									   LLCore::HttpHandler * handler);
 
-LLCore::HttpHandle requestPostWithLLSD(LLCore::HttpRequest::ptr_t & request,
+inline LLCore::HttpHandle requestPostWithLLSD(LLCore::HttpRequest::ptr_t & request,
 	LLCore::HttpRequest::policy_t policy_id,
 	LLCore::HttpRequest::priority_t priority,
 	const std::string & url,
 	const LLSD & body,
 	LLCore::HttpOptions::ptr_t & options,
 	LLCore::HttpHeaders::ptr_t & headers,
-	LLCore::HttpHandler * handler);
+	LLCore::HttpHandler * handler)
+{
+    return requestPostWithLLSD(request.get(), policy_id, priority,
+        url, body, options.get(), headers.get(), handler);
+}
+
+inline LLCore::HttpHandle requestPostWithLLSD(LLCore::HttpRequest::ptr_t & request,
+    LLCore::HttpRequest::policy_t policy_id,
+    LLCore::HttpRequest::priority_t priority,
+    const std::string & url,
+    const LLSD & body,
+    LLCore::HttpHandler * handler)
+{
+    return requestPostWithLLSD(request.get(), policy_id, priority,
+        url, body, NULL, NULL, handler);
+}
+
 
 /// Issue a standard HttpRequest::requestPut() call but using
 /// and LLSD object as the request body.  Conventions are the
@@ -146,14 +162,29 @@ LLCore::HttpHandle requestPutWithLLSD(LLCore::HttpRequest * request,
 	LLCore::HttpHeaders * headers,
 	LLCore::HttpHandler * handler);
 
-LLCore::HttpHandle requestPutWithLLSD(LLCore::HttpRequest::ptr_t & request,
+inline LLCore::HttpHandle requestPutWithLLSD(LLCore::HttpRequest::ptr_t & request,
 	LLCore::HttpRequest::policy_t policy_id,
 	LLCore::HttpRequest::priority_t priority,
 	const std::string & url,
 	const LLSD & body,
 	LLCore::HttpOptions::ptr_t & options,
 	LLCore::HttpHeaders::ptr_t & headers,
-	LLCore::HttpHandler * handler);
+	LLCore::HttpHandler * handler)
+{
+    return requestPutWithLLSD(request.get(), policy_id, priority,
+        url, body, options.get(), headers.get(), handler);
+}
+
+inline LLCore::HttpHandle requestPutWithLLSD(LLCore::HttpRequest::ptr_t & request,
+    LLCore::HttpRequest::policy_t policy_id,
+    LLCore::HttpRequest::priority_t priority,
+    const std::string & url,
+    const LLSD & body,
+    LLCore::HttpHandler * handler)
+{
+    return requestPutWithLLSD(request.get(), policy_id, priority,
+        url, body, NULL, NULL, handler);
+}
 
 /// Issue a standard HttpRequest::requestPatch() call but using
 /// and LLSD object as the request body.  Conventions are the
@@ -180,15 +211,29 @@ LLCore::HttpHandle requestPatchWithLLSD(LLCore::HttpRequest * request,
     LLCore::HttpHeaders * headers,
     LLCore::HttpHandler * handler);
 
-LLCore::HttpHandle requestPatchWithLLSD(LLCore::HttpRequest::ptr_t & request,
+inline LLCore::HttpHandle requestPatchWithLLSD(LLCore::HttpRequest::ptr_t & request,
     LLCore::HttpRequest::policy_t policy_id,
     LLCore::HttpRequest::priority_t priority,
     const std::string & url,
     const LLSD & body,
     LLCore::HttpOptions::ptr_t & options,
     LLCore::HttpHeaders::ptr_t & headers,
-    LLCore::HttpHandler * handler);
+    LLCore::HttpHandler * handler)
+{
+    return requestPatchWithLLSD(request.get(), policy_id, priority,
+        url, body, options.get(), headers.get(), handler);
+}
 
+inline LLCore::HttpHandle requestPatchWithLLSD(LLCore::HttpRequest::ptr_t & request,
+    LLCore::HttpRequest::policy_t policy_id,
+    LLCore::HttpRequest::priority_t priority,
+    const std::string & url,
+    const LLSD & body,
+    LLCore::HttpHandler * handler)
+{
+    return requestPatchWithLLSD(request.get(), policy_id, priority,
+        url, body, NULL, NULL, handler);
+}
 
 /// The HttpCoroHandler is a specialization of the LLCore::HttpHandler for 
 /// interacting with coroutines. When the request is completed the response 
diff --git a/indra/mac_crash_logger/CMakeLists.txt b/indra/mac_crash_logger/CMakeLists.txt
index c59645bd70e..ce20284b59e 100755
--- a/indra/mac_crash_logger/CMakeLists.txt
+++ b/indra/mac_crash_logger/CMakeLists.txt
@@ -4,6 +4,7 @@ project(mac_crash_logger)
 
 include(00-Common)
 include(LLCommon)
+include(LLCoreHttp)
 include(LLCrashLogger)
 include(LLMath)
 include(LLMessage)
@@ -11,8 +12,10 @@ include(LLVFS)
 include(LLXML)
 include(Linking)
 include(LLSharedLibs)
+include(Boost)
 
 include_directories(
+	${LLCOREHTTP_INCLUDE_DIRS}
     ${LLCOMMON_INCLUDE_DIRS}
     ${LLCRASHLOGGER_INCLUDE_DIRS}
     ${LLMATH_INCLUDE_DIRS}
@@ -71,7 +74,10 @@ target_link_libraries(mac-crash-logger
     ${LLMESSAGE_LIBRARIES}
     ${LLVFS_LIBRARIES}
     ${LLMATH_LIBRARIES}
+	${LLCOREHTTP_LIBRARIES}
     ${LLCOMMON_LIBRARIES}
+	${BOOST_CONTEXT_LIBRARY}
+	${BOOST_COROUTINE_LIBRARY}
     )
 
 add_custom_command(
diff --git a/indra/win_crash_logger/CMakeLists.txt b/indra/win_crash_logger/CMakeLists.txt
index c6070020db4..a52c8cc42b6 100755
--- a/indra/win_crash_logger/CMakeLists.txt
+++ b/indra/win_crash_logger/CMakeLists.txt
@@ -4,6 +4,7 @@ project(win_crash_logger)
 
 include(00-Common)
 include(LLCommon)
+include(LLCoreHttp)
 include(LLCrashLogger)
 include(LLMath)
 include(LLMessage)
@@ -13,8 +14,10 @@ include(LLXML)
 include(Linking)
 include(LLSharedLibs)
 include(GoogleBreakpad)
+include(Boost)
 
 include_directories(
+	${LLCOREHTTP_INCLUDE_DIRS}
     ${LLCOMMON_INCLUDE_DIRS}
     ${LLCRASHLOGGER_INCLUDE_DIRS}
     ${LLMATH_INCLUDE_DIRS}
@@ -77,7 +80,10 @@ target_link_libraries(windows-crash-logger
     ${LLXML_LIBRARIES}
     ${LLMESSAGE_LIBRARIES}
     ${LLMATH_LIBRARIES}
+	${LLCOREHTTP_LIBRARIES}
     ${LLCOMMON_LIBRARIES}
+    ${BOOST_CONTEXT_LIBRARY}
+    ${BOOST_COROUTINE_LIBRARY}
     ${WINDOWS_LIBRARIES}
     ${DXGUID_LIBRARY}
     ${GOOGLE_PERFTOOLS_LIBRARIES}
-- 
GitLab