Chromium Code Reviews| 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 "net/base/io_buffer.h" | |
| 15 #include "net/base/test_completion_callback.h" | |
| 16 #include "net/disk_cache/disk_cache.h" | |
| 17 #include "net/disk_cache/simple/simple_backend_impl.h" | |
| 18 #include "net/http/http_cache.h" | |
| 19 #include "net/http/http_response_headers.h" | |
| 20 #include "net/http/http_util.h" | |
| 21 | |
| 22 using disk_cache::Backend; | |
| 23 using disk_cache::Entry; | |
| 24 using disk_cache::SimpleBackendImpl; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 base::MessageLoopForIO* g_message_loop = nullptr; | |
| 29 | |
| 30 scoped_ptr<SimpleBackendImpl> CreateSimpleBackend( | |
| 31 const base::FilePath& cache_path) { | |
| 32 scoped_ptr<SimpleBackendImpl> cache_backend(new SimpleBackendImpl( | |
| 33 cache_path, 0, net::DISK_CACHE, g_message_loop->task_runner(), NULL)); | |
| 34 | |
| 35 net::TestCompletionCallback cb; | |
| 36 int rv = cache_backend->Init(cb.callback()); | |
| 37 if (cb.GetResult(rv) != net::OK) | |
| 38 return nullptr; | |
| 39 return cache_backend; | |
| 40 } | |
| 41 | |
| 42 // Print all of a cache's keys to stdout. | |
| 43 bool ListKeys(Backend* cache_backend) { | |
| 44 scoped_ptr<Backend::Iterator> entry_iterator = | |
| 45 cache_backend->CreateIterator(); | |
| 46 Entry* entry = nullptr; | |
| 47 net::TestCompletionCallback cb; | |
| 48 int rv = entry_iterator->OpenNextEntry(&entry, cb.callback()); | |
| 49 while (cb.GetResult(rv) == net::OK) { | |
| 50 std::string url = entry->GetKey(); | |
| 51 std::cout << url << std::endl; | |
| 52 entry->Close(); | |
| 53 entry = nullptr; | |
| 54 rv = entry_iterator->OpenNextEntry(&entry, cb.callback()); | |
| 55 } | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 // Print a key's stream to stdout. | |
| 60 bool GetKeyStream(Backend* cache_backend, const std::string& key, int index) { | |
| 61 if (index < 0 || index > 2) { | |
| 62 std::cerr << "Invalid stream index." << std::endl; | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 Entry* cache_entry; | |
| 67 net::TestCompletionCallback cb; | |
| 68 int rv = cache_backend->OpenEntry(key, &cache_entry, cb.callback()); | |
| 69 if (cb.GetResult(rv) != net::OK) { | |
| 70 std::cerr << "Couldn't find key's entry." << std::endl; | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 const int kInitBufferSize = 8192; | |
| 75 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer()); | |
| 76 buffer->SetCapacity(kInitBufferSize); | |
| 77 for (;;) { | |
|
gavinp
2016/02/23 15:38:15
Nit: I very slightly prefer "while (true)" here, b
gabadie
2016/02/23 15:43:51
Done.
| |
| 78 rv = cache_entry->ReadData(index, buffer->offset(), buffer.get(), | |
| 79 buffer->capacity() - buffer->offset(), | |
| 80 cb.callback()); | |
| 81 rv = cb.GetResult(rv); | |
| 82 if (rv < 0) { | |
| 83 cache_entry->Close(); | |
| 84 std::cerr << "Stream read error." << std::endl; | |
| 85 return false; | |
| 86 } | |
| 87 buffer->set_offset(buffer->offset() + rv); | |
| 88 if (rv == 0) { | |
|
gavinp
2016/02/23 15:38:15
Nit: you could lose these braces.
gabadie
2016/02/23 15:43:51
Done.
| |
| 89 break; | |
| 90 } | |
| 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 cache." << std::endl; | |
|
gavinp
2016/02/23 15:38:15
Nit: "a cache" or "the cache"
(yes, I'm telling y
gabadie
2016/02/23 15:43:51
Done.
| |
| 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 header)" << std::endl; | |
|
gavinp
2016/02/23 15:38:15
nit: headers
gabadie
2016/02/23 15:43:51
Done.
| |
| 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 base::CommandLine::StringVector args = command_line.GetArgs(); | |
| 152 if (args.size() < 3U) { | |
| 153 PrintHelp(); | |
| 154 return 1; | |
| 155 } | |
| 156 | |
| 157 base::FilePath cache_path(args[0]); | |
| 158 std::string cache_backend_type(args[1]); | |
| 159 std::string subcommand(args[2]); | |
| 160 | |
| 161 g_message_loop = &message_loop; | |
| 162 scoped_ptr<Backend> cache_backend; | |
| 163 if (cache_backend_type == "simple") { | |
| 164 cache_backend = CreateSimpleBackend(cache_path); | |
| 165 } else { | |
| 166 std::cerr << "Unknown cache type." << std::endl; | |
| 167 PrintHelp(); | |
| 168 return 1; | |
| 169 } | |
| 170 | |
| 171 if (!cache_backend) { | |
| 172 std::cerr << "Invalid cache." << std::endl; | |
| 173 PrintHelp(); | |
| 174 return 1; | |
| 175 } | |
| 176 | |
| 177 bool successful_command = false; | |
| 178 if (subcommand == "validate") { | |
| 179 if (args.size() != 3) { | |
| 180 PrintHelp(); | |
| 181 return 1; | |
| 182 } | |
| 183 successful_command = true; | |
| 184 } else if (subcommand == "list_keys") { | |
| 185 if (args.size() != 3) { | |
| 186 PrintHelp(); | |
| 187 return 1; | |
| 188 } | |
| 189 successful_command = ListKeys(cache_backend.get()); | |
| 190 } else if (subcommand == "get_stream") { | |
| 191 if (args.size() != 5) { | |
| 192 PrintHelp(); | |
| 193 return 1; | |
| 194 } | |
| 195 std::string key(args[3]); | |
| 196 int index = 0; | |
| 197 if (!base::StringToInt(args[4], &index)) { | |
| 198 std::cerr << "<index> must be an integer." << std::endl; | |
| 199 PrintHelp(); | |
| 200 return 1; | |
| 201 } | |
| 202 successful_command = GetKeyStream(cache_backend.get(), key, index); | |
| 203 } else if (subcommand == "delete_key") { | |
| 204 if (args.size() != 4) { | |
| 205 PrintHelp(); | |
| 206 return 1; | |
| 207 } | |
| 208 std::string key(args[3]); | |
| 209 successful_command = DeleteKey(cache_backend.get(), key); | |
| 210 } else { | |
| 211 std::cerr << "Unknown subcommand." << std::endl; | |
| 212 PrintHelp(); | |
| 213 } | |
| 214 return successful_command ? 0 : 1; | |
|
gavinp
2016/02/23 15:38:14
Nit: you could return !successful_command here.
gabadie
2016/02/23 15:43:51
Done.
| |
| 215 } | |
| OLD | NEW |