Skip to content

Commit 39c7bba

Browse files
committed
Code cleanup
1 parent f2476f2 commit 39c7bba

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

httplib.h

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ inline void read_file(const std::string &path, std::string &out) {
10601060

10611061
inline std::string file_extension(const std::string &path) {
10621062
std::smatch m;
1063-
auto re = std::regex("\\.([a-zA-Z0-9]+)$");
1063+
static auto re = std::regex("\\.([a-zA-Z0-9]+)$");
10641064
if (std::regex_search(path, m, re)) { return m[1].str(); }
10651065
return std::string();
10661066
}
@@ -2823,7 +2823,7 @@ inline void Server::stop() {
28232823
}
28242824

28252825
inline bool Server::parse_request_line(const char *s, Request &req) {
2826-
static std::regex re(
2826+
const static std::regex re(
28272827
"(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI) "
28282828
"(([^?]+)(?:\\?(.*?))?) (HTTP/1\\.[01])\r\n");
28292829

@@ -3487,52 +3487,49 @@ inline bool Client::redirect(const Request &req, Response &res) {
34873487
auto location = res.get_header_value("location");
34883488
if (location.empty()) { return false; }
34893489

3490-
std::regex re(
3490+
const static std::regex re(
34913491
R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)");
34923492

34933493
std::smatch m;
3494-
if (regex_match(location, m, re)) {
3495-
auto next_scheme = m[1].str();
3496-
auto next_host = m[2].str();
3497-
auto next_path = m[3].str();
3498-
if (next_host.empty()) { next_host = host_; }
3499-
if (next_path.empty()) { next_path = "/"; }
3494+
if (!regex_match(location, m, re)) { return false; }
35003495

3501-
auto scheme = is_ssl() ? "https" : "http";
3496+
auto next_scheme = m[1].str();
3497+
auto next_host = m[2].str();
3498+
auto next_path = m[3].str();
3499+
if (next_host.empty()) { next_host = host_; }
3500+
if (next_path.empty()) { next_path = "/"; }
35023501

3503-
if (next_scheme == scheme && next_host == host_) {
3504-
return detail::redirect(*this, req, res, next_path);
3505-
} else {
3506-
if (next_scheme == "https") {
3502+
auto scheme = is_ssl() ? "https" : "http";
3503+
3504+
if (next_scheme == scheme && next_host == host_) {
3505+
return detail::redirect(*this, req, res, next_path);
3506+
} else {
3507+
if (next_scheme == "https") {
35073508
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3508-
SSLClient cli(next_host.c_str());
3509-
cli.set_follow_location(true);
3510-
return detail::redirect(cli, req, res, next_path);
3509+
SSLClient cli(next_host.c_str());
3510+
cli.set_follow_location(true);
3511+
return detail::redirect(cli, req, res, next_path);
35113512
#else
3512-
return false;
3513+
return false;
35133514
#endif
3514-
} else {
3515-
Client cli(next_host.c_str());
3516-
cli.set_follow_location(true);
3517-
return detail::redirect(cli, req, res, next_path);
3518-
}
3515+
} else {
3516+
Client cli(next_host.c_str());
3517+
cli.set_follow_location(true);
3518+
return detail::redirect(cli, req, res, next_path);
35193519
}
35203520
}
3521-
return false;
35223521
}
35233522

35243523
inline bool Client::write_request(Stream &strm, const Request &req,
35253524
bool last_connection) {
35263525
BufferStream bstrm;
35273526

35283527
// Request line
3529-
static std::regex re(
3528+
const static std::regex re(
35303529
R"(^([^:/?#]+://[^/?#]*)?([^?#]*(?:\?[^#]*)?(?:#.*)?))");
35313530

35323531
std::smatch m;
3533-
if (!regex_match(req.path, m, re)) {
3534-
return false;
3535-
}
3532+
if (!regex_match(req.path, m, re)) { return false; }
35363533

35373534
auto path = m[1].str() + detail::encode_url(m[2].str());
35383535

@@ -3656,9 +3653,7 @@ inline bool Client::process_request(Stream &strm, const Request &req,
36563653
Response &res, bool last_connection,
36573654
bool &connection_close) {
36583655
// Send request
3659-
if (!write_request(strm, req, last_connection)) {
3660-
return false;
3661-
}
3656+
if (!write_request(strm, req, last_connection)) { return false; }
36623657

36633658
// Receive response and headers
36643659
if (!read_response_line(strm, res) ||
@@ -4011,9 +4006,7 @@ inline void Client::set_follow_location(bool on) { follow_location_ = on; }
40114006

40124007
inline void Client::set_compress(bool on) { compress_ = on; }
40134008

4014-
inline void Client::set_interface(const char *intf) {
4015-
interface_ = intf;
4016-
}
4009+
inline void Client::set_interface(const char *intf) { interface_ = intf; }
40174010

40184011
/*
40194012
* SSL Implementation

0 commit comments

Comments
 (0)