Browse Source

Allow easier building without specific build system setup.

Alex Szpakowski 3 years ago
parent
commit
00f2a28f6f

+ 2 - 2
Android.mk

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

+ 11 - 8
src/CMakeLists.txt

@@ -2,7 +2,6 @@ cmake_minimum_required (VERSION 3.13)
 
 ### Basic compilation settings
 set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
-add_definitions (-DNOMINMAX)
 
 include_directories (
 	${CMAKE_CURRENT_SOURCE_DIR}
@@ -10,10 +9,7 @@ include_directories (
 )
 
 ### 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_link_options ("-fvisibility=hidden")
 endif ()
@@ -24,6 +20,7 @@ add_library (https MODULE
 )
 
 add_library (https-common STATIC
+	common/HTTPS.cpp
 	common/HTTPRequest.cpp
 	common/HTTPSClient.cpp
 	common/PlaintextConnection.cpp
@@ -95,22 +92,26 @@ include_directories (${LUA_INCLUDE_DIR})
 target_link_libraries (https ${LUA_LIBRARIES})
 
 if (USE_CURL_BACKEND)
+	set(HTTPS_BACKEND_CURL ON)
 	find_package (CURL REQUIRED)
 	include_directories (${CURL_INCLUDE_DIR})
 	target_link_libraries (https https-curl ${CURL_LIBRARIES})
 endif ()
 
 if (USE_OPENSSL_BACKEND)
+	set(HTTPS_BACKEND_OPENSSL ON)
 	find_package (OpenSSL REQUIRED)
 	include_directories (${OPENSSL_INCLUDE_DIR})
 	target_link_libraries (https https-openssl ${OPENSSL_LIBRARIES})
 endif ()
 
 if (USE_SCHANNEL_BACKEND)
+	set(HTTPS_BACKEND_SCHANNEL ON)
 	target_link_libraries (https https-schannel ws2_32 secur32)
 endif ()
 
 if (USE_NSURL_BACKEND)
+	set(HTTPS_BACKEND_NSURL ON)
 	set_target_properties(
 		https-nsurl
 		PROPERTIES
@@ -122,12 +123,14 @@ if (USE_NSURL_BACKEND)
 endif ()
 
 if (USE_ANDROID_BACKEND)
+	set(HTTPS_BACKEND_ANDROID ON)
 	target_link_libraries (https https-android)
 	message(STATUS "Ensure to add the Java files to your project too!")
 endif ()
 
-### Generate config.h
+### Generate config-generated.h
+add_compile_definitions(HTTPS_HAVE_CONFIG_GENERATED_H)
 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"
 
-#ifdef USE_ANDROID_BACKEND
+#ifdef HTTPS_BACKEND_ANDROID
 
 #include <sstream>
 #include <type_traits>
@@ -209,4 +209,4 @@ jclass AndroidClient::getHTTPSClass() const
 	return httpsClass;
 }
 
-#endif
+#endif // HTTPS_BACKEND_ANDROID

+ 3 - 3
src/android/AndroidClient.h

@@ -1,12 +1,12 @@
 #pragma once
 
-#include "common/config.h"
+#include "../common/config.h"
 
-#ifdef USE_ANDROID_BACKEND
+#ifdef HTTPS_BACKEND_ANDROID
 
 #include <jni.h>
 
-#include "common/HTTPSClient.h"
+#include "../common/HTTPSClient.h"
 
 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")))

+ 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 "../macos/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>
-#ifndef USE_WINSOCK
+#ifndef HTTPS_USE_WINSOCK
 #	include <netdb.h>
 #	include <unistd.h>
 #	include <sys/types.h>
@@ -8,28 +8,28 @@
 #else
 #	include <winsock2.h>
 #	include <ws2tcpip.h>
-#endif // USE_WINSOCK
+#endif // HTTPS_USE_WINSOCK
 
 #include "PlaintextConnection.h"
 
-#ifdef USE_WINSOCK
+#ifdef HTTPS_USE_WINSOCK
 	static void close(int fd)
 	{
 		closesocket(fd);
 	}
-#endif // USE_WINSOCK
+#endif // HTTPS_USE_WINSOCK
 
 PlaintextConnection::PlaintextConnection()
 	: fd(-1)
 {
-#ifdef USE_WINSOCK
+#ifdef HTTPS_USE_WINSOCK
 	static bool wsaInit = false;
 	if (!wsaInit)
 	{
 		WSADATA data;
 		WSAStartup(MAKEWORD(2, 2), &data);
 	}
-#endif // USE_WINSOCK
+#endif // HTTPS_USE_WINSOCK
 }
 
 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 "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 <stdexcept>
 #include <sstream>
 #include <vector>
 
-#include "CurlClient.h"
-
 CurlClient::Curl::Curl()
 {
 	void *handle = dlopen("libcurl.so", RTLD_LAZY);
@@ -120,3 +122,5 @@ HTTPSClient::Reply CurlClient::request(const HTTPSClient::Request &req)
 }
 
 CurlClient::Curl CurlClient::curl;
+
+#endif // HTTPS_BACKEND_CURL

+ 7 - 1
src/generic/CurlClient.h

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

+ 6 - 2
src/generic/OpenSSLConnection.cpp

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

+ 8 - 2
src/generic/OpenSSLConnection.h

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

+ 23 - 93
src/lua/main.cpp

@@ -1,59 +1,7 @@
 #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)
 {
@@ -103,8 +51,6 @@ static int w_request(lua_State *L)
 	auto url = w_checkstring(L, 1);
 	HTTPSClient::Request req(url);
 
-	std::string errorMessage("No applicable implementation found");
-	bool foundClient = false;
 	bool advanced = false;
 
 	if (lua_istable(L, 2))
@@ -131,52 +77,36 @@ static int w_request(lua_State *L)
 		lua_pop(L, 1);
 	}
 
-	for (size_t i = 0; clients[i]; ++i)
+	try
 	{
-		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);
-			}
-		}
-
-		foundClient = true;
-		break;
+		reply = request(req);
 	}
-
-	if (!foundClient)
+	catch (const std::exception& e)
 	{
+		std::string errorMessage = e.what();
 		lua_pushnil(L);
 		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);
 

+ 7 - 1
src/macos/NSURLClient.h

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

+ 7 - 3
src/macos/NSURLClient.mm

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

+ 8 - 3
src/windows/SChannelConnection.cpp

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

+ 8 - 2
src/windows/SChannelConnection.h

@@ -1,7 +1,11 @@
 #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>
 
@@ -29,3 +33,5 @@ private:
 	size_t decrypt(char *buffer, size_t size, bool recurse = true);
 	void destroyContext();
 };
+
+#endif // HTTPS_BACKEND_SCHANNEL