mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-27 20:58:28 +00:00
Merge remote-tracking branch 'origin/master' into unify-serving-paths
This commit is contained in:
commit
90bd0f7daf
2 files changed
+295
-156
No files matched your search
+231
-108
@@ -157,28 +157,38 @@ static void read_file(const std::string &path, std::string &out) {
|
||||
fs.read(&out[0], static_cast<std::streamsize>(size));
|
||||
}
|
||||
|
||||
// httpbingo.org intermittently answers with gateway errors unrelated to the
|
||||
// client under test.
|
||||
static bool is_gateway_error(const Result &res) {
|
||||
return res && (res->status == StatusCode::BadGateway_502 ||
|
||||
res->status == StatusCode::ServiceUnavailable_503 ||
|
||||
res->status == StatusCode::GatewayTimeout_504);
|
||||
// Failures of the services that the *_Online tests reach, unrelated to the
|
||||
// client under test: httpbingo.org intermittently answers with gateway errors,
|
||||
// and badssl.com drops connections from CI runners before it sends its
|
||||
// certificate. A handshake that fails before any verdict on the certificate is
|
||||
// left to the tests against local servers.
|
||||
static bool is_service_failure(const Result &res) {
|
||||
if (res) {
|
||||
return res->status == StatusCode::BadGateway_502 ||
|
||||
res->status == StatusCode::ServiceUnavailable_503 ||
|
||||
res->status == StatusCode::GatewayTimeout_504;
|
||||
}
|
||||
return res.error() == Error::SSLConnection;
|
||||
}
|
||||
|
||||
template <typename F> static Result retry_gateway_errors(F &&send) {
|
||||
template <typename F> static Result retry_service_failures(F &&send) {
|
||||
auto res = send();
|
||||
for (auto wait : {1, 2}) {
|
||||
if (!is_gateway_error(res)) { break; }
|
||||
if (!is_service_failure(res)) { break; }
|
||||
std::this_thread::sleep_for(std::chrono::seconds(wait));
|
||||
res = send();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
#define SKIP_ON_GATEWAY_ERROR(res) \
|
||||
#define SKIP_ON_SERVICE_FAILURE(res) \
|
||||
do { \
|
||||
if (is_gateway_error(res)) { \
|
||||
GTEST_SKIP() << "gateway error " << (res)->status; \
|
||||
if (is_service_failure(res)) { \
|
||||
if (res) { \
|
||||
GTEST_SKIP() << "service failure: status " << (res)->status; \
|
||||
} else { \
|
||||
GTEST_SKIP() << "service failure: " << to_string((res).error()); \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@@ -2083,6 +2093,9 @@ TEST(ParseDispositionParamsTest, QuotedValues) {
|
||||
} cases[] = {
|
||||
{"name=\"file1\"; filename=\"a.txt\"",
|
||||
{{"name", "file1"}, {"filename", "a.txt"}}},
|
||||
// MIME parameter names are case-insensitive, but their values are not.
|
||||
{"NaMe=\"Upload\"; FiLeNaMe=\"Report.TXT\"",
|
||||
{{"name", "Upload"}, {"filename", "Report.TXT"}}},
|
||||
// Whitespace around '=' and ';' is still trimmed
|
||||
{"name=\"file2\" ;filename = \"a.html\"",
|
||||
{{"name", "file2"}, {"filename", "a.html"}}},
|
||||
@@ -2853,8 +2866,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
||||
cli.set_connection_timeout(5);
|
||||
|
||||
{
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
@@ -2862,8 +2875,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
||||
|
||||
{
|
||||
Headers headers = {make_range_header({{1, -1}})};
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||
@@ -2871,8 +2884,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
||||
|
||||
{
|
||||
Headers headers = {make_range_header({{1, 10}})};
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("bcdefghijk", res->body);
|
||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||
@@ -2883,8 +2896,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
||||
// acceptable per RFC 9110 §15.3.7, so we accept either status code.
|
||||
{
|
||||
Headers headers = {make_range_header({{0, 31}})};
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
||||
@@ -2893,8 +2906,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
||||
|
||||
{
|
||||
Headers headers = {make_range_header({{0, -1}})};
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
||||
@@ -2906,8 +2919,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
||||
// in real servers, so we only verify the request succeeds.
|
||||
{
|
||||
Headers headers = {make_range_header({{0, 32}})};
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path, headers); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
}
|
||||
}
|
||||
@@ -3222,9 +3235,9 @@ TEST(CancelTest, NoCancel_Online) {
|
||||
#endif
|
||||
cli.set_connection_timeout(std::chrono::seconds(5));
|
||||
|
||||
auto res = retry_gateway_errors(
|
||||
auto res = retry_service_failures(
|
||||
[&] { return cli.Get(path, [](uint64_t, uint64_t) { return true; }); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
@@ -3245,9 +3258,9 @@ TEST(CancelTest, WithCancelSmallPayload_Online) {
|
||||
Client cli(host, port);
|
||||
#endif
|
||||
|
||||
auto res = retry_gateway_errors(
|
||||
auto res = retry_service_failures(
|
||||
[&] { return cli.Get(path, [](uint64_t, uint64_t) { return false; }); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
cli.set_connection_timeout(std::chrono::seconds(5));
|
||||
ASSERT_TRUE(!res);
|
||||
EXPECT_EQ(Error::Canceled, res.error());
|
||||
@@ -3266,12 +3279,12 @@ TEST(CancelTest, WithCancelLargePayload_Online) {
|
||||
#endif
|
||||
cli.set_connection_timeout(std::chrono::seconds(5));
|
||||
|
||||
auto res = retry_gateway_errors([&] {
|
||||
auto res = retry_service_failures([&] {
|
||||
auto count = 0;
|
||||
return cli.Get(path,
|
||||
[&count](uint64_t, uint64_t) { return (count++ == 0); });
|
||||
});
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(!res);
|
||||
EXPECT_EQ(Error::Canceled, res.error());
|
||||
}
|
||||
@@ -3861,18 +3874,18 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
||||
#endif
|
||||
|
||||
{
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||
}
|
||||
|
||||
{
|
||||
auto res = retry_gateway_errors([&] {
|
||||
auto res = retry_service_failures([&] {
|
||||
return cli.Get(
|
||||
path, Headers{make_basic_authentication_header("hello", "world")});
|
||||
});
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
auto body = remove_whitespace(res->body);
|
||||
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
||||
@@ -3882,8 +3895,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
||||
|
||||
{
|
||||
cli.set_basic_auth("hello", "world");
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
auto body = remove_whitespace(res->body);
|
||||
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
||||
@@ -3893,16 +3906,16 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
||||
|
||||
{
|
||||
cli.set_basic_auth("hello", "bad");
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||
}
|
||||
|
||||
{
|
||||
cli.set_basic_auth("bad", "world");
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||
}
|
||||
@@ -3921,8 +3934,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
|
||||
SSLClient cli(host, port);
|
||||
|
||||
{
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(unauth_path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(unauth_path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||
}
|
||||
@@ -3931,8 +3944,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
|
||||
|
||||
cli.set_digest_auth("hello", "world");
|
||||
for (const auto &path : paths) {
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path.c_str()); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path.c_str()); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
auto body = remove_whitespace(res->body);
|
||||
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
||||
@@ -3942,8 +3955,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
|
||||
|
||||
cli.set_digest_auth("hello", "bad");
|
||||
for (const auto &path : paths) {
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path.c_str()); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path.c_str()); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||
}
|
||||
@@ -4155,8 +4168,8 @@ TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) {
|
||||
|
||||
cli.set_direct_hostname_addr_map({{another_host, wrong_ip}});
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
TEST(SpecifyServerIPAddressTest, RealHostname_Online) {
|
||||
@@ -4341,8 +4354,8 @@ TEST(AbsoluteRedirectTest, Redirect_Online) {
|
||||
#endif
|
||||
|
||||
cli.set_follow_location(true);
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -4358,8 +4371,8 @@ TEST(RedirectTest, Redirect_Online) {
|
||||
#endif
|
||||
|
||||
cli.set_follow_location(true);
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -4375,8 +4388,8 @@ TEST(RelativeRedirectTest, Redirect_Online) {
|
||||
#endif
|
||||
|
||||
cli.set_follow_location(true);
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -4392,8 +4405,8 @@ TEST(TooManyRedirectTest, Redirect_Online) {
|
||||
#endif
|
||||
|
||||
cli.set_follow_location(true);
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(!res);
|
||||
EXPECT_EQ(Error::ExceedRedirectCount, res.error());
|
||||
}
|
||||
@@ -4420,10 +4433,10 @@ TEST(YahooRedirectTest, Redirect_Online) {
|
||||
TEST(HttpsToHttpRedirectTest, Redirect_Online) {
|
||||
SSLClient cli(REDIR_HOST);
|
||||
cli.set_follow_location(true);
|
||||
auto res = retry_gateway_errors([&] {
|
||||
auto res = retry_service_failures([&] {
|
||||
return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
|
||||
});
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -4436,9 +4449,9 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) {
|
||||
params.emplace("url", "http://example.com");
|
||||
params.emplace("status_code", "302");
|
||||
|
||||
auto res = retry_gateway_errors(
|
||||
auto res = retry_service_failures(
|
||||
[&] { return cli.Get(REDIR_PATH, params, Headers{}); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -4450,10 +4463,10 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) {
|
||||
Params params;
|
||||
params.emplace("url", "http://example.com");
|
||||
|
||||
auto res = retry_gateway_errors([&] {
|
||||
auto res = retry_service_failures([&] {
|
||||
return cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
|
||||
});
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -15125,16 +15138,16 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
|
||||
cli.set_connection_timeout(5);
|
||||
|
||||
{
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("bcdefghijk", res->body);
|
||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||
}
|
||||
|
||||
{
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("bcdefghijk", res->body);
|
||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||
@@ -16818,10 +16831,8 @@ TEST(SSLClientTest, UpdateCAStoreWithPem_Online) {
|
||||
// Verify client is still valid and can make connections
|
||||
httplib_client.enable_server_certificate_verification(true);
|
||||
auto res = httplib_client.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
// Google may return 200 or 301 depending on various factors
|
||||
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
||||
res->status == StatusCode::MovedPermanently_301);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ServerNameIndication_Online) {
|
||||
@@ -16829,8 +16840,8 @@ TEST(SSLClientTest, ServerNameIndication_Online) {
|
||||
auto path = std::string{"/get"};
|
||||
|
||||
SSLClient cli(host, 443);
|
||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
auto res = retry_service_failures([&] { return cli.Get(path); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -16839,7 +16850,8 @@ TEST(SSLClientTest, ServerCertificateVerificationError_Online) {
|
||||
// Use a site that will cause SSL verification failure due to self-signed cert
|
||||
SSLClient cli("self-signed.badssl.com", 443);
|
||||
cli.enable_server_certificate_verification(true);
|
||||
auto res = cli.Get("/");
|
||||
auto res = retry_service_failures([&] { return cli.Get("/"); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
|
||||
ASSERT_TRUE(!res);
|
||||
EXPECT_EQ(Error::SSLServerVerification, res.error());
|
||||
@@ -16869,7 +16881,8 @@ TEST(SSLClientTest, ServerHostnameVerificationError_Online) {
|
||||
cli.enable_server_certificate_verification(true);
|
||||
cli.enable_server_hostname_verification(true);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
auto res = retry_service_failures([&] { return cli.Get("/"); });
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(!res);
|
||||
EXPECT_EQ(Error::SSLServerHostnameVerification, res.error());
|
||||
|
||||
@@ -16977,16 +16990,16 @@ TEST(WebSocketSSLVerifyTest, WindowsCertificateVerification) {
|
||||
TEST(SSLClientTest, ServerCertificateVerification1_Online) {
|
||||
Client cli("https://google.com");
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ServerCertificateVerification2_Online) {
|
||||
SSLClient cli("google.com");
|
||||
cli.set_ca_cert_path(CA_CERT_FILE);
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ServerCertificateVerification3_Online) {
|
||||
@@ -17051,8 +17064,8 @@ TEST(SSLClientTest, ServerCertificateVerification5_Online) {
|
||||
SSLClient cli("google.com");
|
||||
cli.load_ca_cert_store(cert.data(), cert.size());
|
||||
const auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ServerCertificateVerification6_Online) {
|
||||
@@ -17083,8 +17096,8 @@ TEST(SSLClientTest, ServerCertificateVerification6_Online) {
|
||||
SSLClient cli("google.com");
|
||||
cli.load_ca_cert_store(cert, sizeof(cert));
|
||||
const auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, WildcardHostNameMatch_Online) {
|
||||
@@ -17095,8 +17108,8 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) {
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, WildcardHostNameMatchCase_Online) {
|
||||
@@ -17108,8 +17121,8 @@ TEST(SSLClientTest, WildcardHostNameMatchCase_Online) {
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, HostNameMatchCase_Online) {
|
||||
@@ -17120,8 +17133,8 @@ TEST(SSLClientTest, HostNameMatchCase_Online) {
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, Issue2004_Online) {
|
||||
@@ -17129,11 +17142,8 @@ TEST(SSLClientTest, Issue2004_Online) {
|
||||
client.set_follow_location(true);
|
||||
|
||||
auto res = client.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::OK_200, res->status);
|
||||
|
||||
auto body = res->body;
|
||||
EXPECT_EQ(body.substr(0, 15), "<!doctype html>");
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ErrorReportingWhenInvalid) {
|
||||
@@ -18877,10 +18887,10 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) {
|
||||
TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) {
|
||||
Client cli(REDIR_HOST);
|
||||
cli.set_follow_location(true);
|
||||
auto res = retry_gateway_errors([&] {
|
||||
auto res = retry_service_failures([&] {
|
||||
return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
|
||||
});
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
@@ -18894,9 +18904,9 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) {
|
||||
params.emplace("url", "http://example.com");
|
||||
params.emplace("status_code", "302");
|
||||
|
||||
auto res = retry_gateway_errors(
|
||||
auto res = retry_service_failures(
|
||||
[&] { return cli.Get(REDIR_PATH, params, Headers{}); });
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -18908,10 +18918,10 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) {
|
||||
Params params;
|
||||
params.emplace("url", "http://example.com");
|
||||
|
||||
auto res = retry_gateway_errors([&] {
|
||||
auto res = retry_service_failures([&] {
|
||||
return cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
|
||||
});
|
||||
SKIP_ON_GATEWAY_ERROR(res);
|
||||
SKIP_ON_SERVICE_FAILURE(res);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
@@ -19287,8 +19297,8 @@ TEST(SSLClientTest, EnableSystemCaWithCustomCa_Online) {
|
||||
cli.enable_server_certificate_verification(true);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
// The custom CA remains effective when combined with the system CA
|
||||
@@ -19591,8 +19601,8 @@ TEST(UniversalClientTest, EnableSystemCaWithCustomCa_Online) {
|
||||
cli.enable_server_certificate_verification(true);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
// Any status: Google may redirect or rate-limit CI runners.
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||
}
|
||||
|
||||
// The system CA policy must carry over to the client created internally for
|
||||
@@ -28695,7 +28705,7 @@ TEST(WebSocketTest, RSVBitsMustBeZero) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// RSV2 set (0x20)
|
||||
@@ -28707,7 +28717,7 @@ TEST(WebSocketTest, RSVBitsMustBeZero) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// RSV3 set (0x10)
|
||||
@@ -28719,7 +28729,7 @@ TEST(WebSocketTest, RSVBitsMustBeZero) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// No RSV bits set - should succeed
|
||||
@@ -28754,7 +28764,7 @@ TEST(WebSocketTest, ControlFrameValidation) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// Close with FIN=0 - must be rejected
|
||||
@@ -28769,7 +28779,7 @@ TEST(WebSocketTest, ControlFrameValidation) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// Ping with payload_len=126 (extended length) - must be rejected
|
||||
@@ -28787,7 +28797,7 @@ TEST(WebSocketTest, ControlFrameValidation) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// Ping with FIN=1 and payload_len=125 - should succeed
|
||||
@@ -28828,7 +28838,7 @@ TEST(WebSocketTest, PayloadLength64BitMSBMustBeZero) {
|
||||
bool fin;
|
||||
EXPECT_EQ(
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, false, 1024),
|
||||
ws::impl::FrameRead::Fail);
|
||||
ws::impl::FrameRead::ProtocolError);
|
||||
}
|
||||
|
||||
// MSB clear - should pass length parsing (will be rejected by max_len,
|
||||
@@ -30963,6 +30973,24 @@ static std::string masked_frame(uint8_t first_byte,
|
||||
return f + payload;
|
||||
}
|
||||
|
||||
// Sends frames on a new raw connection to /ws and returns the first n bytes the
|
||||
// server sends back, or what came before it closed.
|
||||
static std::string raw_websocket_reply(int port, const std::string &frames,
|
||||
size_t n) {
|
||||
auto sock = connect_raw_websocket(port, "/ws");
|
||||
if (sock == INVALID_SOCKET) { return std::string(); }
|
||||
detail::send_socket(sock, frames.data(), frames.size(), 0);
|
||||
std::string reply;
|
||||
char buf[256];
|
||||
while (reply.size() < n) {
|
||||
auto got = ::recv(sock, buf, sizeof(buf), 0);
|
||||
if (got <= 0) { break; }
|
||||
reply.append(buf, static_cast<size_t>(got));
|
||||
}
|
||||
detail::close_socket(sock);
|
||||
return reply;
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, AssemblesFragmentsAroundAPing) {
|
||||
std::atomic<bool> done{false};
|
||||
std::string received;
|
||||
@@ -31360,24 +31388,119 @@ TEST(WebSocketTest, AnswersAMalformedCloseWithProtocolError) {
|
||||
// Also between the fragments of a message.
|
||||
for (const auto &prefix : {std::string(), masked_frame(0x02, "a")}) {
|
||||
for (const auto &c : cases) {
|
||||
auto sock = connect_raw_websocket(port, "/ws");
|
||||
ASSERT_NE(INVALID_SOCKET, sock);
|
||||
auto frames = prefix + masked_frame(0x88, c.first);
|
||||
detail::send_socket(sock, frames.data(), frames.size(), 0);
|
||||
EXPECT_EQ(std::string("\x88") + static_cast<char>(c.second.size()) +
|
||||
c.second,
|
||||
raw_websocket_reply(port, prefix + masked_frame(0x88, c.first),
|
||||
2 + c.second.size()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string reply;
|
||||
TEST(WebSocketTest, FailsABadFrameWithACloseStatus) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(0);
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
// A header that announces one byte over the limit, and none of its payload
|
||||
std::string too_big = "\x82\xff";
|
||||
uint64_t len = CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH + 1;
|
||||
for (int shift = 56; shift >= 0; shift -= 8) {
|
||||
too_big += static_cast<char>((len >> shift) & 0xff);
|
||||
}
|
||||
too_big.append(4, '\0');
|
||||
|
||||
const std::string protocol_error("\x03\xea"); // 1002
|
||||
const std::string too_big_error("\x03\xf1"); // 1009
|
||||
|
||||
// What the client sends and the status the server closes with
|
||||
std::vector<std::pair<std::string, std::string>> cases = {
|
||||
{masked_frame(0xc1, "a"), protocol_error}, // RSV1
|
||||
{std::string("\x81\x01") + "a", protocol_error}, // not masked
|
||||
{masked_frame(0x09, ""), protocol_error}, // fragmented Ping
|
||||
{masked_frame(0x83, ""), protocol_error}, // reserved opcode
|
||||
{masked_frame(0x8b, ""), protocol_error}, // reserved control opcode
|
||||
{masked_frame(0x80, "a"), protocol_error}, // continuation, no start
|
||||
{masked_frame(0x01, "a") + masked_frame(0x81, "b"),
|
||||
protocol_error}, // a new message inside a fragmented one
|
||||
{std::string("\x81\xff\x80", 3) + std::string(11, '\0'),
|
||||
protocol_error}, // 64-bit length with its top bit set
|
||||
{too_big, too_big_error},
|
||||
};
|
||||
for (const auto &c : cases) {
|
||||
EXPECT_EQ(std::string("\x88\x02") + c.second,
|
||||
raw_websocket_reply(port, c.first, 4));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, ClientFailsAMaskedFrameWithProtocolError) {
|
||||
SilentListener listener;
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
|
||||
// A peer that completes the handshake, then sends a masked frame, which a
|
||||
// server must never do, and keeps what the client sends back.
|
||||
std::string reply;
|
||||
std::thread peer([&]() {
|
||||
auto sock = listener.accept();
|
||||
if (sock == INVALID_SOCKET) { return; }
|
||||
std::string req;
|
||||
char c;
|
||||
while (req.find("\r\n\r\n") == std::string::npos &&
|
||||
::recv(sock, &c, 1, 0) == 1) {
|
||||
req += c;
|
||||
}
|
||||
const std::string field = "Sec-WebSocket-Key: ";
|
||||
auto pos = req.find(field);
|
||||
if (pos != std::string::npos) {
|
||||
pos += field.size();
|
||||
auto key = req.substr(pos, req.find("\r\n", pos) - pos);
|
||||
auto res = "HTTP/1.1 101 Switching Protocols\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Accept: " +
|
||||
detail::websocket_accept_key(key) + "\r\n\r\n" +
|
||||
masked_frame(0x81, "a");
|
||||
detail::send_socket(sock, res.data(), res.size(), 0);
|
||||
// FIN + Close, masked with a length of 2, the mask key, the code
|
||||
char buf[256];
|
||||
while (reply.size() < 2 + c.second.size()) {
|
||||
while (reply.size() < 8) {
|
||||
auto n = ::recv(sock, buf, sizeof(buf), 0);
|
||||
if (n <= 0) { break; }
|
||||
reply.append(buf, static_cast<size_t>(n));
|
||||
}
|
||||
detail::close_socket(sock);
|
||||
EXPECT_EQ(std::string("\x88") + static_cast<char>(c.second.size()) +
|
||||
c.second,
|
||||
reply);
|
||||
}
|
||||
}
|
||||
detail::close_socket(sock);
|
||||
});
|
||||
|
||||
ws::WebSocketClient client("ws://127.0.0.1:" + std::to_string(listener.port) +
|
||||
"/ws");
|
||||
auto connected = static_cast<bool>(client.connect());
|
||||
std::string msg;
|
||||
auto r = connected ? client.read(msg) : ws::Fail;
|
||||
peer.join();
|
||||
ASSERT_TRUE(connected);
|
||||
EXPECT_EQ(ws::Fail, r);
|
||||
|
||||
detail::BufferStream strm;
|
||||
strm.write(reply.data(), reply.size());
|
||||
ws::Opcode opcode;
|
||||
std::string payload;
|
||||
bool fin;
|
||||
ASSERT_EQ(
|
||||
ws::impl::FrameRead::Ok,
|
||||
ws::impl::read_websocket_frame(strm, opcode, payload, fin, true, 125));
|
||||
EXPECT_EQ(ws::Opcode::Close, opcode);
|
||||
EXPECT_EQ(std::string("\x03\xea"), payload);
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
|
||||
Reference in new issue
Block a user