Skip to content

Commit 7e6357e

Browse files
Added support for Unix sockets.
1 parent 9243fb1 commit 7e6357e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ conn->SetProxy("37.187.100.23:3128");
213213
RestClient::Response res = conn->get("/get");
214214
```
215215

216+
## Unix Socket Support
217+
218+
- https://docs.docker.com/develop/sdk/examples/
219+
- $ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json
220+
221+
Note that the URL used with a unix socket has only ONE leading forward slash.
222+
223+
```cpp
224+
RestClient::Connection* conn = new RestClient::Connection("http:/v1.30");
225+
conn->SetUnixSocketPath("/var/run/docker.sock");
226+
RestClient::HeaderFields headers;
227+
headers["Accept"] = "application/json; charset=UTF-8";
228+
headers["Expect"] = "";
229+
conn->SetHeaders(headers);
230+
auto resp = conn->get("/images/json");
231+
```
232+
216233
## Dependencies
217234
- [libcurl][]
218235

include/restclient-cpp/connection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class Connection {
120120
std::string keyPassword;
121121
std::string customUserAgent;
122122
std::string uriProxy;
123+
std::string unixSocketPath;
123124
RequestInfo lastRequest;
124125
} Info;
125126

@@ -165,6 +166,9 @@ class Connection {
165166
// set CURLOPT_PROXY
166167
void SetProxy(const std::string& uriProxy);
167168

169+
// set CURLOPT_UNIX_SOCKET_PATH
170+
void SetUnixSocketPath(const std::string& unixSocketPath);
171+
168172
std::string GetUserAgent();
169173

170174
RestClient::Connection::Info GetInfo();
@@ -212,6 +216,7 @@ class Connection {
212216
std::string keyPath;
213217
std::string keyPassword;
214218
std::string uriProxy;
219+
std::string unixSocketPath;
215220
RestClient::Response performCurlRequest(const std::string& uri);
216221
};
217222
}; // namespace RestClient

source/connection.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ RestClient::Connection::GetInfo() {
7171
ret.keyPassword = this->keyPassword;
7272

7373
ret.uriProxy = this->uriProxy;
74+
ret.unixSocketPath = this->unixSocketPath;
7475

7576
return ret;
7677
}
@@ -265,6 +266,19 @@ RestClient::Connection::SetProxy(const std::string& uriProxy) {
265266
}
266267
}
267268

269+
/**
270+
* @brief set custom Unix socket path for connection.
271+
* See https://curl.haxx.se/libcurl/c/CURLOPT_UNIX_SOCKET_PATH.html
272+
*
273+
* @param unixSocketPath - path to Unix socket (ex: /var/run/docker.sock)
274+
*
275+
*/
276+
void
277+
RestClient::Connection::SetUnixSocketPath(const std::string& unixSocketPath) {
278+
this->unixSocketPath = unixSocketPath;
279+
}
280+
281+
268282
/**
269283
* @brief helper function to get called from the actual request methods to
270284
* prepare the curlHandle for transfer with generic options, perform the
@@ -376,6 +390,12 @@ RestClient::Connection::performCurlRequest(const std::string& uri) {
376390
1L);
377391
}
378392

393+
// set Unix socket path, if requested
394+
if (!this->unixSocketPath.empty()) {
395+
curl_easy_setopt(this->curlHandle, CURLOPT_UNIX_SOCKET_PATH,
396+
this->unixSocketPath.c_str());
397+
}
398+
379399
res = curl_easy_perform(this->curlHandle);
380400
if (res != CURLE_OK) {
381401
switch (res) {

0 commit comments

Comments
 (0)