From d7b3fdab62725ef25bba61418e2bc9a444e23c9e Mon Sep 17 00:00:00 2001
From: Miku AuahDark <lumi.hotaru@gmail.com>
Date: Tue, 27 Jun 2023 19:42:42 +0800
Subject: [PATCH 1/2] Add HTTPS_BACKEND_CURL_LINKED preprocessor AND CMake
 option.

Specifying HTTPS_BACKEND_CURL_LINKED means the libcurl will be linked along with the resulting https shared library, which may link with more binaries than necessary.

Only enable this option if you have policy of prohibiting shared object loading at runtime or if your platform doesn't support it.
---
 src/CMakeLists.txt               |  4 ++++
 src/common/config-generated.h.in |  1 +
 src/generic/CurlClient.cpp       | 17 +++++++++++++++++
 3 files changed, 22 insertions(+)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 23a8919..8bda2b1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -131,9 +131,13 @@ target_link_libraries (https https-common)
 
 if (USE_CURL_BACKEND)
 	set(HTTPS_BACKEND_CURL ON)
+	option(HTTPS_BACKEND_CURL_LINKED "Require libcurl at load-time instead at runtime." OFF)
 	find_package (CURL REQUIRED)
 	include_directories (${CURL_INCLUDE_DIRS})
 	target_link_libraries (https https-curl)
+	if (HTTPS_BACKEND_CURL_LINKED)
+		target_link_libraries(https ${CURL_LIBRARIES})
+	endif ()
 endif ()
 
 if (USE_OPENSSL_BACKEND)
diff --git a/src/common/config-generated.h.in b/src/common/config-generated.h.in
index 08403cb..02c8406 100644
--- a/src/common/config-generated.h.in
+++ b/src/common/config-generated.h.in
@@ -1,4 +1,5 @@
 #cmakedefine HTTPS_BACKEND_CURL
+#cmakedefine HTTPS_BACKEND_CURL_LINKED
 #cmakedefine HTTPS_BACKEND_OPENSSL
 #cmakedefine HTTPS_BACKEND_SCHANNEL
 #cmakedefine HTTPS_BACKEND_NSURL
diff --git a/src/generic/CurlClient.cpp b/src/generic/CurlClient.cpp
index ac84689..49d7cb4 100644
--- a/src/generic/CurlClient.cpp
+++ b/src/generic/CurlClient.cpp
@@ -48,6 +48,19 @@ CurlClient::Curl::Curl()
 , slist_append(nullptr)
 , slist_free_all(nullptr)
 {
+#ifdef HTTPS_BACKEND_CURL_LINKED
+	// Linked cURL always available.
+	global_cleanup = &curl_global_cleanup;
+	easy_init = &curl_easy_init;
+	easy_cleanup = &curl_easy_cleanup;
+	easy_setopt = &curl_easy_setopt;
+	easy_perform = &curl_easy_perform;
+	easy_getinfo = &curl_easy_getinfo;
+	slist_append = &curl_slist_append;
+	slist_free_all = &curl_slist_free_all;
+
+	curl_global_init(CURL_GLOBAL_DEFAULT);
+#else
 #ifdef _WIN32
 	handle = (void *) LoadLibraryA("libcurl.dll");
 #else
@@ -78,6 +91,8 @@ CurlClient::Curl::Curl()
 		return;
 
 	global_init(CURL_GLOBAL_DEFAULT);
+#endif // HTTPS_BACKEND_CURL_LINKED
+
 	loaded = true;
 }
 
@@ -86,12 +101,14 @@ CurlClient::Curl::~Curl()
 	if (loaded)
 		global_cleanup();
 
+#ifndef HTTPS_BACKEND_CURL_LINKED
 	if (handle)
 #ifdef _WIN32
 		FreeLibrary((HMODULE) handle);
 #else
 		dlclose(handle);
 #endif
+#endif
 }
 
 static char toUppercase(char c)

From 0be8aa09187b1bb1d033b168009c422774b9aa3f Mon Sep 17 00:00:00 2001
From: TurtleP <jpostelnek@outlook.com>
Date: Thu, 14 Sep 2023 14:47:48 -0400
Subject: [PATCH 2/2] fix the static linking

---
 src/CMakeLists.txt                 | 2 +-
 src/generic/CurlClient.cpp         | 4 ++++
 src/lua/{main.cpp => luahttps.cpp} | 0
 3 files changed, 5 insertions(+), 1 deletion(-)
 rename src/lua/{main.cpp => luahttps.cpp} (100%)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8bda2b1..1b9632f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -33,7 +33,7 @@ endif ()
 
 ### "Libraries"
 add_library (https MODULE
-	lua/main.cpp
+	lua/luahttps.cpp
 )
 
 add_library (https-common STATIC
diff --git a/src/generic/CurlClient.cpp b/src/generic/CurlClient.cpp
index 49d7cb4..61c2055 100644
--- a/src/generic/CurlClient.cpp
+++ b/src/generic/CurlClient.cpp
@@ -12,12 +12,14 @@
 #include <sstream>
 #include <vector>
 
+#ifndef HTTPS_BACKEND_CURL_LINKED
 // Dynamic library loader
 #ifdef _WIN32
 #include <windows.h>
 #else
 #include <dlfcn.h>
 #endif
+#endif
 
 typedef struct StringReader
 {
@@ -25,6 +27,7 @@ typedef struct StringReader
 	size_t pos;
 } StringReader;
 
+#ifndef HTTPS_BACKEND_CURL_LINKED
 template <class T>
 static inline bool loadSymbol(T &var, void *handle, const char *name)
 {
@@ -35,6 +38,7 @@ static inline bool loadSymbol(T &var, void *handle, const char *name)
 #endif
 	return var != nullptr;
 }
+#endif
 
 CurlClient::Curl::Curl()
 : handle(nullptr)
diff --git a/src/lua/main.cpp b/src/lua/luahttps.cpp
similarity index 100%
rename from src/lua/main.cpp
rename to src/lua/luahttps.cpp