Skip to content

Commit b6ee2a2

Browse files
committed
Fix port reuse problem
1 parent 2ba0e7a commit b6ee2a2

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

test/test.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10674,19 +10674,18 @@ TEST(VulnerabilityTest, CRLFInjection) {
1067410674
TEST(VulnerabilityTest, CRLFInjectionInHeaders) {
1067510675
auto server_thread = std::thread([] {
1067610676
auto srv = ::socket(AF_INET, SOCK_STREAM, 0);
10677-
int on = 1;
10678-
::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR,
10677+
10678+
httplib::default_socket_options(srv);
1067910679
#ifdef _WIN32
10680-
reinterpret_cast<const char *>(&on),
10681-
#else
10682-
&on,
10680+
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so
10681+
// remove the option.
10682+
detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
1068310683
#endif
10684-
sizeof(on));
1068510684

1068610685
sockaddr_in addr{};
1068710686
addr.sin_family = AF_INET;
1068810687
addr.sin_port = htons(PORT);
10689-
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
10688+
::inet_pton(AF_INET, HOST, &addr.sin_addr);
1069010689
::bind(srv, reinterpret_cast<sockaddr *>(&addr), sizeof(addr));
1069110690
::listen(srv, 1);
1069210691

@@ -10737,16 +10736,15 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) {
1073710736

1073810737
std::this_thread::sleep_for(std::chrono::milliseconds(200));
1073910738

10740-
auto cli = httplib::Client("127.0.0.1", PORT);
10739+
auto cli = httplib::Client(HOST, PORT);
1074110740

1074210741
auto headers = httplib::Headers{
1074310742
{"A", "B\r\n\r\nGET /pwned HTTP/1.1\r\nHost: 127.0.0.1:1234\r\n\r\n"},
1074410743
{"Connection", "keep-alive"}};
1074510744

1074610745
auto res = cli.Get("/hi", headers);
1074710746
EXPECT_FALSE(res);
10748-
10749-
if (res) { EXPECT_EQ(httplib::Error::InvalidHeaders, res.error()); }
10747+
EXPECT_EQ(httplib::Error::InvalidHeaders, res.error());
1075010748

1075110749
server_thread.join();
1075210750
}

0 commit comments

Comments
 (0)