Skip to content

Commit

Permalink
Merge pull request #282 from edmBernard/master
Browse files Browse the repository at this point in the history
Add unix socket support
  • Loading branch information
tstack authored Apr 22, 2020
2 parents 9887556 + 1c8bfe5 commit 7e75d7d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cpr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(${CPR_LIBRARIES}
session.cpp
timeout.cpp
util.cpp
unix_socket.cpp
ssl_options.cpp

# Header files (useful in IDEs)
Expand All @@ -38,8 +39,9 @@ add_library(${CPR_LIBRARIES}
"${CPR_INCLUDE_DIRS}/cpr/timeout.h"
"${CPR_INCLUDE_DIRS}/cpr/util.h"
"${CPR_INCLUDE_DIRS}/cpr/ssl_options.h"
"${CPR_INCLUDE_DIRS}/cpr/limit_rate.h"
"${CPR_INCLUDE_DIRS}/cpr/verbose.h")
"${CPR_INCLUDE_DIRS}/cpr/limit_rate.h"
"${CPR_INCLUDE_DIRS}/cpr/verbose.h"
"${CPR_INCLUDE_DIRS}/cpr/unix_socket.h")

message(STATUS "Using CURL_LIBRARIES: ${CURL_LIBRARIES}.")
target_link_libraries(${CPR_LIBRARIES}
Expand Down
10 changes: 10 additions & 0 deletions cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Session::Impl {
void SetVerbose(const Verbose& verbose);
void SetVerifySsl(const VerifySsl& verify);
void SetLimitRate(const LimitRate& limit_rate);
void SetUnixSocket(const UnixSocket& unix_socket);

Response Delete();
Response Download(std::ofstream& file);
Expand Down Expand Up @@ -342,6 +343,13 @@ void Session::Impl::SetVerifySsl(const VerifySsl& verify) {
}
}

void Session::Impl::SetUnixSocket(const UnixSocket& unix_socket) {
auto curl = curl_->handle;
if (curl) {
curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, unix_socket.GetUnixSocketString());
}
}

Response Session::Impl::Delete() {
auto curl = curl_->handle;
if (curl) {
Expand Down Expand Up @@ -587,6 +595,8 @@ void Session::SetOption(Body&& body) { pimpl_->SetBody(std::move(body)); }
void Session::SetOption(const LowSpeed& low_speed) { pimpl_->SetLowSpeed(low_speed); }
void Session::SetOption(const VerifySsl& verify) { pimpl_->SetVerifySsl(verify); }
void Session::SetOption(const Verbose& verbose) { pimpl_->SetVerbose(verbose); }
void Session::SetOption(const UnixSocket& unix_socket) { pimpl_->SetUnixSocket(unix_socket); }

Response Session::Delete() { return pimpl_->Delete(); }
Response Session::Download(std::ofstream& file) { return pimpl_->Download(file); }
Response Session::Get() { return pimpl_->Get(); }
Expand Down
10 changes: 10 additions & 0 deletions cpr/unix_socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#include "cpr/unix_socket.h"

namespace cpr {

const char* UnixSocket::GetUnixSocketString() const noexcept {
return unix_socket_.data();
}

} // namespace cpr
3 changes: 3 additions & 0 deletions include/cpr/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cpr/session.h"
#include "cpr/verbose.h"
#include "cpr/limit_rate.h"
#include "cpr/unix_socket.h"

namespace cpr {

Expand Down Expand Up @@ -56,6 +57,7 @@ class Session {
void SetBody(const Body& body);
void SetLowSpeed(const LowSpeed& low_speed);
void SetVerifySsl(const VerifySsl& verify);
void SetUnixSocket(const UnixSocket& unix_socket);

// Used in templated functions
void SetOption(const Url& url);
Expand All @@ -82,6 +84,7 @@ class Session {
void SetOption(const LowSpeed& low_speed);
void SetOption(const VerifySsl& verify);
void SetOption(const Verbose& verbose);
void SetOption(const UnixSocket& unix_socket);

Response Delete();
Response Download(std::ofstream& file);
Expand Down
24 changes: 24 additions & 0 deletions include/cpr/unix_socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CPR_UNIX_SOCKET_H
#define CPR_UNIX_SOCKET_H

#include <string>

#include "cpr/defines.h"

namespace cpr {

class UnixSocket {
public:
template <typename UnixSocketType>
UnixSocket(UnixSocketType&& unix_socket)
: unix_socket_{CPR_FWD(unix_socket)} {}

const char* GetUnixSocketString() const noexcept;

private:
std::string unix_socket_;
};

} // namespace cpr

#endif

0 comments on commit 7e75d7d

Please sign in to comment.