Skip to content

Commit 5767128

Browse files
authored
Merge pull request #1 from craflin/curl-wrapper
Add CURL wrapper
2 parents c67d9c1 + a7ce765 commit 5767128

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ project(gchsd VERSION 0.2.0)
77
set(CDEPLOY_NO_DEBUG_BUILD True)
88
set(CDEPLOY_NO_COMPILER True)
99

10+
find_package(CURL REQUIRED)
11+
if(NOT TARGET CURL::libcurl)
12+
add_library(CURL::libcurl INTERFACE IMPORTED)
13+
set_target_properties(CURL::libcurl
14+
PROPERTIES
15+
INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}"
16+
INTERFACE_LINK_LIBRARIES "${CURL_LIBRARIES}"
17+
)
18+
endif()
19+
1020
include(CDeploy)
1121

1222
add_subdirectory(3rdparty)
@@ -19,7 +29,7 @@ set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A mirroring Git HTTP server daemon")
1929
set(CPACK_PACKAGING_INSTALL_PREFIX "/")
2030
#set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
2131
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/craflin/gchsd")
22-
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.15)")
32+
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.15),libcurl4")
2333
set(CPACK_DEBIAN_PACKAGE_SECTION "net")
2434

2535
include(CPack)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The project is a C++/CURL clone of jonasmalacofilho's [git-cache-http-server](ht
1212
## Build Instructions
1313

1414
* Clone the repository and initialize submodules.
15+
* Install a dev package of curl. (e.g. `libcurl4-openssl-dev`)
1516
* Build the project with `cmake`.
1617
* You can build a `deb` package using the target `package` in CMake.
1718

src/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
set(sources
33
Address.hpp
4+
HttpRequest.cpp
5+
HttpRequest.hpp
46
Main.cpp
57
Settings.cpp
68
Settings.hpp
@@ -13,7 +15,7 @@ add_executable(gchsd
1315
)
1416

1517
target_link_libraries(gchsd PRIVATE
16-
libnstd::Socket
18+
libnstd::Socket CURL::libcurl
1719
)
1820

1921
source_group("" FILES ${sources})

src/HttpRequest.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
#include <curl/curl.h>
3+
4+
#include "HttpRequest.hpp"
5+
6+
static class Curl
7+
{
8+
public:
9+
Curl()
10+
{
11+
curl_global_init(CURL_GLOBAL_ALL);
12+
}
13+
~Curl()
14+
{
15+
curl_global_cleanup();
16+
}
17+
18+
} curl;
19+
20+
HttpRequest::HttpRequest() : curl(0) {}
21+
22+
HttpRequest::~HttpRequest()
23+
{
24+
if(curl)
25+
curl_easy_cleanup(curl);
26+
}
27+
28+
bool HttpRequest::get(const String& url, Buffer& data, bool checkCertificate)
29+
{
30+
if(!curl)
31+
{
32+
curl = curl_easy_init();
33+
if(!curl)
34+
{
35+
error = "Could not initialize curl.";
36+
return false;
37+
}
38+
}
39+
else
40+
curl_easy_reset(curl);
41+
42+
struct WriteResult
43+
{
44+
static size_t writeResponse(void *ptr, size_t size, size_t nmemb, void *stream)
45+
{
46+
WriteResult* result = (struct WriteResult *)stream;
47+
size_t newBytes = size * nmemb;
48+
size_t totalSize = result->buffer->size() + newBytes;
49+
if(totalSize > (size_t)result->buffer->capacity())
50+
result->buffer->reserve(totalSize * 2);
51+
result->buffer->append((const byte*)ptr, newBytes);
52+
return newBytes;
53+
}
54+
55+
Buffer* buffer;
56+
} writeResult = { &data };
57+
58+
curl_easy_setopt(curl, CURLOPT_URL, (const char*)url);
59+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteResult::writeResponse);
60+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeResult);
61+
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 40);
62+
if(!checkCertificate)
63+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
64+
65+
data.clear();
66+
data.reserve(1500);
67+
68+
CURLcode status = curl_easy_perform(curl);
69+
if(status != 0)
70+
{
71+
error.printf("%s.", curl_easy_strerror(status));
72+
return false;
73+
}
74+
75+
long code;
76+
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
77+
if(code != 200)
78+
{
79+
error.printf("Server responded with code %u.", (uint)code);
80+
return false;
81+
}
82+
83+
error.clear();
84+
return true;
85+
}

src/HttpRequest.hpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
#pragma once
4+
5+
#include <nstd/String.hpp>
6+
#include <nstd/Buffer.hpp>
7+
8+
class HttpRequest
9+
{
10+
public:
11+
HttpRequest();
12+
~HttpRequest();
13+
14+
const String& getErrorString() {return error;}
15+
16+
bool get(const String& url, Buffer& data, bool checkCertificate = true);
17+
18+
private:
19+
void* curl;
20+
String error;
21+
};

0 commit comments

Comments
 (0)