| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/quic/quic_in_memory_cache.h" | 5 #include "net/tools/quic/quic_in_memory_cache.h" |
| 6 | 6 |
| 7 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "net/tools/balsa/balsa_frame.h" |
| 11 #include "net/tools/balsa/balsa_headers.h" | 12 #include "net/tools/balsa/balsa_headers.h" |
| 13 #include "net/tools/balsa/noop_balsa_visitor.h" |
| 14 #include "net/tools/quic/spdy_utils.h" |
| 12 | 15 |
| 13 using base::FilePath; | 16 using base::FilePath; |
| 14 using base::StringPiece; | 17 using base::StringPiece; |
| 15 using std::string; | 18 using std::string; |
| 16 | 19 |
| 17 namespace net { | 20 namespace net { |
| 18 namespace tools { | 21 namespace tools { |
| 19 | 22 |
| 20 // Specifies the directory used during QuicInMemoryCache | 23 // Specifies the directory used during QuicInMemoryCache |
| 21 // construction to seed the cache. Cache directory can be | 24 // construction to seed the cache. Cache directory can be |
| (...skipping 23 matching lines...) Expand all Loading... |
| 45 bool done_framing() const { return done_framing_; } | 48 bool done_framing() const { return done_framing_; } |
| 46 const string& body() const { return body_; } | 49 const string& body() const { return body_; } |
| 47 | 50 |
| 48 private: | 51 private: |
| 49 bool done_framing_; | 52 bool done_framing_; |
| 50 string body_; | 53 string body_; |
| 51 }; | 54 }; |
| 52 | 55 |
| 53 } // namespace | 56 } // namespace |
| 54 | 57 |
| 58 QuicInMemoryCache::Response::Response() : response_type_(REGULAR_RESPONSE) {} |
| 59 |
| 60 QuicInMemoryCache::Response::~Response() {} |
| 61 |
| 55 // static | 62 // static |
| 56 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { | 63 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { |
| 57 return Singleton<QuicInMemoryCache>::get(); | 64 return Singleton<QuicInMemoryCache>::get(); |
| 58 } | 65 } |
| 59 | 66 |
| 60 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( | 67 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( |
| 61 StringPiece host, | 68 StringPiece host, |
| 62 StringPiece path) const { | 69 StringPiece path) const { |
| 63 ResponseMap::const_iterator it = responses_.find(GetKey(host, path)); | 70 ResponseMap::const_iterator it = responses_.find(GetKey(host, path)); |
| 64 if (it == responses_.end()) { | 71 if (it == responses_.end()) { |
| 65 return nullptr; | 72 return nullptr; |
| 66 } | 73 } |
| 67 return it->second; | 74 return it->second; |
| 68 } | 75 } |
| 69 | 76 |
| 70 void QuicInMemoryCache::AddSimpleResponse(StringPiece host, | 77 void QuicInMemoryCache::AddSimpleResponse(StringPiece host, |
| 71 StringPiece path, | 78 StringPiece path, |
| 72 int response_code, | 79 int response_code, |
| 73 StringPiece response_detail, | 80 StringPiece response_detail, |
| 74 StringPiece body) { | 81 StringPiece body) { |
| 75 BalsaHeaders response_headers; | 82 SpdyHeaderBlock response_headers; |
| 76 response_headers.SetRequestFirstlineFromStringPieces( | 83 response_headers[":version"] = "HTTP/1.1"; |
| 77 "HTTP/1.1", base::IntToString(response_code), response_detail); | 84 string status = base::IntToString(response_code) + " "; |
| 78 response_headers.AppendHeader("content-length", | 85 response_detail.AppendToString(&status); |
| 79 base::IntToString(body.length())); | 86 response_headers[":status"] = status; |
| 80 | 87 response_headers["content-length"] = base::IntToString(body.length()); |
| 81 AddResponse(host, path, response_headers, body); | 88 AddResponse(host, path, response_headers, body); |
| 82 } | 89 } |
| 83 | 90 |
| 84 void QuicInMemoryCache::AddResponse(StringPiece host, | 91 void QuicInMemoryCache::AddResponse(StringPiece host, |
| 85 StringPiece path, | 92 StringPiece path, |
| 86 const BalsaHeaders& response_headers, | 93 const SpdyHeaderBlock& response_headers, |
| 87 StringPiece response_body) { | 94 StringPiece response_body) { |
| 88 AddResponseImpl(host, path, REGULAR_RESPONSE, response_headers, | 95 AddResponseImpl(host, path, REGULAR_RESPONSE, response_headers, |
| 89 response_body); | 96 response_body); |
| 90 } | 97 } |
| 91 | 98 |
| 92 void QuicInMemoryCache::AddSpecialResponse(StringPiece host, | 99 void QuicInMemoryCache::AddSpecialResponse(StringPiece host, |
| 93 StringPiece path, | 100 StringPiece path, |
| 94 SpecialResponseType response_type) { | 101 SpecialResponseType response_type) { |
| 95 AddResponseImpl(host, path, response_type, BalsaHeaders(), ""); | 102 AddResponseImpl(host, path, response_type, SpdyHeaderBlock(), ""); |
| 96 } | 103 } |
| 97 | 104 |
| 98 QuicInMemoryCache::QuicInMemoryCache() { | 105 QuicInMemoryCache::QuicInMemoryCache() { |
| 99 Initialize(); | 106 Initialize(); |
| 100 } | 107 } |
| 101 | 108 |
| 102 void QuicInMemoryCache::ResetForTests() { | 109 void QuicInMemoryCache::ResetForTests() { |
| 103 STLDeleteValues(&responses_); | 110 STLDeleteValues(&responses_); |
| 104 Initialize(); | 111 Initialize(); |
| 105 } | 112 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 base.remove_prefix(7); | 175 base.remove_prefix(7); |
| 169 } | 176 } |
| 170 } | 177 } |
| 171 int path_start = base.find_first_of('/'); | 178 int path_start = base.find_first_of('/'); |
| 172 DCHECK_LT(0, path_start); | 179 DCHECK_LT(0, path_start); |
| 173 StringPiece host(base.substr(0, path_start)); | 180 StringPiece host(base.substr(0, path_start)); |
| 174 StringPiece path(base.substr(path_start)); | 181 StringPiece path(base.substr(path_start)); |
| 175 if (path[path.length() - 1] == ',') { | 182 if (path[path.length() - 1] == ',') { |
| 176 path.remove_suffix(1); | 183 path.remove_suffix(1); |
| 177 } | 184 } |
| 178 AddResponse(host, path, response_headers, caching_visitor.body()); | 185 AddResponse(host, path, |
| 186 SpdyUtils::ResponseHeadersToSpdyHeaders(response_headers), |
| 187 caching_visitor.body()); |
| 179 } | 188 } |
| 180 } | 189 } |
| 181 | 190 |
| 182 QuicInMemoryCache::~QuicInMemoryCache() { | 191 QuicInMemoryCache::~QuicInMemoryCache() { |
| 183 STLDeleteValues(&responses_); | 192 STLDeleteValues(&responses_); |
| 184 } | 193 } |
| 185 | 194 |
| 186 void QuicInMemoryCache::AddResponseImpl( | 195 void QuicInMemoryCache::AddResponseImpl( |
| 187 StringPiece host, | 196 StringPiece host, |
| 188 StringPiece path, | 197 StringPiece path, |
| 189 SpecialResponseType response_type, | 198 SpecialResponseType response_type, |
| 190 const BalsaHeaders& response_headers, | 199 const SpdyHeaderBlock& response_headers, |
| 191 StringPiece response_body) { | 200 StringPiece response_body) { |
| 192 string key = GetKey(host, path); | 201 string key = GetKey(host, path); |
| 193 VLOG(1) << "Adding response for: " << key; | 202 VLOG(1) << "Adding response for: " << key; |
| 194 if (ContainsKey(responses_, key)) { | 203 if (ContainsKey(responses_, key)) { |
| 195 LOG(DFATAL) << "Response for '" << key << "' already exists!"; | 204 LOG(DFATAL) << "Response for '" << key << "' already exists!"; |
| 196 return; | 205 return; |
| 197 } | 206 } |
| 198 Response* new_response = new Response(); | 207 Response* new_response = new Response(); |
| 199 new_response->set_response_type(response_type); | 208 new_response->set_response_type(response_type); |
| 200 new_response->set_headers(response_headers); | 209 new_response->set_headers(response_headers); |
| 201 new_response->set_body(response_body); | 210 new_response->set_body(response_body); |
| 202 responses_[key] = new_response; | 211 responses_[key] = new_response; |
| 203 } | 212 } |
| 204 | 213 |
| 205 string QuicInMemoryCache::GetKey(StringPiece host, StringPiece path) const { | 214 string QuicInMemoryCache::GetKey(StringPiece host, StringPiece path) const { |
| 206 return host.as_string() + path.as_string(); | 215 return host.as_string() + path.as_string(); |
| 207 } | 216 } |
| 208 | 217 |
| 209 } // namespace tools | 218 } // namespace tools |
| 210 } // namespace net | 219 } // namespace net |
| OLD | NEW |