mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-10 09:43:51 +00:00
Merge pull request #89 from dolphineye/request-cancelation
Request cancelation feature
This commit is contained in:
commit
f4981459b3
@ -107,7 +107,7 @@ std::pair<std::string, std::string> make_range_header(uint64_t value, Args... ar
|
|||||||
|
|
||||||
typedef std::multimap<std::string, std::string> Params;
|
typedef std::multimap<std::string, std::string> Params;
|
||||||
typedef std::smatch Match;
|
typedef std::smatch Match;
|
||||||
typedef std::function<void (uint64_t current, uint64_t total)> Progress;
|
typedef std::function<bool (uint64_t current, uint64_t total)> Progress;
|
||||||
|
|
||||||
struct MultipartFile {
|
struct MultipartFile {
|
||||||
std::string filename;
|
std::string filename;
|
||||||
@ -804,7 +804,9 @@ inline bool read_content_with_length(Stream& strm, std::string& out, size_t len,
|
|||||||
r += n;
|
r += n;
|
||||||
|
|
||||||
if (progress) {
|
if (progress) {
|
||||||
progress(r, len);
|
if (!progress(r, len)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
60
test/test.cc
60
test/test.cc
@ -230,6 +230,66 @@ TEST(ConnectionErrorTest, Timeout)
|
|||||||
ASSERT_TRUE(res == nullptr);
|
ASSERT_TRUE(res == nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(CancelTest, NoCancel) {
|
||||||
|
auto host = "httpbin.org";
|
||||||
|
auto sec = 5;
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
|
auto port = 443;
|
||||||
|
httplib::SSLClient cli(host, port, sec);
|
||||||
|
#else
|
||||||
|
auto port = 80;
|
||||||
|
httplib::Client cli(host, port, sec);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
httplib::Headers headers;
|
||||||
|
auto res = cli.Get("/range/32", headers, [](uint64_t, uint64_t) {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(CancelTest, WithCancelSmallPayload) {
|
||||||
|
auto host = "httpbin.org";
|
||||||
|
auto sec = 5;
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
|
auto port = 443;
|
||||||
|
httplib::SSLClient cli(host, port, sec);
|
||||||
|
#else
|
||||||
|
auto port = 80;
|
||||||
|
httplib::Client cli(host, port, sec);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
httplib::Headers headers;
|
||||||
|
auto res = cli.Get("/range/32", headers, [](uint64_t, uint64_t) {
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
ASSERT_TRUE(res == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(CancelTest, WithCancelLargePayload) {
|
||||||
|
auto host = "httpbin.org";
|
||||||
|
auto sec = 5;
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
|
auto port = 443;
|
||||||
|
httplib::SSLClient cli(host, port, sec);
|
||||||
|
#else
|
||||||
|
auto port = 80;
|
||||||
|
httplib::Client cli(host, port, sec);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
uint32_t count = 0;
|
||||||
|
httplib::Headers headers;
|
||||||
|
auto res = cli.Get("/range/65536", headers, [&count](uint64_t, uint64_t) {
|
||||||
|
return (count++ == 0);
|
||||||
|
});
|
||||||
|
ASSERT_TRUE(res == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Server, BindAndListenSeparately) {
|
TEST(Server, BindAndListenSeparately) {
|
||||||
Server svr;
|
Server svr;
|
||||||
int port = svr.bind_to_any_port("localhost");
|
int port = svr.bind_to_any_port("localhost");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user