From 2e7fa50bf84f3cf4ff10a0d7ff548f1446b8ce4d Mon Sep 17 00:00:00 2001
From: Joe <rubberduckie3554@gmail.com>
Date: Tue, 6 Jul 2021 18:55:57 -0400
Subject: [PATCH 1/4] Fix things for windows builds

---
 CMakeLists.txt             |  6 ++++++
 examples/coeurl.cpp        |  2 +-
 include/coeurl/request.hpp | 16 ++++++++--------
 lib/client.cpp             | 15 +++++++--------
 lib/request.cpp            | 14 +++++++-------
 meson.build                | 22 +++++++++++++++++++---
 tests/requests.cpp         | 12 ++++++------
 tests/requests_tls.cpp     | 12 ++++++------
 8 files changed, 60 insertions(+), 39 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8231bae..58a7c83 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -83,6 +83,12 @@ endif()
 find_package(spdlog 1.0.0 CONFIG REQUIRED)
 target_link_libraries(coeurl PUBLIC spdlog::spdlog)
 
+IF(MSVC)
+    add_compile_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN)
+	target_link_libraries(coeurl wsock32 ws2_32)
+	target_link_libraries(coeurl iphlpapi)
+ENDIF()
+
 
 # INSTALL MAGIC
 include(GNUInstallDirs)
diff --git a/examples/coeurl.cpp b/examples/coeurl.cpp
index ca7cad4..b4e750e 100644
--- a/examples/coeurl.cpp
+++ b/examples/coeurl.cpp
@@ -25,7 +25,7 @@ int main(int, char **) {
       });
 
     auto r = std::make_shared<coeurl::Request>(
-        &g, coeurl::Request::Method::POST, "http://localhost:5000/post");
+        &g, coeurl::Request::Method::Post, "http://localhost:5000/post");
     r->request("ABCD");
     r->on_complete([](const coeurl::Request &) {});
     g.submit_request(r);
diff --git a/include/coeurl/request.hpp b/include/coeurl/request.hpp
index a5a4e7b..d8f53c9 100644
--- a/include/coeurl/request.hpp
+++ b/include/coeurl/request.hpp
@@ -12,13 +12,13 @@ struct Client;
 
 struct Request {
   enum class Method {
-    DELETE,
-    GET,
-    HEAD,
-    OPTIONS,
-    PATCH,
-    POST,
-    PUT,
+    Delete,
+    Get,
+    Head,
+    Options,
+    Patch,
+    Post,
+    Put,
   };
 
   Request(Client *client, Method m, std::string url_);
@@ -75,7 +75,7 @@ private:
     Done,
   } status = Status::Running;
   CURLcode curl_error = CURLcode::CURLE_OK;
-  Method method = Method::GET;
+  Method method = Method::Get;
 
   std::function<void(const Request &)> on_complete_;
   std::function<void(size_t progress, size_t total)> on_upload_progress_,
diff --git a/lib/client.cpp b/lib/client.cpp
index 21d9381..d750da0 100644
--- a/lib/client.cpp
+++ b/lib/client.cpp
@@ -168,7 +168,7 @@ void Client::remsock(SockInfo *f) {
 
 /* Assign information to a SockInfo structure */
 void Client::setsock(SockInfo *f, curl_socket_t s, int act) {
-  int kind = ((act & CURL_POLL_IN) ? EV_READ : 0) |
+  short kind = ((act & CURL_POLL_IN) ? EV_READ : 0) |
              ((act & CURL_POLL_OUT) ? EV_WRITE : 0) | EV_PERSIST;
 
   f->sockfd = s;
@@ -214,7 +214,6 @@ Client::Client() {
   std::once_flag once;
 #ifdef WIN32
   std::call_once(once, evthread_use_windows_threads);
-  (void)threading;
 #elif defined(EVENT__HAVE_PTHREADS)
   std::call_once(once, evthread_use_pthreads);
 #else
@@ -350,7 +349,7 @@ void Client::remove_request(Request *r) {
 void Client::get(std::string url, std::function<void(const Request &)> callback,
                  const Headers &headers, long max_redirects) {
   auto req =
-      std::make_shared<Request>(this, Request::Method::GET, std::move(url));
+      std::make_shared<Request>(this, Request::Method::Get, std::move(url));
 
   req->on_complete(std::move(callback));
 
@@ -367,7 +366,7 @@ void Client::delete_(std::string url,
                      std::function<void(const Request &)> callback,
                      const Headers &headers, long max_redirects) {
   auto req =
-      std::make_shared<Request>(this, Request::Method::DELETE, std::move(url));
+      std::make_shared<Request>(this, Request::Method::Delete, std::move(url));
 
   req->on_complete(std::move(callback));
 
@@ -384,7 +383,7 @@ void Client::head(std::string url,
                   std::function<void(const Request &)> callback,
                   const Headers &headers, long max_redirects) {
   auto req =
-      std::make_shared<Request>(this, Request::Method::HEAD, std::move(url));
+      std::make_shared<Request>(this, Request::Method::Head, std::move(url));
 
   req->on_complete(std::move(callback));
 
@@ -401,7 +400,7 @@ void Client::options(std::string url,
                      std::function<void(const Request &)> callback,
                      const Headers &headers, long max_redirects) {
   auto req =
-      std::make_shared<Request>(this, Request::Method::OPTIONS, std::move(url));
+      std::make_shared<Request>(this, Request::Method::Options, std::move(url));
 
   req->on_complete(std::move(callback));
 
@@ -419,7 +418,7 @@ void Client::put(std::string url, std::string request_body,
                  std::function<void(const Request &)> callback,
                  const Headers &headers, long max_redirects) {
   auto req =
-      std::make_shared<Request>(this, Request::Method::PUT, std::move(url));
+      std::make_shared<Request>(this, Request::Method::Put, std::move(url));
 
   req->request(request_body, mimetype);
   req->on_complete(std::move(callback));
@@ -438,7 +437,7 @@ void Client::post(std::string url, std::string request_body,
                   std::function<void(const Request &)> callback,
                   const Headers &headers, long max_redirects) {
   auto req =
-      std::make_shared<Request>(this, Request::Method::POST, std::move(url));
+      std::make_shared<Request>(this, Request::Method::Post, std::move(url));
 
   req->request(request_body, mimetype);
   req->on_complete(std::move(callback));
diff --git a/lib/request.cpp b/lib/request.cpp
index 35368c1..460a90a 100644
--- a/lib/request.cpp
+++ b/lib/request.cpp
@@ -124,27 +124,27 @@ Request::Request(Client *client, Method m, std::string url__)
   curl_easy_setopt(this->easy, CURLOPT_ACCEPT_ENCODING, "");
 
   switch (m) {
-  case Method::DELETE:
+  case Method::Delete:
     curl_easy_setopt(this->easy, CURLOPT_HTTPGET, 0L);
     curl_easy_setopt(this->easy, CURLOPT_CUSTOMREQUEST, "DELETE");
     break;
-  case Method::GET:
+  case Method::Get:
     curl_easy_setopt(this->easy, CURLOPT_HTTPGET, 1L);
     break;
-  case Method::HEAD:
+  case Method::Head:
     curl_easy_setopt(this->easy, CURLOPT_NOBODY, 1L);
     break;
-  case Method::OPTIONS:
+  case Method::Options:
     curl_easy_setopt(this->easy, CURLOPT_CUSTOMREQUEST, "OPTIONS");
     break;
-  case Method::PATCH:
+  case Method::Patch:
     curl_easy_setopt(this->easy, CURLOPT_CUSTOMREQUEST, "PATCH");
     break;
-  case Method::POST:
+  case Method::Post:
     curl_easy_setopt(this->easy, CURLOPT_POST, 1L);
     request("");
     break;
-  case Method::PUT:
+  case Method::Put:
     curl_easy_setopt(this->easy, CURLOPT_CUSTOMREQUEST, "PUT");
     request("");
     break;
diff --git a/meson.build b/meson.build
index aee4970..dadb2be 100644
--- a/meson.build
+++ b/meson.build
@@ -24,7 +24,6 @@ if (not libevent_dep.found()
 cmake = import('cmake')
 libevent_options = cmake.subproject_options()
     libevent_options.add_cmake_defines({
-      'CMAKE_C_FLAGS': '-fPIC',
       'EVENT__LIBRARY_TYPE': 'STATIC',
       'BUILD_SHARED_LIBS': false,
       'BUILD_SHARED_AND_STATIC_LIBS': false,
@@ -34,6 +33,11 @@ libevent_options = cmake.subproject_options()
       'EVENT__DISABLE_REGRESS': true,
       'EVENT__DISABLE_SAMPLES': true,
       })
+    if target_machine.system() != 'windows'
+        libcurl_options.add_cmake_defines({
+          'CMAKE_C_FLAGS':  '-fPIC',
+        })
+    endif
     libevent_options.set_override_option('werror', 'false')
     libevent_options.set_override_option('warning_level', '0')
     libevent_proj = cmake.subproject('libevent', options: libevent_options)
@@ -55,12 +59,16 @@ if (not libcurl_dep.found()
 cmake = import('cmake')
 libcurl_options = cmake.subproject_options()
     libcurl_options.add_cmake_defines({
-      'CMAKE_C_FLAGS': '-fPIC',
       'BUILD_SHARED_LIBS': false,
       'BUILD_SHARED_AND_STATIC_LIBS': false,
       'HTTP_ONLY': true,
       'CMAKE_USE_LIBSSH2': false,
       })
+    if target_machine.system() != 'windows'
+        libcurl_options.add_cmake_defines({
+          'CMAKE_C_FLAGS':  '-fPIC',
+        })
+    endif
     libcurl_options.set_override_option('werror', 'false')
     libcurl_options.set_override_option('warning_level', '0')
     libcurl_proj = cmake.subproject('curl', options: libcurl_options)
@@ -72,7 +80,15 @@ include = include_directories('include')
 
 defines = []
 if target_machine.system() == 'windows'
-  defines += '-DWIN32_LEAN_AND_MEAN'
+  defines += ['-DNOMINMAX', '-DWIN32_LEAN_AND_MEAN']
+endif
+
+if target_machine.system() == 'windows'
+  cc = meson.get_compiler('c')
+  deps += [
+		cc.find_library('ws2_32', required: true),  # socket functions
+		cc.find_library('iphlpapi', required: true) # if_nametoindex needed for libevent
+  ]
 endif
 
 lib = library('coeurl', ['lib/client.cpp', 'lib/request.cpp'],
diff --git a/tests/requests.cpp b/tests/requests.cpp
index 45943cc..c0b3bc5 100644
--- a/tests/requests.cpp
+++ b/tests/requests.cpp
@@ -25,7 +25,7 @@ TEST_CASE("Basic request") {
 TEST_CASE("Basic manual request") {
   Client g{};
 
-  auto r = std::make_shared<Request>(&g, Request::Method::GET,
+  auto r = std::make_shared<Request>(&g, Request::Method::Get,
                                      "http://localhost:5000/");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "http://localhost:5000/");
@@ -77,7 +77,7 @@ TEST_CASE("Max redirects") {
 TEST_CASE("Basic manual POST request") {
   Client g{};
 
-  auto r = std::make_shared<Request>(&g, Request::Method::POST,
+  auto r = std::make_shared<Request>(&g, Request::Method::Post,
                                      "http://localhost:5000/post");
   r->request("ABCD");
   r->on_complete([](const Request &r) {
@@ -92,7 +92,7 @@ TEST_CASE("Basic manual POST request") {
 TEST_CASE("Basic manual PUT request") {
   Client g{};
 
-  auto r = std::make_shared<Request>(&g, Request::Method::PUT,
+  auto r = std::make_shared<Request>(&g, Request::Method::Put,
                                      "http://localhost:5000/put");
   r->request("ABCD");
   r->on_complete([](const Request &r) {
@@ -107,7 +107,7 @@ TEST_CASE("Basic manual PUT request") {
 TEST_CASE("Basic manual HEAD request") {
   Client g{};
 
-  auto r = std::make_shared<Request>(&g, Request::Method::HEAD,
+  auto r = std::make_shared<Request>(&g, Request::Method::Head,
                                      "http://localhost:5000/");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "http://localhost:5000/");
@@ -121,7 +121,7 @@ TEST_CASE("Basic manual HEAD request") {
 TEST_CASE("Basic manual OPTIONS request") {
   Client g{};
 
-  auto r = std::make_shared<Request>(&g, Request::Method::OPTIONS,
+  auto r = std::make_shared<Request>(&g, Request::Method::Options,
                                      "http://localhost:5000/");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "http://localhost:5000/");
@@ -137,7 +137,7 @@ TEST_CASE("Basic manual OPTIONS request") {
 TEST_CASE("Basic manual DELETE request") {
   Client g{};
 
-  auto r = std::make_shared<Request>(&g, Request::Method::DELETE,
+  auto r = std::make_shared<Request>(&g, Request::Method::Delete,
                                      "http://localhost:5000/delete");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "http://localhost:5000/delete");
diff --git a/tests/requests_tls.cpp b/tests/requests_tls.cpp
index ebc4a8e..aa2410a 100644
--- a/tests/requests_tls.cpp
+++ b/tests/requests_tls.cpp
@@ -29,7 +29,7 @@ TEST_CASE("Basic manual request") {
   Client g{};
   g.set_verify_peer(false);
 
-  auto r = std::make_shared<Request>(&g, Request::Method::GET,
+  auto r = std::make_shared<Request>(&g, Request::Method::Get,
                                      "https://localhost:5443/");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "https://localhost:5443/");
@@ -85,7 +85,7 @@ TEST_CASE("Basic manual POST request") {
   Client g{};
   g.set_verify_peer(false);
 
-  auto r = std::make_shared<Request>(&g, Request::Method::POST,
+  auto r = std::make_shared<Request>(&g, Request::Method::Post,
                                      "https://localhost:5443/post");
   r->request("ABCD");
   r->on_complete([](const Request &r) {
@@ -101,7 +101,7 @@ TEST_CASE("Basic manual PUT request") {
   Client g{};
   g.set_verify_peer(false);
 
-  auto r = std::make_shared<Request>(&g, Request::Method::PUT,
+  auto r = std::make_shared<Request>(&g, Request::Method::Put,
                                      "https://localhost:5443/put");
   r->request("ABCD");
   r->on_complete([](const Request &r) {
@@ -117,7 +117,7 @@ TEST_CASE("Basic manual HEAD request") {
   Client g{};
   g.set_verify_peer(false);
 
-  auto r = std::make_shared<Request>(&g, Request::Method::HEAD,
+  auto r = std::make_shared<Request>(&g, Request::Method::Head,
                                      "https://localhost:5443/");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "https://localhost:5443/");
@@ -132,7 +132,7 @@ TEST_CASE("Basic manual OPTIONS request") {
   Client g{};
   g.set_verify_peer(false);
 
-  auto r = std::make_shared<Request>(&g, Request::Method::OPTIONS,
+  auto r = std::make_shared<Request>(&g, Request::Method::Options,
                                      "https://localhost:5443/");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "https://localhost:5443/");
@@ -149,7 +149,7 @@ TEST_CASE("Basic manual DELETE request") {
   Client g{};
   g.set_verify_peer(false);
 
-  auto r = std::make_shared<Request>(&g, Request::Method::DELETE,
+  auto r = std::make_shared<Request>(&g, Request::Method::Delete,
                                      "https://localhost:5443/delete");
   r->on_complete([](const Request &r) {
     CHECK(r.url() == "https://localhost:5443/delete");
-- 
GitLab


From 6d0afb657d4b07a80d6ac6e485bd8ef8a498a41b Mon Sep 17 00:00:00 2001
From: Joe <rubberduckie3554@gmail.com>
Date: Tue, 6 Jul 2021 19:31:52 -0400
Subject: [PATCH 2/4] More fixes for windows

---
 lib/client.cpp | 10 +++++++---
 meson.build    |  3 ++-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/lib/client.cpp b/lib/client.cpp
index d750da0..b3214be 100644
--- a/lib/client.cpp
+++ b/lib/client.cpp
@@ -211,15 +211,19 @@ int Client::sock_cb(CURL *e, curl_socket_t s, int what, void *cbp,
 }
 
 Client::Client() {
-  std::once_flag once;
+  std::once_flag threads_once;
 #ifdef WIN32
-  std::call_once(once, evthread_use_windows_threads);
+  std::call_once(threads_once, evthread_use_windows_threads);
 #elif defined(EVENT__HAVE_PTHREADS)
-  std::call_once(once, evthread_use_pthreads);
+  std::call_once(threads_once, evthread_use_pthreads);
 #else
 #error "No supported threading backend!"
 #endif
 
+  /* Make sure the SSL or WinSock backends are initialized */
+  std::once_flag curl_once;
+  std::call_once(curl_once, curl_global_init, CURL_GLOBAL_DEFAULT);
+
   this->evbase = event_base_new();
   this->multi = curl_multi_init();
   event_assign(&this->timer_event, this->evbase, -1, 0, timer_cb, this);
diff --git a/meson.build b/meson.build
index dadb2be..22ab9b1 100644
--- a/meson.build
+++ b/meson.build
@@ -32,9 +32,10 @@ libevent_options = cmake.subproject_options()
       'EVENT__DISABLE_TESTS': true,
       'EVENT__DISABLE_REGRESS': true,
       'EVENT__DISABLE_SAMPLES': true,
+      'EVENT__MSVC_STATIC_RUNTIME': ['mt', 'mtd', 'static_from_buildtype'].contains(get_option('b_vscrt')),
       })
     if target_machine.system() != 'windows'
-        libcurl_options.add_cmake_defines({
+        libevent_options.add_cmake_defines({
           'CMAKE_C_FLAGS':  '-fPIC',
         })
     endif
-- 
GitLab


From 41d31d2739479471543976adc9b1dca3de5a6d1f Mon Sep 17 00:00:00 2001
From: Joe <rubberduckie3554@gmail.com>
Date: Wed, 7 Jul 2021 00:06:41 -0400
Subject: [PATCH 3/4] Fix missing visibility modifier for target_link_libraries

---
 CMakeLists.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 58a7c83..602f77a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -85,8 +85,8 @@ target_link_libraries(coeurl PUBLIC spdlog::spdlog)
 
 IF(MSVC)
     add_compile_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN)
-	target_link_libraries(coeurl wsock32 ws2_32)
-	target_link_libraries(coeurl iphlpapi)
+	target_link_libraries(coeurl PUBLIC wsock32 ws2_32)
+	target_link_libraries(coeurl PUBLIC iphlpapi)
 ENDIF()
 
 
-- 
GitLab


From 8ba08819554096ee35e7f81f09549568cd891034 Mon Sep 17 00:00:00 2001
From: Joe <rubberduckie3554@gmail.com>
Date: Wed, 7 Jul 2021 00:12:27 -0400
Subject: [PATCH 4/4] Fix win32 compiler definitions

---
 CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 602f77a..1ad1c9a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -84,7 +84,7 @@ find_package(spdlog 1.0.0 CONFIG REQUIRED)
 target_link_libraries(coeurl PUBLIC spdlog::spdlog)
 
 IF(MSVC)
-    add_compile_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN)
+    add_compile_definitions(NOMINMAX WIN32_LEAN_AND_MEAN)
 	target_link_libraries(coeurl PUBLIC wsock32 ws2_32)
 	target_link_libraries(coeurl PUBLIC iphlpapi)
 ENDIF()
-- 
GitLab