Skip to content

Commit d2fae40

Browse files
committed
Fixed build errors
1 parent de844e6 commit d2fae40

File tree

2 files changed

+73
-51
lines changed

2 files changed

+73
-51
lines changed

httplib.h

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,9 @@ class Client {
700700

701701
void set_basic_auth(const char *username, const char *password);
702702

703+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
703704
void set_digest_auth(const char *username, const char *password);
705+
#endif
704706

705707
void set_follow_location(bool on);
706708

@@ -712,7 +714,9 @@ class Client {
712714

713715
void set_proxy_basic_auth(const char *username, const char *password);
714716

717+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
715718
void set_proxy_digest_auth(const char *username, const char *password);
719+
#endif
716720

717721
void set_logger(Logger logger);
718722

@@ -736,8 +740,10 @@ class Client {
736740

737741
std::string basic_auth_username_;
738742
std::string basic_auth_password_;
743+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
739744
std::string digest_auth_username_;
740745
std::string digest_auth_password_;
746+
#endif
741747

742748
bool follow_location_ = false;
743749

@@ -750,8 +756,10 @@ class Client {
750756

751757
std::string proxy_basic_auth_username_;
752758
std::string proxy_basic_auth_password_;
759+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
753760
std::string proxy_digest_auth_username_;
754761
std::string proxy_digest_auth_password_;
762+
#endif
755763

756764
Logger logger_;
757765

@@ -764,17 +772,21 @@ class Client {
764772
keep_alive_max_count_ = rhs.keep_alive_max_count_;
765773
basic_auth_username_ = rhs.basic_auth_username_;
766774
basic_auth_password_ = rhs.basic_auth_password_;
775+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
767776
digest_auth_username_ = rhs.digest_auth_username_;
768777
digest_auth_password_ = rhs.digest_auth_password_;
778+
#endif
769779
follow_location_ = rhs.follow_location_;
770780
compress_ = rhs.compress_;
771781
interface_ = rhs.interface_;
772782
proxy_host_ = rhs.proxy_host_;
773783
proxy_port_ = rhs.proxy_port_;
774784
proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
775785
proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
786+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
776787
proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_;
777788
proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
789+
#endif
778790
logger_ = rhs.logger_;
779791
}
780792

@@ -783,9 +795,11 @@ class Client {
783795
bool read_response_line(Stream &strm, Response &res);
784796
bool write_request(Stream &strm, const Request &req, bool last_connection);
785797
bool redirect(const Request &req, Response &res);
786-
bool connect(socket_t sock, Response &res, bool &error);
787798
bool handle_request(Stream &strm, const Request &req, Response &res,
788799
bool last_connection, bool &connection_close);
800+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
801+
bool connect(socket_t sock, Response &res, bool &error);
802+
#endif
789803

790804
std::shared_ptr<Response> send_with_content_provider(
791805
const char *method, const char *path, const Headers &headers,
@@ -3498,6 +3512,57 @@ inline bool Client::send(const std::vector<Request> &requests,
34983512
return true;
34993513
}
35003514

3515+
inline bool Client::handle_request(Stream &strm, const Request &req,
3516+
Response &res, bool last_connection,
3517+
bool &connection_close) {
3518+
if (req.path.empty()) { return false; }
3519+
3520+
bool ret;
3521+
3522+
if (!is_ssl() && !proxy_host_.empty()) {
3523+
auto req2 = req;
3524+
req2.path = "http://" + host_and_port_ + req.path;
3525+
ret = process_request(strm, req2, res, last_connection, connection_close);
3526+
} else {
3527+
ret = process_request(strm, req, res, last_connection, connection_close);
3528+
}
3529+
3530+
if (!ret) { return false; }
3531+
3532+
if (300 < res.status && res.status < 400 && follow_location_) {
3533+
ret = redirect(req, res);
3534+
}
3535+
3536+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3537+
if (res.status == 401 || res.status == 407) {
3538+
auto is_proxy = res.status == 407;
3539+
const auto &username =
3540+
is_proxy ? proxy_digest_auth_username_ : digest_auth_username_;
3541+
const auto &password =
3542+
is_proxy ? proxy_digest_auth_password_ : digest_auth_password_;
3543+
3544+
if (!username.empty() && !password.empty()) {
3545+
std::map<std::string, std::string> auth;
3546+
if (parse_www_authenticate(res, auth, is_proxy)) {
3547+
Request new_req = req;
3548+
auto key = is_proxy ? "Proxy-Authorization" : "WWW-Authorization";
3549+
new_req.headers.erase(key);
3550+
new_req.headers.insert(make_digest_authentication_header(
3551+
req, auth, 1, random_string(10), username, password, is_proxy));
3552+
3553+
Response new_res;
3554+
3555+
ret = send(new_req, new_res);
3556+
if (ret) { res = new_res; }
3557+
}
3558+
}
3559+
}
3560+
#endif
3561+
3562+
return ret;
3563+
}
3564+
3565+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
35013566
inline bool Client::connect(socket_t sock, Response &res, bool &error) {
35023567
error = true;
35033568
Response res2;
@@ -3548,57 +3613,8 @@ inline bool Client::connect(socket_t sock, Response &res, bool &error) {
35483613

35493614
return true;
35503615
}
3551-
3552-
inline bool Client::handle_request(Stream &strm, const Request &req,
3553-
Response &res, bool last_connection,
3554-
bool &connection_close) {
3555-
if (req.path.empty()) { return false; }
3556-
3557-
bool ret;
3558-
3559-
if (!is_ssl() && !proxy_host_.empty()) {
3560-
auto req2 = req;
3561-
req2.path = "http://" + host_and_port_ + req.path;
3562-
ret = process_request(strm, req2, res, last_connection, connection_close);
3563-
} else {
3564-
ret = process_request(strm, req, res, last_connection, connection_close);
3565-
}
3566-
3567-
if (!ret) { return false; }
3568-
3569-
if (300 < res.status && res.status < 400 && follow_location_) {
3570-
ret = redirect(req, res);
3571-
}
3572-
3573-
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3574-
if (res.status == 401 || res.status == 407) {
3575-
auto is_proxy = res.status == 407;
3576-
const auto &username =
3577-
is_proxy ? proxy_digest_auth_username_ : digest_auth_username_;
3578-
const auto &password =
3579-
is_proxy ? proxy_digest_auth_password_ : digest_auth_password_;
3580-
3581-
if (!username.empty() && !password.empty()) {
3582-
std::map<std::string, std::string> auth;
3583-
if (parse_www_authenticate(res, auth, is_proxy)) {
3584-
Request new_req = req;
3585-
auto key = is_proxy ? "Proxy-Authorization" : "WWW-Authorization";
3586-
new_req.headers.erase(key);
3587-
new_req.headers.insert(make_digest_authentication_header(
3588-
req, auth, 1, random_string(10), username, password, is_proxy));
3589-
3590-
Response new_res;
3591-
3592-
ret = send(new_req, new_res);
3593-
if (ret) { res = new_res; }
3594-
}
3595-
}
3596-
}
35973616
#endif
35983617

3599-
return ret;
3600-
}
3601-
36023618
inline bool Client::redirect(const Request &req, Response &res) {
36033619
if (req.redirect_count == 0) { return false; }
36043620

@@ -4131,11 +4147,13 @@ inline void Client::set_basic_auth(const char *username, const char *password) {
41314147
basic_auth_password_ = password;
41324148
}
41334149

4150+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
41344151
inline void Client::set_digest_auth(const char *username,
41354152
const char *password) {
41364153
digest_auth_username_ = username;
41374154
digest_auth_password_ = password;
41384155
}
4156+
#endif
41394157

41404158
inline void Client::set_follow_location(bool on) { follow_location_ = on; }
41414159

@@ -4154,11 +4172,13 @@ inline void Client::set_proxy_basic_auth(const char *username,
41544172
proxy_basic_auth_password_ = password;
41554173
}
41564174

4175+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
41574176
inline void Client::set_proxy_digest_auth(const char *username,
41584177
const char *password) {
41594178
proxy_digest_auth_username_ = username;
41604179
proxy_digest_auth_password_ = password;
41614180
}
4181+
#endif
41624182

41634183
inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); }
41644184

test/test_proxy.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ void RedirectProxyText(Client& cli, const char *path, bool basic) {
4141
if (basic) {
4242
cli.set_proxy_basic_auth("hello", "world");
4343
} else {
44+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4445
cli.set_proxy_digest_auth("hello", "world");
46+
#endif
4547
}
4648
cli.set_follow_location(true);
4749

@@ -55,12 +57,12 @@ TEST(RedirectTest, HTTPBinNoSSLBasic) {
5557
RedirectProxyText(cli, "/redirect/2", true);
5658
}
5759

60+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
5861
TEST(RedirectTest, HTTPBinNoSSLDigest) {
5962
Client cli("httpbin.org");
6063
RedirectProxyText(cli, "/redirect/2", false);
6164
}
6265

63-
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6466
TEST(RedirectTest, HTTPBinSSLBasic) {
6567
SSLClient cli("httpbin.org");
6668
RedirectProxyText(cli, "/redirect/2", true);

0 commit comments

Comments
 (0)