Skip to content

Commit dae3184

Browse files
committed
Revert "Accept large data transfer over SSL (#1261)"
This reverts commit 307b729.
1 parent 305a7ab commit dae3184

File tree

2 files changed

+42
-87
lines changed

2 files changed

+42
-87
lines changed

httplib.h

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7228,63 +7228,62 @@ inline bool SSLSocketStream::is_writable() const {
72287228
}
72297229

72307230
inline ssize_t SSLSocketStream::read(char *ptr, size_t size) {
7231-
size_t readbytes = 0;
72327231
if (SSL_pending(ssl_) > 0) {
7233-
auto ret = SSL_read_ex(ssl_, ptr, size, &readbytes);
7234-
if (ret == 1) { return static_cast<ssize_t>(readbytes); }
7235-
if (SSL_get_error(ssl_, ret) == SSL_ERROR_ZERO_RETURN) { return 0; }
7236-
return -1;
7237-
}
7238-
if (!is_readable()) { return -1; }
7239-
7240-
auto ret = SSL_read_ex(ssl_, ptr, size, &readbytes);
7241-
if (ret == 1) { return static_cast<ssize_t>(readbytes); }
7242-
auto err = SSL_get_error(ssl_, ret);
7243-
int n = 1000;
7232+
return SSL_read(ssl_, ptr, static_cast<int>(size));
7233+
} else if (is_readable()) {
7234+
auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
7235+
if (ret < 0) {
7236+
auto err = SSL_get_error(ssl_, ret);
7237+
int n = 1000;
72447238
#ifdef _WIN32
7245-
while (--n >= 0 &&
7246-
(err == SSL_ERROR_WANT_READ ||
7247-
(err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT))) {
7239+
while (--n >= 0 && (err == SSL_ERROR_WANT_READ ||
7240+
(err == SSL_ERROR_SYSCALL &&
7241+
WSAGetLastError() == WSAETIMEDOUT))) {
72487242
#else
7249-
while (--n >= 0 && err == SSL_ERROR_WANT_READ) {
7243+
while (--n >= 0 && err == SSL_ERROR_WANT_READ) {
72507244
#endif
7251-
if (SSL_pending(ssl_) > 0) {
7252-
ret = SSL_read_ex(ssl_, ptr, size, &readbytes);
7253-
if (ret == 1) { return static_cast<ssize_t>(readbytes); }
7254-
if (SSL_get_error(ssl_, ret) == SSL_ERROR_ZERO_RETURN) { return 0; }
7255-
return -1;
7245+
if (SSL_pending(ssl_) > 0) {
7246+
return SSL_read(ssl_, ptr, static_cast<int>(size));
7247+
} else if (is_readable()) {
7248+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
7249+
ret = SSL_read(ssl_, ptr, static_cast<int>(size));
7250+
if (ret >= 0) { return ret; }
7251+
err = SSL_get_error(ssl_, ret);
7252+
} else {
7253+
return -1;
7254+
}
7255+
}
72567256
}
7257-
if (!is_readable()) { return -1; }
7258-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
7259-
ret = SSL_read_ex(ssl_, ptr, size, &readbytes);
7260-
if (ret == 1) { return static_cast<ssize_t>(readbytes); }
7261-
err = SSL_get_error(ssl_, ret);
7257+
return ret;
72627258
}
7263-
if (err == SSL_ERROR_ZERO_RETURN) { return 0; }
72647259
return -1;
72657260
}
72667261

72677262
inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) {
7268-
if (!is_writable()) { return -1; }
7269-
size_t written = 0;
7270-
auto ret = SSL_write_ex(ssl_, ptr, size, &written);
7271-
if (ret == 1) { return static_cast<ssize_t>(written); }
7272-
auto err = SSL_get_error(ssl_, ret);
7273-
int n = 1000;
7263+
if (is_writable()) {
7264+
auto ret = SSL_write(ssl_, ptr, static_cast<int>(size));
7265+
if (ret < 0) {
7266+
auto err = SSL_get_error(ssl_, ret);
7267+
int n = 1000;
72747268
#ifdef _WIN32
7275-
while (--n >= 0 &&
7276-
(err == SSL_ERROR_WANT_WRITE ||
7277-
(err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT))) {
7269+
while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE ||
7270+
(err == SSL_ERROR_SYSCALL &&
7271+
WSAGetLastError() == WSAETIMEDOUT))) {
72787272
#else
7279-
while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) {
7273+
while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) {
72807274
#endif
7281-
if (!is_writable()) { return -1; }
7282-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
7283-
ret = SSL_write_ex(ssl_, ptr, size, &written);
7284-
if (ret == 1) { return static_cast<ssize_t>(written); }
7285-
err = SSL_get_error(ssl_, ret);
7275+
if (is_writable()) {
7276+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
7277+
ret = SSL_write(ssl_, ptr, static_cast<int>(size));
7278+
if (ret >= 0) { return ret; }
7279+
err = SSL_get_error(ssl_, ret);
7280+
} else {
7281+
return -1;
7282+
}
7283+
}
7284+
}
7285+
return ret;
72867286
}
7287-
if (err == SSL_ERROR_ZERO_RETURN) { return 0; }
72887287
return -1;
72897288
}
72907289

test/test.cc

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4660,50 +4660,6 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) {
46604660

46614661
t.join();
46624662
}
4663-
4664-
// Disabled due to the out-of-memory problem on GitHub Actions Workflows
4665-
TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) {
4666-
4667-
// prepare large data
4668-
std::random_device seed_gen;
4669-
std::mt19937 random(seed_gen());
4670-
constexpr auto large_size_byte = 2147483648UL + 1048576UL; // 2GiB + 1MiB
4671-
std::vector<std::uint32_t> binary(large_size_byte / sizeof(std::uint32_t));
4672-
std::generate(binary.begin(), binary.end(), [&random]() { return random(); });
4673-
4674-
// server
4675-
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
4676-
ASSERT_TRUE(svr.is_valid());
4677-
4678-
svr.Post("/binary", [&](const Request &req, Response &res) {
4679-
EXPECT_EQ(large_size_byte, req.body.size());
4680-
EXPECT_EQ(0, std::memcmp(binary.data(), req.body.data(), large_size_byte));
4681-
res.set_content(req.body, "application/octet-stream");
4682-
});
4683-
4684-
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
4685-
while (!svr.is_running()) {
4686-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
4687-
}
4688-
4689-
// client POST
4690-
SSLClient cli("localhost", PORT);
4691-
cli.enable_server_certificate_verification(false);
4692-
cli.set_read_timeout(std::chrono::seconds(100));
4693-
cli.set_write_timeout(std::chrono::seconds(100));
4694-
auto res = cli.Post("/binary", reinterpret_cast<char *>(binary.data()),
4695-
large_size_byte, "application/octet-stream");
4696-
4697-
// compare
4698-
EXPECT_EQ(200, res->status);
4699-
EXPECT_EQ(large_size_byte, res->body.size());
4700-
EXPECT_EQ(0, std::memcmp(binary.data(), res->body.data(), large_size_byte));
4701-
4702-
// cleanup
4703-
svr.stop();
4704-
listen_thread.join();
4705-
ASSERT_FALSE(svr.is_running());
4706-
}
47074663
#endif
47084664

47094665
#ifdef _WIN32

0 commit comments

Comments
 (0)