From afc89032c0805daf0236df95e0a18177fd46dc75 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Fri, 3 Sep 2021 08:21:52 +0200 Subject: [PATCH] bump: Upgrade to cpp-httplib 0.9.4 --- LICENSE.adoc | 6 +-- src/third_party/httplib.cpp | 25 +++++----- src/third_party/httplib.h | 96 ++++++++++++++++++++----------------- 3 files changed, 68 insertions(+), 59 deletions(-) diff --git a/LICENSE.adoc b/LICENSE.adoc index 133ad3a2a..ceababfb4 100644 --- a/LICENSE.adoc +++ b/LICENSE.adoc @@ -515,9 +515,9 @@ SUCH DAMAGE. === src/third_party/httplib.* -cpp-httplib - A C++11 cross-platform HTTP/HTTPS library. Copied from -https://github.com/yhirose/cpp-httplib[cpp-httplib] commit -469c6bc2b611ec5d212275e559e58e4da256019d. The library has the following license: +cpp-httplib - A C++11 cross-platform HTTP/HTTPS library. Copied from cpp-httplib +v0.9.4 downloaded from https://github.com/yhirose/cpp-httplib[cpp-httplib]. The +library has the following license: ---- The MIT License (MIT) diff --git a/src/third_party/httplib.cpp b/src/third_party/httplib.cpp index 34a71ca4f..6be2bc044 100644 --- a/src/third_party/httplib.cpp +++ b/src/third_party/httplib.cpp @@ -622,7 +622,7 @@ socket_t create_socket(const char *host, int port, int address_family, auto service = std::to_string(port); if (getaddrinfo(host, service.c_str(), &hints, &result)) { -#ifdef __linux__ +#if defined __linux__ && !defined __ANDROID__ res_init(); #endif return INVALID_SOCKET; @@ -1411,8 +1411,7 @@ bool prepare_content_receiver(T &x, int &status, std::string encoding = x.get_header_value("Content-Encoding"); std::unique_ptr decompressor; - if (encoding.find("gzip") != std::string::npos || - encoding.find("deflate") != std::string::npos) { + if (encoding == "gzip" || encoding == "deflate") { #ifdef CPPHTTPLIB_ZLIB_SUPPORT decompressor = detail::make_unique(); #else @@ -1706,14 +1705,6 @@ std::string params_to_query_str(const Params ¶ms) { return query; } -std::string append_query_params(const char *path, const Params ¶ms) { - std::string path_with_query = path; - const static std::regex re("[^?]+\\?.*"); - auto delm = std::regex_match(path, re) ? '&' : '?'; - path_with_query += delm + params_to_query_str(params); - return path_with_query; -} - void parse_query_text(const std::string &s, Params ¶ms) { std::set cache; split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) { @@ -2347,6 +2338,14 @@ private: } // namespace detail +std::string append_query_params(const char *path, const Params ¶ms) { + std::string path_with_query = path; + const static std::regex re("[^?]+\\?.*"); + auto delm = std::regex_match(path, re) ? '&' : '?'; + path_with_query += delm + detail::params_to_query_str(params); + return path_with_query; +} + // Header utilities std::pair make_range_header(Ranges ranges) { std::string field = "bytes="; @@ -4440,7 +4439,7 @@ Result ClientImpl::Get(const char *path, const Params ¶ms, const Headers &headers, Progress progress) { if (params.empty()) { return Get(path, headers); } - std::string path_with_query = detail::append_query_params(path, params); + std::string path_with_query = append_query_params(path, params); return Get(path_with_query.c_str(), headers, progress); } @@ -4460,7 +4459,7 @@ Result ClientImpl::Get(const char *path, const Params ¶ms, return Get(path, headers, response_handler, content_receiver, progress); } - std::string path_with_query = detail::append_query_params(path, params); + std::string path_with_query = append_query_params(path, params); return Get(path_with_query.c_str(), headers, response_handler, content_receiver, progress); } diff --git a/src/third_party/httplib.h b/src/third_party/httplib.h index f2b4efa93..543ee9afe 100644 --- a/src/third_party/httplib.h +++ b/src/third_party/httplib.h @@ -178,7 +178,9 @@ using socket_t = SOCKET; #include using socket_t = int; +#ifndef INVALID_SOCKET #define INVALID_SOCKET (-1) +#endif #endif //_WIN32 #include @@ -580,23 +582,7 @@ using Logger = std::function; using SocketOptions = std::function; -inline void default_socket_options(socket_t sock) { - int yes = 1; -#ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), - sizeof(yes)); - setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - reinterpret_cast(&yes), sizeof(yes)); -#else -#ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), - sizeof(yes)); -#else - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), - sizeof(yes)); -#endif -#endif -} +void default_socket_options(socket_t sock); class Server { public: @@ -798,33 +784,9 @@ enum class Error { Compression, }; -inline std::string to_string(const Error error) { - switch (error) { - case Error::Success: return "Success"; - case Error::Connection: return "Connection"; - case Error::BindIPAddress: return "BindIPAddress"; - case Error::Read: return "Read"; - case Error::Write: return "Write"; - case Error::ExceedRedirectCount: return "ExceedRedirectCount"; - case Error::Canceled: return "Canceled"; - case Error::SSLConnection: return "SSLConnection"; - case Error::SSLLoadingCerts: return "SSLLoadingCerts"; - case Error::SSLServerVerification: return "SSLServerVerification"; - case Error::UnsupportedMultipartBoundaryChars: - return "UnsupportedMultipartBoundaryChars"; - case Error::Compression: return "Compression"; - case Error::Unknown: return "Unknown"; - default: break; - } - - return "Invalid"; -} +std::string to_string(const Error error); -inline std::ostream &operator<<(std::ostream &os, const Error &obj) { - os << to_string(obj); - os << " (" << static_cast::type>(obj) << ')'; - return os; -} +std::ostream &operator<<(std::ostream &os, const Error &obj); class Result { public: @@ -1544,6 +1506,24 @@ inline ssize_t Stream::write_format(const char *fmt, const Args &... args) { } } +inline void default_socket_options(socket_t sock) { + int yes = 1; +#ifdef _WIN32 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + sizeof(yes)); + setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, + reinterpret_cast(&yes), sizeof(yes)); +#else +#ifdef SO_REUSEPORT + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), + sizeof(yes)); +#else + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + sizeof(yes)); +#endif +#endif +} + template inline Server & Server::set_read_timeout(const std::chrono::duration &duration) { @@ -1568,6 +1548,34 @@ Server::set_idle_interval(const std::chrono::duration &duration) { return *this; } +inline std::string to_string(const Error error) { + switch (error) { + case Error::Success: return "Success"; + case Error::Connection: return "Connection"; + case Error::BindIPAddress: return "BindIPAddress"; + case Error::Read: return "Read"; + case Error::Write: return "Write"; + case Error::ExceedRedirectCount: return "ExceedRedirectCount"; + case Error::Canceled: return "Canceled"; + case Error::SSLConnection: return "SSLConnection"; + case Error::SSLLoadingCerts: return "SSLLoadingCerts"; + case Error::SSLServerVerification: return "SSLServerVerification"; + case Error::UnsupportedMultipartBoundaryChars: + return "UnsupportedMultipartBoundaryChars"; + case Error::Compression: return "Compression"; + case Error::Unknown: return "Unknown"; + default: break; + } + + return "Invalid"; +} + +inline std::ostream &operator<<(std::ostream &os, const Error &obj) { + os << to_string(obj); + os << " (" << static_cast::type>(obj) << ')'; + return os; +} + template inline T Result::get_request_header_value(const char *key, size_t id) const { return detail::get_header_value(request_headers_, key, id, 0); @@ -1618,6 +1626,8 @@ Client::set_write_timeout(const std::chrono::duration &duration) { * .h + .cc. */ +std::string append_query_params(const char *path, const Params ¶ms); + std::pair make_range_header(Ranges ranges); std::pair -- 2.47.2