Przeglądaj źródła

Compare the Range unit case-insensitively

RFC 9110 §14.1 makes range unit names case-insensitive, but
parse_range_header() matched "bytes=" exactly, so a request with
Range: Bytes=1-3 was answered with 416.
yhirose 2 dni temu
rodzic
commit
33d84a12b6
2 zmienionych plików z 8 dodań i 1 usunięć
  1. 1 1
      httplib.h
  2. 7 0
      test/test.cc

+ 1 - 1
httplib.h

@@ -9189,7 +9189,7 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try {
     return std::all_of(str.cbegin(), str.cend(), is_ascii_digit);
   };
 
-  if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) {
+  if (s.size() > 7 && case_ignore::equal(s.substr(0, 6), "bytes=")) {
     const auto pos = static_cast<size_t>(6);
     const auto len = static_cast<size_t>(s.size() - 6);
     auto all_valid_ranges = true;

+ 7 - 0
test/test.cc

@@ -2033,6 +2033,13 @@ TEST(ParseHeaderValueTest, Range) {
     EXPECT_EQ(0, ranges[0].first);
     EXPECT_EQ(-1, ranges[0].second);
   }
+
+  // RFC 9110 14.1: range unit names are case-insensitive.
+  for (const auto &s : {"Bytes=1-10", "BYTES=1-10", "bYtEs=1-10"}) {
+    Ranges ranges;
+    EXPECT_TRUE(detail::parse_range_header(s, ranges)) << s;
+    EXPECT_EQ((Ranges{{1, 10}}), ranges) << s;
+  }
 }
 
 TEST(ParseAcceptEncoding1, AcceptEncoding) {