diff --git a/httplib.h b/httplib.h index 984069f1..381f9d0e 100644 --- a/httplib.h +++ b/httplib.h @@ -15651,9 +15651,9 @@ inline bool Server::check_if_range(Request &req, const std::string &etag, // Weak ETags are not valid for If-Range (RFC 9110 Section 13.1.5) return false; } else { - // HTTP-date comparison + // HTTP-date must exactly match Last-Modified (RFC 9110 Section 13.1.5) auto t = detail::parse_http_date(val); - return (t != static_cast(-1) && mtime <= t); + return (t != static_cast(-1) && mtime == t); } }; diff --git a/test/test.cc b/test/test.cc index 7f977bea..7c9a7e2c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -27144,13 +27144,35 @@ TEST(ETagTest, IfRangeWithDate) { EXPECT_EQ(200, res3->status); EXPECT_EQ(content, res3->body); - // Range request with future If-Range date: should get 206 + // Range request with future If-Range date: should get 200 (full content) Headers h4 = {{"Range", "bytes=0-4"}, {"If-Range", "Sun, 01 Jan 2099 00:00:00 GMT"}}; auto res4 = cli.Get("/static/if_range_date_testfile.txt", h4); ASSERT_TRUE(res4); - EXPECT_EQ(206, res4->status); - EXPECT_EQ("ABCDE", res4->body); + EXPECT_EQ(200, res4->status); + EXPECT_EQ(content, res4->body); + +#if defined(__APPLE__) || defined(__linux__) + // File replaced by one with an older mtime: should get 200 (full content) + const std::string replaced = "abcdefghij9876543210"; + { + std::ofstream ofs(fname); + ofs << replaced; + ASSERT_TRUE(ofs.good()); + } + struct timeval times[2]; + times[0].tv_sec = time(nullptr); + times[0].tv_usec = 0; + times[1].tv_sec = time(nullptr) - 3600; + times[1].tv_usec = 0; + ASSERT_EQ(0, utimes(fname, times)); + + Headers h5 = {{"Range", "bytes=5-9"}, {"If-Range", last_modified}}; + auto res5 = cli.Get("/static/if_range_date_testfile.txt", h5); + ASSERT_TRUE(res5); + EXPECT_EQ(200, res5->status); + EXPECT_EQ(replaced, res5->body); +#endif svr.stop(); t.join();