Skip to content

Commit 2cb08ec

Browse files
Replace curl with httplib
1 parent aeb1e36 commit 2cb08ec

File tree

5 files changed

+8264
-56
lines changed

5 files changed

+8264
-56
lines changed

Controllers/NanoleafController/NanoleafController.cpp

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "NanoleafController.h"
1010
#include "LogManager.h"
11-
#include <curl/curl.h>
11+
#include "httplib.h"
1212

1313
std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t num, std::string* out)
1414
{
@@ -19,76 +19,62 @@ std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t nu
1919

2020
long APIRequest(std::string method, std::string location, std::string URI, json* request_data = nullptr, json* response_data = nullptr)
2121
{
22-
const std::string url("http://"+location+URI);
23-
24-
CURL* curl = curl_easy_init();
25-
26-
/*-------------------------------------------------------------*\
27-
| Set remote URL. |
28-
\*-------------------------------------------------------------*/
29-
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
30-
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
31-
32-
/*-------------------------------------------------------------*\
33-
| Don't bother trying IPv6, which would increase DNS resolution |
34-
| time. |
35-
\*-------------------------------------------------------------*/
36-
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
22+
const std::string url("http://"+location);
3723

3824
/*-------------------------------------------------------------*\
39-
| Don't wait forever, time out after 10 seconds. |
25+
| Create httplib Client and variables to hold result |
4026
\*-------------------------------------------------------------*/
41-
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
27+
httplib::Client client(url.c_str());
28+
int status = 0;
29+
std::string body = "";
4230

43-
/*-------------------------------------------------------------*\
44-
| Follow HTTP redirects if necessary. |
45-
\*-------------------------------------------------------------*/
46-
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
47-
48-
if(request_data)
31+
if(method == "GET")
4932
{
50-
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, request_data->dump().c_str());
33+
httplib::Result result = client.Get(URI.c_str());
34+
35+
status = result->status;
36+
body = result->body;
5137
}
38+
else if(method == "PUT")
39+
{
40+
if(request_data)
41+
{
42+
httplib::Result result = client.Put(URI.c_str(), request_data->dump(), "application/json");
5243

53-
/*-------------------------------------------------------------*\
54-
| Response information. |
55-
\*-------------------------------------------------------------*/
56-
long httpCode(0);
57-
std::unique_ptr<std::string> httpData(new std::string());
44+
status = result->status;
45+
body = result->body;
46+
}
47+
else
48+
{
49+
httplib::Result result = client.Put(URI.c_str());
5850

59-
/*-------------------------------------------------------------*\
60-
| Hook up data handling function. |
61-
\*-------------------------------------------------------------*/
62-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
51+
status = result->status;
52+
body = result->body;
53+
}
54+
}
55+
else if(method == "DELETE")
56+
{
57+
httplib::Result result = client.Delete(URI.c_str());
6358

64-
/*-------------------------------------------------------------*\
65-
| Hook up data container (will be passed as the last parameter |
66-
| to the callback handling function). Can be any pointer type, |
67-
| since it will internally be passed as a void pointer. |
68-
\*-------------------------------------------------------------*/
69-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
59+
status = result->status;
60+
body = result->body;
61+
}
7062

71-
/*-------------------------------------------------------------*\
72-
| Run our HTTP GET command, capture the HTTP response code, and |
73-
| clean up. |
74-
\*-------------------------------------------------------------*/
75-
curl_easy_perform(curl);
76-
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
77-
curl_easy_cleanup(curl);
63+
LOG_DEBUG("[Nanoleaf] Result %d %s", status, body.c_str());
7864

79-
if((httpCode / 100) == 2)
65+
if((status / 100) == 2)
8066
{
8167
if(response_data)
8268
{
83-
*response_data = json::parse(*httpData.get());
69+
*response_data = json::parse(body);
8470
}
8571
}
8672
else
8773
{
88-
LOG_DEBUG("[Nanoleaf] HTTP %i:Could not %s from %s", httpCode, method, url);
74+
//LOG_DEBUG("[Nanoleaf] HTTP %i:Could not %s from %s", httpCode, method.c_str(), url.c_str());
8975
}
9076

91-
return httpCode;
77+
return status;
9278
}
9379

9480
NanoleafController::NanoleafController(std::string a_address, int a_port, std::string a_auth_token)

Controllers/NanoleafController/NanoleafControllerDetect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void DetectNanoleafControllers(std::vector<RGBController*> &rgb_controllers)
3030
}
3131
catch(...)
3232
{
33-
LOG_DEBUG("[Nanoleaf] Could not connect to device at %s:%s using auth_token %s", device["ip"], device["port"], device["auth_token"]);
33+
LOG_DEBUG("[Nanoleaf] Could not connect to device at %s:%s using auth_token %s", device["ip"].get<std::string>().c_str(), device["port"].get<std::string>().c_str(), device["auth_token"].get<std::string>().c_str());
3434
}
3535
}
3636
}

Controllers/NanoleafController/RGBController_Nanoleaf.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "RGBController_Nanoleaf.h"
1010
#include "ResourceManager.h"
1111
#include "LogManager.h"
12-
#include <curl/curl.h>
1312
#include "json.hpp"
1413
using json = nlohmann::json;
1514

OpenRGB.pro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ INCLUDEPATH +=
6161
dependencies/CRCpp/ \
6262
dependencies/hueplusplus-1.0.0/include \
6363
dependencies/hueplusplus-1.0.0/include/hueplusplus \
64+
dependencies/httplib \
6465
dependencies/json/ \
6566
dependencies/libe131/src/ \
6667
dependencies/libcmmk/include/ \
@@ -1116,9 +1117,6 @@ FORMS +=
11161117
qt/OpenRGBZonesBulkResizer.ui \
11171118
qt/TabLabel.ui \
11181119

1119-
LIBS += \
1120-
-lcurl \
1121-
11221120
#-----------------------------------------------------------------------------------------------#
11231121
# Windows-specific Configuration #
11241122
#-----------------------------------------------------------------------------------------------#
@@ -1257,6 +1255,7 @@ win32:HEADERS +=
12571255
win32:contains(QMAKE_TARGET.arch, x86_64) {
12581256
LIBS += \
12591257
-lws2_32 \
1258+
-liphlpapi \
12601259
-L"$$PWD/dependencies/winring0/x64/" -lWinRing0x64 \
12611260
-L"$$PWD/dependencies/libusb-1.0.22/MS64/dll" -llibusb-1.0 \
12621261
-L"$$PWD/dependencies/hidapi-win/x64/" -lhidapi \
@@ -1265,6 +1264,7 @@ win32:contains(QMAKE_TARGET.arch, x86_64) {
12651264
win32:contains(QMAKE_TARGET.arch, x86) {
12661265
LIBS += \
12671266
-lws2_32 \
1267+
-liphlpapi \
12681268
-L"$$PWD/dependencies/winring0/Win32/" -lWinRing0 \
12691269
-L"$$PWD/dependencies/libusb-1.0.22/MS32/dll" -llibusb-1.0 \
12701270
-L"$$PWD/dependencies/hidapi-win/x86/" -lhidapi \

0 commit comments

Comments
 (0)