Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "url/url_util.h" | 5 #include "url/url_util.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "url/url_canon_internal.h" | 11 #include "url/url_canon_internal.h" |
| 12 #include "url/url_file.h" | 12 #include "url/url_file.h" |
| 13 #include "url/url_util_internal.h" | 13 #include "url/url_util_internal.h" |
| 14 | 14 |
| 15 namespace url { | 15 namespace url { |
| 16 | 16 |
| 17 const char kFileScheme[] = "file"; | |
| 18 const char kFileSystemScheme[] = "filesystem"; | |
| 19 const char kMailtoScheme[] = "mailto"; | |
| 20 | |
| 21 namespace { | 17 namespace { |
| 22 | 18 |
| 23 // ASCII-specific tolower. The standard library's tolower is locale sensitive, | 19 // ASCII-specific tolower. The standard library's tolower is locale sensitive, |
| 24 // so we don't want to use it here. | 20 // so we don't want to use it here. |
| 25 template<class Char> | 21 template<class Char> |
| 26 inline Char ToLowerASCII(Char c) { | 22 inline Char ToLowerASCII(Char c) { |
| 27 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; | 23 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
| 28 } | 24 } |
| 29 | 25 |
| 30 // Backend for LowerCaseEqualsASCII. | 26 // Backend for LowerCaseEqualsASCII. |
| 31 template<typename Iter> | 27 template<typename Iter> |
| 32 inline bool DoLowerCaseEqualsASCII(Iter a_begin, Iter a_end, const char* b) { | 28 inline bool DoLowerCaseEqualsASCII(Iter a_begin, Iter a_end, const char* b) { |
| 33 for (Iter it = a_begin; it != a_end; ++it, ++b) { | 29 for (Iter it = a_begin; it != a_end; ++it, ++b) { |
| 34 if (!*b || ToLowerASCII(*it) != *b) | 30 if (!*b || ToLowerASCII(*it) != *b) |
| 35 return false; | 31 return false; |
| 36 } | 32 } |
| 37 return *b == 0; | 33 return *b == 0; |
| 38 } | 34 } |
| 39 | 35 |
| 40 const int kNumStandardURLSchemes = 8; | 36 const int kNumStandardURLSchemes = 8; |
| 41 const char* kStandardURLSchemes[kNumStandardURLSchemes] = { | 37 const char* kStandardURLSchemes[kNumStandardURLSchemes] = { |
| 42 "http", | 38 "http", |
| 43 "https", | 39 "https", |
| 44 kFileScheme, // Yes, file urls can have a hostname! | 40 url::kFileScheme, // Yes, file urls can have a hostname! |
|
blundell
2014/05/28 11:21:49
nit: it looks like this code is already in the url
Sungmann Cho
2014/05/28 11:54:59
Done.
| |
| 45 "ftp", | 41 "ftp", |
| 46 "gopher", | 42 "gopher", |
| 47 "ws", // WebSocket. | 43 "ws", // WebSocket. |
| 48 "wss", // WebSocket secure. | 44 "wss", // WebSocket secure. |
| 49 kFileSystemScheme, | 45 url::kFileSystemScheme, |
| 50 }; | 46 }; |
| 51 | 47 |
| 52 // List of the currently installed standard schemes. This list is lazily | 48 // List of the currently installed standard schemes. This list is lazily |
| 53 // initialized by InitStandardSchemes and is leaked on shutdown to prevent | 49 // initialized by InitStandardSchemes and is leaked on shutdown to prevent |
| 54 // any destructors from being called that will slow us down or cause problems. | 50 // any destructors from being called that will slow us down or cause problems. |
| 55 std::vector<const char*>* standard_schemes = NULL; | 51 std::vector<const char*>* standard_schemes = NULL; |
| 56 | 52 |
| 57 // See the LockStandardSchemes declaration in the header. | 53 // See the LockStandardSchemes declaration in the header. |
| 58 bool standard_schemes_locked = false; | 54 bool standard_schemes_locked = false; |
| 59 | 55 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 } | 150 } |
| 155 #endif | 151 #endif |
| 156 | 152 |
| 157 Component scheme; | 153 Component scheme; |
| 158 if (!ExtractScheme(spec, spec_len, &scheme)) | 154 if (!ExtractScheme(spec, spec_len, &scheme)) |
| 159 return false; | 155 return false; |
| 160 | 156 |
| 161 // This is the parsed version of the input URL, we have to canonicalize it | 157 // This is the parsed version of the input URL, we have to canonicalize it |
| 162 // before storing it in our object. | 158 // before storing it in our object. |
| 163 bool success; | 159 bool success; |
| 164 if (DoCompareSchemeComponent(spec, scheme, kFileScheme)) { | 160 if (DoCompareSchemeComponent(spec, scheme, url::kFileScheme)) { |
| 165 // File URLs are special. | 161 // File URLs are special. |
| 166 ParseFileURL(spec, spec_len, &parsed_input); | 162 ParseFileURL(spec, spec_len, &parsed_input); |
| 167 success = CanonicalizeFileURL(spec, spec_len, parsed_input, | 163 success = CanonicalizeFileURL(spec, spec_len, parsed_input, |
| 168 charset_converter, output, output_parsed); | 164 charset_converter, output, output_parsed); |
| 169 } else if (DoCompareSchemeComponent(spec, scheme, kFileSystemScheme)) { | 165 } else if (DoCompareSchemeComponent(spec, scheme, url::kFileSystemScheme)) { |
| 170 // Filesystem URLs are special. | 166 // Filesystem URLs are special. |
| 171 ParseFileSystemURL(spec, spec_len, &parsed_input); | 167 ParseFileSystemURL(spec, spec_len, &parsed_input); |
| 172 success = CanonicalizeFileSystemURL(spec, spec_len, parsed_input, | 168 success = CanonicalizeFileSystemURL(spec, spec_len, parsed_input, |
| 173 charset_converter, output, | 169 charset_converter, output, |
| 174 output_parsed); | 170 output_parsed); |
| 175 | 171 |
| 176 } else if (DoIsStandard(spec, scheme)) { | 172 } else if (DoIsStandard(spec, scheme)) { |
| 177 // All "normal" URLs. | 173 // All "normal" URLs. |
| 178 ParseStandardURL(spec, spec_len, &parsed_input); | 174 ParseStandardURL(spec, spec_len, &parsed_input); |
| 179 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, | 175 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, |
| 180 charset_converter, output, output_parsed); | 176 charset_converter, output, output_parsed); |
| 181 | 177 |
| 182 } else if (DoCompareSchemeComponent(spec, scheme, kMailtoScheme)) { | 178 } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) { |
| 183 // Mailto are treated like a standard url with only a scheme, path, query | 179 // Mailto are treated like a standard url with only a scheme, path, query |
| 184 ParseMailtoURL(spec, spec_len, &parsed_input); | 180 ParseMailtoURL(spec, spec_len, &parsed_input); |
| 185 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output, | 181 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output, |
| 186 output_parsed); | 182 output_parsed); |
| 187 | 183 |
| 188 } else { | 184 } else { |
| 189 // "Weird" URLs like data: and javascript: | 185 // "Weird" URLs like data: and javascript: |
| 190 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input); | 186 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input); |
| 191 success = CanonicalizePathURL(spec, spec_len, parsed_input, output, | 187 success = CanonicalizePathURL(spec, spec_len, parsed_input, output, |
| 192 output_parsed); | 188 output_parsed); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 // ref). | 324 // ref). |
| 329 Replacements<CHAR> replacements_no_scheme = replacements; | 325 Replacements<CHAR> replacements_no_scheme = replacements; |
| 330 replacements_no_scheme.SetScheme(NULL, Component()); | 326 replacements_no_scheme.SetScheme(NULL, Component()); |
| 331 return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(), | 327 return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(), |
| 332 recanonicalized_parsed, replacements_no_scheme, | 328 recanonicalized_parsed, replacements_no_scheme, |
| 333 charset_converter, output, out_parsed); | 329 charset_converter, output, out_parsed); |
| 334 } | 330 } |
| 335 | 331 |
| 336 // If we get here, then we know the scheme doesn't need to be replaced, so can | 332 // If we get here, then we know the scheme doesn't need to be replaced, so can |
| 337 // just key off the scheme in the spec to know how to do the replacements. | 333 // just key off the scheme in the spec to know how to do the replacements. |
| 338 if (DoCompareSchemeComponent(spec, parsed.scheme, kFileScheme)) { | 334 if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileScheme)) { |
| 339 return ReplaceFileURL(spec, parsed, replacements, charset_converter, output, | 335 return ReplaceFileURL(spec, parsed, replacements, charset_converter, output, |
| 340 out_parsed); | 336 out_parsed); |
| 341 } | 337 } |
| 342 if (DoCompareSchemeComponent(spec, parsed.scheme, kFileSystemScheme)) { | 338 if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileSystemScheme)) { |
| 343 return ReplaceFileSystemURL(spec, parsed, replacements, charset_converter, | 339 return ReplaceFileSystemURL(spec, parsed, replacements, charset_converter, |
| 344 output, out_parsed); | 340 output, out_parsed); |
| 345 } | 341 } |
| 346 if (DoIsStandard(spec, parsed.scheme)) { | 342 if (DoIsStandard(spec, parsed.scheme)) { |
| 347 return ReplaceStandardURL(spec, parsed, replacements, charset_converter, | 343 return ReplaceStandardURL(spec, parsed, replacements, charset_converter, |
| 348 output, out_parsed); | 344 output, out_parsed); |
| 349 } | 345 } |
| 350 if (DoCompareSchemeComponent(spec, parsed.scheme, kMailtoScheme)) { | 346 if (DoCompareSchemeComponent(spec, parsed.scheme, url::kMailToScheme)) { |
| 351 return ReplaceMailtoURL(spec, parsed, replacements, output, out_parsed); | 347 return ReplaceMailtoURL(spec, parsed, replacements, output, out_parsed); |
| 352 } | 348 } |
| 353 | 349 |
| 354 // Default is a path URL. | 350 // Default is a path URL. |
| 355 return ReplacePathURL(spec, parsed, replacements, output, out_parsed); | 351 return ReplacePathURL(spec, parsed, replacements, output, out_parsed); |
| 356 } | 352 } |
| 357 | 353 |
| 358 } // namespace | 354 } // namespace |
| 359 | 355 |
| 360 void Initialize() { | 356 void Initialize() { |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 return DoCompareSchemeComponent(spec, component, compare_to); | 574 return DoCompareSchemeComponent(spec, component, compare_to); |
| 579 } | 575 } |
| 580 | 576 |
| 581 bool CompareSchemeComponent(const base::char16* spec, | 577 bool CompareSchemeComponent(const base::char16* spec, |
| 582 const Component& component, | 578 const Component& component, |
| 583 const char* compare_to) { | 579 const char* compare_to) { |
| 584 return DoCompareSchemeComponent(spec, component, compare_to); | 580 return DoCompareSchemeComponent(spec, component, compare_to); |
| 585 } | 581 } |
| 586 | 582 |
| 587 } // namespace url | 583 } // namespace url |
| OLD | NEW |