mirror of
https://github.com/yhirose/cpp-httplib
synced 2026-06-08 18:30:49 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| af56b7ec0b | |||
| 6c3e8482f7 | |||
| 390f2c41f6 | |||
| aa04feebb4 | |||
| 45f3694f82 | |||
| c5c54b31e2 | |||
| 69c84c9597 | |||
| ae63b89cbf | |||
| ff038f98b7 | |||
| fb739dbaec | |||
| 50fce538c6 |
+1
-1
@@ -58,4 +58,4 @@ pem:
|
||||
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||
|
||||
clean:
|
||||
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark *.pem
|
||||
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request *.pem
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||
#define CPPHTTPLIB_HTTPLIB_H
|
||||
|
||||
#define CPPHTTPLIB_VERSION "0.16.1"
|
||||
#define CPPHTTPLIB_VERSION "0.16.3"
|
||||
|
||||
/*
|
||||
* Configuration
|
||||
@@ -269,7 +269,12 @@ using socket_t = int;
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#if OPENSSL_VERSION_NUMBER < 0x30000000L
|
||||
#if defined(OPENSSL_IS_BORINGSSL)
|
||||
#if OPENSSL_VERSION_NUMBER < 0x1010107f
|
||||
#error Please use OpenSSL or a current version of BoringSSL
|
||||
#endif
|
||||
#define SSL_get1_peer_certificate SSL_get_peer_certificate
|
||||
#elif OPENSSL_VERSION_NUMBER < 0x30000000L
|
||||
#error Sorry, OpenSSL versions prior to 3.0.0 are not supported
|
||||
#endif
|
||||
|
||||
@@ -727,7 +732,7 @@ private:
|
||||
fn();
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL)
|
||||
OPENSSL_thread_stop();
|
||||
#endif
|
||||
}
|
||||
@@ -3251,7 +3256,13 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
|
||||
const auto addrlen = host.length();
|
||||
if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
|
||||
|
||||
#ifdef SOCK_CLOEXEC
|
||||
auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC,
|
||||
hints.ai_protocol);
|
||||
#else
|
||||
auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
|
||||
#endif
|
||||
|
||||
if (sock != INVALID_SOCKET) {
|
||||
sockaddr_un addr{};
|
||||
addr.sun_family = AF_UNIX;
|
||||
@@ -3261,10 +3272,14 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
|
||||
hints.ai_addrlen = static_cast<socklen_t>(
|
||||
sizeof(addr) - sizeof(addr.sun_path) + addrlen);
|
||||
|
||||
#ifndef SOCK_CLOEXEC
|
||||
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
||||
#endif
|
||||
|
||||
if (socket_options) { socket_options(sock); }
|
||||
|
||||
if (!bind_or_connect(sock, hints)) {
|
||||
bool dummy;
|
||||
if (!bind_or_connect(sock, hints, dummy)) {
|
||||
close_socket(sock);
|
||||
sock = INVALID_SOCKET;
|
||||
}
|
||||
@@ -3306,11 +3321,18 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
|
||||
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
}
|
||||
#else
|
||||
|
||||
#ifdef SOCK_CLOEXEC
|
||||
auto sock =
|
||||
socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol);
|
||||
#else
|
||||
auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
if (sock == INVALID_SOCKET) { continue; }
|
||||
|
||||
#ifndef _WIN32
|
||||
#if !defined _WIN32 && !defined SOCK_CLOEXEC
|
||||
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
|
||||
close_socket(sock);
|
||||
continue;
|
||||
@@ -3342,12 +3364,15 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
|
||||
}
|
||||
|
||||
// bind or connect
|
||||
if (bind_or_connect(sock, *rp)) {
|
||||
auto quit = false;
|
||||
if (bind_or_connect(sock, *rp, quit)) {
|
||||
freeaddrinfo(result);
|
||||
return sock;
|
||||
}
|
||||
|
||||
close_socket(sock);
|
||||
|
||||
if (quit) { break; }
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
@@ -3448,7 +3473,7 @@ inline socket_t create_client_socket(
|
||||
time_t write_timeout_usec, const std::string &intf, Error &error) {
|
||||
auto sock = create_socket(
|
||||
host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options),
|
||||
[&](socket_t sock2, struct addrinfo &ai) -> bool {
|
||||
[&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool {
|
||||
if (!intf.empty()) {
|
||||
#ifdef USE_IF2IP
|
||||
auto ip_from_if = if2ip(address_family, intf);
|
||||
@@ -3472,7 +3497,10 @@ inline socket_t create_client_socket(
|
||||
}
|
||||
error = wait_until_socket_is_ready(sock2, connection_timeout_sec,
|
||||
connection_timeout_usec);
|
||||
if (error != Error::Success) { return false; }
|
||||
if (error != Error::Success) {
|
||||
if (error == Error::ConnectionTimeout) { quit = true; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
set_nonblocking(sock2, false);
|
||||
@@ -6449,7 +6477,7 @@ Server::create_server_socket(const std::string &host, int port,
|
||||
return detail::create_socket(
|
||||
host, std::string(), port, address_family_, socket_flags, tcp_nodelay_,
|
||||
std::move(socket_options),
|
||||
[](socket_t sock, struct addrinfo &ai) -> bool {
|
||||
[](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool {
|
||||
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
|
||||
return false;
|
||||
}
|
||||
@@ -6505,7 +6533,16 @@ inline bool Server::listen_internal() {
|
||||
#ifndef _WIN32
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined _WIN32
|
||||
// sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT,
|
||||
// OVERLAPPED
|
||||
socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0);
|
||||
#elif defined SOCK_CLOEXEC
|
||||
socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC);
|
||||
#else
|
||||
socket_t sock = accept(svr_sock_, nullptr, nullptr);
|
||||
#endif
|
||||
|
||||
if (sock == INVALID_SOCKET) {
|
||||
if (errno == EMFILE) {
|
||||
@@ -9096,11 +9133,14 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
|
||||
return true;
|
||||
},
|
||||
[&](SSL *ssl2) {
|
||||
#if defined(OPENSSL_IS_BORINGSSL)
|
||||
SSL_set_tlsext_host_name(ssl2, host_.c_str());
|
||||
#else
|
||||
// NOTE: Direct call instead of using the OpenSSL macro to suppress
|
||||
// -Wold-style-cast warning
|
||||
// SSL_set_tlsext_host_name(ssl2, host_.c_str());
|
||||
SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name,
|
||||
static_cast<void *>(const_cast<char *>(host_.c_str())));
|
||||
#endif
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -9321,7 +9361,7 @@ inline Client::Client(const std::string &scheme_host_port,
|
||||
cli_ = detail::make_unique<ClientImpl>(scheme_host_port, 80,
|
||||
client_cert_path, client_key_path);
|
||||
}
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
inline Client::Client(const std::string &host, int port)
|
||||
: cli_(detail::make_unique<ClientImpl>(host, port)) {}
|
||||
|
||||
+11
-8
@@ -5167,8 +5167,14 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) {
|
||||
res.set_content("Hello World!", "text/plain");
|
||||
});
|
||||
|
||||
auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); });
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
listen_thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
SSLClient cli(HOST, PORT);
|
||||
cli.enable_server_certificate_verification(false);
|
||||
@@ -5176,7 +5182,7 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) {
|
||||
|
||||
auto result = cli.Post(
|
||||
"/hi", content.size(),
|
||||
[&content](size_t offset, size_t length, DataSink &sink) {
|
||||
[&content](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
|
||||
sink.write(content.c_str(), content.size());
|
||||
return true;
|
||||
},
|
||||
@@ -5189,7 +5195,7 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) {
|
||||
// Recoonect
|
||||
result = cli.Post(
|
||||
"/hi", content.size(),
|
||||
[&content](size_t offset, size_t length, DataSink &sink) {
|
||||
[&content](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
|
||||
sink.write(content.c_str(), content.size());
|
||||
return true;
|
||||
},
|
||||
@@ -5199,16 +5205,13 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) {
|
||||
|
||||
result = cli.Post(
|
||||
"/hi", content.size(),
|
||||
[&content](size_t offset, size_t length, DataSink &sink) {
|
||||
[&content](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
|
||||
sink.write(content.c_str(), content.size());
|
||||
return true;
|
||||
},
|
||||
"text/plain");
|
||||
ASSERT_TRUE(result);
|
||||
EXPECT_EQ(200, result->status);
|
||||
|
||||
svr.stop();
|
||||
f.wait();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user