| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/flip_server/mem_cache.h" | 5 #include "net/tools/flip_server/mem_cache.h" |
| 6 | 6 |
| 7 #include <dirent.h> | 7 #include <dirent.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "net/tools/dump_cache/url_to_filename_encoder.h" | 22 #include "net/tools/dump_cache/url_to_filename_encoder.h" |
| 23 #include "net/tools/dump_cache/url_utilities.h" | 23 #include "net/tools/dump_cache/url_utilities.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 // The directory where cache locates); | 26 // The directory where cache locates); |
| 27 const char FLAGS_cache_base_dir[] = "."; | 27 const char FLAGS_cache_base_dir[] = "."; |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 namespace net { | 30 namespace net { |
| 31 | 31 |
| 32 void StoreBodyAndHeadersVisitor::ProcessBodyData(const char *input, | 32 void StoreBodyAndHeadersVisitor::ProcessBodyData(const char* input, |
| 33 size_t size) { | 33 size_t size) { |
| 34 body.append(input, size); | 34 body.append(input, size); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void StoreBodyAndHeadersVisitor::HandleHeaderError(BalsaFrame* framer) { | 37 void StoreBodyAndHeadersVisitor::HandleHeaderError(BalsaFrame* framer) { |
| 38 HandleError(); | 38 HandleError(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void StoreBodyAndHeadersVisitor::HandleHeaderWarning(BalsaFrame* framer) { | 41 void StoreBodyAndHeadersVisitor::HandleHeaderWarning(BalsaFrame* framer) { |
| 42 HandleError(); | 42 HandleError(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void StoreBodyAndHeadersVisitor::HandleChunkingError(BalsaFrame* framer) { | 45 void StoreBodyAndHeadersVisitor::HandleChunkingError(BalsaFrame* framer) { |
| 46 HandleError(); | 46 HandleError(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void StoreBodyAndHeadersVisitor::HandleBodyError(BalsaFrame* framer) { | 49 void StoreBodyAndHeadersVisitor::HandleBodyError(BalsaFrame* framer) { |
| 50 HandleError(); | 50 HandleError(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 FileData::FileData(const BalsaHeaders* headers, | 53 FileData::FileData(const BalsaHeaders* headers, |
| 54 const std::string& filename, | 54 const std::string& filename, |
| 55 const std::string& body) | 55 const std::string& body) |
| 56 : filename_(filename) | 56 : filename_(filename), body_(body) { |
| 57 , body_(body) { | |
| 58 if (headers) { | 57 if (headers) { |
| 59 headers_.reset(new BalsaHeaders); | 58 headers_.reset(new BalsaHeaders); |
| 60 headers_->CopyFrom(*headers); | 59 headers_->CopyFrom(*headers); |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 | 62 |
| 64 FileData::FileData() {} | 63 FileData::FileData() {} |
| 65 | 64 |
| 66 FileData::~FileData() {} | 65 FileData::~FileData() {} |
| 67 | 66 |
| 68 MemoryCache::MemoryCache() : cwd_(FLAGS_cache_base_dir) {} | 67 MemoryCache::MemoryCache() : cwd_(FLAGS_cache_base_dir) {} |
| 69 | 68 |
| 70 MemoryCache::~MemoryCache() { | 69 MemoryCache::~MemoryCache() { ClearFiles(); } |
| 71 ClearFiles(); | |
| 72 } | |
| 73 | 70 |
| 74 void MemoryCache::CloneFrom(const MemoryCache& mc) { | 71 void MemoryCache::CloneFrom(const MemoryCache& mc) { |
| 75 DCHECK_NE(this, &mc); | 72 DCHECK_NE(this, &mc); |
| 76 ClearFiles(); | 73 ClearFiles(); |
| 77 files_ = mc.files_; | 74 files_ = mc.files_; |
| 78 cwd_ = mc.cwd_; | 75 cwd_ = mc.cwd_; |
| 79 } | 76 } |
| 80 | 77 |
| 81 void MemoryCache::AddFiles() { | 78 void MemoryCache::AddFiles() { |
| 82 std::deque<std::string> paths; | 79 std::deque<std::string> paths; |
| 83 paths.push_back(cwd_ + "/GET_"); | 80 paths.push_back(cwd_ + "/GET_"); |
| 84 DIR* current_dir = NULL; | 81 DIR* current_dir = NULL; |
| 85 while (!paths.empty()) { | 82 while (!paths.empty()) { |
| 86 while (current_dir == NULL && !paths.empty()) { | 83 while (current_dir == NULL && !paths.empty()) { |
| 87 std::string current_dir_name = paths.front(); | 84 std::string current_dir_name = paths.front(); |
| 88 VLOG(1) << "Attempting to open dir: \"" << current_dir_name << "\""; | 85 VLOG(1) << "Attempting to open dir: \"" << current_dir_name << "\""; |
| 89 current_dir = opendir(current_dir_name.c_str()); | 86 current_dir = opendir(current_dir_name.c_str()); |
| 90 paths.pop_front(); | 87 paths.pop_front(); |
| 91 | 88 |
| 92 if (current_dir == NULL) { | 89 if (current_dir == NULL) { |
| 93 perror("Unable to open directory. "); | 90 perror("Unable to open directory. "); |
| 94 current_dir_name.clear(); | 91 current_dir_name.clear(); |
| 95 continue; | 92 continue; |
| 96 } | 93 } |
| 97 | 94 |
| 98 if (current_dir) { | 95 if (current_dir) { |
| 99 VLOG(1) << "Succeeded opening"; | 96 VLOG(1) << "Succeeded opening"; |
| 100 for (struct dirent* dir_data = readdir(current_dir); | 97 for (struct dirent* dir_data = readdir(current_dir); dir_data != NULL; |
| 101 dir_data != NULL; | |
| 102 dir_data = readdir(current_dir)) { | 98 dir_data = readdir(current_dir)) { |
| 103 std::string current_entry_name = | 99 std::string current_entry_name = |
| 104 current_dir_name + "/" + dir_data->d_name; | 100 current_dir_name + "/" + dir_data->d_name; |
| 105 if (dir_data->d_type == DT_REG) { | 101 if (dir_data->d_type == DT_REG) { |
| 106 VLOG(1) << "Found file: " << current_entry_name; | 102 VLOG(1) << "Found file: " << current_entry_name; |
| 107 ReadAndStoreFileContents(current_entry_name.c_str()); | 103 ReadAndStoreFileContents(current_entry_name.c_str()); |
| 108 } else if (dir_data->d_type == DT_DIR) { | 104 } else if (dir_data->d_type == DT_DIR) { |
| 109 VLOG(1) << "Found subdir: " << current_entry_name; | 105 VLOG(1) << "Found subdir: " << current_entry_name; |
| 110 if (std::string(dir_data->d_name) != "." && | 106 if (std::string(dir_data->d_name) != "." && |
| 111 std::string(dir_data->d_name) != "..") { | 107 std::string(dir_data->d_name) != "..") { |
| 112 VLOG(1) << "Adding to search path: " << current_entry_name; | 108 VLOG(1) << "Adding to search path: " << current_entry_name; |
| 113 paths.push_front(current_entry_name); | 109 paths.push_front(current_entry_name); |
| 114 } | 110 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 filename_contents[7] = '1'; | 147 filename_contents[7] = '1'; |
| 152 | 148 |
| 153 size_t pos = 0; | 149 size_t pos = 0; |
| 154 size_t old_pos = 0; | 150 size_t old_pos = 0; |
| 155 while (true) { | 151 while (true) { |
| 156 old_pos = pos; | 152 old_pos = pos; |
| 157 pos += framer.ProcessInput(filename_contents.data() + pos, | 153 pos += framer.ProcessInput(filename_contents.data() + pos, |
| 158 filename_contents.size() - pos); | 154 filename_contents.size() - pos); |
| 159 if (framer.Error() || pos == old_pos) { | 155 if (framer.Error() || pos == old_pos) { |
| 160 LOG(ERROR) << "Unable to make forward progress, or error" | 156 LOG(ERROR) << "Unable to make forward progress, or error" |
| 161 " framing file: " << filename; | 157 " framing file: " << filename; |
| 162 if (framer.Error()) { | 158 if (framer.Error()) { |
| 163 LOG(INFO) << "********************************************ERROR!"; | 159 LOG(INFO) << "********************************************ERROR!"; |
| 164 return; | 160 return; |
| 165 } | 161 } |
| 166 return; | 162 return; |
| 167 } | 163 } |
| 168 if (framer.MessageFullyRead()) { | 164 if (framer.MessageFullyRead()) { |
| 169 // If no Content-Length or Transfer-Encoding was captured in the | 165 // If no Content-Length or Transfer-Encoding was captured in the |
| 170 // file, then the rest of the data is the body. Many of the captures | 166 // file, then the rest of the data is the body. Many of the captures |
| 171 // from within Chrome don't have content-lengths. | 167 // from within Chrome don't have content-lengths. |
| 172 if (!visitor.body.length()) | 168 if (!visitor.body.length()) |
| 173 visitor.body = filename_contents.substr(pos); | 169 visitor.body = filename_contents.substr(pos); |
| 174 break; | 170 break; |
| 175 } | 171 } |
| 176 } | 172 } |
| 177 visitor.headers.RemoveAllOfHeader("content-length"); | 173 visitor.headers.RemoveAllOfHeader("content-length"); |
| 178 visitor.headers.RemoveAllOfHeader("transfer-encoding"); | 174 visitor.headers.RemoveAllOfHeader("transfer-encoding"); |
| 179 visitor.headers.RemoveAllOfHeader("connection"); | 175 visitor.headers.RemoveAllOfHeader("connection"); |
| 180 visitor.headers.AppendHeader("transfer-encoding", "chunked"); | 176 visitor.headers.AppendHeader("transfer-encoding", "chunked"); |
| 181 visitor.headers.AppendHeader("connection", "keep-alive"); | 177 visitor.headers.AppendHeader("connection", "keep-alive"); |
| 182 | 178 |
| 183 // Experiment with changing headers for forcing use of cached | 179 // Experiment with changing headers for forcing use of cached |
| 184 // versions of content. | 180 // versions of content. |
| 185 // TODO(mbelshe) REMOVE ME | 181 // TODO(mbelshe) REMOVE ME |
| 186 #if 0 | 182 #if 0 |
| 187 // TODO(mbelshe) append current date. | 183 // TODO(mbelshe) append current date. |
| 188 visitor.headers.RemoveAllOfHeader("date"); | 184 visitor.headers.RemoveAllOfHeader("date"); |
| 189 if (visitor.headers.HasHeader("expires")) { | 185 if (visitor.headers.HasHeader("expires")) { |
| 190 visitor.headers.RemoveAllOfHeader("expires"); | 186 visitor.headers.RemoveAllOfHeader("expires"); |
| 191 visitor.headers.AppendHeader("expires", | 187 visitor.headers.AppendHeader("expires", |
| 192 "Fri, 30 Aug, 2019 12:00:00 GMT"); | 188 "Fri, 30 Aug, 2019 12:00:00 GMT"); |
| 193 } | 189 } |
| 194 #endif | 190 #endif |
| 195 DCHECK_GE(std::string(filename).size(), cwd_.size() + 1); | 191 DCHECK_GE(std::string(filename).size(), cwd_.size() + 1); |
| 196 DCHECK_EQ(std::string(filename).substr(0, cwd_.size()), cwd_); | 192 DCHECK_EQ(std::string(filename).substr(0, cwd_.size()), cwd_); |
| 197 DCHECK_EQ(filename[cwd_.size()], '/'); | 193 DCHECK_EQ(filename[cwd_.size()], '/'); |
| 198 std::string filename_stripped = std::string(filename).substr(cwd_.size() + 1); | 194 std::string filename_stripped = std::string(filename).substr(cwd_.size() + 1); |
| 199 LOG(INFO) << "Adding file (" << visitor.body.length() << " bytes): " | 195 LOG(INFO) << "Adding file (" << visitor.body.length() |
| 200 << filename_stripped; | 196 << " bytes): " << filename_stripped; |
| 201 size_t slash_pos = filename_stripped.find('/'); | 197 size_t slash_pos = filename_stripped.find('/'); |
| 202 if (slash_pos == std::string::npos) { | 198 if (slash_pos == std::string::npos) { |
| 203 slash_pos = filename_stripped.size(); | 199 slash_pos = filename_stripped.size(); |
| 204 } | 200 } |
| 205 InsertFile(&visitor.headers, | 201 InsertFile( |
| 206 filename_stripped.substr(0, slash_pos), | 202 &visitor.headers, filename_stripped.substr(0, slash_pos), visitor.body); |
| 207 visitor.body); | |
| 208 } | 203 } |
| 209 | 204 |
| 210 FileData* MemoryCache::GetFileData(const std::string& filename) { | 205 FileData* MemoryCache::GetFileData(const std::string& filename) { |
| 211 Files::iterator fi = files_.end(); | 206 Files::iterator fi = files_.end(); |
| 212 if (EndsWith(filename, ".html", true)) { | 207 if (EndsWith(filename, ".html", true)) { |
| 213 fi = files_.find(filename.substr(0, filename.size() - 5) + ".http"); | 208 fi = files_.find(filename.substr(0, filename.size() - 5) + ".http"); |
| 214 } | 209 } |
| 215 if (fi == files_.end()) | 210 if (fi == files_.end()) |
| 216 fi = files_.find(filename); | 211 fi = files_.find(filename); |
| 217 | 212 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 248 } | 243 } |
| 249 | 244 |
| 250 void MemoryCache::ClearFiles() { | 245 void MemoryCache::ClearFiles() { |
| 251 for (Files::const_iterator i = files_.begin(); i != files_.end(); ++i) { | 246 for (Files::const_iterator i = files_.begin(); i != files_.end(); ++i) { |
| 252 delete i->second; | 247 delete i->second; |
| 253 } | 248 } |
| 254 files_.clear(); | 249 files_.clear(); |
| 255 } | 250 } |
| 256 | 251 |
| 257 } // namespace net | 252 } // namespace net |
| OLD | NEW |