mirror of
https://github.com/yhirose/cpp-httplib
synced 2026-06-08 18:30:49 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d05c343602 | |||
| 33f67386fe | |||
| 56d8168dc4 | |||
| 5d87cc0558 | |||
| cb41947eb4 | |||
| 0857eba17b | |||
| 020b0db090 | |||
| bf0760fde4 |
@@ -1,13 +1,17 @@
|
||||
//
|
||||
// httplib.h
|
||||
//
|
||||
// Copyright (c) 2021 Yuji Hirose. All rights reserved.
|
||||
// Copyright (c) 2022 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||
#define CPPHTTPLIB_HTTPLIB_H
|
||||
|
||||
#define CPPHTTPLIB_VERSION_MAJOR 0
|
||||
#define CPPHTTPLIB_VERSION_MINOR 10
|
||||
#define CPPHTTPLIB_VERSION_PATCH 5
|
||||
|
||||
/*
|
||||
* Configuration
|
||||
*/
|
||||
@@ -170,6 +174,7 @@ using socket_t = SOCKET;
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#ifdef __linux__
|
||||
@@ -227,7 +232,7 @@ using socket_t = int;
|
||||
#endif
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
@@ -973,7 +978,7 @@ public:
|
||||
|
||||
void stop();
|
||||
|
||||
void set_hostname_addr_map(const std::map<std::string, std::string> addr_map);
|
||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||
|
||||
void set_default_headers(Headers headers);
|
||||
|
||||
@@ -1308,7 +1313,7 @@ public:
|
||||
|
||||
void stop();
|
||||
|
||||
void set_hostname_addr_map(const std::map<std::string, std::string> addr_map);
|
||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||
|
||||
void set_default_headers(Headers headers);
|
||||
|
||||
@@ -2649,11 +2654,14 @@ inline bool bind_ip_address(socket_t sock, const char *host) {
|
||||
#endif
|
||||
|
||||
#ifdef USE_IF2IP
|
||||
inline std::string if2ip(const std::string &ifn) {
|
||||
inline std::string if2ip(int address_family, const std::string &ifn) {
|
||||
struct ifaddrs *ifap;
|
||||
getifaddrs(&ifap);
|
||||
std::string addr_candidate;
|
||||
for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr && ifn == ifa->ifa_name) {
|
||||
if (ifa->ifa_addr && ifn == ifa->ifa_name &&
|
||||
(AF_UNSPEC == address_family ||
|
||||
ifa->ifa_addr->sa_family == address_family)) {
|
||||
if (ifa->ifa_addr->sa_family == AF_INET) {
|
||||
auto sa = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
|
||||
char buf[INET_ADDRSTRLEN];
|
||||
@@ -2661,11 +2669,26 @@ inline std::string if2ip(const std::string &ifn) {
|
||||
freeifaddrs(ifap);
|
||||
return std::string(buf, INET_ADDRSTRLEN);
|
||||
}
|
||||
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
|
||||
auto sa = reinterpret_cast<struct sockaddr_in6 *>(ifa->ifa_addr);
|
||||
if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) {
|
||||
char buf[INET6_ADDRSTRLEN] = {};
|
||||
if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) {
|
||||
// equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL
|
||||
auto s6_addr_head = sa->sin6_addr.s6_addr[0];
|
||||
if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) {
|
||||
addr_candidate = std::string(buf, INET6_ADDRSTRLEN);
|
||||
} else {
|
||||
freeifaddrs(ifap);
|
||||
return std::string(buf, INET6_ADDRSTRLEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifap);
|
||||
return std::string();
|
||||
return addr_candidate;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2680,7 +2703,7 @@ inline socket_t create_client_socket(
|
||||
[&](socket_t sock2, struct addrinfo &ai) -> bool {
|
||||
if (!intf.empty()) {
|
||||
#ifdef USE_IF2IP
|
||||
auto ip = if2ip(intf);
|
||||
auto ip = if2ip(address_family, intf);
|
||||
if (ip.empty()) { ip = intf; }
|
||||
if (!bind_ip_address(sock2, ip.c_str())) {
|
||||
error = Error::BindIPAddress;
|
||||
@@ -4146,36 +4169,36 @@ inline bool has_crlf(const char *s) {
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
template <typename CTX, typename Init, typename Update, typename Final>
|
||||
inline std::string message_digest(const std::string &s, Init init,
|
||||
Update update, Final final,
|
||||
size_t digest_length) {
|
||||
std::vector<unsigned char> md(digest_length, 0);
|
||||
CTX ctx;
|
||||
init(&ctx);
|
||||
update(&ctx, s.data(), s.size());
|
||||
final(md.data(), &ctx);
|
||||
inline std::string message_digest(const std::string &s, const EVP_MD *algo) {
|
||||
auto context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>(
|
||||
EVP_MD_CTX_new(), EVP_MD_CTX_free);
|
||||
|
||||
unsigned int hash_length = 0;
|
||||
unsigned char hash[EVP_MAX_MD_SIZE];
|
||||
|
||||
EVP_DigestInit_ex(context.get(), algo, nullptr);
|
||||
EVP_DigestUpdate(context.get(), s.c_str(), s.size());
|
||||
EVP_DigestFinal_ex(context.get(), hash, &hash_length);
|
||||
|
||||
std::stringstream ss;
|
||||
for (auto c : md) {
|
||||
ss << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)c;
|
||||
for (auto i = 0u; i < hash_length; ++i) {
|
||||
ss << std::hex << std::setw(2) << std::setfill('0')
|
||||
<< (unsigned int)hash[i];
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
inline std::string MD5(const std::string &s) {
|
||||
return message_digest<MD5_CTX>(s, MD5_Init, MD5_Update, MD5_Final,
|
||||
MD5_DIGEST_LENGTH);
|
||||
return message_digest(s, EVP_md5());
|
||||
}
|
||||
|
||||
inline std::string SHA_256(const std::string &s) {
|
||||
return message_digest<SHA256_CTX>(s, SHA256_Init, SHA256_Update, SHA256_Final,
|
||||
SHA256_DIGEST_LENGTH);
|
||||
return message_digest(s, EVP_sha256());
|
||||
}
|
||||
|
||||
inline std::string SHA_512(const std::string &s) {
|
||||
return message_digest<SHA512_CTX>(s, SHA512_Init, SHA512_Update, SHA512_Final,
|
||||
SHA512_DIGEST_LENGTH);
|
||||
return message_digest(s, EVP_sha512());
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4212,10 +4235,14 @@ class WSInit {
|
||||
public:
|
||||
WSInit() {
|
||||
WSADATA wsaData;
|
||||
WSAStartup(0x0002, &wsaData);
|
||||
if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true;
|
||||
}
|
||||
|
||||
~WSInit() { WSACleanup(); }
|
||||
~WSInit() {
|
||||
if (is_valid_) WSACleanup();
|
||||
}
|
||||
|
||||
bool is_valid_ = false;
|
||||
};
|
||||
|
||||
static WSInit wsinit_;
|
||||
@@ -6171,7 +6198,10 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
|
||||
|
||||
#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT
|
||||
if (!req.has_header("User-Agent")) {
|
||||
req.headers.emplace("User-Agent", "cpp-httplib/0.10.3");
|
||||
auto agent = "cpp-httplib/" + std::to_string(CPPHTTPLIB_VERSION_MAJOR) +
|
||||
"." + std::to_string(CPPHTTPLIB_VERSION_MINOR) + "." +
|
||||
std::to_string(CPPHTTPLIB_VERSION_PATCH);
|
||||
req.headers.emplace("User-Agent", agent);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -6949,8 +6979,8 @@ inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; }
|
||||
|
||||
inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; }
|
||||
|
||||
inline void ClientImpl::set_hostname_addr_map(
|
||||
const std::map<std::string, std::string> addr_map) {
|
||||
inline void
|
||||
ClientImpl::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
|
||||
addr_map_ = std::move(addr_map);
|
||||
}
|
||||
|
||||
@@ -8075,8 +8105,8 @@ inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); }
|
||||
|
||||
inline void Client::stop() { cli_->stop(); }
|
||||
|
||||
inline void Client::set_hostname_addr_map(
|
||||
const std::map<std::string, std::string> addr_map) {
|
||||
inline void
|
||||
Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
|
||||
cli_->set_hostname_addr_map(std::move(addr_map));
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,12 @@ cert2_pem = custom_target(
|
||||
command: [openssl, 'req', '-x509', '-config', test_conf, '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-out', '@OUTPUT@', '-extensions', 'SAN']
|
||||
)
|
||||
|
||||
key_encrypted_pem = custom_target(
|
||||
'key_encrypted_pem',
|
||||
output: 'key_encrypted.pem',
|
||||
command: [openssl, 'genrsa', '-passout', 'pass:test123!', '-out', '@OUTPUT@', '2048']
|
||||
)
|
||||
|
||||
cert_encrypted_pem = custom_target(
|
||||
'cert_encrypted_pem',
|
||||
input: key_encrypted_pem,
|
||||
|
||||
+25
-12
@@ -1419,10 +1419,9 @@ TEST(InvalidFormatTest, StatusCode) {
|
||||
TEST(URLFragmentTest, WithFragment) {
|
||||
Server svr;
|
||||
|
||||
svr.Get("/hi",
|
||||
[](const Request &req, Response &/*res*/) {
|
||||
EXPECT_TRUE(req.target == "/hi");
|
||||
});
|
||||
svr.Get("/hi", [](const Request &req, Response & /*res*/) {
|
||||
EXPECT_TRUE(req.target == "/hi");
|
||||
});
|
||||
|
||||
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
||||
|
||||
@@ -4369,6 +4368,20 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) {
|
||||
ASSERT_EQ(200, res->status);
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST(SSLClientTest, SetInterfaceWithINET6) {
|
||||
auto cli = std::make_shared<httplib::Client>("https://httpbin.org");
|
||||
ASSERT_TRUE(cli != nullptr);
|
||||
|
||||
cli->set_address_family(AF_INET6);
|
||||
cli->set_interface("en0");
|
||||
|
||||
auto res = cli->Get("/get");
|
||||
ASSERT_TRUE(res);
|
||||
ASSERT_EQ(200, res->status);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(SSLClientServerTest, ClientCertPresent) {
|
||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
|
||||
CLIENT_CA_CERT_DIR);
|
||||
@@ -4838,15 +4851,15 @@ TEST(MultipartFormDataTest, LargeData) {
|
||||
return true;
|
||||
});
|
||||
|
||||
EXPECT_TRUE(std::string(files[0].name) == "document");
|
||||
EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
|
||||
EXPECT_TRUE(files[0].filename == "2MB_data");
|
||||
EXPECT_TRUE(files[0].content_type == "application/octet-stream");
|
||||
EXPECT_TRUE(std::string(files[0].name) == "document");
|
||||
EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
|
||||
EXPECT_TRUE(files[0].filename == "2MB_data");
|
||||
EXPECT_TRUE(files[0].content_type == "application/octet-stream");
|
||||
|
||||
EXPECT_TRUE(files[1].name == "hello");
|
||||
EXPECT_TRUE(files[1].content == "world");
|
||||
EXPECT_TRUE(files[1].filename == "");
|
||||
EXPECT_TRUE(files[1].content_type == "");
|
||||
EXPECT_TRUE(files[1].name == "hello");
|
||||
EXPECT_TRUE(files[1].content == "world");
|
||||
EXPECT_TRUE(files[1].filename == "");
|
||||
EXPECT_TRUE(files[1].content_type == "");
|
||||
} else {
|
||||
std::string body;
|
||||
content_reader([&](const char *data, size_t data_length) {
|
||||
|
||||
Reference in New Issue
Block a user