瀏覽代碼

Merge pull request #3 from slime73/cleanup

Some cleanup to make it easier to build within other codebases.
Alex Szpakowski 3 年之前
父節點
當前提交
c07fb1aa3f

+ 2 - 2
Android.mk

@@ -11,11 +11,11 @@ LOCAL_ARM_NEON := true
 
 
 LOCAL_C_INCLUDES := \
 LOCAL_C_INCLUDES := \
 	${LOCAL_PATH}/src \
 	${LOCAL_PATH}/src \
-	${LOCAL_PATH}/src/android \
-	${LOCAL_PATH}/src/android/ndk-build
+	${LOCAL_PATH}/src/android
 
 
 LOCAL_SRC_FILES := \
 LOCAL_SRC_FILES := \
 	src/lua/main.cpp \
 	src/lua/main.cpp \
+	src/common/HTTPS.cpp \
 	src/common/HTTPRequest.cpp \
 	src/common/HTTPRequest.cpp \
 	src/common/HTTPSClient.cpp \
 	src/common/HTTPSClient.cpp \
 	src/common/PlaintextConnection.cpp \
 	src/common/PlaintextConnection.cpp \

+ 1 - 1
license.txt

@@ -1,4 +1,4 @@
-Copyright (c) 2019-2021 LOVE Development Team
+Copyright (c) 2019-2022 LOVE Development Team
 
 
 This software is provided 'as-is', without any express or implied
 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 warranty.  In no event will the authors be held liable for any damages

+ 20 - 13
src/CMakeLists.txt

@@ -2,7 +2,6 @@ cmake_minimum_required (VERSION 3.13)
 
 
 ### Basic compilation settings
 ### Basic compilation settings
 set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
 set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
-add_definitions (-DNOMINMAX)
 
 
 include_directories (
 include_directories (
 	${CMAKE_CURRENT_SOURCE_DIR}
 	${CMAKE_CURRENT_SOURCE_DIR}
@@ -10,10 +9,7 @@ include_directories (
 )
 )
 
 
 ### Compiler-specific flags
 ### Compiler-specific flags
-if (MSVC)
-	set (DLLEXPORT "__declspec(dllexport)")
-elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
-	set (DLLEXPORT "__attribute__((visibility(\"default\")))")
+if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
 	add_compile_options ("-fvisibility=hidden")
 	add_compile_options ("-fvisibility=hidden")
 	add_link_options ("-fvisibility=hidden")
 	add_link_options ("-fvisibility=hidden")
 endif ()
 endif ()
@@ -24,6 +20,7 @@ add_library (https MODULE
 )
 )
 
 
 add_library (https-common STATIC
 add_library (https-common STATIC
+	common/HTTPS.cpp
 	common/HTTPRequest.cpp
 	common/HTTPRequest.cpp
 	common/HTTPSClient.cpp
 	common/HTTPSClient.cpp
 	common/PlaintextConnection.cpp
 	common/PlaintextConnection.cpp
@@ -42,7 +39,7 @@ add_library (https-schannel STATIC EXCLUDE_FROM_ALL
 )
 )
 
 
 add_library (https-nsurl STATIC EXCLUDE_FROM_ALL
 add_library (https-nsurl STATIC EXCLUDE_FROM_ALL
-	macos/NSURLClient.mm
+	apple/NSURLClient.mm
 )
 )
 
 
 add_library (https-android STATIC EXCLUDE_FROM_ALL
 add_library (https-android STATIC EXCLUDE_FROM_ALL
@@ -54,7 +51,7 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
 	option (USE_CURL_BACKEND "Use the libcurl backend" ON)
 	option (USE_CURL_BACKEND "Use the libcurl backend" ON)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" ON)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" ON)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
-	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
+	option (USE_NSURL_BACKEND "Use the NSUrl backend (apple-only)" OFF)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
@@ -62,7 +59,7 @@ elseif (WIN32)
 	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
 	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" ON)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" ON)
-	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
+	option (USE_NSURL_BACKEND "Use the NSUrl backend (apple-only)" OFF)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" ON)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" ON)
@@ -70,7 +67,7 @@ elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
 	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
 	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
-	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" ON)
+	option (USE_NSURL_BACKEND "Use the NSUrl backend (apple-only)" ON)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
@@ -78,7 +75,7 @@ elseif (ANDROID)
 	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
 	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
-	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
+	option (USE_NSURL_BACKEND "Use the NSUrl backend (apple-only)" OFF)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" ON)
 	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" ON)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
@@ -95,22 +92,26 @@ include_directories (${LUA_INCLUDE_DIR})
 target_link_libraries (https ${LUA_LIBRARIES})
 target_link_libraries (https ${LUA_LIBRARIES})
 
 
 if (USE_CURL_BACKEND)
 if (USE_CURL_BACKEND)
+	set(HTTPS_BACKEND_CURL ON)
 	find_package (CURL REQUIRED)
 	find_package (CURL REQUIRED)
 	include_directories (${CURL_INCLUDE_DIR})
 	include_directories (${CURL_INCLUDE_DIR})
 	target_link_libraries (https https-curl ${CURL_LIBRARIES})
 	target_link_libraries (https https-curl ${CURL_LIBRARIES})
 endif ()
 endif ()
 
 
 if (USE_OPENSSL_BACKEND)
 if (USE_OPENSSL_BACKEND)
+	set(HTTPS_BACKEND_OPENSSL ON)
 	find_package (OpenSSL REQUIRED)
 	find_package (OpenSSL REQUIRED)
 	include_directories (${OPENSSL_INCLUDE_DIR})
 	include_directories (${OPENSSL_INCLUDE_DIR})
 	target_link_libraries (https https-openssl ${OPENSSL_LIBRARIES})
 	target_link_libraries (https https-openssl ${OPENSSL_LIBRARIES})
 endif ()
 endif ()
 
 
 if (USE_SCHANNEL_BACKEND)
 if (USE_SCHANNEL_BACKEND)
+	set(HTTPS_BACKEND_SCHANNEL ON)
 	target_link_libraries (https https-schannel ws2_32 secur32)
 	target_link_libraries (https https-schannel ws2_32 secur32)
 endif ()
 endif ()
 
 
 if (USE_NSURL_BACKEND)
 if (USE_NSURL_BACKEND)
+	set(HTTPS_BACKEND_NSURL ON)
 	set_target_properties(
 	set_target_properties(
 		https-nsurl
 		https-nsurl
 		PROPERTIES
 		PROPERTIES
@@ -122,12 +123,18 @@ if (USE_NSURL_BACKEND)
 endif ()
 endif ()
 
 
 if (USE_ANDROID_BACKEND)
 if (USE_ANDROID_BACKEND)
+	set(HTTPS_BACKEND_ANDROID ON)
 	target_link_libraries (https https-android)
 	target_link_libraries (https https-android)
 	message(STATUS "Ensure to add the Java files to your project too!")
 	message(STATUS "Ensure to add the Java files to your project too!")
 endif ()
 endif ()
 
 
-### Generate config.h
+if (USE_WINSOCK)
+	set(HTTPS_USE_WINSOCK ON)
+endif ()
+
+### Generate config-generated.h
+add_compile_definitions(HTTPS_HAVE_CONFIG_GENERATED_H)
 configure_file (
 configure_file (
-	common/config.h.in
-	common/config.h
+	common/config-generated.h.in
+	common/config-generated.h
 )
 )

+ 2 - 2
src/android/AndroidClient.cpp

@@ -1,6 +1,6 @@
 #include "AndroidClient.h"
 #include "AndroidClient.h"
 
 
-#ifdef USE_ANDROID_BACKEND
+#ifdef HTTPS_BACKEND_ANDROID
 
 
 #include <sstream>
 #include <sstream>
 #include <type_traits>
 #include <type_traits>
@@ -209,4 +209,4 @@ jclass AndroidClient::getHTTPSClass() const
 	return httpsClass;
 	return httpsClass;
 }
 }
 
 
-#endif
+#endif // HTTPS_BACKEND_ANDROID

+ 3 - 3
src/android/AndroidClient.h

@@ -1,12 +1,12 @@
 #pragma once
 #pragma once
 
 
-#include "common/config.h"
+#include "../common/config.h"
 
 
-#ifdef USE_ANDROID_BACKEND
+#ifdef HTTPS_BACKEND_ANDROID
 
 
 #include <jni.h>
 #include <jni.h>
 
 
-#include "common/HTTPSClient.h"
+#include "../common/HTTPSClient.h"
 
 
 class AndroidClient: public HTTPSClient
 class AndroidClient: public HTTPSClient
 {
 {

+ 0 - 2
src/android/ndk-build/common/config.h

@@ -1,2 +0,0 @@
-#define USE_ANDROID_BACKEND
-#define DLLEXPORT __attribute__((visibility ("default")))

+ 7 - 1
src/macos/NSURLClient.h → src/apple/NSURLClient.h

@@ -1,6 +1,10 @@
 #pragma once
 #pragma once
 
 
-#include "common/HTTPSClient.h"
+#include "../common/config.h"
+
+#ifdef HTTPS_BACKEND_NSURL
+
+#include "../common/HTTPSClient.h"
 
 
 class NSURLClient : public HTTPSClient
 class NSURLClient : public HTTPSClient
 {
 {
@@ -8,3 +12,5 @@ public:
 	virtual bool valid() const override;
 	virtual bool valid() const override;
 	virtual HTTPSClient::Reply request(const HTTPSClient::Request &req) override;
 	virtual HTTPSClient::Reply request(const HTTPSClient::Request &req) override;
 };
 };
+
+#endif // HTTPS_BACKEND_NSURL

+ 7 - 3
src/macos/NSURLClient.mm → src/apple/NSURLClient.mm

@@ -1,7 +1,9 @@
-#import <Foundation/Foundation.h>
-
 #include "NSURLClient.h"
 #include "NSURLClient.h"
 
 
+#ifdef HTTPS_BACKEND_NSURL
+
+#import <Foundation/Foundation.h>
+
 #if ! __has_feature(objc_arc)
 #if ! __has_feature(objc_arc)
 #error "ARC is off"
 #error "ARC is off"
 #endif
 #endif
@@ -81,4 +83,6 @@ HTTPSClient::Reply NSURLClient::request(const HTTPSClient::Request &req)
 	}
 	}
 
 
 	return reply;
 	return reply;
-} }
+}}
+
+#endif // HTTPS_BACKEND_NSURL

+ 69 - 0
src/common/HTTPS.cpp

@@ -0,0 +1,69 @@
+#include "HTTPS.h"
+#include "config.h"
+#include "ConnectionClient.h"
+
+#include <stdexcept>
+
+#ifdef HTTPS_BACKEND_CURL
+#	include "../generic/CurlClient.h"
+#endif
+#ifdef HTTPS_BACKEND_OPENSSL
+#	include "../generic/OpenSSLConnection.h"
+#endif
+#ifdef HTTPS_BACKEND_SCHANNEL
+#	include "../windows/SChannelConnection.h"
+#endif
+#ifdef HTTPS_BACKEND_NSURL
+#	include "../apple/NSURLClient.h"
+#endif
+#ifdef HTTPS_BACKEND_ANDROID
+#	include "../android/AndroidClient.h"
+#endif
+
+#ifdef HTTPS_BACKEND_CURL
+	static CurlClient curlclient;
+#endif
+#ifdef HTTPS_BACKEND_OPENSSL
+	static ConnectionClient<OpenSSLConnection> opensslclient;
+#endif
+#ifdef HTTPS_BACKEND_SCHANNEL
+	static ConnectionClient<SChannelConnection> schannelclient;
+#endif
+#ifdef HTTPS_BACKEND_NSURL
+	static NSURLClient nsurlclient;
+#endif
+#ifdef HTTPS_BACKEND_ANDROID
+	static AndroidClient androidclient;
+#endif
+
+static HTTPSClient *clients[] = {
+#ifdef HTTPS_BACKEND_CURL
+	&curlclient,
+#endif
+#ifdef HTTPS_BACKEND_OPENSSL
+	&opensslclient,
+#endif
+#ifdef HTTPS_BACKEND_SCHANNEL
+	&schannelclient,
+#endif
+#ifdef HTTPS_BACKEND_NSURL
+	&nsurlclient,
+#endif
+#ifdef HTTPS_BACKEND_ANDROID
+	&androidclient,
+#endif
+	nullptr,
+};
+
+HTTPSClient::Reply request(const HTTPSClient::Request &req)
+{
+	for (size_t i = 0; clients[i]; ++i)
+	{
+		HTTPSClient &client = *clients[i];
+
+		if (client.valid())
+			return client.request(req);
+	}
+
+	throw std::runtime_error("No applicable HTTPS implementation found");
+}

+ 5 - 0
src/common/HTTPS.h

@@ -0,0 +1,5 @@
+#pragma once
+
+#include "HTTPSClient.h"
+
+HTTPSClient::Reply request(const HTTPSClient::Request &req);

+ 7 - 7
src/common/PlaintextConnection.cpp

@@ -1,6 +1,6 @@
-#include "common/config.h"
+#include "config.h"
 #include <cstring>
 #include <cstring>
-#ifndef USE_WINSOCK
+#ifndef HTTPS_USE_WINSOCK
 #	include <netdb.h>
 #	include <netdb.h>
 #	include <unistd.h>
 #	include <unistd.h>
 #	include <sys/types.h>
 #	include <sys/types.h>
@@ -8,28 +8,28 @@
 #else
 #else
 #	include <winsock2.h>
 #	include <winsock2.h>
 #	include <ws2tcpip.h>
 #	include <ws2tcpip.h>
-#endif // USE_WINSOCK
+#endif // HTTPS_USE_WINSOCK
 
 
 #include "PlaintextConnection.h"
 #include "PlaintextConnection.h"
 
 
-#ifdef USE_WINSOCK
+#ifdef HTTPS_USE_WINSOCK
 	static void close(int fd)
 	static void close(int fd)
 	{
 	{
 		closesocket(fd);
 		closesocket(fd);
 	}
 	}
-#endif // USE_WINSOCK
+#endif // HTTPS_USE_WINSOCK
 
 
 PlaintextConnection::PlaintextConnection()
 PlaintextConnection::PlaintextConnection()
 	: fd(-1)
 	: fd(-1)
 {
 {
-#ifdef USE_WINSOCK
+#ifdef HTTPS_USE_WINSOCK
 	static bool wsaInit = false;
 	static bool wsaInit = false;
 	if (!wsaInit)
 	if (!wsaInit)
 	{
 	{
 		WSADATA data;
 		WSADATA data;
 		WSAStartup(MAKEWORD(2, 2), &data);
 		WSAStartup(MAKEWORD(2, 2), &data);
 	}
 	}
-#endif // USE_WINSOCK
+#endif // HTTPS_USE_WINSOCK
 }
 }
 
 
 PlaintextConnection::~PlaintextConnection()
 PlaintextConnection::~PlaintextConnection()

+ 7 - 0
src/common/config-generated.h.in

@@ -0,0 +1,7 @@
+#cmakedefine HTTPS_BACKEND_CURL
+#cmakedefine HTTPS_BACKEND_OPENSSL
+#cmakedefine HTTPS_BACKEND_SCHANNEL
+#cmakedefine HTTPS_BACKEND_NSURL
+#cmakedefine HTTPS_BACKEND_ANDROID
+#cmakedefine HTTPS_USE_WINSOCK
+#cmakedefine DEBUG_SCHANNEL

+ 33 - 0
src/common/config.h

@@ -0,0 +1,33 @@
+#pragma once
+
+#if defined(HTTPS_HAVE_CONFIG_GENERATED_H)
+	#include "common/config-generated.h"
+#elif defined(WIN32) || defined(_WIN32)
+	#define HTTPS_BACKEND_SCHANNEL
+	#define HTTPS_USE_WINSOCK
+#elif defined(__ANDROID__)
+	#define HTTPS_BACKEND_ANDROID
+#elif defined(__APPLE__)
+	#define HTTPS_BACKEND_NSURL
+#elif defined(linux) || defined(__linux) || defined(__linux__)
+	#if defined __has_include
+		#if __has_include(<curl/curl.h>)
+			#define HTTPS_BACKEND_CURL
+		#endif
+		#if __has_include(<openssl/ssl.h>)
+			#define HTTPS_BACKEND_OPENSSL
+		#endif
+	#else
+		// Hope for the best...
+		#define HTTPS_BACKEND_CURL
+		#define HTTPS_BACKEND_OPENSSL
+	#endif
+#endif
+
+#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
+	#define HTTPS_DLLEXPORT __declspec(dllexport)
+#elif defined(__GNUC__) || defined(__clang__)
+	#define HTTPS_DLLEXPORT __attribute__ ((visibility("default")))
+#else
+	#define HTTPS_DLLEXPORT
+#endif

+ 0 - 8
src/common/config.h.in

@@ -1,8 +0,0 @@
-#cmakedefine USE_CURL_BACKEND
-#cmakedefine USE_OPENSSL_BACKEND
-#cmakedefine USE_SCHANNEL_BACKEND
-#cmakedefine USE_NSURL_BACKEND
-#cmakedefine USE_ANDROID_BACKEND
-#cmakedefine USE_WINSOCK
-#define DLLEXPORT @DLLEXPORT@
-#cmakedefine DEBUG_SCHANNEL

+ 6 - 2
src/generic/CurlClient.cpp

@@ -1,10 +1,12 @@
+#include "CurlClient.h"
+
+#ifdef HTTPS_BACKEND_CURL
+
 #include <dlfcn.h>
 #include <dlfcn.h>
 #include <stdexcept>
 #include <stdexcept>
 #include <sstream>
 #include <sstream>
 #include <vector>
 #include <vector>
 
 
-#include "CurlClient.h"
-
 CurlClient::Curl::Curl()
 CurlClient::Curl::Curl()
 {
 {
 	void *handle = dlopen("libcurl.so", RTLD_LAZY);
 	void *handle = dlopen("libcurl.so", RTLD_LAZY);
@@ -120,3 +122,5 @@ HTTPSClient::Reply CurlClient::request(const HTTPSClient::Request &req)
 }
 }
 
 
 CurlClient::Curl CurlClient::curl;
 CurlClient::Curl CurlClient::curl;
+
+#endif // HTTPS_BACKEND_CURL

+ 7 - 1
src/generic/CurlClient.h

@@ -1,8 +1,12 @@
 #pragma once
 #pragma once
 
 
+#include "../common/config.h"
+
+#ifdef HTTPS_BACKEND_CURL
+
 #include <curl/curl.h>
 #include <curl/curl.h>
 
 
-#include "common/HTTPSClient.h"
+#include "../common/HTTPSClient.h"
 
 
 class CurlClient : public HTTPSClient
 class CurlClient : public HTTPSClient
 {
 {
@@ -26,3 +30,5 @@ private:
 		void (*slist_free_all)(curl_slist *list);
 		void (*slist_free_all)(curl_slist *list);
 	} curl;
 	} curl;
 };
 };
+
+#endif // HTTPS_BACKEND_CURL

+ 6 - 2
src/generic/OpenSSLConnection.cpp

@@ -1,7 +1,9 @@
-#include <dlfcn.h>
-
 #include "OpenSSLConnection.h"
 #include "OpenSSLConnection.h"
 
 
+#ifdef HTTPS_BACKEND_OPENSSL
+
+#include <dlfcn.h>
+
 // Not present in openssl 1.1 headers
 // Not present in openssl 1.1 headers
 #define SSL_CTRL_OPTIONS 32
 #define SSL_CTRL_OPTIONS 32
 
 
@@ -142,3 +144,5 @@ void OpenSSLConnection::close()
 }
 }
 
 
 OpenSSLConnection::SSLFuncs OpenSSLConnection::ssl;
 OpenSSLConnection::SSLFuncs OpenSSLConnection::ssl;
+
+#endif // HTTPS_BACKEND_OPENSSL

+ 8 - 2
src/generic/OpenSSLConnection.h

@@ -1,9 +1,13 @@
 #pragma once
 #pragma once
 
 
+#include "../common/config.h"
+
+#ifdef HTTPS_BACKEND_OPENSSL
+
 #include <openssl/ssl.h>
 #include <openssl/ssl.h>
 
 
-#include "common/Connection.h"
-#include "common/PlaintextConnection.h"
+#include "../common/Connection.h"
+#include "../common/PlaintextConnection.h"
 
 
 class OpenSSLConnection : public Connection
 class OpenSSLConnection : public Connection
 {
 {
@@ -52,3 +56,5 @@ private:
 	};
 	};
 	static SSLFuncs ssl;
 	static SSLFuncs ssl;
 };
 };
+
+#endif // HTTPS_BACKEND_OPENSSL

+ 25 - 93
src/lua/main.cpp

@@ -1,59 +1,7 @@
 #include <lua.hpp>
 #include <lua.hpp>
 
 
-// Sorry for the ifdef soup ahead
-#include "common/config.h"
-#include "common/HTTPSClient.h"
-#include "common/ConnectionClient.h"
-#ifdef USE_CURL_BACKEND
-#	include "generic/CurlClient.h"
-#endif
-#ifdef USE_OPENSSL_BACKEND
-#	include "generic/OpenSSLConnection.h"
-#endif
-#ifdef USE_SCHANNEL_BACKEND
-#	include "windows/SChannelConnection.h"
-#endif
-#ifdef USE_NSURL_BACKEND
-#	include "macos/NSURLClient.h"
-#endif
-#ifdef USE_ANDROID_BACKEND
-#	include "android/AndroidClient.h"
-#endif
-
-#ifdef USE_CURL_BACKEND
-	static CurlClient curlclient;
-#endif
-#ifdef USE_OPENSSL_BACKEND
-	static ConnectionClient<OpenSSLConnection> opensslclient;
-#endif
-#ifdef USE_SCHANNEL_BACKEND
-	static ConnectionClient<SChannelConnection> schannelclient;
-#endif
-#ifdef USE_NSURL_BACKEND
-	static NSURLClient nsurlclient;
-#endif
-#ifdef USE_ANDROID_BACKEND
-	static AndroidClient androidclient;
-#endif
-
-static HTTPSClient *clients[] = {
-#ifdef USE_CURL_BACKEND
-	&curlclient,
-#endif
-#ifdef USE_OPENSSL_BACKEND
-	&opensslclient,
-#endif
-#ifdef USE_SCHANNEL_BACKEND
-	&schannelclient,
-#endif
-#ifdef USE_NSURL_BACKEND
-	&nsurlclient,
-#endif
-#ifdef USE_ANDROID_BACKEND
-	&androidclient,
-#endif
-	nullptr,
-};
+#include "../common/HTTPS.h"
+#include "../common/config.h"
 
 
 static std::string w_checkstring(lua_State *L, int idx)
 static std::string w_checkstring(lua_State *L, int idx)
 {
 {
@@ -103,8 +51,6 @@ static int w_request(lua_State *L)
 	auto url = w_checkstring(L, 1);
 	auto url = w_checkstring(L, 1);
 	HTTPSClient::Request req(url);
 	HTTPSClient::Request req(url);
 
 
-	std::string errorMessage("No applicable implementation found");
-	bool foundClient = false;
 	bool advanced = false;
 	bool advanced = false;
 
 
 	if (lua_istable(L, 2))
 	if (lua_istable(L, 2))
@@ -131,52 +77,38 @@ static int w_request(lua_State *L)
 		lua_pop(L, 1);
 		lua_pop(L, 1);
 	}
 	}
 
 
-	for (size_t i = 0; clients[i]; ++i)
-	{
-		HTTPSClient &client = *clients[i];
-		HTTPSClient::Reply reply;
-
-		if (!client.valid())
-			continue;
-
-		try
-		{
-			reply = client.request(req);
-		}
-		catch(const std::exception& e)
-		{
-			errorMessage = e.what();
-			break;
-		}
-		
-		lua_pushinteger(L, reply.responseCode);
-		w_pushstring(L, reply.body);
-
-		if (advanced)
-		{
-			lua_newtable(L);
-			for (const auto &header : reply.headers)
-			{
-				w_pushstring(L, header.first);
-				w_pushstring(L, header.second);
-				lua_settable(L, -3);
-			}
-		}
+	HTTPSClient::Reply reply;
 
 
-		foundClient = true;
-		break;
+	try
+	{
+		reply = request(req);
 	}
 	}
-
-	if (!foundClient)
+	catch (const std::exception& e)
 	{
 	{
+		std::string errorMessage = e.what();
 		lua_pushnil(L);
 		lua_pushnil(L);
 		lua_pushstring(L, errorMessage.c_str());
 		lua_pushstring(L, errorMessage.c_str());
+		return 2;
+	}
+
+	lua_pushinteger(L, reply.responseCode);
+	w_pushstring(L, reply.body);
+
+	if (advanced)
+	{
+		lua_newtable(L);
+		for (const auto &header : reply.headers)
+		{
+			w_pushstring(L, header.first);
+			w_pushstring(L, header.second);
+			lua_settable(L, -3);
+		}
 	}
 	}
 
 
-	return (advanced && foundClient) ? 3 : 2;
+	return advanced ? 3 : 2;
 }
 }
 
 
-extern "C" int DLLEXPORT luaopen_https(lua_State *L)
+extern "C" int HTTPS_DLLEXPORT luaopen_https(lua_State *L)
 {
 {
 	lua_newtable(L);
 	lua_newtable(L);
 
 

+ 8 - 3
src/windows/SChannelConnection.cpp

@@ -1,4 +1,10 @@
 #define SECURITY_WIN32
 #define SECURITY_WIN32
+#define NOMINMAX
+
+#include "SChannelConnection.h"
+
+#ifdef HTTPS_BACKEND_SCHANNEL
+
 #include <windows.h>
 #include <windows.h>
 #include <security.h>
 #include <security.h>
 #include <schnlsp.h>
 #include <schnlsp.h>
@@ -7,9 +13,6 @@
 #include <memory>
 #include <memory>
 #include <array>
 #include <array>
 
 
-#include "common/config.h"
-#include "SChannelConnection.h"
-
 #ifndef SCH_USE_STRONG_CRYPTO
 #ifndef SCH_USE_STRONG_CRYPTO
 #	define SCH_USE_STRONG_CRYPTO 0x00400000
 #	define SCH_USE_STRONG_CRYPTO 0x00400000
 #endif
 #endif
@@ -459,3 +462,5 @@ bool SChannelConnection::valid()
 {
 {
 	return true;
 	return true;
 }
 }
+
+#endif // HTTPS_BACKEND_SCHANNEL

+ 8 - 2
src/windows/SChannelConnection.h

@@ -1,7 +1,11 @@
 #pragma once
 #pragma once
 
 
-#include "common/Connection.h"
-#include "common/PlaintextConnection.h"
+#include "../common/config.h"
+
+#ifdef HTTPS_BACKEND_SCHANNEL
+
+#include "../common/Connection.h"
+#include "../common/PlaintextConnection.h"
 
 
 #include <vector>
 #include <vector>
 
 
@@ -29,3 +33,5 @@ private:
 	size_t decrypt(char *buffer, size_t size, bool recurse = true);
 	size_t decrypt(char *buffer, size_t size, bool recurse = true);
 	void destroyContext();
 	void destroyContext();
 };
 };
+
+#endif // HTTPS_BACKEND_SCHANNEL