Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for unix sockets #721

Closed
nqle opened this issue Oct 26, 2020 · 3 comments
Closed

Support for unix sockets #721

nqle opened this issue Oct 26, 2020 · 3 comments

Comments

@nqle
Copy link

nqle commented Oct 26, 2020

Hey,
I didn't know where else to ask.
I was curious about whether there are future plans to support unix domain sockets for local interprocess communication or is this project rather focused on web applications and tcp sockets?

@yhirose
Copy link
Owner

yhirose commented Oct 26, 2020

@nqle, thanks for the question. I am using this library for HTTP support in my projects. I don't have any plan to support Unix socket because the projects don't need it. Hope it helps!

@yhirose yhirose closed this as completed Oct 26, 2020
@okirmis
Copy link

okirmis commented Feb 28, 2021

@nqle If you still need it, I hacked something together (quick & dirty) for my own spare time project (communicating with docker sockets):

In create_socket I added the following code before the auto service = std::to_string(port); line:

  std::string hostName = host;

  int sock = INVALID_SOCKET;
  if (hostName.size() > 5 && hostName.substr(0, 5) == "unix:") {
      sock = socket(AF_UNIX, SOCK_STREAM, 0);

      sockaddr_un addr{};
      addr.sun_family = AF_UNIX;
      strncpy(
          addr.sun_path,
          hostName.substr(5, hostName.size() - 5).c_str(),
          std::min(sizeof(addr.sun_path) - 1, hostName.size() - 5)
      );

      addrinfo info{};
      info.ai_addr = (sockaddr *)&addr;
      info.ai_addrlen = sizeof(addr);
      info.ai_next = nullptr;
      info.ai_socktype = SOCK_STREAM;
      info.ai_family = AF_UNIX;
      info.ai_canonname = nullptr;
      info.ai_protocol = 0;

      if (bind_or_connect(sock, info)) {
          return sock;
      }

      return INVALID_SOCKET;
  }

Also, include #include <sys/un.h>.

Then you can run

httplib::Client client("unix:/var/run/docker.sock");
client.set_default_headers({ { "Host", "localhost" } });
auto response = client.Get("/v1.24/containers/json");

for example to talk to the local docker socket.

Maybe it helps!

@nqle
Copy link
Author

nqle commented Mar 1, 2021

Thanks for your effort! I might use it at some point.

@yhirose yhirose mentioned this issue Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants