Skip to content

Commit bc9251e

Browse files
committed
Work around incompatibility in <regex> in libc++
libc++ (the implementation of the C++ standard library usually used by Clang) throws an exception for the regex used by parse_headers before this patch for certain strings. Work around this by simplifying the regex and parsing the header lines "by hand" partially. I have repro'd this problem with Xcode 11.1 which I believe uses libc++ version 8. This may be a bug in libc++ as I can't see why the regex would result in asymptotic run-time complexity for any strings. However, it may take a while for libc++ to be fixed and for everyone to migrate to it, so it makes sense to work around it in this codebase for now.
1 parent a9e942d commit bc9251e

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

httplib.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ inline bool read_headers(Stream &strm, Headers &headers) {
15661566
// the left or right side of the header value:
15671567
// - https://stackoverflow.com/questions/50179659/
15681568
// - https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
1569-
static std::regex re(R"((.+?):[\t ]*(.+?)[\t ]*\r\n)");
1569+
static std::regex re(R"((.+?):[\t ]*(.+))");
15701570

15711571
const auto bufsiz = 2048;
15721572
char buf[bufsiz];
@@ -1575,9 +1575,23 @@ inline bool read_headers(Stream &strm, Headers &headers) {
15751575

15761576
for (;;) {
15771577
if (!line_reader.getline()) { return false; }
1578-
if (!strcmp(line_reader.ptr(), "\r\n")) { break; }
1578+
const char *end = line_reader.ptr() + line_reader.size();
1579+
auto erase_last_char = [&](char c) {
1580+
if (line_reader.ptr() == end || end[-1] != c) {
1581+
return false;
1582+
}
1583+
end--;
1584+
return true;
1585+
};
1586+
if (!erase_last_char('\n')) { continue; }
1587+
if (!erase_last_char('\r')) { continue; }
1588+
1589+
// Blank line indicates end of headers.
1590+
if (line_reader.ptr() == end) { break; }
1591+
1592+
while (erase_last_char(' ') || erase_last_char('\t')) {}
15791593
std::cmatch m;
1580-
if (std::regex_match(line_reader.ptr(), m, re)) {
1594+
if (std::regex_match(line_reader.ptr(), end, m, re)) {
15811595
auto key = std::string(m[1]);
15821596
auto val = std::string(m[2]);
15831597
headers.emplace(key, val);

test/test.cc

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,30 @@ TEST_F(ServerTest, MultipartFormDataGzip) {
17661766
}
17671767
#endif
17681768

1769+
// Sends a raw request to a server listening at HOST:PORT.
1770+
static bool send_request(time_t read_timeout_sec, const std::string& req) {
1771+
auto client_sock =
1772+
detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5);
1773+
1774+
if (client_sock == INVALID_SOCKET) { return false; }
1775+
1776+
return detail::process_and_close_socket(
1777+
true, client_sock, 1, read_timeout_sec, 0,
1778+
[&](Stream& strm, bool /*last_connection*/,
1779+
bool &/*connection_close*/) -> bool {
1780+
if (req.size() !=
1781+
static_cast<size_t>(strm.write(req.data(), req.size()))) {
1782+
return false;
1783+
}
1784+
1785+
char buf[512];
1786+
1787+
detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
1788+
while (line_reader.getline()) {}
1789+
return true;
1790+
});
1791+
}
1792+
17691793
TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
17701794
Server svr;
17711795
std::string header_value;
@@ -1782,34 +1806,47 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
17821806

17831807
// Only space and horizontal tab are whitespace. Make sure other whitespace-
17841808
// like characters are not treated the same - use vertical tab and escape.
1785-
auto client_sock =
1786-
detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5);
1787-
ASSERT_TRUE(client_sock != INVALID_SOCKET);
17881809
const std::string req =
17891810
"GET /validate-ws-in-headers HTTP/1.1\r\n"
17901811
"foo: \t \v bar \e\t \r\n"
17911812
"Connection: close\r\n"
17921813
"\r\n";
17931814

1794-
bool process_ok = detail::process_and_close_socket(
1795-
true, client_sock, 1, 5, 0,
1796-
[&](Stream& strm, bool /*last_connection*/,
1797-
bool &/*connection_close*/) -> bool {
1798-
if (req.size() !=
1799-
static_cast<size_t>(strm.write(req.data(), req.size()))) {
1800-
return false;
1801-
}
1815+
ASSERT_TRUE(send_request(5, req));
1816+
svr.stop();
1817+
t.join();
1818+
EXPECT_EQ(header_value, "\v bar \e");
1819+
}
18021820

1803-
char buf[512];
1821+
TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
1822+
Server svr;
1823+
svr.Get("/hi",
1824+
[&](const Request & /*req*/, Response &res) {
1825+
res.set_content("ok", "text/plain");
1826+
});
18041827

1805-
detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
1806-
while (line_reader.getline()) {}
1807-
return true;
1808-
});
1809-
ASSERT_TRUE(process_ok);
1828+
// Server read timeout must be longer than the client read timeout for the
1829+
// bug to reproduce, probably to force the server to process a request
1830+
// without a trailing blank line.
1831+
const time_t client_read_timeout_sec = 1;
1832+
svr.set_read_timeout(client_read_timeout_sec + 1, 0);
1833+
bool listen_thread_ok = false;
1834+
thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
1835+
while (!svr.is_running()) {
1836+
msleep(1);
1837+
}
1838+
1839+
// A certain header line causes an exception if the header property is parsed
1840+
// naively with a single regex. This occurs with libc++ but not libstdc++.
1841+
const std::string req =
1842+
"GET /hi HTTP/1.1\r\n"
1843+
" : "
1844+
" ";
1845+
1846+
ASSERT_TRUE(send_request(client_read_timeout_sec, req));
18101847
svr.stop();
18111848
t.join();
1812-
EXPECT_EQ(header_value, "\v bar \e");
1849+
EXPECT_TRUE(listen_thread_ok);
18131850
}
18141851

18151852
class ServerTestWithAI_PASSIVE : public ::testing::Test {

0 commit comments

Comments
 (0)