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"
17 using base::IntToString
;
18 using base::StringPiece
;
24 QuicInMemoryCache::Response::Response() : response_type_(REGULAR_RESPONSE
) {}
26 QuicInMemoryCache::Response::~Response() {}
29 QuicInMemoryCache
* QuicInMemoryCache::GetInstance() {
30 return Singleton
<QuicInMemoryCache
>::get();
33 const QuicInMemoryCache::Response
* QuicInMemoryCache::GetResponse(
35 StringPiece path
) const {
36 ResponseMap::const_iterator it
= responses_
.find(GetKey(host
, path
));
37 if (it
== responses_
.end()) {
43 void QuicInMemoryCache::AddSimpleResponse(StringPiece host
,
46 StringPiece response_detail
,
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
,
60 const SpdyHeaderBlock
& response_headers
,
61 StringPiece response_body
) {
62 AddResponseImpl(host
, path
, REGULAR_RESPONSE
, response_headers
,
66 void QuicInMemoryCache::AddSpecialResponse(StringPiece host
,
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.";
83 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: "
85 FilePath
directory(FilePath::FromUTF8Unsafe(cache_directory
));
86 base::FileEnumerator
file_list(directory
,
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
) {
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] == '/') {
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(),
109 if (headers_end
< 1) {
110 LOG(DFATAL
) << "Headers invalid or empty, ignoring: " << file
;
115 HttpUtil::AssembleRawHeaders(file_contents
.data(), headers_end
);
117 scoped_refptr
<HttpResponseHeaders
> response_headers
=
118 new HttpResponseHeaders(raw_headers
);
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);
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(
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!";
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();