From 4135334a51a2604e1b17205d774715f5cef81beb Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Nov 2024 17:17:05 -0500 Subject: [PATCH] Cleaner API (breaking change) --- httplib.h | 517 ++++++++++++--------------------------------------- test/test.cc | 251 +++++++++++++------------ 2 files changed, 258 insertions(+), 510 deletions(-) diff --git a/httplib.h b/httplib.h index 727d2d7546..50521012e5 100644 --- a/httplib.h +++ b/httplib.h @@ -1204,34 +1204,30 @@ class ClientImpl { virtual bool is_valid() const; - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, Progress progress = nullptr); Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); + Progress progress = nullptr); Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); + Progress progress = nullptr); Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); + ContentReceiver content_receiver, Progress progress = nullptr); Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); + ContentReceiver content_receiver, Progress progress = nullptr); Result Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + Progress progres = nullptr); Result Get(const std::string &path, const Params ¶ms, const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + ContentReceiver content_receiver, Progress progress = nullptr); Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress = nullptr); Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress = nullptr); @@ -1243,20 +1239,14 @@ class ClientImpl { Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); + Progress progress = nullptr); Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1271,9 +1261,7 @@ class ClientImpl { const std::string &content_type); Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); + const Params ¶ms, Progress progress = nullptr); Result Post(const std::string &path, const MultipartFormDataItems &items); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1286,20 +1274,14 @@ class ClientImpl { Result Put(const std::string &path); Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); + Progress progress = nullptr); Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); Result Put(const std::string &path, @@ -1313,9 +1295,7 @@ class ClientImpl { const std::string &content_type); Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); + const Params ¶ms, Progress progress = nullptr); Result Put(const std::string &path, const MultipartFormDataItems &items); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1327,24 +1307,15 @@ class ClientImpl { Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1360,26 +1331,17 @@ class ClientImpl { Result Delete(const std::string &path); Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); + Progress progress = nullptr); Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress); + const std::string &content_type, Progress progress = nullptr); Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); @@ -1479,8 +1441,8 @@ class ClientImpl { // shutdown_socket // close_socket // should ONLY be called when socket_mutex_ is locked. - // Also, shutdown_ssl and close_socket should also NOT be called concurrently - // with a DIFFERENT thread sending requests using that socket. + // Also, shutdown_ssl and close_socket should also NOT be called + // concurrently with a DIFFERENT thread sending requests using that socket. virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); void shutdown_socket(Socket &socket) const; void close_socket(Socket &socket); @@ -1638,34 +1600,30 @@ class Client { bool is_valid() const; - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, Progress progress = nullptr); Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); + Progress progress = nullptr); Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); + Progress progress = nullptr); Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); + ContentReceiver content_receiver, Progress progress = nullptr); Result Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); + Progress progress = nullptr); Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - + ContentReceiver content_receiver, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + Progress progress = nullptr); Result Get(const std::string &path, const Params ¶ms, const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + ContentReceiver content_receiver, Progress progress = nullptr); Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress = nullptr); Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress = nullptr); @@ -1677,20 +1635,14 @@ class Client { Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1705,9 +1657,7 @@ class Client { const std::string &content_type); Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); + const Params ¶ms, Progress progress = nullptr); Result Post(const std::string &path, const MultipartFormDataItems &items); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1720,20 +1670,14 @@ class Client { Result Put(const std::string &path); Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); + Progress progress = nullptr); Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); Result Put(const std::string &path, @@ -1747,9 +1691,7 @@ class Client { const std::string &content_type); Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); + const Params ¶ms, Progress progress = nullptr); Result Put(const std::string &path, const MultipartFormDataItems &items); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1761,24 +1703,15 @@ class Client { Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); + const std::string &content_type, Progress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1794,26 +1727,17 @@ class Client { Result Delete(const std::string &path); Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); + Progress progress = nullptr); Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress); + const std::string &content_type, Progress progress = nullptr); Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); + const std::string &content_type, Progress progress = nullptr); Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress); + Progress progress = nullptr); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); @@ -2242,8 +2166,8 @@ Client::set_write_timeout(const std::chrono::duration &duration) { } /* - * Forward declarations and types that will be part of the .h file if split into - * .h + .cc. + * Forward declarations and types that will be part of the .h file if split + * into .h + .cc. */ std::string hosted_at(const std::string &hostname); @@ -2952,8 +2876,8 @@ inline bool mmap::open(const char *path) { LARGE_INTEGER size{}; if (!::GetFileSizeEx(hFile_, &size)) { return false; } - // If the following line doesn't compile due to QuadPart, update Windows SDK. - // See: + // If the following line doesn't compile due to QuadPart, update Windows + // SDK. See: // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 if (static_cast(size.QuadPart) > (std::numeric_limits::max)()) { @@ -3474,8 +3398,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa * * WSA_FLAG_NO_HANDLE_INHERIT: - * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with - * SP1, and later + * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 + * with SP1, and later * */ if (sock == INVALID_SOCKET) { @@ -3769,12 +3693,12 @@ inline constexpr unsigned int str2tag_core(const char *s, size_t l, unsigned int h) { return (l == 0) ? h - : str2tag_core( - s + 1, l - 1, - // Unsets the 6 high bits of h, therefore no overflow happens - (((std::numeric_limits::max)() >> 6) & - h * 33) ^ - static_cast(*s)); + : str2tag_core(s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no + // overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(*s)); } inline unsigned int str2tag(const std::string &s) { @@ -4288,8 +4212,9 @@ inline bool read_content_chunked(Stream &strm, T &x, // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked // transfer coding is complete when a chunk with a chunk-size of zero is - // received, possibly followed by a trailer section, and finally terminated by - // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // received, possibly followed by a trailer section, and finally terminated + // by an empty line". + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 // // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section // does't care for the existence of the final CRLF. In other words, it seems @@ -7428,8 +7353,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { { std::lock_guard guard(socket_mutex_); - // Set this to false immediately - if it ever gets set to true by the end of - // the request, we know another thread instructed us to close the socket. + // Set this to false immediately - if it ever gets set to true by the end + // of the request, we know another thread instructed us to close the + // socket. socket_should_be_closed_when_request_is_done_ = false; auto is_alive = false; @@ -8026,8 +7952,8 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( const MultipartFormDataProviderItems &provider_items) const { size_t cur_item = 0; size_t cur_start = 0; - // cur_item and cur_start are copied to within the std::function and maintain - // state between successive calls + // cur_item and cur_start are copied to within the std::function and + // maintain state between successive calls return [&, cur_item, cur_start](size_t offset, DataSink &sink) mutable -> bool { if (!offset && !items.empty()) { @@ -8075,18 +8001,10 @@ ClientImpl::process_socket(const Socket &socket, inline bool ClientImpl::is_ssl() const { return false; } -inline Result ClientImpl::Get(const std::string &path) { - return Get(path, Headers(), Progress()); -} - inline Result ClientImpl::Get(const std::string &path, Progress progress) { return Get(path, Headers(), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { - return Get(path, headers, Progress()); -} - inline Result ClientImpl::Get(const std::string &path, const Headers &headers, Progress progress) { Request req; @@ -8098,11 +8016,6 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return send_(std::move(req)); } -inline Result ClientImpl::Get(const std::string &path, - ContentReceiver content_receiver) { - return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); -} - inline Result ClientImpl::Get(const std::string &path, ContentReceiver content_receiver, Progress progress) { @@ -8110,11 +8023,6 @@ inline Result ClientImpl::Get(const std::string &path, std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return Get(path, headers, nullptr, std::move(content_receiver), nullptr); -} - inline Result ClientImpl::Get(const std::string &path, const Headers &headers, ContentReceiver content_receiver, Progress progress) { @@ -8122,20 +8030,6 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, Headers(), std::move(response_handler), - std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, headers, std::move(response_handler), - std::move(content_receiver), nullptr); -} - inline Result ClientImpl::Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, @@ -8163,6 +8057,14 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return send_(std::move(req)); } +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + Progress progress) { + if (params.empty()) { return Get(path, std::move(progress)); } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, std::move(progress)); +} + inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, const Headers &headers, Progress progress) { if (params.empty()) { return Get(path, headers); } @@ -8171,6 +8073,13 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, return Get(path_with_query, headers, std::move(progress)); } +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, params, Headers{}, nullptr, std::move(content_receiver), + std::move(progress)); +} + inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, @@ -8179,6 +8088,14 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, std::move(progress)); } +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, params, Headers{}, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, @@ -8223,13 +8140,6 @@ inline Result ClientImpl::Post(const std::string &path, const char *body, return Post(path, Headers(), body, content_length, content_type, nullptr); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, @@ -8238,25 +8148,12 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, nullptr, nullptr, content_type, progress); } -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return Post(path, Headers(), body, content_type); -} - inline Result ClientImpl::Post(const std::string &path, const std::string &body, const std::string &content_type, Progress progress) { return Post(path, Headers(), body, content_type, progress); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, @@ -8300,12 +8197,6 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, nullptr); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Post(path, headers, query, "application/x-www-form-urlencoded"); -} - inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress) { auto query = detail::params_to_query_str(params); @@ -8363,13 +8254,6 @@ inline Result ClientImpl::Put(const std::string &path, const char *body, return Put(path, Headers(), body, content_length, content_type); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, @@ -8378,25 +8262,12 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, nullptr, nullptr, content_type, progress); } -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return Put(path, Headers(), body, content_type); -} - inline Result ClientImpl::Put(const std::string &path, const std::string &body, const std::string &content_type, Progress progress) { return Put(path, Headers(), body, content_type, progress); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, @@ -8440,12 +8311,6 @@ inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { return Put(path, Headers(), params); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Put(path, headers, query, "application/x-www-form-urlencoded"); -} - inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress) { auto query = detail::params_to_query_str(params); @@ -8496,12 +8361,6 @@ inline Result ClientImpl::Patch(const std::string &path) { return Patch(path, std::string(), std::string()); } -inline Result ClientImpl::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Patch(path, Headers(), body, content_length, content_type); -} - inline Result ClientImpl::Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, @@ -8509,12 +8368,6 @@ inline Result ClientImpl::Patch(const std::string &path, const char *body, return Patch(path, Headers(), body, content_length, content_type, progress); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return Patch(path, headers, body, content_length, content_type, nullptr); -} - inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, @@ -8524,12 +8377,6 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, content_type, progress); } -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Patch(path, Headers(), body, content_type); -} - inline Result ClientImpl::Patch(const std::string &path, const std::string &body, const std::string &content_type, @@ -8537,12 +8384,6 @@ inline Result ClientImpl::Patch(const std::string &path, return Patch(path, Headers(), body, content_type, progress); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Patch(path, headers, body, content_type, nullptr); -} - inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, @@ -8591,12 +8432,6 @@ inline Result ClientImpl::Delete(const std::string &path, return Delete(path, headers, std::string(), std::string()); } -inline Result ClientImpl::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, Headers(), body, content_length, content_type); -} - inline Result ClientImpl::Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, @@ -8604,13 +8439,6 @@ inline Result ClientImpl::Delete(const std::string &path, const char *body, return Delete(path, Headers(), body, content_length, content_type, progress); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, headers, body, content_length, content_type, nullptr); -} - inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, @@ -8628,12 +8456,6 @@ inline Result ClientImpl::Delete(const std::string &path, return send_(std::move(req)); } -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Delete(path, Headers(), body.data(), body.size(), content_type); -} - inline Result ClientImpl::Delete(const std::string &path, const std::string &body, const std::string &content_type, @@ -8642,13 +8464,6 @@ inline Result ClientImpl::Delete(const std::string &path, progress); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Delete(path, headers, body.data(), body.size(), content_type); -} - inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, const std::string &body, @@ -8678,8 +8493,8 @@ inline void ClientImpl::stop() { // If there is anything ongoing right now, the ONLY thread-safe thing we can // do is to shutdown_socket, so that threads using this socket suddenly // discover they can't read/write any more and error out. Everything else - // (closing the socket, shutting ssl down) is unsafe because these actions are - // not thread-safe. + // (closing the socket, shutting ssl down) is unsafe because these actions + // are not thread-safe. if (socket_requests_in_flight_ > 0) { shutdown_socket(socket_); @@ -9289,7 +9104,8 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { if (ca_cert_store) { if (ctx_) { if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { - // Free memory allocated for old cert and use new store `ca_cert_store` + // Free memory allocated for old cert and use new store + // `ca_cert_store` SSL_CTX_set_cert_store(ctx_, ca_cert_store); } } else { @@ -9313,7 +9129,8 @@ inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { return is_valid() && ClientImpl::create_and_connect_socket(socket, error); } -// Assumes that socket_mutex_ is locked and that there are no requests in flight +// Assumes that socket_mutex_ is locked and that there are no requests in +// flight inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, bool &success, Error &error) { success = true; @@ -9712,10 +9529,6 @@ inline bool Client::is_valid() const { return cli_ != nullptr && cli_->is_valid(); } -inline Result Client::Get(const std::string &path) { return cli_->Get(path); } -inline Result Client::Get(const std::string &path, const Headers &headers) { - return cli_->Get(path, headers); -} inline Result Client::Get(const std::string &path, Progress progress) { return cli_->Get(path, std::move(progress)); } @@ -9723,14 +9536,6 @@ inline Result Client::Get(const std::string &path, const Headers &headers, Progress progress) { return cli_->Get(path, headers, std::move(progress)); } -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(content_receiver)); -} inline Result Client::Get(const std::string &path, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, std::move(content_receiver), std::move(progress)); @@ -9740,18 +9545,6 @@ inline Result Client::Get(const std::string &path, const Headers &headers, return cli_->Get(path, headers, std::move(content_receiver), std::move(progress)); } -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver)); -} inline Result Client::Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { @@ -9764,16 +9557,31 @@ inline Result Client::Get(const std::string &path, const Headers &headers, return cli_->Get(path, headers, std::move(response_handler), std::move(content_receiver), std::move(progress)); } +inline Result Client::Get(const std::string &path, const Params ¶ms, + Progress progress) { + return cli_->Get(path, params, Headers{}, std::move(progress)); +} inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, Progress progress) { return cli_->Get(path, params, headers, std::move(progress)); } +inline Result Client::Get(const std::string &path, const Params ¶ms, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, std::move(content_receiver), + std::move(progress)); +} inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, params, headers, std::move(content_receiver), std::move(progress)); } +inline Result Client::Get(const std::string &path, const Params ¶ms, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, @@ -9796,30 +9604,16 @@ inline Result Client::Post(const std::string &path, const char *body, const std::string &content_type) { return cli_->Post(path, body, content_length, content_type); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_length, content_type); -} inline Result Client::Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, Progress progress) { return cli_->Post(path, headers, body, content_length, content_type, progress); } -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Post(path, body, content_type); -} inline Result Client::Post(const std::string &path, const std::string &body, const std::string &content_type, Progress progress) { return cli_->Post(path, body, content_type, progress); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_type); -} inline Result Client::Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, Progress progress) { @@ -9851,10 +9645,6 @@ inline Result Client::Post(const std::string &path, const Headers &headers, inline Result Client::Post(const std::string &path, const Params ¶ms) { return cli_->Post(path, params); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Post(path, headers, params); -} inline Result Client::Post(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress) { return cli_->Post(path, headers, params, progress); @@ -9884,29 +9674,15 @@ inline Result Client::Put(const std::string &path, const char *body, const std::string &content_type) { return cli_->Put(path, body, content_length, content_type); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_length, content_type); -} inline Result Client::Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, Progress progress) { return cli_->Put(path, headers, body, content_length, content_type, progress); } -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Put(path, body, content_type); -} inline Result Client::Put(const std::string &path, const std::string &body, const std::string &content_type, Progress progress) { return cli_->Put(path, body, content_type, progress); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_type); -} inline Result Client::Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, Progress progress) { @@ -9935,13 +9711,6 @@ inline Result Client::Put(const std::string &path, const Headers &headers, const std::string &content_type) { return cli_->Put(path, headers, std::move(content_provider), content_type); } -inline Result Client::Put(const std::string &path, const Params ¶ms) { - return cli_->Put(path, params); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Put(path, headers, params); -} inline Result Client::Put(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress) { return cli_->Put(path, headers, params, progress); @@ -9968,22 +9737,12 @@ Client::Put(const std::string &path, const Headers &headers, inline Result Client::Patch(const std::string &path) { return cli_->Patch(path); } -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, body, content_length, content_type); -} inline Result Client::Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, Progress progress) { return cli_->Patch(path, body, content_length, content_type, progress); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_length, content_type); -} inline Result Client::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, @@ -9991,20 +9750,11 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, return cli_->Patch(path, headers, body, content_length, content_type, progress); } -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, body, content_type); -} inline Result Client::Patch(const std::string &path, const std::string &body, const std::string &content_type, Progress progress) { return cli_->Patch(path, body, content_type, progress); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_type); -} inline Result Client::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, @@ -10040,22 +9790,12 @@ inline Result Client::Delete(const std::string &path) { inline Result Client::Delete(const std::string &path, const Headers &headers) { return cli_->Delete(path, headers); } -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, body, content_length, content_type); -} inline Result Client::Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, Progress progress) { return cli_->Delete(path, body, content_length, content_type, progress); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_length, content_type); -} inline Result Client::Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, @@ -10063,20 +9803,11 @@ inline Result Client::Delete(const std::string &path, const Headers &headers, return cli_->Delete(path, headers, body, content_length, content_type, progress); } -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, body, content_type); -} inline Result Client::Delete(const std::string &path, const std::string &body, const std::string &content_type, Progress progress) { return cli_->Delete(path, body, content_type, progress); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_type); -} inline Result Client::Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, diff --git a/test/test.cc b/test/test.cc index d2a30783e7..05084e264a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -513,37 +513,37 @@ TEST(GetHeaderValueTest, RegularValueInt) { TEST(GetHeaderValueTest, Range) { { - Headers headers = {make_range_header({{1, -1}})}; + auto headers = Headers{make_range_header({{1, -1}})}; auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-", val); } { - Headers headers = {make_range_header({{-1, 1}})}; + auto headers = Headers{make_range_header({{-1, 1}})}; auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=-1", val); } { - Headers headers = {make_range_header({{1, 10}})}; + auto headers = Headers{make_range_header({{1, 10}})}; auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-10", val); } { - Headers headers = {make_range_header({{1, 10}, {100, -1}})}; + auto headers = Headers{make_range_header({{1, 10}, {100, -1}})}; auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-10, 100-", val); } { - Headers headers = {make_range_header({{1, 10}, {100, 200}})}; + auto headers = Headers{make_range_header({{1, 10}, {100, 200}})}; auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-10, 100-200", val); } { - Headers headers = {make_range_header({{0, 0}, {-1, 1}})}; + auto headers = Headers{make_range_header({{0, 0}, {-1, 1}})}; auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=0-0, -1", val); } @@ -789,24 +789,47 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { #endif cli.set_connection_timeout(2); - std::string body; - auto res = cli.Get( - "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", - [&](const Response &response) { - EXPECT_EQ(StatusCode::OK_200, response.status); - return true; - }, - [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; - }); - ASSERT_TRUE(res); + { + std::string body; + auto res = cli.Get( + "/httpgallery/chunked/chunkedimage.aspx", + [&](const Response &response) { + EXPECT_EQ(StatusCode::OK_200, response.status); + return true; + }, + [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + ASSERT_TRUE(res); - std::string out; - detail::read_file("./image.jpg", out); + std::string out; + detail::read_file("./image.jpg", out); - EXPECT_EQ(StatusCode::OK_200, res->status); - EXPECT_EQ(out, body); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(out, body); + } + + { + std::string body; + auto res = cli.Get( + "/httpgallery/chunked/chunkedimage.aspx", Params{}, + [&](const Response &response) { + EXPECT_EQ(StatusCode::OK_200, response.status); + return true; + }, + [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + ASSERT_TRUE(res); + + std::string out; + detail::read_file("./image.jpg", out); + + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(out, body); + } } TEST(RangeTest, FromHTTPBin_Online) { @@ -835,7 +858,7 @@ TEST(RangeTest, FromHTTPBin_Online) { } { - Headers headers = {make_range_header({{1, -1}})}; + auto headers = Headers{make_range_header({{1, -1}})}; auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body); @@ -843,7 +866,7 @@ TEST(RangeTest, FromHTTPBin_Online) { } { - Headers headers = {make_range_header({{1, 10}})}; + auto headers = Headers{make_range_header({{1, 10}})}; auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); @@ -851,7 +874,7 @@ TEST(RangeTest, FromHTTPBin_Online) { } { - Headers headers = {make_range_header({{0, 31}})}; + auto headers = Headers{make_range_header({{0, 31}})}; auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); @@ -859,7 +882,7 @@ TEST(RangeTest, FromHTTPBin_Online) { } { - Headers headers = {make_range_header({{0, -1}})}; + auto headers = Headers{make_range_header({{0, -1}})}; auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); @@ -867,7 +890,7 @@ TEST(RangeTest, FromHTTPBin_Online) { } { - Headers headers = {make_range_header({{0, 32}})}; + auto headers = Headers{make_range_header({{0, 32}})}; auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); @@ -1053,9 +1076,8 @@ TEST(CancelTest, NoCancelPost) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return true; }); + auto res = cli.Post("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return true; }); ASSERT_TRUE(res); EXPECT_EQ("Hello World!", res->body); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -1080,9 +1102,8 @@ TEST(CancelTest, WithCancelSmallPayloadPost) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Post("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1106,9 +1127,8 @@ TEST(CancelTest, WithCancelLargePayloadPost) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Post("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1132,9 +1152,8 @@ TEST(CancelTest, NoCancelPut) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return true; }); + auto res = cli.Put("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return true; }); ASSERT_TRUE(res); EXPECT_EQ("Hello World!", res->body); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -1159,9 +1178,8 @@ TEST(CancelTest, WithCancelSmallPayloadPut) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Put("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1185,9 +1203,8 @@ TEST(CancelTest, WithCancelLargePayloadPut) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Put("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1211,9 +1228,8 @@ TEST(CancelTest, NoCancelPatch) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return true; }); + auto res = cli.Patch("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return true; }); ASSERT_TRUE(res); EXPECT_EQ("Hello World!", res->body); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -1238,9 +1254,8 @@ TEST(CancelTest, WithCancelSmallPayloadPatch) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Patch("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1264,9 +1279,8 @@ TEST(CancelTest, WithCancelLargePayloadPatch) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Patch("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1290,9 +1304,8 @@ TEST(CancelTest, NoCancelDelete) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return true; }); + auto res = cli.Delete("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return true; }); ASSERT_TRUE(res); EXPECT_EQ("Hello World!", res->body); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -1317,9 +1330,8 @@ TEST(CancelTest, WithCancelSmallPayloadDelete) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Delete("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1343,9 +1355,8 @@ TEST(CancelTest, WithCancelLargePayloadDelete) { Client cli(HOST, PORT); cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = - cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Delete("/", JSON_DATA, "application/json", + [](uint64_t, uint64_t) { return false; }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } @@ -1374,8 +1385,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { } { - auto res = - cli.Get(path, {make_basic_authentication_header("hello", "world")}); + auto res = cli.Get( + path, Headers{make_basic_authentication_header("hello", "world")}); ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); @@ -1592,7 +1603,7 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) { params.emplace("url", "http://www.google.com"); params.emplace("status_code", "302"); - auto res = cli.Get("/httpbin/redirect-to", params, Headers{}); + auto res = cli.Get("/httpbin/redirect-to", params); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -1604,7 +1615,7 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) { Params params; params.emplace("url", "http://www.google.com"); - auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{}); + auto res = cli.Get("/httpbin/redirect-to?status_code=302", params); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -2004,7 +2015,7 @@ TEST(ErrorHandlerTest, ContentLength) { { Client cli(HOST, PORT); - auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); + auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -2087,7 +2098,7 @@ TEST(ExceptionTest, WithExceptionHandler) { Client cli(HOST, PORT); for (size_t j = 0; j < 100; j++) { - auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); + auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -2098,7 +2109,7 @@ TEST(ExceptionTest, WithExceptionHandler) { cli.set_keep_alive(true); for (size_t j = 0; j < 100; j++) { - auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); + auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -3349,7 +3360,7 @@ TEST_F(ServerTest, UserDefinedMIMETypeMapping) { } TEST_F(ServerTest, StaticFileRange) { - auto res = cli_.Get("/dir/test.abcde", {{make_range_header({{2, 3}})}}); + auto res = cli_.Get("/dir/test.abcde", Headers{make_range_header({{2, 3}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); @@ -3360,8 +3371,8 @@ TEST_F(ServerTest, StaticFileRange) { } TEST_F(ServerTest, StaticFileRanges) { - auto res = - cli_.Get("/dir/test.abcde", {{make_range_header({{1, 2}, {4, -1}})}}); + auto res = cli_.Get("/dir/test.abcde", + Headers{make_range_header({{1, 2}, {4, -1}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_TRUE( @@ -3373,7 +3384,7 @@ TEST_F(ServerTest, StaticFileRanges) { } TEST_F(ServerTest, StaticFileRangeHead) { - auto res = cli_.Head("/dir/test.abcde", {{make_range_header({{2, 3}})}}); + auto res = cli_.Head("/dir/test.abcde", Headers{make_range_header({{2, 3}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); @@ -3383,7 +3394,7 @@ TEST_F(ServerTest, StaticFileRangeHead) { } TEST_F(ServerTest, StaticFileRangeBigFile) { - auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{-1, 5}})}}); + auto res = cli_.Get("/dir/1MB.txt", Headers{make_range_header({{-1, 5}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -3395,7 +3406,7 @@ TEST_F(ServerTest, StaticFileRangeBigFile) { } TEST_F(ServerTest, StaticFileRangeBigFile2) { - auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{1, 4097}})}}); + auto res = cli_.Get("/dir/1MB.txt", Headers{make_range_header({{1, 4097}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -3724,7 +3735,7 @@ TEST_F(ServerTest, CaseInsensitiveTransferEncoding) { } TEST_F(ServerTest, GetStreamed2) { - auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}}); + auto res = cli_.Get("/streamed", Headers{make_range_header({{2, 3}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("2", res->get_header_value("Content-Length")); @@ -3742,7 +3753,8 @@ TEST_F(ServerTest, GetStreamed) { } TEST_F(ServerTest, GetStreamedWithRange1) { - auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}}); + auto res = + cli_.Get("/streamed-with-range", Headers{make_range_header({{3, 5}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); @@ -3752,7 +3764,8 @@ TEST_F(ServerTest, GetStreamedWithRange1) { } TEST_F(ServerTest, GetStreamedWithRange2) { - auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}}); + auto res = + cli_.Get("/streamed-with-range", Headers{make_range_header({{1, -1}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); @@ -3762,7 +3775,7 @@ TEST_F(ServerTest, GetStreamedWithRange2) { } TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { - auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-3"}}); + auto res = cli_.Get("/streamed-with-range", Headers{{"Range", "bytes=-3"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); @@ -3772,7 +3785,8 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { } TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { - auto res = cli_.Get("/streamed-with-range?error", {{"Range", "bytes=-9999"}}); + auto res = + cli_.Get("/streamed-with-range?error", Headers{{"Range", "bytes=-9999"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); @@ -3781,8 +3795,9 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { } TEST_F(ServerTest, GetStreamedWithRangeError) { - auto res = cli_.Get("/streamed-with-range", - {{"Range", "bytes=92233720368547758079223372036854775806-" + auto res = + cli_.Get("/streamed-with-range", + Headers{{"Range", "bytes=92233720368547758079223372036854775806-" "92233720368547758079223372036854775807"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); @@ -3794,8 +3809,8 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { TEST_F(ServerTest, GetRangeWithMaxLongLength) { auto res = cli_.Get( "/with-range", - {{"Range", - "bytes=0-" + std::to_string(std::numeric_limits::max())}}); + Headers{{"Range", + "bytes=0-" + std::to_string(std::numeric_limits::max())}}); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); @@ -3803,7 +3818,7 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) { } TEST_F(ServerTest, GetRangeWithZeroToInfinite) { - auto res = cli_.Get("/with-range", { + auto res = cli_.Get("/with-range", Headers{ {"Range", "bytes=0-"}, {"Accept-Encoding", ""}, }); @@ -3816,8 +3831,8 @@ TEST_F(ServerTest, GetRangeWithZeroToInfinite) { } TEST_F(ServerTest, GetStreamedWithRangeMultipart) { - auto res = - cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}}); + auto res = cli_.Get("/streamed-with-range", + Headers{make_range_header({{1, 2}, {4, 5}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("267", res->get_header_value("Content-Length")); @@ -3831,8 +3846,8 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) { ranges.emplace_back(0, -1); } - auto res = - cli_.Get("/streamed-with-range?error", {{make_range_header(ranges)}}); + auto res = cli_.Get("/streamed-with-range?error", + Headers{make_range_header(ranges)}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); @@ -3842,7 +3857,7 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) { TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) { auto res = cli_.Get("/streamed-with-range?error", - {{make_range_header({{0, -1}, {0, -1}})}}); + Headers{make_range_header({{0, -1}, {0, -1}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); @@ -3851,8 +3866,9 @@ TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) { } TEST_F(ServerTest, GetStreamedWithRangesMoreThanTwoOverwrapping) { - auto res = cli_.Get("/streamed-with-range?error", - {{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})}}); + auto res = + cli_.Get("/streamed-with-range?error", + Headers{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); @@ -3902,7 +3918,7 @@ TEST_F(ServerTest, ClientStop) { } TEST_F(ServerTest, GetWithRange1) { - auto res = cli_.Get("/with-range", { + auto res = cli_.Get("/with-range", Headers{ make_range_header({{3, 5}}), {"Accept-Encoding", ""}, }); @@ -3915,7 +3931,7 @@ TEST_F(ServerTest, GetWithRange1) { } TEST_F(ServerTest, GetWithRange2) { - auto res = cli_.Get("/with-range", { + auto res = cli_.Get("/with-range", Headers{ make_range_header({{1, -1}}), {"Accept-Encoding", ""}, }); @@ -3928,7 +3944,7 @@ TEST_F(ServerTest, GetWithRange2) { } TEST_F(ServerTest, GetWithRange3) { - auto res = cli_.Get("/with-range", { + auto res = cli_.Get("/with-range", Headers{ make_range_header({{0, 0}}), {"Accept-Encoding", ""}, }); @@ -3941,7 +3957,7 @@ TEST_F(ServerTest, GetWithRange3) { } TEST_F(ServerTest, GetWithRange4) { - auto res = cli_.Get("/with-range", { + auto res = cli_.Get("/with-range", Headers{ make_range_header({{-1, 2}}), {"Accept-Encoding", ""}, }); @@ -3954,13 +3970,15 @@ TEST_F(ServerTest, GetWithRange4) { } TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) { - auto res = cli_.Get("/with-range", {{make_range_header({{10000, 20000}})}}); + auto res = + cli_.Get("/with-range", Headers{make_range_header({{10000, 20000}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } TEST_F(ServerTest, GetWithRangeMultipart) { - auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}}); + auto res = + cli_.Get("/with-range", Headers{make_range_header({{1, 2}, {4, 5}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("267", res->get_header_value("Content-Length")); @@ -3969,15 +3987,15 @@ TEST_F(ServerTest, GetWithRangeMultipart) { } TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { - auto res = - cli_.Get("/with-range", {{make_range_header({{-1, 2}, {10000, 30000}})}}); + auto res = cli_.Get("/with-range", + Headers{make_range_header({{-1, 2}, {10000, 30000}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } TEST_F(ServerTest, GetWithRangeCustomizedResponse) { auto res = cli_.Get("/with-range-customized-response", - {{make_range_header({{1, 2}})}}); + Headers{make_range_header({{1, 2}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::BadRequest_400, res->status); EXPECT_EQ(true, res->has_header("Content-Length")); @@ -3987,7 +4005,7 @@ TEST_F(ServerTest, GetWithRangeCustomizedResponse) { TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) { auto res = cli_.Get("/with-range-customized-response", - {{make_range_header({{1, 2}, {4, 5}})}}); + Headers{make_range_header({{1, 2}, {4, 5}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::BadRequest_400, res->status); EXPECT_EQ(true, res->has_header("Content-Length")); @@ -3996,7 +4014,7 @@ TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) { } TEST_F(ServerTest, Issue1772) { - auto res = cli_.Get("/issue1772", {{make_range_header({{1000, -1}})}}); + auto res = cli_.Get("/issue1772", Headers{make_range_header({{1000, -1}})}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } @@ -5681,7 +5699,7 @@ TEST(GetWithParametersTest, GetWithParameters) { params.emplace("hello", "world"); params.emplace("hello2", "world2"); params.emplace("hello3", "world3"); - auto res = cli.Get("/", params, Headers{}); + auto res = cli.Get("/", params); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -5738,11 +5756,10 @@ TEST(GetWithParametersTest, GetWithParameters2) { params.emplace("hello", "world"); std::string body; - auto res = cli.Get("/", params, Headers{}, - [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; - }); + auto res = cli.Get("/", params, [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -5764,7 +5781,7 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { Client cli(host); #endif - cli.set_default_headers({make_range_header({{1, 10}})}); + cli.set_default_headers(Headers{make_range_header({{1, 10}})}); cli.set_connection_timeout(5); { @@ -6572,7 +6589,7 @@ TEST(SendAPI, WithParamsInRequest) { ASSERT_TRUE(res); } { - auto res = cli.Get("/", {{"test", "test_value"}}, Headers{}); + auto res = cli.Get("/", Params{{"test", "test_value"}}); ASSERT_TRUE(res); } } @@ -6701,8 +6718,8 @@ TEST(YahooRedirectTest3, NewResultInterface_Online) { #ifdef CPPHTTPLIB_BROTLI_SUPPORT TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) { Client cli("https://cdnjs.cloudflare.com"); - auto res = - cli.Get("/ajax/libs/jquery/3.5.1/jquery.js", {{"Accept-Encoding", "br"}}); + auto res = cli.Get("/ajax/libs/jquery/3.5.1/jquery.js", + Headers{{"Accept-Encoding", "br"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -6731,7 +6748,7 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) { params.emplace("url", "http://www.google.com"); params.emplace("status_code", "302"); - auto res = cli.Get("/httpbin/redirect-to", params, Headers{}); + auto res = cli.Get("/httpbin/redirect-to", params); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -6743,7 +6760,7 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) { Params params; params.emplace("url", "http://www.google.com"); - auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{}); + auto res = cli.Get("/httpbin/redirect-to?status_code=302", params); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -7898,7 +7915,7 @@ TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { svr.wait_until_ready(); Client cli(HOST, PORT); - cli.Get("/test", {{"Test", "_\n\r_\n\r_"}}); + cli.Get("/test", Headers{{"Test", "_\n\r_\n\r_"}}); } #ifndef _WIN32