Change QuicMemoryCacheBackend::InitializeDirectory() to handle cases where the specified directory contains only files, not subdirectories naming hosts. gfe-relnote: n/a - tools only https://bugs.chromium.org/p/chromium/issues/detail?id=1034470 PiperOrigin-RevId: 285875350 Change-Id: I352d71066f37d9a02280633ad439ad784c2531f2
diff --git a/quic/tools/quic_memory_cache_backend.cc b/quic/tools/quic_memory_cache_backend.cc index ddf94fe..c7ac912 100644 --- a/quic/tools/quic_memory_cache_backend.cc +++ b/quic/tools/quic_memory_cache_backend.cc
@@ -106,7 +106,12 @@ quiche::QuicheStringPiece base) { DCHECK(base[0] != '/') << base; size_t path_start = base.find_first_of('/'); - DCHECK(path_start != quiche::QuicheStringPiece::npos) << base; + if (path_start == quiche::QuicheStringPiece::npos) { + host_ = std::string(base); + path_ = ""; + return; + } + host_ = std::string(base.substr(0, path_start)); size_t query_start = base.find_first_of(','); if (query_start > 0) {
diff --git a/quic/tools/quic_memory_cache_backend_test.cc b/quic/tools/quic_memory_cache_backend_test.cc index c028e41..e4b8d8e 100644 --- a/quic/tools/quic_memory_cache_backend_test.cc +++ b/quic/tools/quic_memory_cache_backend_test.cc
@@ -4,6 +4,7 @@ #include "net/third_party/quiche/src/quic/tools/quic_memory_cache_backend.h" +#include "net/third_party/quiche/src/quic/platform/api/quic_file_utils.h" #include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h" #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" #include "net/third_party/quiche/src/quic/tools/quic_backend_response.h" @@ -115,6 +116,32 @@ EXPECT_LT(0U, response->body().length()); } +TEST_F(QuicMemoryCacheBackendTest, UsesOriginalUrlOnly) { + // Tests that if the URL cannot be inferred correctly from the path + // because the directory does not include the hostname, that the + // X-Original-Url header's value will be used. + std::string dir; + std::string path = "map.html"; + for (const std::string& file : ReadFileContents(CacheDirectory())) { + if (quiche::QuicheTextUtils::EndsWithIgnoreCase(file, "map.html")) { + dir = file; + dir.erase(dir.length() - path.length() - 1); + break; + } + } + ASSERT_NE("", dir); + + cache_.InitializeBackend(dir); + const Response* response = + cache_.GetResponse("test.example.com", "/site_map.html"); + ASSERT_TRUE(response); + ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); + EXPECT_EQ("200", response->headers().find(":status")->second); + // Connection headers are not valid in HTTP/2. + EXPECT_FALSE(QuicContainsKey(response->headers(), "connection")); + EXPECT_LT(0U, response->body().length()); +} + TEST_F(QuicMemoryCacheBackendTest, DefaultResponse) { // Verify GetResponse returns nullptr when no default is set. const Response* response = cache_.GetResponse("www.google.com", "/");