From 3db29e4267821cbf28becd0b15fbf67d3753509a Mon Sep 17 00:00:00 2001 From: Andrew McDaniel Date: Tue, 1 Oct 2024 21:37:02 -0400 Subject: [PATCH 1/3] Add documentation for using unix domain sockets. --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 0a525122ad..6b67f84d43 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,33 @@ cli.enable_server_host_verification(false); > [!NOTE] > When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. +Unix Domain Socket Support +-------------------------- + +Unix Domain Socket support is available on Linux and macOS. + +```c++ +// Create a server +httplib::Server svr("./my-socket.sock"); +// Add an endpoint +svr.get("/hi", [](const httplib::Request &, httplib::Response &res) { + res.set_content("Hello World!", "text/plain"); +}); +// Start the server in a separate thread +std::thread t{[&] { + svr.set_address_family(AF_UNIX).listen(this->address, 80); +}}; + + +httplib::Client cli("./my-socket.sock"); +cli.set_address_family(AF_UNIX); +auto res = cli.Get("/hi"); +``` + +"my-socket.sock" can be a relative path or an absolute path. You application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. + + + Server ------ From ea2a0872957d031209ee9cce4451d84752e02327 Mon Sep 17 00:00:00 2001 From: Andrew McDaniel Date: Wed, 2 Oct 2024 13:23:48 -0400 Subject: [PATCH 2/3] Formatting --- README.md | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 6b67f84d43..7148e5502a 100644 --- a/README.md +++ b/README.md @@ -85,33 +85,6 @@ cli.enable_server_host_verification(false); > [!NOTE] > When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. -Unix Domain Socket Support --------------------------- - -Unix Domain Socket support is available on Linux and macOS. - -```c++ -// Create a server -httplib::Server svr("./my-socket.sock"); -// Add an endpoint -svr.get("/hi", [](const httplib::Request &, httplib::Response &res) { - res.set_content("Hello World!", "text/plain"); -}); -// Start the server in a separate thread -std::thread t{[&] { - svr.set_address_family(AF_UNIX).listen(this->address, 80); -}}; - - -httplib::Client cli("./my-socket.sock"); -cli.set_address_family(AF_UNIX); -auto res = cli.Get("/hi"); -``` - -"my-socket.sock" can be a relative path or an absolute path. You application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. - - - Server ------ @@ -870,6 +843,23 @@ Use `poll` instead of `select` `select` system call is used as default since it's more widely supported. If you want to let cpp-httplib use `poll` instead, you can do so with `CPPHTTPLIB_USE_POLL`. +Unix Domain Socket Support +-------------------------- + +Unix Domain Socket support is available on Linux and macOS. + +```c++ +// Server +httplib::Server svr("./my-socket.sock"); +svr.set_address_family(AF_UNIX).listen("./my-socket.sock", 80); + +// Client +httplib::Client cli("./my-socket.sock"); +cli.set_address_family(AF_UNIX); +``` + +"my-socket.sock" can be a relative path or an absolute path. You application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. + Split httplib.h into .h and .cc ------------------------------- From a00f250eda10845f9c5e68e74eaf6866416ae075 Mon Sep 17 00:00:00 2001 From: Andrew McDaniel Date: Thu, 28 Nov 2024 14:34:44 -0500 Subject: [PATCH 3/3] Fix for infinite loop --- httplib.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index b4f89d6183..3f98e77537 100644 --- a/httplib.h +++ b/httplib.h @@ -612,7 +612,6 @@ using Ranges = std::vector; struct Request { std::string method; std::string path; - Params params; Headers headers; std::string body; @@ -624,6 +623,7 @@ struct Request { // for server std::string version; std::string target; + Params params; MultipartFormDataMap files; Ranges ranges; Match matches; @@ -2271,7 +2271,7 @@ inline std::wstring u8string_to_wstring(const char *s) { wlen = ::MultiByteToWideChar( CP_UTF8, 0, s, len, const_cast(reinterpret_cast(ws.data())), wlen); - if (wlen != static_cast(ws.size())) { ws.clear(); } + if (wlen != ws.size()) { ws.clear(); } } return ws; } @@ -6340,6 +6340,7 @@ inline void Server::stop() { std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); detail::shutdown_socket(sock); detail::close_socket(sock); + is_running_ = false; } is_decommisioned = false; } @@ -6739,7 +6740,7 @@ inline bool Server::listen_internal() { { std::unique_ptr task_queue(new_task_queue()); - while (svr_sock_ != INVALID_SOCKET) { + while (svr_sock_ != INVALID_SOCKET && is_running()) { #ifndef _WIN32 if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { #endif @@ -7420,7 +7421,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { inline bool ClientImpl::is_ssl_peer_could_be_closed(SSL *ssl) const { char buf[1]; return !SSL_peek(ssl, buf, 1) && - SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; + SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; } #endif @@ -7438,7 +7439,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_alive && is_ssl()) { - if (is_ssl_peer_could_be_closed(socket_.ssl)) { is_alive = false; } + if (is_ssl_peer_could_be_closed(socket_.ssl)) { + is_alive = false; + } } #endif @@ -7797,13 +7800,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, { detail::BufferStream bstrm; - const auto &path_with_query = - req.params.empty() ? req.path - : append_query_params(req.path, req.params); - - const auto &path = - url_encode_ ? detail::encode_url(https://github.com/path_with_query) : path_with_query; - + const auto &path = url_encode_ ? detail::encode_url(https://github.com/req.path) : req.path; detail::write_request_line(bstrm, req.method, path); header_writer_(bstrm, req.headers);