Browse Source

update https library to love2d/lua-https@76d92e7

Sasha Szpakowski 1 year ago
parent
commit
a48639d112

+ 44 - 0
platform/xcode/liblove.xcodeproj/project.pbxproj

@@ -76,6 +76,17 @@
 		D9DB6E402B4B41580037A1F6 /* GLSL.ext.ARM.h in Headers */ = {isa = PBXBuildFile; fileRef = D9DB6E3A2B4B41570037A1F6 /* GLSL.ext.ARM.h */; };
 		D9DB6E412B4B41580037A1F6 /* GLSL.ext.QCOM.h in Headers */ = {isa = PBXBuildFile; fileRef = D9DB6E3B2B4B41580037A1F6 /* GLSL.ext.QCOM.h */; };
 		D9DB6E452B4B80E80037A1F6 /* build_info.h in Headers */ = {isa = PBXBuildFile; fileRef = D9DB6E442B4B80E80037A1F6 /* build_info.h */; };
+		D9F0C2CC2C68091200BB2D25 /* LibraryLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = D9F0C2CB2C68091200BB2D25 /* LibraryLoader.h */; };
+		D9F0C2D42C680A5500BB2D25 /* CurlClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2CD2C680A5500BB2D25 /* CurlClient.cpp */; };
+		D9F0C2D52C680A5500BB2D25 /* CurlClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2CD2C680A5500BB2D25 /* CurlClient.cpp */; };
+		D9F0C2D62C680A5500BB2D25 /* CurlClient.h in Headers */ = {isa = PBXBuildFile; fileRef = D9F0C2CE2C680A5500BB2D25 /* CurlClient.h */; };
+		D9F0C2D72C680A5500BB2D25 /* LinktimeLibraryLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2CF2C680A5500BB2D25 /* LinktimeLibraryLoader.cpp */; };
+		D9F0C2D82C680A5500BB2D25 /* LinktimeLibraryLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2CF2C680A5500BB2D25 /* LinktimeLibraryLoader.cpp */; };
+		D9F0C2D92C680A5500BB2D25 /* OpenSSLConnection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2D02C680A5500BB2D25 /* OpenSSLConnection.cpp */; };
+		D9F0C2DA2C680A5500BB2D25 /* OpenSSLConnection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2D02C680A5500BB2D25 /* OpenSSLConnection.cpp */; };
+		D9F0C2DB2C680A5500BB2D25 /* OpenSSLConnection.h in Headers */ = {isa = PBXBuildFile; fileRef = D9F0C2D12C680A5500BB2D25 /* OpenSSLConnection.h */; };
+		D9F0C2DC2C680A5500BB2D25 /* UnixLibraryLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2D22C680A5500BB2D25 /* UnixLibraryLoader.cpp */; };
+		D9F0C2DD2C680A5500BB2D25 /* UnixLibraryLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D9F0C2D22C680A5500BB2D25 /* UnixLibraryLoader.cpp */; };
 		FA0A3A5F23366CE9001C269E /* floattypes.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0A3A5D23366CE9001C269E /* floattypes.h */; };
 		FA0A3A6023366CE9001C269E /* floattypes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0A3A5E23366CE9001C269E /* floattypes.cpp */; };
 		FA0A3A6123366CE9001C269E /* floattypes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0A3A5E23366CE9001C269E /* floattypes.cpp */; };
@@ -1424,6 +1435,13 @@
 		D9DB6E3A2B4B41570037A1F6 /* GLSL.ext.ARM.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GLSL.ext.ARM.h; sourceTree = "<group>"; };
 		D9DB6E3B2B4B41580037A1F6 /* GLSL.ext.QCOM.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GLSL.ext.QCOM.h; sourceTree = "<group>"; };
 		D9DB6E442B4B80E80037A1F6 /* build_info.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = build_info.h; sourceTree = "<group>"; };
+		D9F0C2CB2C68091200BB2D25 /* LibraryLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LibraryLoader.h; sourceTree = "<group>"; };
+		D9F0C2CD2C680A5500BB2D25 /* CurlClient.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CurlClient.cpp; sourceTree = "<group>"; };
+		D9F0C2CE2C680A5500BB2D25 /* CurlClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CurlClient.h; sourceTree = "<group>"; };
+		D9F0C2CF2C680A5500BB2D25 /* LinktimeLibraryLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LinktimeLibraryLoader.cpp; sourceTree = "<group>"; };
+		D9F0C2D02C680A5500BB2D25 /* OpenSSLConnection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OpenSSLConnection.cpp; sourceTree = "<group>"; };
+		D9F0C2D12C680A5500BB2D25 /* OpenSSLConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenSSLConnection.h; sourceTree = "<group>"; };
+		D9F0C2D22C680A5500BB2D25 /* UnixLibraryLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UnixLibraryLoader.cpp; sourceTree = "<group>"; };
 		FA08F5AE16C7525600F007B5 /* liblove-macosx.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "liblove-macosx.plist"; path = "macosx/liblove-macosx.plist"; sourceTree = "<group>"; };
 		FA0A3A5D23366CE9001C269E /* floattypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = floattypes.h; sourceTree = "<group>"; };
 		FA0A3A5E23366CE9001C269E /* floattypes.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = floattypes.cpp; sourceTree = "<group>"; };
@@ -2386,6 +2404,19 @@
 			path = ResourceLimits;
 			sourceTree = "<group>";
 		};
+		D9F0C2D32C680A5500BB2D25 /* generic */ = {
+			isa = PBXGroup;
+			children = (
+				D9F0C2CD2C680A5500BB2D25 /* CurlClient.cpp */,
+				D9F0C2CE2C680A5500BB2D25 /* CurlClient.h */,
+				D9F0C2CF2C680A5500BB2D25 /* LinktimeLibraryLoader.cpp */,
+				D9F0C2D02C680A5500BB2D25 /* OpenSSLConnection.cpp */,
+				D9F0C2D12C680A5500BB2D25 /* OpenSSLConnection.h */,
+				D9F0C2D22C680A5500BB2D25 /* UnixLibraryLoader.cpp */,
+			);
+			path = generic;
+			sourceTree = "<group>";
+		};
 		FA08F5AC16C751BA00F007B5 /* Resources */ = {
 			isa = PBXGroup;
 			children = (
@@ -3593,6 +3624,7 @@
 			children = (
 				FA94729827A6F9AC00817677 /* apple */,
 				FA94725327A6EE1B00817677 /* common */,
+				D9F0C2D32C680A5500BB2D25 /* generic */,
 				FA94725127A6EE1B00817677 /* lua */,
 			);
 			path = src;
@@ -3618,6 +3650,7 @@
 				FA94725E27A6EE1B00817677 /* HTTPS.h */,
 				FA94725527A6EE1B00817677 /* HTTPSClient.cpp */,
 				FA94725627A6EE1B00817677 /* HTTPSClient.h */,
+				D9F0C2CB2C68091200BB2D25 /* LibraryLoader.h */,
 				FA94725D27A6EE1B00817677 /* PlaintextConnection.cpp */,
 				FA94725727A6EE1B00817677 /* PlaintextConnection.h */,
 			);
@@ -4128,6 +4161,7 @@
 				FA0B7D3E1A95902C000E1D17 /* Texture.h in Headers */,
 				FA0B7ECA1A95902C000E1D17 /* threads.h in Headers */,
 				FADF54361E3DAE6E00012CC0 /* wrap_SpriteBatch.h in Headers */,
+				D9F0C2D62C680A5500BB2D25 /* CurlClient.h in Headers */,
 				FA0B7DB01A95902C000E1D17 /* wrap_CompressedImageData.h in Headers */,
 				FAC7CD8D1FE35E95006A60C7 /* physfs_platforms.h in Headers */,
 				FABDA9B82552448300B5C523 /* b2_motor_joint.h in Headers */,
@@ -4300,6 +4334,7 @@
 				FA0B7DD81A95902C000E1D17 /* MathModule.h in Headers */,
 				217DFBDA1D9F6D490055D849 /* auxiliar.h in Headers */,
 				FA0B7EC71A95902C000E1D17 /* ThreadModule.h in Headers */,
+				D9F0C2DB2C680A5500BB2D25 /* OpenSSLConnection.h in Headers */,
 				FA4F2BAD1DE1E37000CA37D7 /* RecordingDevice.h in Headers */,
 				FABDA9CE2552448300B5C523 /* b2_time_of_impact.h in Headers */,
 				FA0B792B1A958E3B000E1D17 /* Matrix.h in Headers */,
@@ -4324,6 +4359,7 @@
 				FA93C4531F315B960087CCD4 /* FormatHandler.h in Headers */,
 				FA0B7CD81A95902C000E1D17 /* Audio.h in Headers */,
 				FA0B7CF61A95902C000E1D17 /* File.h in Headers */,
+				D9F0C2CC2C68091200BB2D25 /* LibraryLoader.h in Headers */,
 				FA18CF2123DCF67900263725 /* spirv.hpp in Headers */,
 				FA0A3A5F23366CE9001C269E /* floattypes.h in Headers */,
 				FADF54091E3D78F700012CC0 /* Video.h in Headers */,
@@ -4746,6 +4782,7 @@
 				D99081F02BB2473900D2B0E4 /* JoystickSDL3.cpp in Sources */,
 				FADF54031E3D77B500012CC0 /* wrap_TextBatch.cpp in Sources */,
 				FA0B7DCB1A95902C000E1D17 /* Keyboard.cpp in Sources */,
+				D9F0C2DD2C680A5500BB2D25 /* UnixLibraryLoader.cpp in Sources */,
 				FA0B7DFB1A95902C000E1D17 /* Body.cpp in Sources */,
 				FA0B7ED21A95902C000E1D17 /* wrap_ThreadModule.cpp in Sources */,
 				FADF54261E3DA5BA00012CC0 /* Mesh.cpp in Sources */,
@@ -4758,7 +4795,9 @@
 				FA18CF4023DCF67900263725 /* spirv_parser.cpp in Sources */,
 				FA24348821D401CB00B8918A /* attribute.cpp in Sources */,
 				FACA02FC1F5E39810084B28F /* wrap_CompressedData.cpp in Sources */,
+				D9F0C2DA2C680A5500BB2D25 /* OpenSSLConnection.cpp in Sources */,
 				FABDA9A32552448300B5C523 /* b2_friction_joint.cpp in Sources */,
+				D9F0C2D52C680A5500BB2D25 /* CurlClient.cpp in Sources */,
 				FAF140AD1E20934C00F898D2 /* Versions.cpp in Sources */,
 				FA0B793C1A958E3B000E1D17 /* runtime.cpp in Sources */,
 				FA6BDF8A280B62A000240F2A /* GraphicsReadback.mm in Sources */,
@@ -4960,6 +4999,7 @@
 				FABDA9D92552448300B5C523 /* b2_settings.cpp in Sources */,
 				FA0B7DB21A95902C000E1D17 /* wrap_Image.cpp in Sources */,
 				FA0B7E891A95902C000E1D17 /* Decoder.cpp in Sources */,
+				D9F0C2D82C680A5500BB2D25 /* LinktimeLibraryLoader.cpp in Sources */,
 				FAECA1B31F3164700095D008 /* CompressedSlice.cpp in Sources */,
 				FA18CF1823DCF67900263725 /* spirv_glsl.cpp in Sources */,
 				FA0B7E3D1A95902C000E1D17 /* wrap_Body.cpp in Sources */,
@@ -5175,6 +5215,7 @@
 				D99081EF2BB2473900D2B0E4 /* JoystickSDL3.cpp in Sources */,
 				FAF140931E20934C00F898D2 /* PpScanner.cpp in Sources */,
 				FA9D53AC1F5307E900125C6B /* Deprecations.cpp in Sources */,
+				D9F0C2DC2C680A5500BB2D25 /* UnixLibraryLoader.cpp in Sources */,
 				FA0B7CCD1A95902C000E1D17 /* Audio.cpp in Sources */,
 				FA0B7DCA1A95902C000E1D17 /* Keyboard.cpp in Sources */,
 				FA9D8DDD1DEF842A002CD881 /* Drawable.cpp in Sources */,
@@ -5187,7 +5228,9 @@
 				217DFBFB1D9F6D490055D849 /* serial.c in Sources */,
 				FA18CED823DBC6E000263725 /* StreamBuffer.mm in Sources */,
 				FADF54251E3DA5BA00012CC0 /* Mesh.cpp in Sources */,
+				D9F0C2D92C680A5500BB2D25 /* OpenSSLConnection.cpp in Sources */,
 				FA4F2BE51DE6650600CA37D7 /* wrap_Transform.cpp in Sources */,
+				D9F0C2D42C680A5500BB2D25 /* CurlClient.cpp in Sources */,
 				FABDA9A22552448300B5C523 /* b2_friction_joint.cpp in Sources */,
 				FA0B7CDC1A95902C000E1D17 /* Source.cpp in Sources */,
 				FA6BDF89280B62A000240F2A /* GraphicsReadback.mm in Sources */,
@@ -5389,6 +5432,7 @@
 				FA0B7ACB1A958EA3000E1D17 /* list.c in Sources */,
 				FA0B7E7E1A95902C000E1D17 /* Joint.cpp in Sources */,
 				FA0B7DB11A95902C000E1D17 /* wrap_Image.cpp in Sources */,
+				D9F0C2D72C680A5500BB2D25 /* LinktimeLibraryLoader.cpp in Sources */,
 				FABDA9D82552448300B5C523 /* b2_settings.cpp in Sources */,
 				FAC7CD831FE35E95006A60C7 /* physfs_platform_windows.c in Sources */,
 				FA0B7E881A95902C000E1D17 /* Decoder.cpp in Sources */,

+ 4 - 3
src/libraries/luahttps/src/android/AndroidClient.cpp

@@ -5,7 +5,7 @@
 #include <sstream>
 #include <type_traits>
 
-#include <dlfcn.h>
+#include "../common/LibraryLoader.h"
 
 // We want std::string that contains null byte, hence length of 1.
 // NOLINTNEXTLINE
@@ -52,10 +52,11 @@ static std::string getStringUTF(JNIEnv *env, jstring str)
 AndroidClient::AndroidClient()
 : HTTPSClient()
 {
+	LibraryLoader::handle *library = LibraryLoader::GetCurrentProcessHandle();
 	// Look for SDL_AndroidGetJNIEnv
-	SDL_AndroidGetJNIEnv = (decltype(SDL_AndroidGetJNIEnv)) dlsym(RTLD_DEFAULT, "SDL_AndroidGetJNIEnv");
+	LibraryLoader::LoadSymbol(SDL_AndroidGetJNIEnv, library, "SDL_AndroidGetJNIEnv");
 	// Look for SDL_AndroidGetActivity
-	SDL_AndroidGetActivity = (decltype(SDL_AndroidGetActivity)) dlsym(RTLD_DEFAULT, "SDL_AndroidGetActivity");
+	LibraryLoader::LoadSymbol(SDL_AndroidGetActivity, library, "SDL_AndroidGetActivity");
 }
 
 bool AndroidClient::valid() const

+ 5 - 0
src/libraries/luahttps/src/apple/NSURLClient.mm

@@ -78,6 +78,11 @@ HTTPSClient::Reply NSURLClient::request(const HTTPSClient::Request &req)
 		}
 	}
 
+	if (reply.responseCode == 0 && body == nil && error != nil)
+	{
+		reply.body = toCppString(error.localizedDescription);
+	}
+
 	return reply;
 }}
 

+ 4 - 0
src/libraries/luahttps/src/common/HTTPS.cpp

@@ -1,6 +1,7 @@
 #include "HTTPS.h"
 #include "config.h"
 #include "ConnectionClient.h"
+#include "LibraryLoader.h"
 
 #include <stdexcept>
 
@@ -65,6 +66,9 @@ static HTTPSClient *clients[] = {
 	nullptr,
 };
 
+// Call into the library loader to make sure it is linked in
+static LibraryLoader::handle* dummyProcessHandle = LibraryLoader::GetCurrentProcessHandle();
+
 HTTPSClient::Reply request(const HTTPSClient::Request &req)
 {
 	for (size_t i = 0; clients[i]; ++i)

+ 20 - 0
src/libraries/luahttps/src/common/LibraryLoader.h

@@ -0,0 +1,20 @@
+#pragma once
+
+namespace LibraryLoader
+{
+	using handle = void;
+	using function = void();
+
+	handle *OpenLibrary(const char *name);
+	void CloseLibrary(handle *handle);
+	handle* GetCurrentProcessHandle();
+
+	function *GetFunction(handle *handle, const char *name);
+
+	template<class T>
+	inline bool LoadSymbol(T& var, handle *handle, const char *name)
+	{
+		var = reinterpret_cast<T>(GetFunction(handle, name));
+		return var != nullptr;
+	}
+}

+ 5 - 0
src/libraries/luahttps/src/common/config.h

@@ -10,6 +10,7 @@
 #elif defined(WIN32) || defined(_WIN32)
 	#define HTTPS_BACKEND_SCHANNEL
 	#define HTTPS_USE_WINSOCK
+	#define HTTPS_LIBRARY_LOADER_WINDOWS
 	#include <winapifamily.h>
 	#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
 		// WinINet is only supported on desktop.
@@ -23,9 +24,13 @@
 	#endif
 #elif defined(__ANDROID__)
 	#define HTTPS_BACKEND_ANDROID
+	#define HTTPS_LIBRARY_LOADER_UNIX
 #elif defined(__APPLE__)
 	#define HTTPS_BACKEND_NSURL
+	#define HTTPS_LIBRARY_LOADER_UNIX
 #elif defined(linux) || defined(__linux) || defined(__linux__)
+	#define HTTPS_LIBRARY_LOADER_UNIX
+
 	#if defined __has_include
 		#if __has_include(<curl/curl.h>)
 			#define HTTPS_BACKEND_CURL

+ 14 - 39
src/libraries/luahttps/src/generic/CurlClient.cpp

@@ -1,8 +1,3 @@
-#ifdef _WIN32
-#define NOMINMAX
-#define WIN32_LEAN_AND_MEAN
-#endif
-
 #include "CurlClient.h"
 
 #ifdef HTTPS_BACKEND_CURL
@@ -12,30 +7,12 @@
 #include <sstream>
 #include <vector>
 
-// Dynamic library loader
-#ifdef _WIN32
-#include <windows.h>
-#else
-#include <dlfcn.h>
-#endif
-
 typedef struct StringReader
 {
 	const std::string *str;
 	size_t pos;
 } StringReader;
 
-template <class T>
-static inline bool loadSymbol(T &var, void *handle, const char *name)
-{
-#ifdef _WIN32
-	var = (T) GetProcAddress((HMODULE) handle, name);
-#else
-	var = (T) dlsym(handle, name);
-#endif
-	return var != nullptr;
-}
-
 CurlClient::Curl::Curl()
 : handle(nullptr)
 , loaded(false)
@@ -48,33 +25,35 @@ CurlClient::Curl::Curl()
 , slist_append(nullptr)
 , slist_free_all(nullptr)
 {
+	using namespace LibraryLoader;
+
 #ifdef _WIN32
-	handle = (void *) LoadLibraryA("libcurl.dll");
+	handle = OpenLibrary("libcurl.dll");
 #else
-	handle = dlopen("libcurl.so.4", RTLD_LAZY);
+	handle = OpenLibrary("libcurl.so.4");
 #endif
 	if (!handle)
 		return;
 
 	// Load symbols
 	decltype(&curl_global_init) global_init = nullptr;
-	if (!loadSymbol(global_init, handle, "curl_global_init"))
+	if (!LoadSymbol(global_init, handle, "curl_global_init"))
 		return;
-	if (!loadSymbol(global_cleanup, handle, "curl_global_cleanup"))
+	if (!LoadSymbol(global_cleanup, handle, "curl_global_cleanup"))
 		return;
-	if (!loadSymbol(easy_init, handle, "curl_easy_init"))
+	if (!LoadSymbol(easy_init, handle, "curl_easy_init"))
 		return;
-	if (!loadSymbol(easy_cleanup, handle, "curl_easy_cleanup"))
+	if (!LoadSymbol(easy_cleanup, handle, "curl_easy_cleanup"))
 		return;
-	if (!loadSymbol(easy_setopt, handle, "curl_easy_setopt"))
+	if (!LoadSymbol(easy_setopt, handle, "curl_easy_setopt"))
 		return;
-	if (!loadSymbol(easy_perform, handle, "curl_easy_perform"))
+	if (!LoadSymbol(easy_perform, handle, "curl_easy_perform"))
 		return;
-	if (!loadSymbol(easy_getinfo, handle, "curl_easy_getinfo"))
+	if (!LoadSymbol(easy_getinfo, handle, "curl_easy_getinfo"))
 		return;
-	if (!loadSymbol(slist_append, handle, "curl_slist_append"))
+	if (!LoadSymbol(slist_append, handle, "curl_slist_append"))
 		return;
-	if (!loadSymbol(slist_free_all, handle, "curl_slist_free_all"))
+	if (!LoadSymbol(slist_free_all, handle, "curl_slist_free_all"))
 		return;
 
 	global_init(CURL_GLOBAL_DEFAULT);
@@ -87,11 +66,7 @@ CurlClient::Curl::~Curl()
 		global_cleanup();
 
 	if (handle)
-#ifdef _WIN32
-		FreeLibrary((HMODULE) handle);
-#else
-		dlclose(handle);
-#endif
+		LibraryLoader::CloseLibrary(handle);
 }
 
 static char toUppercase(char c)

+ 2 - 1
src/libraries/luahttps/src/generic/CurlClient.h

@@ -7,6 +7,7 @@
 #include <curl/curl.h>
 
 #include "../common/HTTPSClient.h"
+#include "../common/LibraryLoader.h"
 
 class CurlClient : public HTTPSClient
 {
@@ -19,7 +20,7 @@ private:
 	{
 		Curl();
 		~Curl();
-		void *handle;
+		LibraryLoader::handle *handle;
 		bool loaded;
 
 		decltype(&curl_global_cleanup) global_cleanup;

+ 66 - 0
src/libraries/luahttps/src/generic/LinktimeLibraryLoader.cpp

@@ -0,0 +1,66 @@
+#include "../common/config.h"
+#include "../common/LibraryLoader.h"
+
+#ifdef HTTPS_LIBRARY_LOADER_LINKTIME
+
+#include <cstring>
+
+#ifdef HTTPS_BACKEND_CURL
+#include <curl/curl.h>
+
+static char CurlHandle;
+#endif
+
+#if defined(HTTPS_BACKEND_OPENSSL) || defined(HTTPS_BACKEND_ANDROID)
+#	error "Selected backends that are not compatible with this loader"
+#endif
+
+namespace LibraryLoader
+{
+	handle *OpenLibrary(const char *name)
+	{
+#ifdef HTTPS_BACKEND_CURL
+		if (strstr(name, "libcurl") == name)
+			return reinterpret_cast<handle *>(&CurlHandle);
+#endif
+		return nullptr;
+	}
+
+	void CloseLibrary(handle *)
+	{
+	}
+
+	handle* GetCurrentProcessHandle()
+	{
+		return nullptr;
+	}
+
+	function *GetFunction(handle *handle, const char *name)
+	{
+#define RETURN_MATCHING_FUNCTION(func) \
+	if (strcmp(name, #func) == 0) \
+		return reinterpret_cast<function *>(&func);
+
+#ifdef HTTPS_BACKEND_CURL
+		if (handle == &CurlHandle)
+		{
+			RETURN_MATCHING_FUNCTION(curl_global_init);
+			RETURN_MATCHING_FUNCTION(curl_global_cleanup);
+			RETURN_MATCHING_FUNCTION(curl_easy_init);
+			RETURN_MATCHING_FUNCTION(curl_easy_cleanup);
+			RETURN_MATCHING_FUNCTION(curl_easy_setopt);
+			RETURN_MATCHING_FUNCTION(curl_easy_perform);
+			RETURN_MATCHING_FUNCTION(curl_easy_getinfo);
+			RETURN_MATCHING_FUNCTION(curl_slist_append);
+			RETURN_MATCHING_FUNCTION(curl_slist_free_all);
+		}
+#endif
+
+#undef RETURN_MATCHING_FUNCTION
+
+		return nullptr;
+	}
+}
+
+#endif // HTTPS_LIBRARY_LOADER_LINKTIME
+

+ 58 - 47
src/libraries/luahttps/src/generic/OpenSSLConnection.cpp

@@ -2,65 +2,72 @@
 
 #ifdef HTTPS_BACKEND_OPENSSL
 
-#include <dlfcn.h>
+#include "../common/LibraryLoader.h"
 
 // Not present in openssl 1.1 headers
 #define SSL_CTRL_OPTIONS 32
 
-template <class T>
-static inline bool loadSymbol(T &var, void *handle, const char *name)
+static bool TryOpenLibraries(const char *sslName, LibraryLoader::handle *& sslHandle, const char *cryptoName, LibraryLoader::handle *&cryptoHandle)
 {
-	var = reinterpret_cast<T>(dlsym(handle, name));
-	return var != nullptr;
+	sslHandle = LibraryLoader::OpenLibrary(sslName);
+	cryptoHandle = LibraryLoader::OpenLibrary(cryptoName);
+
+	if (sslHandle && cryptoHandle)
+		return true;
+
+	if (sslHandle)
+		LibraryLoader::CloseLibrary(sslHandle);
+	if (cryptoHandle)
+		LibraryLoader::CloseLibrary(cryptoHandle);
+	return false;
 }
 
 OpenSSLConnection::SSLFuncs::SSLFuncs()
 {
-	valid = false;
+	using namespace LibraryLoader;
 
-	// Try OpenSSL 1.1
-	void *sslhandle = dlopen("libssl.so.1.1", RTLD_LAZY);
-	void *cryptohandle = dlopen("libcrypto.so.1.1", RTLD_LAZY);
-	// Try OpenSSL 1.0
-	if (!sslhandle || !cryptohandle)
-	{
-		sslhandle = dlopen("libssl.so.1.0.0", RTLD_LAZY);
-		cryptohandle = dlopen("libcrypto.so.1.0.0", RTLD_LAZY);
-	}
-	// Try OpenSSL without version
-	if (!sslhandle || !cryptohandle)
-	{
-		sslhandle = dlopen("libssl.so", RTLD_LAZY);
-		cryptohandle = dlopen("libcrypto.so", RTLD_LAZY);
-	}
-	// Give up
-	if (!sslhandle || !cryptohandle)
+	handle *sslhandle = nullptr;
+	handle *cryptohandle = nullptr;
+
+	valid = TryOpenLibraries("libssl.so.3", sslhandle, "libcrypto.so.3", cryptohandle)
+		|| TryOpenLibraries("libssl.so.1.1", sslhandle, "libcrypto.so.1.1", cryptohandle)
+		|| TryOpenLibraries("libssl.so.1.0.0", sslhandle, "libcrypto.so.1.0.0", cryptohandle)
+		// Try the version-less name last, it may not be compatible or tested
+		|| TryOpenLibraries("libssl.so", sslhandle, "libcrypto.so", cryptohandle);
+	if (!valid)
 		return;
 
 	valid = true;
-	valid = valid && (loadSymbol(library_init, sslhandle, "SSL_library_init") ||
-			loadSymbol(init_ssl, sslhandle, "OPENSSL_init_ssl"));
-
-	valid = valid && loadSymbol(CTX_new, sslhandle, "SSL_CTX_new");
-	valid = valid && loadSymbol(CTX_ctrl, sslhandle, "SSL_CTX_ctrl");
-	valid = valid && loadSymbol(CTX_set_verify, sslhandle, "SSL_CTX_set_verify");
-	valid = valid && loadSymbol(CTX_set_default_verify_paths, sslhandle, "SSL_CTX_set_default_verify_paths");
-	valid = valid && loadSymbol(CTX_free, sslhandle, "SSL_CTX_free");
-
-	valid = valid && loadSymbol(SSL_new, sslhandle, "SSL_new");
-	valid = valid && loadSymbol(SSL_free, sslhandle, "SSL_free");
-	valid = valid && loadSymbol(set_fd, sslhandle, "SSL_set_fd");
-	valid = valid && loadSymbol(connect, sslhandle, "SSL_connect");
-	valid = valid && loadSymbol(read, sslhandle, "SSL_read");
-	valid = valid && loadSymbol(write, sslhandle, "SSL_write");
-	valid = valid && loadSymbol(shutdown, sslhandle, "SSL_shutdown");
-	valid = valid && loadSymbol(get_verify_result, sslhandle, "SSL_get_verify_result");
-	valid = valid && loadSymbol(get_peer_certificate, sslhandle, "SSL_get_peer_certificate");
-
-	valid = valid && (loadSymbol(SSLv23_method, sslhandle, "SSLv23_method") ||
-			loadSymbol(SSLv23_method, sslhandle, "TLS_method"));
-
-	valid = valid && loadSymbol(check_host, cryptohandle, "X509_check_host");
+	valid = valid && (
+			LoadSymbol(init_ssl, sslhandle, "OPENSSL_init_ssl") ||
+			LoadSymbol(library_init, sslhandle, "SSL_library_init"));
+
+	valid = valid && LoadSymbol(CTX_new, sslhandle, "SSL_CTX_new");
+	valid = valid && LoadSymbol(CTX_ctrl, sslhandle, "SSL_CTX_ctrl");
+	if (valid)
+		LoadSymbol(CTX_set_options, sslhandle, "SSL_CTX_set_options");
+	valid = valid && LoadSymbol(CTX_set_verify, sslhandle, "SSL_CTX_set_verify");
+	valid = valid && LoadSymbol(CTX_set_default_verify_paths, sslhandle, "SSL_CTX_set_default_verify_paths");
+	valid = valid && LoadSymbol(CTX_free, sslhandle, "SSL_CTX_free");
+
+	valid = valid && LoadSymbol(SSL_new, sslhandle, "SSL_new");
+	valid = valid && LoadSymbol(SSL_free, sslhandle, "SSL_free");
+	valid = valid && LoadSymbol(set_fd, sslhandle, "SSL_set_fd");
+	valid = valid && LoadSymbol(connect, sslhandle, "SSL_connect");
+	valid = valid && LoadSymbol(read, sslhandle, "SSL_read");
+	valid = valid && LoadSymbol(write, sslhandle, "SSL_write");
+	valid = valid && LoadSymbol(shutdown, sslhandle, "SSL_shutdown");
+	valid = valid && LoadSymbol(get_verify_result, sslhandle, "SSL_get_verify_result");
+	valid = valid && (LoadSymbol(get_peer_certificate, sslhandle, "SSL_get1_peer_certificate") ||
+			LoadSymbol(get_peer_certificate, sslhandle, "SSL_get_peer_certificate"));
+
+	valid = valid && (
+			LoadSymbol(SSLv23_method, sslhandle, "TLS_client_method") ||
+			LoadSymbol(SSLv23_method, sslhandle, "TLS_method") ||
+			LoadSymbol(SSLv23_method, sslhandle, "SSLv23_method"));
+
+	valid = valid && LoadSymbol(check_host, cryptohandle, "X509_check_host");
+	valid = valid && LoadSymbol(X509_free, cryptohandle, "X509_free");
 
 	if (library_init)
 		library_init();
@@ -81,7 +88,10 @@ OpenSSLConnection::OpenSSLConnection()
 	if (!context)
 		return;
 
-	ssl.CTX_ctrl(context, SSL_CTRL_OPTIONS, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3, nullptr);
+	if (ssl.CTX_set_options)
+		ssl.CTX_set_options(context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
+	else
+		ssl.CTX_ctrl(context, SSL_CTRL_OPTIONS, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3, nullptr);
 	ssl.CTX_set_verify(context, SSL_VERIFY_PEER, nullptr);
 	ssl.CTX_set_default_verify_paths(context);
 }
@@ -123,6 +133,7 @@ bool OpenSSLConnection::connect(const std::string &hostname, uint16_t port)
 		close();
 		return false;
 	}
+	ssl.X509_free(cert);
 
 	return true;
 }

+ 2 - 0
src/libraries/luahttps/src/generic/OpenSSLConnection.h

@@ -36,6 +36,7 @@ private:
 
 		SSL_CTX *(*CTX_new)(const SSL_METHOD *method);
 		long (*CTX_ctrl)(SSL_CTX *ctx, int cmd, long larg, void *parg);
+		long (*CTX_set_options)(SSL_CTX *ctx, long options);
 		void (*CTX_set_verify)(SSL_CTX *ctx, int mode, void *verify_callback);
 		int (*CTX_set_default_verify_paths)(SSL_CTX *ctx);
 		void (*CTX_free)(SSL_CTX *ctx);
@@ -53,6 +54,7 @@ private:
 		const SSL_METHOD *(*SSLv23_method)();
 
 		int (*check_host)(X509 *cert, const char *name, size_t namelen, unsigned int flags, char **peername);
+		void (*X509_free)(X509* cert);
 	};
 	static SSLFuncs ssl;
 };

+ 33 - 0
src/libraries/luahttps/src/generic/UnixLibraryLoader.cpp

@@ -0,0 +1,33 @@
+#include "../common/config.h"
+#include "../common/LibraryLoader.h"
+
+#ifdef HTTPS_LIBRARY_LOADER_UNIX
+
+#include <dlfcn.h>
+
+namespace LibraryLoader
+{
+	handle *OpenLibrary(const char *name)
+	{
+		return dlopen(name, RTLD_LAZY);
+	}
+
+	void CloseLibrary(handle *handle)
+	{
+		if (handle)
+			dlclose(handle);
+	}
+
+	handle* GetCurrentProcessHandle()
+	{
+		return RTLD_DEFAULT;
+	}
+
+	function *GetFunction(handle *handle, const char *name)
+	{
+		return reinterpret_cast<function *>(dlsym(handle, name));
+	}
+}
+
+#endif // HTTPS_LIBRARY_LOADER_UNIX
+

+ 35 - 0
src/libraries/luahttps/src/windows/WindowsLibraryLoader.cpp

@@ -0,0 +1,35 @@
+#include "../common/config.h"
+#include "../common/LibraryLoader.h"
+
+#ifdef HTTPS_LIBRARY_LOADER_WINDOWS
+#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
+
+#include <windows.h>
+
+namespace LibraryLoader
+{
+	handle *OpenLibrary(const char *name)
+	{
+		return reinterpret_cast<handle *>(LoadLibraryA(name));
+	}
+
+	void CloseLibrary(handle *handle)
+	{
+		if (handle)
+			FreeLibrary(reinterpret_cast<HMODULE>(handle));
+	}
+
+	handle* GetCurrentProcessHandle()
+	{
+		return reinterpret_cast<handle *>(GetModuleHandle(nullptr));
+	}
+
+	function *GetFunction(handle *handle, const char *name)
+	{
+		HMODULE nativeHandle = reinterpret_cast<HMODULE>(handle);
+		return reinterpret_cast<function *>(GetProcAddress(nativeHandle, name));
+	}
+}
+
+#endif // HTTPS_LIBRARY_LOADER_WINDOWS