OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dump_cache/cache_dumper.h" | 5 #include "net/tools/dump_cache/cache_dumper.h" |
6 | 6 |
7 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
8 #include "net/disk_cache/entry_impl.h" | 8 #include "net/disk_cache/entry_impl.h" |
9 #include "net/http/http_cache.h" | 9 #include "net/http/http_cache.h" |
10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
11 #include "net/http/http_response_info.h" | 11 #include "net/http/http_response_info.h" |
12 #include "net/tools/dump_cache/url_to_filename_encoder.h" | 12 #include "net/tools/dump_cache/url_to_filename_encoder.h" |
13 | 13 |
14 bool CacheDumper::CreateEntry(const std::string& key, | 14 bool CacheDumper::CreateEntry(const std::string& key, |
15 disk_cache::Entry** entry) { | 15 disk_cache::Entry** entry) { |
16 return cache_->CreateEntry(key, entry); | 16 return cache_->CreateEntry(key, entry); |
17 } | 17 } |
18 | 18 |
19 bool CacheDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, | 19 int CacheDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, |
20 net::IOBuffer* buf, int buf_len) { | 20 net::IOBuffer* buf, int buf_len, |
21 int written = entry->WriteData(index, offset, buf, buf_len, NULL, false); | 21 net::CompletionCallback* callback) { |
22 return written == buf_len; | 22 return entry->WriteData(index, offset, buf, buf_len, callback, false); |
23 } | 23 } |
24 | 24 |
25 void CacheDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, | 25 void CacheDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, |
26 base::Time last_modified) { | 26 base::Time last_modified) { |
27 if (entry) { | 27 if (entry) { |
28 static_cast<disk_cache::EntryImpl*>(entry)->SetTimes(last_used, | 28 static_cast<disk_cache::EntryImpl*>(entry)->SetTimes(last_used, |
29 last_modified); | 29 last_modified); |
30 entry->Close(); | 30 entry->Close(); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 // A version of CreateDirectory which supports lengthy filenames. | 34 // A version of CreateDirectory which supports lengthy filenames. |
35 // Returns true on success, false on failure. | 35 // Returns true on success, false on failure. |
36 bool SafeCreateDirectory(const std::wstring& path) { | 36 bool SafeCreateDirectory(const std::wstring& path) { |
37 #ifdef WIN32_LARGE_FILENAME_SUPPORT | 37 #ifdef WIN32_LARGE_FILENAME_SUPPORT |
38 // Due to large paths on windows, it can't simply do a | 38 // Due to large paths on windows, it can't simply do a |
39 // CreateDirectory("a/b/c"). Instead, create each subdirectory manually. | 39 // CreateDirectory("a/b/c"). Instead, create each subdirectory manually. |
40 bool rv = false; | 40 bool rv = false; |
41 std::wstring::size_type pos(0); | 41 std::wstring::size_type pos(0); |
42 std::wstring backslash(L"\\"); | 42 std::wstring backslash(L"\\"); |
43 | 43 |
44 // If the path starts with the long file header, skip over that | 44 // If the path starts with the long file header, skip over that |
45 const std::wstring kLargeFilenamePrefix(L"\\\\?\\"); | 45 const std::wstring kLargeFilenamePrefix(L"\\\\?\\"); |
46 std::wstring header(kLargeFilenamePrefix); | 46 std::wstring header(kLargeFilenamePrefix); |
47 if (path.find(header) == 0) | 47 if (path.find(header) == 0) |
48 pos = 4; | 48 pos = 4; |
49 | 49 |
50 // Create the subdirectories individually | 50 // Create the subdirectories individually |
51 while((pos = path.find(backslash, pos)) != std::wstring::npos) { | 51 while ((pos = path.find(backslash, pos)) != std::wstring::npos) { |
52 std::wstring subdir = path.substr(0, pos); | 52 std::wstring subdir = path.substr(0, pos); |
53 CreateDirectoryW(subdir.c_str(), NULL); | 53 CreateDirectoryW(subdir.c_str(), NULL); |
54 // we keep going even if directory creation failed. | 54 // we keep going even if directory creation failed. |
55 pos++; | 55 pos++; |
56 } | 56 } |
57 // Now create the full path | 57 // Now create the full path |
58 return CreateDirectoryW(path.c_str(), NULL) == TRUE; | 58 return CreateDirectoryW(path.c_str(), NULL) == TRUE; |
59 #else | 59 #else |
60 return file_util::CreateDirectory(path); | 60 return file_util::CreateDirectory(path); |
61 #endif | 61 #endif |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 output->append(name); | 134 output->append(name); |
135 output->append(": "); | 135 output->append(": "); |
136 output->append(value); | 136 output->append(value); |
137 output->append("\r\n"); | 137 output->append("\r\n"); |
138 } | 138 } |
139 | 139 |
140 // Mark the end of headers | 140 // Mark the end of headers |
141 output->append("\r\n"); | 141 output->append("\r\n"); |
142 } | 142 } |
143 | 143 |
144 bool DiskDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, | 144 int DiskDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, |
145 net::IOBuffer* buf, int buf_len) { | 145 net::IOBuffer* buf, int buf_len, |
| 146 net::CompletionCallback* callback) { |
146 if (!entry_) | 147 if (!entry_) |
147 return false; | 148 return 0; |
148 | 149 |
149 std::string headers; | 150 std::string headers; |
150 const char *data; | 151 const char *data; |
151 size_t len; | 152 size_t len; |
152 if (index == 0) { // Stream 0 is the headers. | 153 if (index == 0) { // Stream 0 is the headers. |
153 net::HttpResponseInfo response_info; | 154 net::HttpResponseInfo response_info; |
154 bool truncated; | 155 bool truncated; |
155 if (!net::HttpCache::ParseResponseInfo(buf->data(), buf_len, | 156 if (!net::HttpCache::ParseResponseInfo(buf->data(), buf_len, |
156 &response_info, &truncated)) | 157 &response_info, &truncated)) |
157 return false; | 158 return 0; |
158 | 159 |
159 // Skip this entry if it was truncated (results in an empty file). | 160 // Skip this entry if it was truncated (results in an empty file). |
160 if (truncated) | 161 if (truncated) |
161 return true; | 162 return buf_len; |
162 | 163 |
163 // Remove the size headers. | 164 // Remove the size headers. |
164 response_info.headers->RemoveHeader("transfer-encoding"); | 165 response_info.headers->RemoveHeader("transfer-encoding"); |
165 response_info.headers->RemoveHeader("content-length"); | 166 response_info.headers->RemoveHeader("content-length"); |
166 response_info.headers->RemoveHeader("x-original-url"); | 167 response_info.headers->RemoveHeader("x-original-url"); |
167 | 168 |
168 // Convert the headers into a string ending with LF. | 169 // Convert the headers into a string ending with LF. |
169 GetNormalizedHeaders(response_info, &headers); | 170 GetNormalizedHeaders(response_info, &headers); |
170 | 171 |
171 // Append a header for the original URL. | 172 // Append a header for the original URL. |
172 std::string url = entry_url_; | 173 std::string url = entry_url_; |
173 // strip off the "XXGET" which may be in the key. | 174 // strip off the "XXGET" which may be in the key. |
174 std::string::size_type pos(0); | 175 std::string::size_type pos(0); |
175 if ((pos = url.find("http")) != 0) { | 176 if ((pos = url.find("http")) != 0) { |
176 if (pos != std::string::npos) | 177 if (pos != std::string::npos) |
177 url = url.substr(pos); | 178 url = url.substr(pos); |
178 } | 179 } |
179 std::string x_original_url = "X-Original-Url: " + url + "\r\n"; | 180 std::string x_original_url = "X-Original-Url: " + url + "\r\n"; |
180 // we know that the last two bytes are CRLF. | 181 // we know that the last two bytes are CRLF. |
181 headers.replace(headers.length() - 2, 0, x_original_url); | 182 headers.replace(headers.length() - 2, 0, x_original_url); |
182 | 183 |
183 data = headers.c_str(); | 184 data = headers.c_str(); |
184 len = headers.size(); | 185 len = headers.size(); |
185 } else if (index == 1) { // Stream 1 is the data. | 186 } else if (index == 1) { // Stream 1 is the data. |
186 data = buf->data(); | 187 data = buf->data(); |
187 len = buf_len; | 188 len = buf_len; |
188 } | 189 } |
189 #ifdef WIN32_LARGE_FILENAME_SUPPORT | 190 #ifdef WIN32_LARGE_FILENAME_SUPPORT |
190 DWORD bytes; | 191 DWORD bytes; |
191 DWORD rv = WriteFile(entry_, data, len, &bytes, 0); | 192 if (!WriteFile(entry_, data, len, &bytes, 0)) |
192 return rv == TRUE && bytes == static_cast<DWORD>(len); | 193 return 0; |
| 194 |
| 195 return bytes; |
193 #else | 196 #else |
194 int bytes = fwrite(data, 1, len, entry_); | 197 return fwrite(data, 1, len, entry_); |
195 return bytes == len; | |
196 #endif | 198 #endif |
197 } | 199 } |
198 | 200 |
199 void DiskDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, | 201 void DiskDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, |
200 base::Time last_modified) { | 202 base::Time last_modified) { |
201 #ifdef WIN32_LARGE_FILENAME_SUPPORT | 203 #ifdef WIN32_LARGE_FILENAME_SUPPORT |
202 CloseHandle(entry_); | 204 CloseHandle(entry_); |
203 #else | 205 #else |
204 file_util::CloseFile(entry_); | 206 file_util::CloseFile(entry_); |
205 #endif | 207 #endif |
206 } | 208 } |
207 | 209 |
OLD | NEW |