cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

Does cpprestsdk support unix domain socket like "/var/run/docker.sock"

Open laoshanxi opened this issue 6 years ago • 5 comments

Does cpprestsdk support unix domain socket like "/var/run/docker.sock"

curl support like this: $ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json

laoshanxi avatar Sep 14 '19 00:09 laoshanxi

I have the same issue. So, I tried to see if I could solve the issue using the existing functions. However, I couldn't solve the issue and would like someone to advise me.

Environment

$ uname -ra
Linux ip-172-31-18-7 5.4.0-1038-aws #40~18.04.1-Ubuntu SMP Sat Feb 6 01:56:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • boost library
    • BOOST_VERSION = 106501
    • BOOST_LIB_VERSION = 1_65_1

Code

#include <cpprest/filestream.h>
#include <cpprest/http_client.h>
#include <boost/asio.hpp>
#include <nlohmann/json.hpp>

using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;

pplx::task<void> get() {
    nlohmann::json *_json_obj;
    return pplx::create_task([] {
        // web:htt::client::native_handle handle == boost::asio::ip::tcp::socket* socket
        // Reference: https://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1client_1_1http__client__config.html#details
        auto native_handle_func = [&](web::http::client::native_handle handle) {
            boost::asio::io_service io;
            boost::asio::local::stream_protocol::endpoint ep("/var/run/docker.sock");
            boost::asio::local::stream_protocol::socket socket(io);
            socket.connect(ep);
            handle = &socket;
        };
        web::http::client::http_client_config config;
        config.set_nativehandle_options(native_handle_func);
        http_client client("http://localhost/images/json", config);
        return client.request(methods::GET);
    })
    .then([_json_obj](http_response response) {
        if (response.status_code() == status_codes::OK) {
            auto body = response.extract_string();
            *_json_obj = nlohmann::json::parse(body.get());
            std::cout << *_json_obj << std::endl;
        }
    });
}

int main() {
  try {
      get().wait();
  } catch (const std::exception& e) {
      std::cout << "ERROR: " << e.what() << std::endl;
      return 1;
  }
}

Compile

$ g++ -std=c++17 ./a.cpp -o a -L/lib64 -lcrypto -lcpprest -lpthread -lboost_system

Results of execution

ERROR: Failed to connect to any resolved endpoint

aii-goto-yusaku avatar Aug 11 '21 07:08 aii-goto-yusaku

I did not found the way to request docker socket, so finally, I moved to a workaround solution use command line. https://github.com/laoshanxi/app-mesh/blob/main/src/daemon/process/DockerProcess.cpp

One possible way is use Go to build a agent to transfer C++ request to Docker Engine, Go have better support on Docker.

laoshanxi avatar Aug 12 '21 04:08 laoshanxi

I am very grateful for your response.

I will use Go and rewrite the C++ part...

aii-goto-yusaku avatar Aug 13 '21 00:08 aii-goto-yusaku

It will be good we can make this as a new open source, I believe this is a common requirement.

laoshanxi avatar Aug 13 '21 01:08 laoshanxi

I am very grateful for your response.

I will use Go and rewrite the C++ part...

One solution to export docker.sock via Nginx without coding see here: https://github.com/laoshanxi/app-mesh/tree/main/src/sdk/docker

Another solution with Golang to proxying REST to docker.sock see here: https://github.com/laoshanxi/app-mesh/blob/main/src/sdk/docker/dockeragent.go

laoshanxi avatar Aug 13 '21 14:08 laoshanxi