Compare commits

..

4 Commits

Author SHA1 Message Date
yhirose
d2c7b447d5 Fix #289: Fixed build problem with Visual C++ 2019-12-13 09:12:50 -05:00
yhirose
72b20c08da Better API names 2019-12-13 06:56:00 -05:00
yhirose
afd6d5f9dc Removed compress parameter and added compress method on client 2019-12-12 23:09:59 -05:00
yhirose
e5827ad16f Fixed build error 2019-12-12 23:09:34 -05:00
4 changed files with 90 additions and 107 deletions

View File

@@ -387,7 +387,7 @@ httplib::Client cli("yahoo.com");
auto res = cli.Get("/");
res->status; // 301
cli.follow_location(true);
cli.set_follow_location(true);
res = cli.Get("/");
res->status; // 200
```
@@ -423,6 +423,13 @@ The server applies gzip compression to the following MIME type contents:
* application/xml
* application/xhtml+xml
### Compress content on client
```c++
cli.set_compress(true);
res = cli.Post("/resource/foo", "...", "text/plain");
```
NOTE
----

View File

@@ -27,7 +27,7 @@ string dump_headers(const Headers &headers) {
return s;
}
string dump_multipart_files(const MultipartFiles &files) {
string dump_multipart_files(const MultipartFormDataMap &files) {
string s;
char buf[BUFSIZ];

162
httplib.h
View File

@@ -657,78 +657,63 @@ public:
std::shared_ptr<Response> Head(const char *path, const Headers &headers);
std::shared_ptr<Response> Post(const char *path, const std::string &body,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
const std::string &body,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Post(const char *path, size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Post(const char *path, const Params &params,
bool compress = false);
std::shared_ptr<Response> Post(const char *path, const Params &params);
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
const Params &params, bool compress = false);
const Params &params);
std::shared_ptr<Response> Post(const char *path,
const MultipartFormDataItems &items,
bool compress = false);
const MultipartFormDataItems &items);
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
const MultipartFormDataItems &items,
bool compress = false);
const MultipartFormDataItems &items);
std::shared_ptr<Response> Put(const char *path, const std::string &body,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Put(const char *path, const Headers &headers,
const std::string &body,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Put(const char *path, size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Put(const char *path, const Headers &headers,
size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Patch(const char *path, const std::string &body,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Patch(const char *path, const Headers &headers,
const std::string &body,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Patch(const char *path, size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Patch(const char *path, const Headers &headers,
size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress = false);
const char *content_type);
std::shared_ptr<Response> Delete(const char *path);
@@ -754,10 +739,12 @@ public:
void set_read_timeout(time_t sec, time_t usec);
void follow_location(bool on);
void set_auth(const char *username, const char *password);
void set_follow_location(bool on);
void set_compress(bool on);
protected:
bool process_request(Stream &strm, const Request &req, Response &res,
bool last_connection, bool &connection_close);
@@ -769,9 +756,10 @@ protected:
size_t keep_alive_max_count_;
time_t read_timeout_sec_;
time_t read_timeout_usec_;
size_t follow_location_;
bool follow_location_;
std::string username_;
std::string password_;
bool compress_;
private:
socket_t create_client_socket() const;
@@ -784,7 +772,7 @@ private:
const Headers &headers, const std::string &body,
size_t content_length,
ContentProvider content_provider,
const char *content_type, bool compress);
const char *content_type);
virtual bool process_and_close_socket(
socket_t sock, size_t request_count,
@@ -2297,11 +2285,11 @@ inline std::string message_digest(const std::string &s, Init init,
size_t digest_length) {
using namespace std;
unsigned char md[digest_length];
std::vector<unsigned char> md(digest_length, 0);
CTX ctx;
init(&ctx);
update(&ctx, s.data(), s.size());
final(md, &ctx);
final(md.data(), &ctx);
stringstream ss;
for (auto c : md) {
@@ -3331,7 +3319,8 @@ inline Client::Client(const char *host, int port, time_t timeout_sec)
keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
follow_location_(false) {}
follow_location_(false),
compress_(false) {}
inline Client::~Client() {}
@@ -3475,14 +3464,14 @@ inline bool Client::redirect(const Request &req, Response &res) {
if (next_scheme == "https") {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLClient cli(next_host.c_str());
cli.follow_location(true);
cli.set_follow_location(true);
return detail::redirect(cli, req, res, next_path);
#else
return false;
#endif
} else {
Client cli(next_host.c_str());
cli.follow_location(true);
cli.set_follow_location(true);
return detail::redirect(cli, req, res, next_path);
}
}
@@ -3570,11 +3559,7 @@ inline void Client::write_request(Stream &strm, const Request &req,
inline std::shared_ptr<Response> Client::send_with_content_provider(
const char *method, const char *path, const Headers &headers,
const std::string &body, size_t content_length,
ContentProvider content_provider, const char *content_type, bool compress) {
#ifndef CPPHTTPLIB_ZLIB_SUPPORT
(void)compress;
#endif
ContentProvider content_provider, const char *content_type) {
Request req;
req.method = method;
req.headers = headers;
@@ -3583,7 +3568,7 @@ inline std::shared_ptr<Response> Client::send_with_content_provider(
req.headers.emplace("Content-Type", content_type);
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
if (compress) {
if (compress_) {
if (content_provider) {
size_t offset = 0;
while (offset < content_length) {
@@ -3771,45 +3756,40 @@ inline std::shared_ptr<Response> Client::Head(const char *path,
inline std::shared_ptr<Response> Client::Post(const char *path,
const std::string &body,
const char *content_type,
bool compress) {
return Post(path, Headers(), body, content_type, compress);
const char *content_type) {
return Post(path, Headers(), body, content_type);
}
inline std::shared_ptr<Response>
Client::Post(const char *path, const Headers &headers, const std::string &body,
const char *content_type, bool compress) {
const char *content_type) {
return send_with_content_provider("POST", path, headers, body, 0, nullptr,
content_type, compress);
content_type);
}
inline std::shared_ptr<Response>
Client::Post(const char *path, const Params &params, bool compress) {
return Post(path, Headers(), params, compress);
Client::Post(const char *path, const Params &params) {
return Post(path, Headers(), params);
}
inline std::shared_ptr<Response> Client::Post(const char *path,
size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress) {
return Post(path, Headers(), content_length, content_provider, content_type,
compress);
const char *content_type) {
return Post(path, Headers(), content_length, content_provider, content_type);
}
inline std::shared_ptr<Response>
Client::Post(const char *path, const Headers &headers, size_t content_length,
ContentProvider content_provider, const char *content_type,
bool compress) {
ContentProvider content_provider, const char *content_type) {
return send_with_content_provider("POST", path, headers, std::string(),
content_length, content_provider,
content_type, compress);
content_type);
}
inline std::shared_ptr<Response> Client::Post(const char *path,
const Headers &headers,
const Params &params,
bool compress) {
const Params &params) {
std::string query;
for (auto it = params.begin(); it != params.end(); ++it) {
if (it != params.begin()) { query += "&"; }
@@ -3818,19 +3798,17 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
query += detail::encode_url(it->second);
}
return Post(path, headers, query, "application/x-www-form-urlencoded",
compress);
return Post(path, headers, query, "application/x-www-form-urlencoded");
}
inline std::shared_ptr<Response>
Client::Post(const char *path, const MultipartFormDataItems &items,
bool compress) {
return Post(path, Headers(), items, compress);
Client::Post(const char *path, const MultipartFormDataItems &items) {
return Post(path, Headers(), items);
}
inline std::shared_ptr<Response>
Client::Post(const char *path, const Headers &headers,
const MultipartFormDataItems &items, bool compress) {
const MultipartFormDataItems &items) {
auto boundary = detail::make_multipart_data_boundary();
std::string body;
@@ -3852,71 +3830,63 @@ Client::Post(const char *path, const Headers &headers,
body += "--" + boundary + "--\r\n";
std::string content_type = "multipart/form-data; boundary=" + boundary;
return Post(path, headers, body, content_type.c_str(), compress);
return Post(path, headers, body, content_type.c_str());
}
inline std::shared_ptr<Response> Client::Put(const char *path,
const std::string &body,
const char *content_type,
bool compress) {
return Put(path, Headers(), body, content_type, compress);
const char *content_type) {
return Put(path, Headers(), body, content_type);
}
inline std::shared_ptr<Response>
Client::Put(const char *path, const Headers &headers, const std::string &body,
const char *content_type, bool compress) {
const char *content_type) {
return send_with_content_provider("PUT", path, headers, body, 0, nullptr,
content_type, compress);
content_type);
}
inline std::shared_ptr<Response> Client::Put(const char *path,
size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress) {
return Put(path, Headers(), content_length, content_provider, content_type,
compress);
const char *content_type) {
return Put(path, Headers(), content_length, content_provider, content_type);
}
inline std::shared_ptr<Response>
Client::Put(const char *path, const Headers &headers, size_t content_length,
ContentProvider content_provider, const char *content_type,
bool compress) {
ContentProvider content_provider, const char *content_type) {
return send_with_content_provider("PUT", path, headers, std::string(),
content_length, content_provider,
content_type, compress);
content_type);
}
inline std::shared_ptr<Response> Client::Patch(const char *path,
const std::string &body,
const char *content_type,
bool compress) {
return Patch(path, Headers(), body, content_type, compress);
const char *content_type) {
return Patch(path, Headers(), body, content_type);
}
inline std::shared_ptr<Response>
Client::Patch(const char *path, const Headers &headers, const std::string &body,
const char *content_type, bool compress) {
const char *content_type) {
return send_with_content_provider("PATCH", path, headers, body, 0, nullptr,
content_type, compress);
content_type);
}
inline std::shared_ptr<Response> Client::Patch(const char *path,
size_t content_length,
ContentProvider content_provider,
const char *content_type,
bool compress) {
return Patch(path, Headers(), content_length, content_provider, content_type,
compress);
const char *content_type) {
return Patch(path, Headers(), content_length, content_provider, content_type);
}
inline std::shared_ptr<Response>
Client::Patch(const char *path, const Headers &headers, size_t content_length,
ContentProvider content_provider, const char *content_type,
bool compress) {
ContentProvider content_provider, const char *content_type) {
return send_with_content_provider("PATCH", path, headers, std::string(),
content_length, content_provider,
content_type, compress);
content_type);
}
inline std::shared_ptr<Response> Client::Delete(const char *path) {
@@ -3976,13 +3946,15 @@ inline void Client::set_read_timeout(time_t sec, time_t usec) {
read_timeout_usec_ = usec;
}
inline void Client::follow_location(bool on) { follow_location_ = on; }
inline void Client::set_auth(const char *username, const char *password) {
username_ = username;
password_ = password;
}
inline void Client::set_follow_location(bool on) { follow_location_ = on; }
inline void Client::set_compress(bool on) { compress_ = on; }
/*
* SSL Implementation
*/

View File

@@ -524,7 +524,7 @@ TEST(AbsoluteRedirectTest, Redirect) {
httplib::Client cli(host);
#endif
cli.follow_location(true);
cli.set_follow_location(true);
auto res = cli.Get("/absolute-redirect/3");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
@@ -539,7 +539,7 @@ TEST(RedirectTest, Redirect) {
httplib::Client cli(host);
#endif
cli.follow_location(true);
cli.set_follow_location(true);
auto res = cli.Get("/redirect/3");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
@@ -554,7 +554,7 @@ TEST(RelativeRedirectTest, Redirect) {
httplib::Client cli(host);
#endif
cli.follow_location(true);
cli.set_follow_location(true);
auto res = cli.Get("/relative-redirect/3");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
@@ -569,7 +569,7 @@ TEST(TooManyRedirectTest, Redirect) {
httplib::Client cli(host);
#endif
cli.follow_location(true);
cli.set_follow_location(true);
auto res = cli.Get("/redirect/21");
ASSERT_TRUE(res == nullptr);
}
@@ -582,7 +582,7 @@ TEST(YahooRedirectTest, Redirect) {
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(301, res->status);
cli.follow_location(true);
cli.set_follow_location(true);
res = cli.Get("/");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
@@ -590,7 +590,7 @@ TEST(YahooRedirectTest, Redirect) {
TEST(HttpsToHttpRedirectTest, Redirect) {
httplib::SSLClient cli("httpbin.org");
cli.follow_location(true);
cli.set_follow_location(true);
auto res =
cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
ASSERT_TRUE(res != nullptr);
@@ -1535,12 +1535,13 @@ TEST_F(ServerTest, PutWithContentProvider) {
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
TEST_F(ServerTest, PutWithContentProviderWithGzip) {
cli_.set_compress(true);
auto res = cli_.Put(
"/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink sink) {
sink("PUT", 3);
},
"text/plain", true);
"text/plain");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
@@ -1548,7 +1549,8 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) {
}
TEST_F(ServerTest, PutLargeFileWithGzip) {
auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain", true);
cli_.set_compress(true);
auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
@@ -1621,7 +1623,8 @@ TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
}
TEST_F(ServerTest, PostContentReceiverGzip) {
auto res = cli_.Post("/content_receiver", "content", "text/plain", true);
cli_.set_compress(true);
auto res = cli_.Post("/content_receiver", "content", "text/plain");
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);
ASSERT_EQ("content", res->body);
@@ -1802,7 +1805,8 @@ TEST_F(ServerTest, MultipartFormDataGzip) {
{"key2", "--abcdefg123", "", ""},
};
auto res = cli_.Post("/gzipmultipart", items, true);
cli_.set_compress(true);
auto res = cli_.Post("/gzipmultipart", items);
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);