Skip to content

Commit

Permalink
http client support uds
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Apr 19, 2024
1 parent 5cbfb92 commit ea48023
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 19 additions & 3 deletions net/http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PooledDialer {
bool tls_ctx_ownership;
std::unique_ptr<ISocketClient> tcpsock;
std::unique_ptr<ISocketClient> tlssock;
std::unique_ptr<ISocketClient> udssock;
std::unique_ptr<Resolver> resolver;

//etsocket seems not support multi thread very well, use tcp_socket now. need to find out why
Expand All @@ -49,6 +50,7 @@ class PooledDialer {
auto tls_cli = new_tls_client(tls_ctx, new_tcp_socket_client(), true);
tcpsock.reset(new_tcp_socket_pool(tcp_cli, -1, true));
tlssock.reset(new_tcp_socket_pool(tls_cli, -1, true));
udssock.reset(new_uds_client());
}

~PooledDialer() {
Expand All @@ -63,6 +65,8 @@ class PooledDialer {
ISocketStream* dial(const T& x, uint64_t timeout = -1UL) {
return dial(x.host_no_port(), x.port(), x.secure(), timeout);
}

ISocketStream* dial(std::string_view uds_path, uint64_t timeout = -1UL);
};

ISocketStream* PooledDialer::dial(std::string_view host, uint16_t port, bool secure, uint64_t timeout) {
Expand Down Expand Up @@ -96,6 +100,14 @@ ISocketStream* PooledDialer::dial(std::string_view host, uint16_t port, bool sec
return nullptr;
}

ISocketStream* PooledDialer::dial(std::string_view uds_path, uint64_t timeout) {
udssock->timeout(timeout);
auto stream = udssock->connect(uds_path.data());
if (!stream)
LOG_ERRNO_RETURN(0, nullptr, "failed to dial to unix socket `", uds_path);
return stream;
}

constexpr uint64_t code3xx() { return 0; }
template<typename...Ts>
constexpr uint64_t code3xx(uint64_t x, Ts...xs)
Expand Down Expand Up @@ -164,9 +176,13 @@ class ClientImpl : public Client {
if (tmo.timeout() == 0)
LOG_ERROR_RETURN(ETIMEDOUT, ROUNDTRIP_FAILED, "connection timedout");
auto &req = op->req;
auto s = (m_proxy && !m_proxy_url.empty())
? m_dialer.dial(m_proxy_url, tmo.timeout())
: m_dialer.dial(req, tmo.timeout());
ISocketStream* s;
if (m_proxy && !m_proxy_url.empty())
s = m_dialer.dial(m_proxy_url, tmo.timeout());
else if (!op->unix_socket_path.empty())
s = m_dialer.dial(std::string_view(op->unix_socket_path), tmo.timeout());
else
s = m_dialer.dial(req, tmo.timeout());
if (!s) {
if (errno == ECONNREFUSED || errno == ENOENT) {
LOG_ERROR_RETURN(0, ROUNDTRIP_FAST_RETRY, "connection refused")
Expand Down
5 changes: 4 additions & 1 deletion net/http/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#pragma once

#include <memory>
#include <string>
#include <photon/net/http/verb.h>
#include <photon/net/http/message.h>
#include <photon/net/http/url.h>
Expand Down Expand Up @@ -48,7 +49,9 @@ class Client : public Object {
uint16_t retry = 5; // default retry: 5 at most
Response resp; // response
int status_code = -1; // status code in response
bool enable_proxy;
bool enable_proxy = false;
std::string unix_socket_path; // If set, Unix Socket will be used instead of TCP.
// URL should still be the format of http://localhost/xxx
IStream* body_stream = nullptr; // use body_stream as body
using BodyWriter = Delegate<ssize_t, Request*>; // or call body_writer if body_stream
BodyWriter body_writer = {}; // is not set
Expand Down

0 comments on commit ea48023

Please sign in to comment.