| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include <iostream> | |
| 6 | |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "net/base/io_buffer.h" | |
| 16 #include "net/base/test_completion_callback.h" | |
| 17 #include "net/disk_cache/disk_cache.h" | |
| 18 #include "net/disk_cache/simple/simple_backend_impl.h" | |
| 19 #include "net/http/http_cache.h" | |
| 20 #include "net/http/http_response_headers.h" | |
| 21 #include "net/http/http_util.h" | |
| 22 | |
| 23 using disk_cache::Backend; | |
| 24 using disk_cache::Entry; | |
| 25 using disk_cache::SimpleBackendImpl; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 base::MessageLoopForIO* g_message_loop = nullptr; | |
| 30 | |
| 31 scoped_ptr<SimpleBackendImpl> CreateSimpleBackend( | |
| 32 const base::FilePath& cache_path) { | |
| 33 scoped_ptr<SimpleBackendImpl> cache_backend(new SimpleBackendImpl( | |
| 34 cache_path, 0, net::DISK_CACHE, g_message_loop->task_runner(), NULL)); | |
| 35 | |
| 36 net::TestCompletionCallback cb; | |
| 37 int rv = cache_backend->Init(cb.callback()); | |
| 38 if (cb.GetResult(rv) != net::OK) | |
| 39 return nullptr; | |
| 40 return cache_backend; | |
| 41 } | |
| 42 | |
| 43 // Print all of a cache's keys to stdout. | |
| 44 bool ListKeys(Backend* cache_backend) { | |
| 45 scoped_ptr<Backend::Iterator> entry_iterator = | |
| 46 cache_backend->CreateIterator(); | |
| 47 Entry* entry = nullptr; | |
| 48 net::TestCompletionCallback cb; | |
| 49 int rv = entry_iterator->OpenNextEntry(&entry, cb.callback()); | |
| 50 while (cb.GetResult(rv) == net::OK) { | |
| 51 std::string url = entry->GetKey(); | |
| 52 std::cout << url << std::endl; | |
| 53 entry->Close(); | |
| 54 entry = nullptr; | |
| 55 rv = entry_iterator->OpenNextEntry(&entry, cb.callback()); | |
| 56 } | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 // Print a key's stream to stdout. | |
| 61 bool GetKeyStream(Backend* cache_backend, const std::string& key, int index) { | |
| 62 if (index < 0 || index > 2) { | |
| 63 std::cerr << "Invalid stream index." << std::endl; | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 Entry* cache_entry; | |
| 68 net::TestCompletionCallback cb; | |
| 69 int rv = cache_backend->OpenEntry(key, &cache_entry, cb.callback()); | |
| 70 if (cb.GetResult(rv) != net::OK) { | |
| 71 std::cerr << "Couldn't find key's entry." << std::endl; | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 const int kInitBufferSize = 8192; | |
| 76 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer()); | |
| 77 buffer->SetCapacity(kInitBufferSize); | |
| 78 while (true) { | |
| 79 rv = cache_entry->ReadData(index, buffer->offset(), buffer.get(), | |
| 80 buffer->capacity() - buffer->offset(), | |
| 81 cb.callback()); | |
| 82 rv = cb.GetResult(rv); | |
| 83 if (rv < 0) { | |
| 84 cache_entry->Close(); | |
| 85 std::cerr << "Stream read error." << std::endl; | |
| 86 return false; | |
| 87 } | |
| 88 buffer->set_offset(buffer->offset() + rv); | |
| 89 if (rv == 0) | |
| 90 break; | |
| 91 buffer->SetCapacity(buffer->offset() * 2); | |
| 92 } | |
| 93 cache_entry->Close(); | |
| 94 if (index == 0) { | |
| 95 net::HttpResponseInfo response_info; | |
| 96 bool truncated_response_info = false; | |
| 97 net::HttpCache::ParseResponseInfo(buffer->StartOfBuffer(), buffer->offset(), | |
| 98 &response_info, &truncated_response_info); | |
| 99 if (truncated_response_info) { | |
| 100 std::cerr << "Truncated HTTP response." << std::endl; | |
| 101 return false; | |
| 102 } | |
| 103 std::cout << net::HttpUtil::ConvertHeadersBackToHTTPResponse( | |
| 104 response_info.headers->raw_headers()); | |
| 105 } else { | |
| 106 std::cout.write(buffer->StartOfBuffer(), buffer->offset()); | |
| 107 } | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 // Delete a specified key from the cache. | |
| 112 bool DeleteKey(Backend* cache_backend, const std::string& key) { | |
| 113 net::TestCompletionCallback cb; | |
| 114 int rv = cache_backend->DoomEntry(key, cb.callback()); | |
| 115 if (cb.GetResult(rv) != net::OK) { | |
| 116 std::cerr << "Couldn't delete key." << std::endl; | |
| 117 return false; | |
| 118 } | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 // Print the command line help. | |
| 123 void PrintHelp() { | |
| 124 std::cout << "cachetool <cache_path> <cache_backend_type> <subcommand> ..." | |
| 125 << std::endl | |
| 126 << std::endl; | |
| 127 std::cout << "Available cache back-end types: simple"; | |
| 128 std::cout << "Available subcommands:" << std::endl; | |
| 129 std::cout << " validate: Verify that the cache can be opened and return, " | |
| 130 << "confirming the cache exists and is of the right type." | |
| 131 << std::endl; | |
| 132 std::cout << " list_keys: List all keys in the cache." << std::endl; | |
| 133 std::cout << " get_stream <key> <index>: Print a particular stream for a" | |
| 134 << " given key." << std::endl; | |
| 135 std::cout << " delete_key <key>: Delete key from cache." << std::endl; | |
| 136 std::cout << "Expected values of <index> are:" << std::endl; | |
| 137 std::cout << " 0 (HTTP response headers)" << std::endl; | |
| 138 std::cout << " 1 (transport encoded content)" << std::endl; | |
| 139 std::cout << " 2 (compiled content)" << std::endl; | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| 143 | |
| 144 int main(int argc, char* argv[]) { | |
| 145 base::AtExitManager at_exit_manager; | |
| 146 base::MessageLoopForIO message_loop; | |
| 147 base::CommandLine::Init(argc, argv); | |
| 148 const base::CommandLine& command_line = | |
| 149 *base::CommandLine::ForCurrentProcess(); | |
| 150 | |
| 151 #if defined(OS_WIN) | |
| 152 std::vector<std::string> args; | |
| 153 { | |
| 154 base::CommandLine::StringVector wide_args = command_line.GetArgs(); | |
| 155 for (const auto& arg : wide_args) { | |
| 156 args.push_back(base::WideToUTF8(arg)); | |
| 157 } | |
| 158 } | |
| 159 #else | |
| 160 base::CommandLine::StringVector args = command_line.GetArgs(); | |
| 161 #endif | |
| 162 if (args.size() < 3U) { | |
| 163 PrintHelp(); | |
| 164 return 1; | |
| 165 } | |
| 166 | |
| 167 base::FilePath cache_path(args[0]); | |
| 168 std::string cache_backend_type(args[1]); | |
| 169 std::string subcommand(args[2]); | |
| 170 | |
| 171 g_message_loop = &message_loop; | |
| 172 scoped_ptr<Backend> cache_backend; | |
| 173 if (cache_backend_type == "simple") { | |
| 174 cache_backend = CreateSimpleBackend(cache_path); | |
| 175 } else { | |
| 176 std::cerr << "Unknown cache type." << std::endl; | |
| 177 PrintHelp(); | |
| 178 return 1; | |
| 179 } | |
| 180 | |
| 181 if (!cache_backend) { | |
| 182 std::cerr << "Invalid cache." << std::endl; | |
| 183 PrintHelp(); | |
| 184 return 1; | |
| 185 } | |
| 186 | |
| 187 bool successful_command = false; | |
| 188 if (subcommand == "validate") { | |
| 189 if (args.size() != 3) { | |
| 190 PrintHelp(); | |
| 191 return 1; | |
| 192 } | |
| 193 successful_command = true; | |
| 194 } else if (subcommand == "list_keys") { | |
| 195 if (args.size() != 3) { | |
| 196 PrintHelp(); | |
| 197 return 1; | |
| 198 } | |
| 199 successful_command = ListKeys(cache_backend.get()); | |
| 200 } else if (subcommand == "get_stream") { | |
| 201 if (args.size() != 5) { | |
| 202 PrintHelp(); | |
| 203 return 1; | |
| 204 } | |
| 205 std::string key(args[3]); | |
| 206 int index = 0; | |
| 207 if (!base::StringToInt(args[4], &index)) { | |
| 208 std::cerr << "<index> must be an integer." << std::endl; | |
| 209 PrintHelp(); | |
| 210 return 1; | |
| 211 } | |
| 212 successful_command = GetKeyStream(cache_backend.get(), key, index); | |
| 213 } else if (subcommand == "delete_key") { | |
| 214 if (args.size() != 4) { | |
| 215 PrintHelp(); | |
| 216 return 1; | |
| 217 } | |
| 218 std::string key(args[3]); | |
| 219 successful_command = DeleteKey(cache_backend.get(), key); | |
| 220 } else { | |
| 221 std::cerr << "Unknown subcommand." << std::endl; | |
| 222 PrintHelp(); | |
| 223 } | |
| 224 return !successful_command; | |
| 225 } | |
| OLD | NEW |