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 // URL filename encoder goals: | 5 // URL filename encoder goals: |
6 // | 6 // |
7 // 1. Allow URLs with arbitrary path-segment length, generating filenames | 7 // 1. Allow URLs with arbitrary path-segment length, generating filenames |
8 // with a maximum of 128 characters. | 8 // with a maximum of 128 characters. |
9 // 2. Provide a somewhat human readable filenames, for easy debugging flow. | 9 // 2. Provide a somewhat human readable filenames, for easy debugging flow. |
10 // 3. Provide reverse-mapping from filenames back to URLs. | 10 // 3. Provide reverse-mapping from filenames back to URLs. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 namespace net { | 87 namespace net { |
88 | 88 |
89 // Helper class for converting a URL into a filename. | 89 // Helper class for converting a URL into a filename. |
90 class UrlToFilenameEncoder { | 90 class UrlToFilenameEncoder { |
91 public: | 91 public: |
92 // Given a |url| and a |base_path|, returns a filename which represents this | 92 // Given a |url| and a |base_path|, returns a filename which represents this |
93 // |url|. |url| may include URL escaping such as %21 for ! | 93 // |url|. |url| may include URL escaping such as %21 for ! |
94 // |legacy_escape| indicates that this function should use the old-style | 94 // |legacy_escape| indicates that this function should use the old-style |
95 // of encoding. | 95 // of encoding. |
96 // TODO(mbelshe): delete the legacy_escape code. | 96 // TODO(mbelshe): delete the legacy_escape code. |
97 static std::string Encode(const std::string& url, std::string base_path, | 97 static std::string Encode(const std::string& url, |
| 98 std::string base_path, |
98 bool legacy_escape) { | 99 bool legacy_escape) { |
99 std::string filename; | 100 std::string filename; |
100 if (!legacy_escape) { | 101 if (!legacy_escape) { |
101 std::string url_no_scheme = UrlUtilities::GetUrlHostPath(url); | 102 std::string url_no_scheme = UrlUtilities::GetUrlHostPath(url); |
102 EncodeSegment(base_path, url_no_scheme, '/', &filename); | 103 EncodeSegment(base_path, url_no_scheme, '/', &filename); |
103 #ifdef WIN32 | 104 #ifdef WIN32 |
104 ReplaceAll(&filename, "/", "\\"); | 105 ReplaceAll(&filename, "/", "\\"); |
105 #endif | 106 #endif |
106 } else { | 107 } else { |
107 std::string clean_url(url); | 108 std::string clean_url(url); |
108 if (clean_url.length() && clean_url[clean_url.length()-1] == '/') | 109 if (clean_url.length() && clean_url[clean_url.length() - 1] == '/') |
109 clean_url.append("index.html"); | 110 clean_url.append("index.html"); |
110 | 111 |
111 std::string host = UrlUtilities::GetUrlHost(clean_url); | 112 std::string host = UrlUtilities::GetUrlHost(clean_url); |
112 filename.append(base_path); | 113 filename.append(base_path); |
113 filename.append(host); | 114 filename.append(host); |
114 #ifdef WIN32 | 115 #ifdef WIN32 |
115 filename.append("\\"); | 116 filename.append("\\"); |
116 #else | 117 #else |
117 filename.append("/"); | 118 filename.append("/"); |
118 #endif | 119 #endif |
(...skipping 24 matching lines...) Expand all Loading... |
143 return filename; | 144 return filename; |
144 } | 145 } |
145 | 146 |
146 // Rewrite HTML in a form that the SPDY in-memory server | 147 // Rewrite HTML in a form that the SPDY in-memory server |
147 // can read. | 148 // can read. |
148 // |filename_prefix| is prepended without escaping. | 149 // |filename_prefix| is prepended without escaping. |
149 // |escaped_ending| is the URL to be encoded into a filename. It may have URL | 150 // |escaped_ending| is the URL to be encoded into a filename. It may have URL |
150 // escaped characters (like %21 for !). | 151 // escaped characters (like %21 for !). |
151 // |dir_separator| is "/" on Unix, "\" on Windows. | 152 // |dir_separator| is "/" on Unix, "\" on Windows. |
152 // |encoded_filename| is the resultant filename. | 153 // |encoded_filename| is the resultant filename. |
153 static void EncodeSegment( | 154 static void EncodeSegment(const std::string& filename_prefix, |
154 const std::string& filename_prefix, | 155 const std::string& escaped_ending, |
155 const std::string& escaped_ending, | 156 char dir_separator, |
156 char dir_separator, | 157 std::string* encoded_filename); |
157 std::string* encoded_filename); | |
158 | 158 |
159 // Decodes a filename that was encoded with EncodeSegment, | 159 // Decodes a filename that was encoded with EncodeSegment, |
160 // yielding back the original URL. | 160 // yielding back the original URL. |
161 static bool Decode(const std::string& encoded_filename, | 161 static bool Decode(const std::string& encoded_filename, |
162 char dir_separator, | 162 char dir_separator, |
163 std::string* decoded_url); | 163 std::string* decoded_url); |
164 | 164 |
165 static const char kEscapeChar; | 165 static const char kEscapeChar; |
166 static const char kTruncationChar; | 166 static const char kTruncationChar; |
167 static const size_t kMaximumSubdirectoryLength; | 167 static const size_t kMaximumSubdirectoryLength; |
168 | 168 |
169 friend class UrlToFilenameEncoderTest; | 169 friend class UrlToFilenameEncoderTest; |
170 | 170 |
171 private: | 171 private: |
172 // Appends a segment of the path, special-casing "." and "..", and | 172 // Appends a segment of the path, special-casing "." and "..", and |
173 // ensuring that the segment does not exceed the path length. If it does, | 173 // ensuring that the segment does not exceed the path length. If it does, |
174 // it chops the end off the segment, writes the segment with a separator of | 174 // it chops the end off the segment, writes the segment with a separator of |
175 // ",-/", and then rewrites segment to contain just the truncated piece so | 175 // ",-/", and then rewrites segment to contain just the truncated piece so |
176 // it can be used in the next iteration. | 176 // it can be used in the next iteration. |
177 // |segment| is a read/write parameter containing segment to write | 177 // |segment| is a read/write parameter containing segment to write |
178 // Note: this should not be called with empty segment. | 178 // Note: this should not be called with empty segment. |
179 static void AppendSegment(std::string* segment, std::string* dest); | 179 static void AppendSegment(std::string* segment, std::string* dest); |
180 | 180 |
181 // Allow reading of old slurped files. | 181 // Allow reading of old slurped files. |
182 static std::string LegacyEscape(const std::string& path); | 182 static std::string LegacyEscape(const std::string& path); |
183 | 183 |
184 // Replace all instances of |from| within |str| as |to|. | 184 // Replace all instances of |from| within |str| as |to|. |
185 static void ReplaceAll(std::string* str, const std::string& from, | 185 static void ReplaceAll(std::string* str, |
| 186 const std::string& from, |
186 const std::string& to) { | 187 const std::string& to) { |
187 std::string::size_type pos(0); | 188 std::string::size_type pos(0); |
188 while ((pos = str->find(from, pos)) != std::string::npos) { | 189 while ((pos = str->find(from, pos)) != std::string::npos) { |
189 str->replace(pos, from.size(), to); | 190 str->replace(pos, from.size(), to); |
190 pos += from.size(); | 191 pos += from.size(); |
191 } | 192 } |
192 } | 193 } |
193 | 194 |
194 // Replace all instances of "/" with "\" in |path|. | 195 // Replace all instances of "/" with "\" in |path|. |
195 static void ConvertToSlashes(std::string* path) { | 196 static void ConvertToSlashes(std::string* path) { |
196 const std::string slash("/"); | 197 const std::string slash("/"); |
197 const std::string backslash("\\"); | 198 const std::string backslash("\\"); |
198 ReplaceAll(path, slash, backslash); | 199 ReplaceAll(path, slash, backslash); |
199 } | 200 } |
200 | 201 |
201 // Replace all instances of "\\" with "%5C%5C" in |path|. | 202 // Replace all instances of "\\" with "%5C%5C" in |path|. |
202 static void StripDoubleSlashes(std::string* path) { | 203 static void StripDoubleSlashes(std::string* path) { |
203 const std::string doubleslash("\\\\"); | 204 const std::string doubleslash("\\\\"); |
204 const std::string escaped_doubleslash("%5C%5C"); | 205 const std::string escaped_doubleslash("%5C%5C"); |
205 ReplaceAll(path, doubleslash, escaped_doubleslash); | 206 ReplaceAll(path, doubleslash, escaped_doubleslash); |
206 } | 207 } |
207 }; | 208 }; |
208 | 209 |
209 } // namespace net | 210 } // namespace net |
210 | 211 |
211 #endif // NET_TOOLS_DUMP_CACHE_URL_TO_FILENAME_ENCODER_H_ | 212 #endif // NET_TOOLS_DUMP_CACHE_URL_TO_FILENAME_ENCODER_H_ |
OLD | NEW |