-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Basic Connection via Curl to Docker Daemon + Ping Test
- Loading branch information
1 parent
0947bd6
commit f228c9f
Showing
4 changed files
with
103 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef DOCKERXX_DOCKER_HPP | ||
#define DOCKERXX_DOCKER_HPP | ||
|
||
#include <curl/curl.h> | ||
#include <string> | ||
|
||
|
||
namespace dockerxx { | ||
|
||
class Docker { | ||
|
||
/* Member Variables */ | ||
private: | ||
CURL* m_curl = nullptr; | ||
bool m_remote_connection; | ||
std::string m_uri; | ||
|
||
/* Constructors */ | ||
public: | ||
Docker(); | ||
explicit Docker(std::string& uri); | ||
|
||
/* Member Functions */ | ||
private: | ||
std::string request(const std::string& path); | ||
|
||
public: | ||
std::string ping(); | ||
|
||
/* Static Functions */ | ||
private: | ||
static size_t write_data(void* ptr, size_t size, size_t nmemb, std::string* userp); | ||
|
||
}; | ||
|
||
} /* namespace dockerxx */ | ||
|
||
#endif /* DOCKERXX_DOCKER_HPP */ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,58 @@ | ||
// | ||
// Created by fabitheguy on 9/19/24. | ||
// | ||
#include <include/docker.hpp> | ||
#include <stdexcept> | ||
#include <utility> | ||
|
||
using namespace dockerxx; | ||
|
||
/* Constructors */ | ||
Docker::Docker() { this->m_remote_connection = false; } | ||
|
||
Docker::Docker(std::string &uri) { | ||
this->m_remote_connection = true; | ||
this->m_uri = std::move(uri); | ||
} | ||
|
||
/* Member functions */ | ||
std::string Docker::request(const std::string &path) { | ||
CURLcode response_code; | ||
std::string response_data; | ||
|
||
if (path.empty()) { | ||
throw std::invalid_argument("Path is empty"); | ||
} | ||
|
||
this->m_curl = curl_easy_init(); | ||
|
||
if (this->m_remote_connection) { | ||
curl_easy_setopt(this->m_curl, CURLOPT_URL, std::string(this->m_uri + path).c_str()); | ||
} else { | ||
curl_easy_setopt(this->m_curl, CURLOPT_URL, std::string("http://localhost" + path).c_str()); | ||
curl_easy_setopt(this->m_curl, CURLOPT_UNIX_SOCKET_PATH, | ||
std::string("/var/run/docker.sock").c_str()); | ||
} | ||
|
||
curl_easy_setopt(this->m_curl, CURLOPT_WRITEFUNCTION, Docker::write_data); | ||
curl_easy_setopt(this->m_curl, CURLOPT_WRITEDATA, &response_data); | ||
|
||
response_code = curl_easy_perform(this->m_curl); | ||
|
||
if (response_code != CURLE_OK) { | ||
throw std::runtime_error("curl_easy_perform() failed: " + | ||
std::string(curl_easy_strerror(response_code))); | ||
} | ||
|
||
curl_easy_cleanup(this->m_curl); | ||
|
||
return response_data; | ||
} | ||
|
||
std::string Docker::ping() { return request("/_ping"); } | ||
|
||
/* Static functions */ | ||
size_t Docker::write_data(void *ptr, size_t size, size_t nmemb, std::string *userp) { | ||
size_t real_size = size * nmemb; | ||
|
||
userp->append(static_cast<char *>(ptr), real_size); | ||
|
||
return real_size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters