We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / tools / quic / quic_in_memory_cache.cc
blob5cc3619139f558856a3171553a01cf7dce03d520
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/tools/quic/quic_in_memory_cache.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_util.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_util.h"
14 #include "net/spdy/spdy_http_utils.h"
16 using base::FilePath;
17 using base::IntToString;
18 using base::StringPiece;
19 using std::string;
21 namespace net {
22 namespace tools {
24 QuicInMemoryCache::Response::Response() : response_type_(REGULAR_RESPONSE) {}
26 QuicInMemoryCache::Response::~Response() {}
28 // static
29 QuicInMemoryCache* QuicInMemoryCache::GetInstance() {
30 return Singleton<QuicInMemoryCache>::get();
33 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse(
34 StringPiece host,
35 StringPiece path) const {
36 ResponseMap::const_iterator it = responses_.find(GetKey(host, path));
37 if (it == responses_.end()) {
38 return nullptr;
40 return it->second;
43 void QuicInMemoryCache::AddSimpleResponse(StringPiece host,
44 StringPiece path,
45 int response_code,
46 StringPiece response_detail,
47 StringPiece body) {
48 SpdyHeaderBlock response_headers;
49 response_headers[":version"] = "HTTP/1.1";
50 string status = IntToString(response_code) + " ";
51 response_detail.AppendToString(&status);
52 response_headers[":status"] = status;
53 response_headers["content-length"] =
54 IntToString(static_cast<int>(body.length()));
55 AddResponse(host, path, response_headers, body);
58 void QuicInMemoryCache::AddResponse(StringPiece host,
59 StringPiece path,
60 const SpdyHeaderBlock& response_headers,
61 StringPiece response_body) {
62 AddResponseImpl(host, path, REGULAR_RESPONSE, response_headers,
63 response_body);
66 void QuicInMemoryCache::AddSpecialResponse(StringPiece host,
67 StringPiece path,
68 SpecialResponseType response_type) {
69 AddResponseImpl(host, path, response_type, SpdyHeaderBlock(), "");
72 QuicInMemoryCache::QuicInMemoryCache() {}
74 void QuicInMemoryCache::ResetForTests() {
75 STLDeleteValues(&responses_);
78 void QuicInMemoryCache::InitializeFromDirectory(const string& cache_directory) {
79 if (cache_directory.empty()) {
80 LOG(DFATAL) << "cache_directory must not be empty.";
81 return;
83 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: "
84 << cache_directory;
85 FilePath directory(FilePath::FromUTF8Unsafe(cache_directory));
86 base::FileEnumerator file_list(directory,
87 true,
88 base::FileEnumerator::FILES);
90 for (FilePath file_iter = file_list.Next(); !file_iter.empty();
91 file_iter = file_list.Next()) {
92 // Need to skip files in .svn directories
93 if (file_iter.value().find(FILE_PATH_LITERAL("/.svn/")) != string::npos) {
94 continue;
97 // Tease apart filename into host and path.
98 string file = file_iter.AsUTF8Unsafe();
99 file.erase(0, cache_directory.length());
100 if (file[0] == '/') {
101 file.erase(0, 1);
104 string file_contents;
105 base::ReadFileToString(file_iter, &file_contents);
106 int file_len = static_cast<int>(file_contents.length());
107 int headers_end = HttpUtil::LocateEndOfHeaders(file_contents.data(),
108 file_len);
109 if (headers_end < 1) {
110 LOG(DFATAL) << "Headers invalid or empty, ignoring: " << file;
111 continue;
114 string raw_headers =
115 HttpUtil::AssembleRawHeaders(file_contents.data(), headers_end);
117 scoped_refptr<HttpResponseHeaders> response_headers =
118 new HttpResponseHeaders(raw_headers);
120 string base;
121 if (response_headers->GetNormalizedHeader("X-Original-Url", &base)) {
122 response_headers->RemoveHeader("X-Original-Url");
123 // Remove the protocol so we can add it below.
124 if (StartsWithASCII(base, "https://", false)) {
125 base = base.substr(8);
126 } else if (StartsWithASCII(base, "http://", false)) {
127 base = base.substr(7);
129 } else {
130 base = file;
133 size_t path_start = base.find_first_of('/');
134 StringPiece host(StringPiece(base).substr(0, path_start));
135 StringPiece path(StringPiece(base).substr(path_start));
136 if (path[path.length() - 1] == ',') {
137 path.remove_suffix(1);
140 StringPiece body(file_contents.data() + headers_end,
141 file_contents.size() - headers_end);
142 SpdyHeaderBlock header_block;
143 CreateSpdyHeadersFromHttpResponse(*response_headers, SPDY3, &header_block);
144 AddResponse(host, path, header_block, body);
148 QuicInMemoryCache::~QuicInMemoryCache() {
149 STLDeleteValues(&responses_);
152 void QuicInMemoryCache::AddResponseImpl(
153 StringPiece host,
154 StringPiece path,
155 SpecialResponseType response_type,
156 const SpdyHeaderBlock& response_headers,
157 StringPiece response_body) {
158 string key = GetKey(host, path);
159 VLOG(1) << "Adding response for: " << key;
160 if (ContainsKey(responses_, key)) {
161 LOG(DFATAL) << "Response for '" << key << "' already exists!";
162 return;
164 Response* new_response = new Response();
165 new_response->set_response_type(response_type);
166 new_response->set_headers(response_headers);
167 new_response->set_body(response_body);
168 responses_[key] = new_response;
171 string QuicInMemoryCache::GetKey(StringPiece host, StringPiece path) const {
172 return host.as_string() + path.as_string();
175 } // namespace tools
176 } // namespace net