Code format
This commit is contained in:
parent
6a848b1a16
commit
ed0719f2bc
61
httplib.h
61
httplib.h
@ -728,7 +728,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
OPENSSL_thread_stop ();
|
OPENSSL_thread_stop();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1825,7 +1825,7 @@ public:
|
|||||||
|
|
||||||
SSL_CTX *ssl_context() const;
|
SSL_CTX *ssl_context() const;
|
||||||
|
|
||||||
void update_certs (X509 *cert, EVP_PKEY *private_key,
|
void update_certs(X509 *cert, EVP_PKEY *private_key,
|
||||||
X509_STORE *client_ca_cert_store = nullptr);
|
X509_STORE *client_ca_cert_store = nullptr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -2824,7 +2824,9 @@ inline bool mmap::open(const char *path) {
|
|||||||
wpath += path[i];
|
wpath += path[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \
|
||||||
|
WINAPI_PARTITION_GAMES) && \
|
||||||
|
(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
||||||
hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||||
OPEN_EXISTING, NULL);
|
OPEN_EXISTING, NULL);
|
||||||
#else
|
#else
|
||||||
@ -2834,7 +2836,8 @@ inline bool mmap::open(const char *path) {
|
|||||||
|
|
||||||
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
|
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
|
||||||
|
|
||||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES)
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \
|
||||||
|
WINAPI_PARTITION_GAMES)
|
||||||
LARGE_INTEGER size{};
|
LARGE_INTEGER size{};
|
||||||
if (!::GetFileSizeEx(hFile_, &size)) { return false; }
|
if (!::GetFileSizeEx(hFile_, &size)) { return false; }
|
||||||
size_ = static_cast<size_t>(size.QuadPart);
|
size_ = static_cast<size_t>(size.QuadPart);
|
||||||
@ -2846,12 +2849,12 @@ inline bool mmap::open(const char *path) {
|
|||||||
size_ = (static_cast<size_t>(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow;
|
size_ = (static_cast<size_t>(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \
|
||||||
|
(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
||||||
hMapping_ =
|
hMapping_ =
|
||||||
::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL);
|
::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL);
|
||||||
#else
|
#else
|
||||||
hMapping_ =
|
hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart,
|
||||||
::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart,
|
|
||||||
size.LowPart, NULL);
|
size.LowPart, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -2860,7 +2863,8 @@ inline bool mmap::open(const char *path) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \
|
||||||
|
(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
||||||
addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0);
|
addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0);
|
||||||
#else
|
#else
|
||||||
addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0);
|
addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0);
|
||||||
@ -8185,7 +8189,8 @@ inline Result ClientImpl::Patch(const std::string &path,
|
|||||||
|
|
||||||
inline Result ClientImpl::Patch(const std::string &path,
|
inline Result ClientImpl::Patch(const std::string &path,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return Patch(path, Headers(), body, content_type, progress);
|
return Patch(path, Headers(), body, content_type, progress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8784,16 +8789,16 @@ inline bool SSLServer::is_valid() const { return ctx_; }
|
|||||||
|
|
||||||
inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; }
|
inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; }
|
||||||
|
|
||||||
inline void SSLServer::update_certs (X509 *cert, EVP_PKEY *private_key,
|
inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key,
|
||||||
X509_STORE *client_ca_cert_store) {
|
X509_STORE *client_ca_cert_store) {
|
||||||
|
|
||||||
std::lock_guard<std::mutex> guard(ctx_mutex_);
|
std::lock_guard<std::mutex> guard(ctx_mutex_);
|
||||||
|
|
||||||
SSL_CTX_use_certificate (ctx_, cert);
|
SSL_CTX_use_certificate(ctx_, cert);
|
||||||
SSL_CTX_use_PrivateKey (ctx_, private_key);
|
SSL_CTX_use_PrivateKey(ctx_, private_key);
|
||||||
|
|
||||||
if (client_ca_cert_store != nullptr) {
|
if (client_ca_cert_store != nullptr) {
|
||||||
SSL_CTX_set_cert_store (ctx_, client_ca_cert_store);
|
SSL_CTX_set_cert_store(ctx_, client_ca_cert_store);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9579,7 +9584,8 @@ inline Result Client::Patch(const std::string &path, const char *body,
|
|||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const char *body,
|
inline Result Client::Patch(const std::string &path, const char *body,
|
||||||
size_t content_length,
|
size_t content_length,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return cli_->Patch(path, body, content_length, content_type, progress);
|
return cli_->Patch(path, body, content_length, content_type, progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
||||||
@ -9589,15 +9595,18 @@ inline Result Client::Patch(const std::string &path, const Headers &headers,
|
|||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
||||||
const char *body, size_t content_length,
|
const char *body, size_t content_length,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
return cli_->Patch(path, headers, body, content_length, content_type, progress);
|
Progress progress) {
|
||||||
|
return cli_->Patch(path, headers, body, content_length, content_type,
|
||||||
|
progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const std::string &body,
|
inline Result Client::Patch(const std::string &path, const std::string &body,
|
||||||
const std::string &content_type) {
|
const std::string &content_type) {
|
||||||
return cli_->Patch(path, body, content_type);
|
return cli_->Patch(path, body, content_type);
|
||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const std::string &body,
|
inline Result Client::Patch(const std::string &path, const std::string &body,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return cli_->Patch(path, body, content_type, progress);
|
return cli_->Patch(path, body, content_type, progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
||||||
@ -9607,7 +9616,8 @@ inline Result Client::Patch(const std::string &path, const Headers &headers,
|
|||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
inline Result Client::Patch(const std::string &path, const Headers &headers,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return cli_->Patch(path, headers, body, content_type, progress);
|
return cli_->Patch(path, headers, body, content_type, progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Patch(const std::string &path, size_t content_length,
|
inline Result Client::Patch(const std::string &path, size_t content_length,
|
||||||
@ -9646,7 +9656,8 @@ inline Result Client::Delete(const std::string &path, const char *body,
|
|||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const char *body,
|
inline Result Client::Delete(const std::string &path, const char *body,
|
||||||
size_t content_length,
|
size_t content_length,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return cli_->Delete(path, body, content_length, content_type, progress);
|
return cli_->Delete(path, body, content_length, content_type, progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
||||||
@ -9656,15 +9667,18 @@ inline Result Client::Delete(const std::string &path, const Headers &headers,
|
|||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
||||||
const char *body, size_t content_length,
|
const char *body, size_t content_length,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
return cli_->Delete(path, headers, body, content_length, content_type, progress);
|
Progress progress) {
|
||||||
|
return cli_->Delete(path, headers, body, content_length, content_type,
|
||||||
|
progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const std::string &body,
|
inline Result Client::Delete(const std::string &path, const std::string &body,
|
||||||
const std::string &content_type) {
|
const std::string &content_type) {
|
||||||
return cli_->Delete(path, body, content_type);
|
return cli_->Delete(path, body, content_type);
|
||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const std::string &body,
|
inline Result Client::Delete(const std::string &path, const std::string &body,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return cli_->Delete(path, body, content_type, progress);
|
return cli_->Delete(path, body, content_type, progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
||||||
@ -9674,7 +9688,8 @@ inline Result Client::Delete(const std::string &path, const Headers &headers,
|
|||||||
}
|
}
|
||||||
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
inline Result Client::Delete(const std::string &path, const Headers &headers,
|
||||||
const std::string &body,
|
const std::string &body,
|
||||||
const std::string &content_type, Progress progress) {
|
const std::string &content_type,
|
||||||
|
Progress progress) {
|
||||||
return cli_->Delete(path, headers, body, content_type, progress);
|
return cli_->Delete(path, headers, body, content_type, progress);
|
||||||
}
|
}
|
||||||
inline Result Client::Options(const std::string &path) {
|
inline Result Client::Options(const std::string &path) {
|
||||||
|
67
test/test.cc
67
test/test.cc
@ -59,8 +59,7 @@ TEST(ClientTest, MoveConstructible) {
|
|||||||
EXPECT_TRUE(std::is_nothrow_move_constructible<Client>::value);
|
EXPECT_TRUE(std::is_nothrow_move_constructible<Client>::value);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ClientTest, MoveAssignable)
|
TEST(ClientTest, MoveAssignable) {
|
||||||
{
|
|
||||||
EXPECT_FALSE(std::is_copy_assignable<Client>::value);
|
EXPECT_FALSE(std::is_copy_assignable<Client>::value);
|
||||||
EXPECT_TRUE(std::is_nothrow_move_assignable<Client>::value);
|
EXPECT_TRUE(std::is_nothrow_move_assignable<Client>::value);
|
||||||
}
|
}
|
||||||
@ -1755,30 +1754,32 @@ TEST(BindServerTest, BindAndListenSeparatelySSLEncryptedKey) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
X509* readCertificate (const std::string& strFileName) {
|
X509 *readCertificate(const std::string &strFileName) {
|
||||||
std::ifstream inStream (strFileName);
|
std::ifstream inStream(strFileName);
|
||||||
std::string strCertPEM ((std::istreambuf_iterator<char>(inStream)), std::istreambuf_iterator<char>());
|
std::string strCertPEM((std::istreambuf_iterator<char>(inStream)),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
if (strCertPEM.empty ()) return (nullptr);
|
if (strCertPEM.empty()) return (nullptr);
|
||||||
|
|
||||||
BIO* pbCert = BIO_new (BIO_s_mem ());
|
BIO *pbCert = BIO_new(BIO_s_mem());
|
||||||
BIO_write (pbCert, strCertPEM.c_str (), (int)strCertPEM.size ());
|
BIO_write(pbCert, strCertPEM.c_str(), (int)strCertPEM.size());
|
||||||
X509* pCert = PEM_read_bio_X509 (pbCert, NULL, 0, NULL);
|
X509 *pCert = PEM_read_bio_X509(pbCert, NULL, 0, NULL);
|
||||||
BIO_free (pbCert);
|
BIO_free(pbCert);
|
||||||
|
|
||||||
return (pCert);
|
return (pCert);
|
||||||
}
|
}
|
||||||
|
|
||||||
EVP_PKEY* readPrivateKey (const std::string& strFileName) {
|
EVP_PKEY *readPrivateKey(const std::string &strFileName) {
|
||||||
std::ifstream inStream (strFileName);
|
std::ifstream inStream(strFileName);
|
||||||
std::string strPrivateKeyPEM ((std::istreambuf_iterator<char>(inStream)), std::istreambuf_iterator<char>());
|
std::string strPrivateKeyPEM((std::istreambuf_iterator<char>(inStream)),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
if (strPrivateKeyPEM.empty ()) return (nullptr);
|
if (strPrivateKeyPEM.empty()) return (nullptr);
|
||||||
|
|
||||||
BIO* pbPrivKey = BIO_new (BIO_s_mem ());
|
BIO *pbPrivKey = BIO_new(BIO_s_mem());
|
||||||
BIO_write (pbPrivKey, strPrivateKeyPEM.c_str (), (int) strPrivateKeyPEM.size ());
|
BIO_write(pbPrivKey, strPrivateKeyPEM.c_str(), (int)strPrivateKeyPEM.size());
|
||||||
EVP_PKEY* pPrivateKey = PEM_read_bio_PrivateKey (pbPrivKey, NULL, NULL, NULL);
|
EVP_PKEY *pPrivateKey = PEM_read_bio_PrivateKey(pbPrivKey, NULL, NULL, NULL);
|
||||||
BIO_free (pbPrivKey);
|
BIO_free(pbPrivKey);
|
||||||
|
|
||||||
return (pPrivateKey);
|
return (pPrivateKey);
|
||||||
}
|
}
|
||||||
@ -1789,26 +1790,26 @@ TEST(BindServerTest, UpdateCerts) {
|
|||||||
ASSERT_TRUE(svr.is_valid());
|
ASSERT_TRUE(svr.is_valid());
|
||||||
ASSERT_TRUE(port > 0);
|
ASSERT_TRUE(port > 0);
|
||||||
|
|
||||||
X509* cert = readCertificate (SERVER_CERT_FILE);
|
X509 *cert = readCertificate(SERVER_CERT_FILE);
|
||||||
X509* ca_cert = readCertificate (CLIENT_CA_CERT_FILE);
|
X509 *ca_cert = readCertificate(CLIENT_CA_CERT_FILE);
|
||||||
EVP_PKEY* key = readPrivateKey (SERVER_PRIVATE_KEY_FILE);
|
EVP_PKEY *key = readPrivateKey(SERVER_PRIVATE_KEY_FILE);
|
||||||
|
|
||||||
ASSERT_TRUE(cert != nullptr);
|
ASSERT_TRUE(cert != nullptr);
|
||||||
ASSERT_TRUE(ca_cert != nullptr);
|
ASSERT_TRUE(ca_cert != nullptr);
|
||||||
ASSERT_TRUE(key != nullptr);
|
ASSERT_TRUE(key != nullptr);
|
||||||
|
|
||||||
X509_STORE* cert_store = X509_STORE_new ();
|
X509_STORE *cert_store = X509_STORE_new();
|
||||||
|
|
||||||
X509_STORE_add_cert (cert_store, ca_cert);
|
X509_STORE_add_cert(cert_store, ca_cert);
|
||||||
|
|
||||||
svr.update_certs (cert, key, cert_store);
|
svr.update_certs(cert, key, cert_store);
|
||||||
|
|
||||||
ASSERT_TRUE(svr.is_valid());
|
ASSERT_TRUE(svr.is_valid());
|
||||||
svr.stop();
|
svr.stop();
|
||||||
|
|
||||||
X509_free (cert);
|
X509_free(cert);
|
||||||
X509_free (ca_cert);
|
X509_free(ca_cert);
|
||||||
EVP_PKEY_free (key);
|
EVP_PKEY_free(key);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -3480,8 +3481,10 @@ TEST_F(ServerTest, GetStreamedWithRangeError) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetRangeWithMaxLongLength) {
|
TEST_F(ServerTest, GetRangeWithMaxLongLength) {
|
||||||
auto res =
|
auto res = cli_.Get(
|
||||||
cli_.Get("/with-range", {{"Range", "bytes=0-" + std::to_string(std::numeric_limits<long>::max())}});
|
"/with-range",
|
||||||
|
{{"Range",
|
||||||
|
"bytes=0-" + std::to_string(std::numeric_limits<long>::max())}});
|
||||||
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
||||||
EXPECT_EQ("0", res->get_header_value("Content-Length"));
|
EXPECT_EQ("0", res->get_header_value("Content-Length"));
|
||||||
EXPECT_EQ(false, res->has_header("Content-Range"));
|
EXPECT_EQ(false, res->has_header("Content-Range"));
|
||||||
@ -3637,7 +3640,8 @@ TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
||||||
auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}})}});
|
auto res = cli_.Get("/with-range-customized-response",
|
||||||
|
{{make_range_header({{1, 2}})}});
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
|
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
|
||||||
EXPECT_EQ(true, res->has_header("Content-Length"));
|
EXPECT_EQ(true, res->has_header("Content-Length"));
|
||||||
@ -3646,7 +3650,8 @@ TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
|
TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
|
||||||
auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}, {4, 5}})}});
|
auto res = cli_.Get("/with-range-customized-response",
|
||||||
|
{{make_range_header({{1, 2}, {4, 5}})}});
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
|
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
|
||||||
EXPECT_EQ(true, res->has_header("Content-Length"));
|
EXPECT_EQ(true, res->has_header("Content-Length"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user