Cleaner API (breaking change)

This commit is contained in:
yhirose 2024-11-29 17:17:05 -05:00
parent 321a86d9f2
commit 335246bc7d
3 changed files with 262 additions and 515 deletions

View File

@ -578,7 +578,7 @@ auto res = cli.Get("/hi", headers);
```
or
```c++
auto res = cli.Get("/hi", {{"Hello", "World!"}});
auto res = cli.Get("/hi", httplib::Headers{{"Hello", "World!"}});
```
or
```c++
@ -675,7 +675,7 @@ auto res = cli.Get("/large-data",
std::string body;
auto res = cli.Get(
"/stream", Headers(),
"/stream",
[&](const Response &response) {
EXPECT_EQ(StatusCode::OK_200, response.status);
return true; // return 'false' if you want to cancel the request.
@ -847,13 +847,13 @@ The default `Accept-Encoding` value contains all possible compression types. So,
```c++
res = cli.Get("/resource/foo");
res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate, br"}});
res = cli.Get("/resource/foo", httplib::Headers{{"Accept-Encoding", "gzip, deflate, br"}});
```
If we don't want a response without compression, we have to set `Accept-Encoding` to an empty string. This behavior is similar to curl.
```c++
res = cli.Get("/resource/foo", {{"Accept-Encoding", ""}});
res = cli.Get("/resource/foo", httplib::Headers{{"Accept-Encoding", ""}});
```
### Compress request body on client

509
httplib.h
View File

@ -1241,34 +1241,30 @@ public:
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);
Progress progress = nullptr);
Result Get(const std::string &path, const Headers &headers,
ContentReceiver content_receiver, Progress progress);
ContentReceiver content_receiver, Progress progress = nullptr);
Result Get(const std::string &path, ResponseHandler response_handler,
ContentReceiver content_receiver);
Result Get(const std::string &path, const Headers &headers,
ResponseHandler response_handler,
ContentReceiver content_receiver);
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 &params,
Progress progres = nullptr);
Result Get(const std::string &path, const Params &params,
const Headers &headers, Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
ContentReceiver content_receiver, Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
const Headers &headers, ContentReceiver content_receiver,
Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
ResponseHandler response_handler, ContentReceiver content_receiver,
Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
const Headers &headers, ResponseHandler response_handler,
ContentReceiver content_receiver, Progress progress = nullptr);
@ -1280,20 +1276,14 @@ public:
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);
@ -1308,9 +1298,7 @@ public:
const std::string &content_type);
Result Post(const std::string &path, const Params &params);
Result Post(const std::string &path, const Headers &headers,
const Params &params);
Result Post(const std::string &path, const Headers &headers,
const Params &params, Progress progress);
const Params &params, Progress progress = nullptr);
Result Post(const std::string &path, const MultipartFormDataItems &items);
Result Post(const std::string &path, const Headers &headers,
const MultipartFormDataItems &items);
@ -1323,20 +1311,14 @@ public:
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);
Progress progress = nullptr);
Result Put(const std::string &path, const std::string &body,
const std::string &content_type);
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,
@ -1350,9 +1332,7 @@ public:
const std::string &content_type);
Result Put(const std::string &path, const Params &params);
Result Put(const std::string &path, const Headers &headers,
const Params &params);
Result Put(const std::string &path, const Headers &headers,
const Params &params, Progress progress);
const Params &params, Progress progress = nullptr);
Result Put(const std::string &path, const MultipartFormDataItems &items);
Result Put(const std::string &path, const Headers &headers,
const MultipartFormDataItems &items);
@ -1364,24 +1344,15 @@ public:
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);
const std::string &content_type, Progress progress = nullptr);
Result Patch(const std::string &path, const std::string &body,
const std::string &content_type);
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);
@ -1397,26 +1368,17 @@ public:
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);
Progress progress = nullptr);
Result Delete(const std::string &path, const Headers &headers,
const char *body, size_t content_length,
const std::string &content_type);
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);
@ -1521,8 +1483,8 @@ protected:
// 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);
@ -1680,34 +1642,30 @@ public:
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);
Progress progress = nullptr);
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);
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 &params,
Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
const Headers &headers, Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
ContentReceiver content_receiver, Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
const Headers &headers, ContentReceiver content_receiver,
Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
ResponseHandler response_handler, ContentReceiver content_receiver,
Progress progress = nullptr);
Result Get(const std::string &path, const Params &params,
const Headers &headers, ResponseHandler response_handler,
ContentReceiver content_receiver, Progress progress = nullptr);
@ -1719,20 +1677,14 @@ public:
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);
@ -1747,9 +1699,7 @@ public:
const std::string &content_type);
Result Post(const std::string &path, const Params &params);
Result Post(const std::string &path, const Headers &headers,
const Params &params);
Result Post(const std::string &path, const Headers &headers,
const Params &params, Progress progress);
const Params &params, Progress progress = nullptr);
Result Post(const std::string &path, const MultipartFormDataItems &items);
Result Post(const std::string &path, const Headers &headers,
const MultipartFormDataItems &items);
@ -1762,20 +1712,14 @@ public:
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);
Progress progress = nullptr);
Result Put(const std::string &path, const std::string &body,
const std::string &content_type);
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,
@ -1789,9 +1733,7 @@ public:
const std::string &content_type);
Result Put(const std::string &path, const Params &params);
Result Put(const std::string &path, const Headers &headers,
const Params &params);
Result Put(const std::string &path, const Headers &headers,
const Params &params, Progress progress);
const Params &params, Progress progress = nullptr);
Result Put(const std::string &path, const MultipartFormDataItems &items);
Result Put(const std::string &path, const Headers &headers,
const MultipartFormDataItems &items);
@ -1803,24 +1745,15 @@ public:
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);
const std::string &content_type, Progress progress = nullptr);
Result Patch(const std::string &path, const std::string &body,
const std::string &content_type);
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);
@ -1836,26 +1769,17 @@ public:
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);
Progress progress = nullptr);
Result Delete(const std::string &path, const Headers &headers,
const char *body, size_t content_length,
const std::string &content_type);
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);
@ -2346,8 +2270,8 @@ Client::set_max_timeout(const std::chrono::duration<Rep, Period> &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);
@ -3113,8 +3037,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<ULONGLONG>(size.QuadPart) >
(std::numeric_limits<decltype(size_)>::max)()) {
@ -3632,8 +3556,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) {
@ -3885,9 +3809,9 @@ 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
: str2tag_core(s + 1, l - 1,
// Unsets the 6 high bits of h, therefore no
// overflow happens
(((std::numeric_limits<unsigned int>::max)() >> 6) &
h * 33) ^
static_cast<unsigned char>(*s));
@ -4399,8 +4323,9 @@ inline bool read_content_chunked(Stream &strm, T &x,
// NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "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
@ -7573,8 +7498,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
{
std::lock_guard<std::mutex> 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;
@ -8181,8 +8107,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()) {
@ -8231,18 +8157,10 @@ inline bool ClientImpl::process_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;
@ -8257,11 +8175,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) {
@ -8269,11 +8182,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) {
@ -8281,20 +8189,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,
@ -8325,6 +8219,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 &params,
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 &params,
const Headers &headers, Progress progress) {
if (params.empty()) { return Get(path, headers); }
@ -8333,6 +8235,13 @@ inline Result ClientImpl::Get(const std::string &path, const Params &params,
return Get(path_with_query, headers, std::move(progress));
}
inline Result ClientImpl::Get(const std::string &path, const Params &params,
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 &params,
const Headers &headers,
ContentReceiver content_receiver,
@ -8341,6 +8250,14 @@ inline Result ClientImpl::Get(const std::string &path, const Params &params,
std::move(progress));
}
inline Result ClientImpl::Get(const std::string &path, const Params &params,
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 &params,
const Headers &headers,
ResponseHandler response_handler,
@ -8388,13 +8305,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,
@ -8403,25 +8313,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,
@ -8465,12 +8362,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 &params) {
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 &params, Progress progress) {
auto query = detail::params_to_query_str(params);
@ -8528,13 +8419,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,
@ -8543,25 +8427,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,
@ -8605,12 +8476,6 @@ inline Result ClientImpl::Put(const std::string &path, const Params &params) {
return Put(path, Headers(), params);
}
inline Result ClientImpl::Put(const std::string &path, const Headers &headers,
const Params &params) {
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 &params, Progress progress) {
auto query = detail::params_to_query_str(params);
@ -8661,12 +8526,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,
@ -8674,12 +8533,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,
@ -8689,12 +8542,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,
@ -8702,12 +8549,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,
@ -8756,12 +8597,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,
@ -8769,13 +8604,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,
@ -8796,12 +8624,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,
@ -8810,13 +8632,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,
@ -8849,8 +8664,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_);
@ -9475,7 +9290,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 {
@ -9499,7 +9315,6 @@ 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
inline bool SSLClient::connect_with_proxy(
Socket &socket,
std::chrono::time_point<std::chrono::steady_clock> start_time,
@ -9916,10 +9731,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));
}
@ -9927,14 +9738,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));
@ -9944,18 +9747,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) {
@ -9968,16 +9759,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 &params,
Progress progress) {
return cli_->Get(path, params, Headers{}, std::move(progress));
}
inline Result Client::Get(const std::string &path, const Params &params,
const Headers &headers, Progress progress) {
return cli_->Get(path, params, headers, std::move(progress));
}
inline Result Client::Get(const std::string &path, const Params &params,
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 &params,
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 &params,
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 &params,
const Headers &headers,
ResponseHandler response_handler,
@ -10000,30 +9806,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) {
@ -10055,10 +9847,6 @@ inline Result Client::Post(const std::string &path, const Headers &headers,
inline Result Client::Post(const std::string &path, const Params &params) {
return cli_->Post(path, params);
}
inline Result Client::Post(const std::string &path, const Headers &headers,
const Params &params) {
return cli_->Post(path, headers, params);
}
inline Result Client::Post(const std::string &path, const Headers &headers,
const Params &params, Progress progress) {
return cli_->Post(path, headers, params, progress);
@ -10088,29 +9876,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) {
@ -10139,13 +9913,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 &params) {
return cli_->Put(path, params);
}
inline Result Client::Put(const std::string &path, const Headers &headers,
const Params &params) {
return cli_->Put(path, headers, params);
}
inline Result Client::Put(const std::string &path, const Headers &headers,
const Params &params, Progress progress) {
return cli_->Put(path, headers, params, progress);
@ -10172,22 +9939,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,
@ -10195,20 +9952,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,
@ -10244,22 +9992,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,
@ -10267,20 +10005,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,

View File

@ -524,37 +524,37 @@ TEST(GetHeaderValueTest, RegularInvalidValueInt) {
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);
}
@ -800,9 +800,10 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
#endif
cli.set_connection_timeout(2);
{
std::string body;
auto res = cli.Get(
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
"/httpgallery/chunked/chunkedimage.aspx",
[&](const Response &response) {
EXPECT_EQ(StatusCode::OK_200, response.status);
return true;
@ -820,6 +821,28 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
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) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org";
@ -846,7 +869,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);
@ -854,7 +877,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);
@ -862,7 +885,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);
@ -870,7 +893,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);
@ -878,7 +901,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);
@ -1064,9 +1087,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);
@ -1091,9 +1113,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());
}
@ -1117,9 +1138,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());
}
@ -1143,9 +1163,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);
@ -1170,9 +1189,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());
}
@ -1196,9 +1214,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());
}
@ -1222,9 +1239,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);
@ -1249,9 +1265,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());
}
@ -1275,9 +1290,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());
}
@ -1301,9 +1315,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);
@ -1328,9 +1341,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());
}
@ -1354,9 +1366,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());
}
@ -1385,8 +1396,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);
@ -1603,7 +1614,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);
}
@ -1615,7 +1626,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);
}
@ -2041,7 +2052,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"));
@ -2124,7 +2135,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"));
@ -2135,7 +2146,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"));
@ -3079,7 +3090,7 @@ TEST_F(ServerTest, GetFileContent) {
}
TEST_F(ServerTest, GetFileContentWithRange) {
auto res = cli_.Get("/file_content", {{make_range_header({{1, 3}})}});
auto res = cli_.Get("/file_content", Headers{{make_range_header({{1, 3}})}});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
@ -3401,7 +3412,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"));
@ -3412,8 +3423,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(
@ -3425,7 +3436,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"));
@ -3435,7 +3446,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"));
@ -3447,7 +3458,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"));
@ -3789,7 +3800,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"));
@ -3807,7 +3818,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"));
@ -3817,7 +3829,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"));
@ -3827,7 +3840,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"));
@ -3837,7 +3850,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"));
@ -3846,8 +3860,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);
@ -3859,7 +3874,8 @@ TEST_F(ServerTest, GetStreamedWithRangeError) {
TEST_F(ServerTest, GetRangeWithMaxLongLength) {
auto res = cli_.Get(
"/with-range",
{{"Range", "bytes=0-" + std::to_string(std::numeric_limits<long>::max())},
Headers{{"Range",
"bytes=0-" + std::to_string(std::numeric_limits<long>::max())},
{"Accept-Encoding", ""}});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
@ -3870,7 +3886,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", ""},
});
@ -3883,8 +3899,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"));
@ -3898,8 +3914,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"));
@ -3909,7 +3925,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"));
@ -3918,8 +3934,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"));
@ -3969,7 +3986,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", ""},
});
@ -3982,7 +3999,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", ""},
});
@ -3995,7 +4012,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", ""},
});
@ -4008,7 +4025,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", ""},
});
@ -4021,13 +4038,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"));
@ -4036,15 +4055,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"));
@ -4054,7 +4073,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"));
@ -4063,7 +4082,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);
}
@ -5766,7 +5785,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);
@ -5823,8 +5842,7 @@ TEST(GetWithParametersTest, GetWithParameters2) {
params.emplace("hello", "world");
std::string body;
auto res = cli.Get("/", params, Headers{},
[&](const char *data, size_t data_length) {
auto res = cli.Get("/", params, [&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
@ -5849,7 +5867,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);
{
@ -6669,7 +6687,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);
}
}
@ -6798,8 +6816,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);
@ -6828,7 +6846,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);
}
@ -6840,7 +6858,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);
}
@ -7994,7 +8012,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_"}});
}
TEST(InvalidHeaderCharsTest, is_field_name) {