mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
Code formatting
This commit is contained in:
parent
bea3ebd7af
commit
0d527e2b83
2 changed files with 61 additions and 57 deletions
86
httplib.h
86
httplib.h
|
@ -52,6 +52,10 @@
|
||||||
#define CPPHTTPLIB_THREAD_POOL_COUNT 8
|
#define CPPHTTPLIB_THREAD_POOL_COUNT 8
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Headers
|
||||||
|
*/
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifndef _CRT_SECURE_NO_WARNINGS
|
#ifndef _CRT_SECURE_NO_WARNINGS
|
||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
@ -61,10 +65,6 @@
|
||||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||||
#endif //_CRT_NONSTDC_NO_DEPRECATE
|
#endif //_CRT_NONSTDC_NO_DEPRECATE
|
||||||
|
|
||||||
/*
|
|
||||||
* Headers
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
typedef __int64 ssize_t;
|
typedef __int64 ssize_t;
|
||||||
|
@ -589,37 +589,45 @@ public:
|
||||||
std::shared_ptr<Response> Head(const char *path, const Headers &headers);
|
std::shared_ptr<Response> Head(const char *path, const Headers &headers);
|
||||||
|
|
||||||
std::shared_ptr<Response> Post(const char *path, const std::string &body,
|
std::shared_ptr<Response> Post(const char *path, const std::string &body,
|
||||||
const char *content_type, bool compress = false);
|
const char *content_type,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
|
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const char *content_type,
|
const char *content_type,
|
||||||
bool compress = false);
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Post(const char *path, const Params ¶ms, bool compress = false);
|
std::shared_ptr<Response> Post(const char *path, const Params ¶ms,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
|
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
|
||||||
const Params ¶ms, bool compress = false);
|
const Params ¶ms, bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Post(const char *path,
|
std::shared_ptr<Response> Post(const char *path,
|
||||||
const MultipartFormDataItems &items, bool compress = false);
|
const MultipartFormDataItems &items,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
|
std::shared_ptr<Response> Post(const char *path, const Headers &headers,
|
||||||
const MultipartFormDataItems &items, bool compress = false);
|
const MultipartFormDataItems &items,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Put(const char *path, const std::string &body,
|
std::shared_ptr<Response> Put(const char *path, const std::string &body,
|
||||||
const char *content_type, bool compress = false);
|
const char *content_type,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Put(const char *path, const Headers &headers,
|
std::shared_ptr<Response> Put(const char *path, const Headers &headers,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const char *content_type, bool compress = false);
|
const char *content_type,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Patch(const char *path, const std::string &body,
|
std::shared_ptr<Response> Patch(const char *path, const std::string &body,
|
||||||
const char *content_type, bool compress = false);
|
const char *content_type,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Patch(const char *path, const Headers &headers,
|
std::shared_ptr<Response> Patch(const char *path, const Headers &headers,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const char *content_type, bool compress = false);
|
const char *content_type,
|
||||||
|
bool compress = false);
|
||||||
|
|
||||||
std::shared_ptr<Response> Delete(const char *path);
|
std::shared_ptr<Response> Delete(const char *path);
|
||||||
|
|
||||||
|
@ -3120,14 +3128,14 @@ inline std::shared_ptr<Response> Client::Head(const char *path,
|
||||||
|
|
||||||
inline std::shared_ptr<Response> Client::Post(const char *path,
|
inline std::shared_ptr<Response> Client::Post(const char *path,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const char *content_type, bool compress) {
|
const char *content_type,
|
||||||
|
bool compress) {
|
||||||
return Post(path, Headers(), body, content_type, compress);
|
return Post(path, Headers(), body, content_type, compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Response> Client::Post(const char *path,
|
inline std::shared_ptr<Response>
|
||||||
const Headers &headers,
|
Client::Post(const char *path, const Headers &headers, const std::string &body,
|
||||||
const std::string &body,
|
const char *content_type, bool compress) {
|
||||||
const char *content_type, bool compress) {
|
|
||||||
Request req;
|
Request req;
|
||||||
req.method = "POST";
|
req.method = "POST";
|
||||||
req.headers = headers;
|
req.headers = headers;
|
||||||
|
@ -3137,9 +3145,7 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
|
||||||
req.body = body;
|
req.body = body;
|
||||||
|
|
||||||
if (compress) {
|
if (compress) {
|
||||||
if (!detail::compress(req.body)) {
|
if (!detail::compress(req.body)) { return nullptr; }
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
req.headers.emplace("Content-Encoding", "gzip");
|
req.headers.emplace("Content-Encoding", "gzip");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3148,13 +3154,15 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
|
||||||
return send(req, *res) ? res : nullptr;
|
return send(req, *res) ? res : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Response> Client::Post(const char *path,
|
inline std::shared_ptr<Response>
|
||||||
const Params ¶ms, bool compress) {
|
Client::Post(const char *path, const Params ¶ms, bool compress) {
|
||||||
return Post(path, Headers(), params, compress);
|
return Post(path, Headers(), params, compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Response>
|
inline std::shared_ptr<Response> Client::Post(const char *path,
|
||||||
Client::Post(const char *path, const Headers &headers, const Params ¶ms, bool compress) {
|
const Headers &headers,
|
||||||
|
const Params ¶ms,
|
||||||
|
bool compress) {
|
||||||
std::string query;
|
std::string query;
|
||||||
for (auto it = params.begin(); it != params.end(); ++it) {
|
for (auto it = params.begin(); it != params.end(); ++it) {
|
||||||
if (it != params.begin()) { query += "&"; }
|
if (it != params.begin()) { query += "&"; }
|
||||||
|
@ -3163,11 +3171,13 @@ Client::Post(const char *path, const Headers &headers, const Params ¶ms, boo
|
||||||
query += detail::encode_url(it->second);
|
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",
|
||||||
|
compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Response>
|
inline std::shared_ptr<Response>
|
||||||
Client::Post(const char *path, const MultipartFormDataItems &items, bool compress) {
|
Client::Post(const char *path, const MultipartFormDataItems &items,
|
||||||
|
bool compress) {
|
||||||
return Post(path, Headers(), items, compress);
|
return Post(path, Headers(), items, compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3205,11 +3215,9 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
|
||||||
return Put(path, Headers(), body, content_type, compress);
|
return Put(path, Headers(), body, content_type, compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Response> Client::Put(const char *path,
|
inline std::shared_ptr<Response>
|
||||||
const Headers &headers,
|
Client::Put(const char *path, const Headers &headers, const std::string &body,
|
||||||
const std::string &body,
|
const char *content_type, bool compress) {
|
||||||
const char *content_type,
|
|
||||||
bool compress) {
|
|
||||||
Request req;
|
Request req;
|
||||||
req.method = "PUT";
|
req.method = "PUT";
|
||||||
req.headers = headers;
|
req.headers = headers;
|
||||||
|
@ -3219,9 +3227,7 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
|
||||||
req.body = body;
|
req.body = body;
|
||||||
|
|
||||||
if (compress) {
|
if (compress) {
|
||||||
if (!detail::compress(req.body)) {
|
if (!detail::compress(req.body)) { return nullptr; }
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
req.headers.emplace("Content-Encoding", "gzip");
|
req.headers.emplace("Content-Encoding", "gzip");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3237,11 +3243,9 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
|
||||||
return Patch(path, Headers(), body, content_type, compress);
|
return Patch(path, Headers(), body, content_type, compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<Response> Client::Patch(const char *path,
|
inline std::shared_ptr<Response>
|
||||||
const Headers &headers,
|
Client::Patch(const char *path, const Headers &headers, const std::string &body,
|
||||||
const std::string &body,
|
const char *content_type, bool compress) {
|
||||||
const char *content_type,
|
|
||||||
bool compress) {
|
|
||||||
Request req;
|
Request req;
|
||||||
req.method = "PATCH";
|
req.method = "PATCH";
|
||||||
req.headers = headers;
|
req.headers = headers;
|
||||||
|
@ -3251,9 +3255,7 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
|
||||||
req.body = body;
|
req.body = body;
|
||||||
|
|
||||||
if (compress) {
|
if (compress) {
|
||||||
if (!detail::compress(req.body)) {
|
if (!detail::compress(req.body)) { return nullptr; }
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
req.headers.emplace("Content-Encoding", "gzip");
|
req.headers.emplace("Content-Encoding", "gzip");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
32
test/test.cc
32
test/test.cc
|
@ -255,16 +255,16 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string body;
|
std::string body;
|
||||||
auto res =
|
auto res = cli.Get(
|
||||||
cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", Headers(),
|
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", Headers(),
|
||||||
[&](const Response& response) {
|
[&](const Response &response) {
|
||||||
EXPECT_EQ(200, response.status);
|
EXPECT_EQ(200, response.status);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[&](const char *data, size_t data_length, uint64_t, uint64_t) {
|
[&](const char *data, size_t data_length, uint64_t, uint64_t) {
|
||||||
body.append(data, data_length);
|
body.append(data, data_length);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
ASSERT_TRUE(res != nullptr);
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
|
||||||
std::string out;
|
std::string out;
|
||||||
|
@ -539,7 +539,8 @@ TEST(YahooRedirectTest, Redirect) {
|
||||||
TEST(HttpsToHttpRedirectTest, Redirect) {
|
TEST(HttpsToHttpRedirectTest, Redirect) {
|
||||||
httplib::SSLClient cli("httpbin.org");
|
httplib::SSLClient cli("httpbin.org");
|
||||||
cli.follow_location(true);
|
cli.follow_location(true);
|
||||||
auto res = cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
|
auto res =
|
||||||
|
cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
|
||||||
ASSERT_TRUE(res != nullptr);
|
ASSERT_TRUE(res != nullptr);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -636,7 +637,8 @@ protected:
|
||||||
[data](uint64_t offset, uint64_t length, DataSink sink) {
|
[data](uint64_t offset, uint64_t length, DataSink sink) {
|
||||||
size_t DATA_CHUNK_SIZE = 4;
|
size_t DATA_CHUNK_SIZE = 4;
|
||||||
const auto &d = *data;
|
const auto &d = *data;
|
||||||
auto out_len = std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
|
auto out_len =
|
||||||
|
std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
|
||||||
sink(&d[static_cast<size_t>(offset)], out_len);
|
sink(&d[static_cast<size_t>(offset)], out_len);
|
||||||
},
|
},
|
||||||
[data] { delete data; });
|
[data] { delete data; });
|
||||||
|
@ -1422,19 +1424,19 @@ TEST_F(ServerTest, KeepAlive) {
|
||||||
ASSERT_TRUE(requests.size() == responses.size());
|
ASSERT_TRUE(requests.size() == responses.size());
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
auto& res = responses[i];
|
auto &res = responses[i];
|
||||||
EXPECT_EQ(200, res.status);
|
EXPECT_EQ(200, res.status);
|
||||||
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
|
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
|
||||||
EXPECT_EQ("Hello World!", res.body);
|
EXPECT_EQ("Hello World!", res.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto& res = responses[3];
|
auto &res = responses[3];
|
||||||
EXPECT_EQ(404, res.status);
|
EXPECT_EQ(404, res.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto& res = responses[4];
|
auto &res = responses[4];
|
||||||
EXPECT_EQ(200, res.status);
|
EXPECT_EQ(200, res.status);
|
||||||
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
|
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
|
||||||
EXPECT_EQ("empty", res.body);
|
EXPECT_EQ("empty", res.body);
|
||||||
|
|
Loading…
Reference in a new issue