yhirose 1 ماه پیش
والد
کامیت
7ae794a6bf
2فایلهای تغییر یافته به همراه27 افزوده شده و 0 حذف شده
  1. 4 0
      httplib.h
  2. 23 0
      test/test.cc

+ 4 - 0
httplib.h

@@ -9646,6 +9646,10 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
 
   Request req;
   req.start_time_ = std::chrono::steady_clock::now();
+  req.remote_addr = remote_addr;
+  req.remote_port = remote_port;
+  req.local_addr = local_addr;
+  req.local_port = local_port;
 
   Response res;
   res.version = "HTTP/1.1";

+ 23 - 0
test/test.cc

@@ -6843,6 +6843,29 @@ TEST(ServerRequestParsingTest, InvalidSpaceInURL) {
   EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
 }
 
+TEST(ServerRequestParsingTest, RemoteAddrSetOnBadRequest) {
+  Server svr;
+
+  svr.set_error_handler([&](const Request &req, Response & /*res*/) {
+    EXPECT_TRUE(!req.remote_addr.empty());
+  });
+
+  thread t = thread([&] { svr.listen(HOST, PORT); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    t.join();
+    ASSERT_FALSE(svr.is_running());
+  });
+
+  svr.wait_until_ready();
+
+  // Send an invalid request line to trigger Bad Request
+  const std::string bad_req = "BADMETHOD / HTTP/1.1\r\nHost: localhost\r\n\r\n";
+  std::string out;
+  ASSERT_TRUE(send_request(5, bad_req, &out));
+  EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
+}
+
 TEST(ServerRequestParsingTest, InvalidFieldValueContains_CR_LF_NUL) {
   std::string out;
   std::string request(