Parcourir la source

Match an If-Range date exactly against Last-Modified (#2707)

* Match an If-Range date exactly against Last-Modified

* Shorten the If-Range date comments

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
Leng Niu il y a 1 jour
Parent
commit
4e6226d7af
2 fichiers modifiés avec 27 ajouts et 5 suppressions
  1. 2 2
      httplib.h
  2. 25 3
      test/test.cc

+ 2 - 2
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<time_t>(-1) && mtime <= t);
+        return (t != static_cast<time_t>(-1) && mtime == t);
       }
     };
 

+ 25 - 3
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();