| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 {kWssScheme, SCHEME_WITH_PORT}, // WebSocket secure. | 33 {kWssScheme, SCHEME_WITH_PORT}, // WebSocket secure. |
| 34 {kFileSystemScheme, SCHEME_WITHOUT_AUTHORITY}, | 34 {kFileSystemScheme, SCHEME_WITHOUT_AUTHORITY}, |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 const int kNumReferrerURLSchemes = 2; | 37 const int kNumReferrerURLSchemes = 2; |
| 38 const SchemeWithType kReferrerURLSchemes[kNumReferrerURLSchemes] = { | 38 const SchemeWithType kReferrerURLSchemes[kNumReferrerURLSchemes] = { |
| 39 {kHttpScheme, SCHEME_WITH_PORT}, | 39 {kHttpScheme, SCHEME_WITH_PORT}, |
| 40 {kHttpsScheme, SCHEME_WITH_PORT}, | 40 {kHttpsScheme, SCHEME_WITH_PORT}, |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 const int kNumPathURLSchemes = 6; |
| 44 const SchemeWithType kPathURLSchemes[kNumPathURLSchemes] = { |
| 45 {kAboutScheme, SCHEME_WITHOUT_PORT}, |
| 46 {kBlobScheme, SCHEME_WITHOUT_PORT}, |
| 47 {kContentScheme, SCHEME_WITHOUT_PORT}, |
| 48 {kContentIDScheme, SCHEME_WITHOUT_PORT}, |
| 49 {kDataScheme, SCHEME_WITHOUT_PORT}, |
| 50 {kJavaScriptScheme, SCHEME_WITHOUT_PORT}, |
| 51 }; |
| 52 |
| 43 // Lists of the currently installed standard and referrer schemes. These lists | 53 // Lists of the currently installed standard and referrer schemes. These lists |
| 44 // are lazily initialized by InitStandardSchemes and InitReferrerSchemes and are | 54 // are lazily initialized by InitStandardSchemes and InitReferrerSchemes and are |
| 45 // leaked on shutdown to prevent any destructors from being called that will | 55 // leaked on shutdown to prevent any destructors from being called that will |
| 46 // slow us down or cause problems. | 56 // slow us down or cause problems. |
| 47 std::vector<SchemeWithType>* standard_schemes = nullptr; | 57 std::vector<SchemeWithType>* standard_schemes = nullptr; |
| 48 std::vector<SchemeWithType>* referrer_schemes = nullptr; | 58 std::vector<SchemeWithType>* referrer_schemes = nullptr; |
| 59 std::vector<SchemeWithType>* path_schemes = nullptr; |
| 49 | 60 |
| 50 // See the LockSchemeRegistries declaration in the header. | 61 // See the LockSchemeRegistries declaration in the header. |
| 51 bool scheme_registries_locked = false; | 62 bool scheme_registries_locked = false; |
| 52 | 63 |
| 53 // This template converts a given character type to the corresponding | 64 // This template converts a given character type to the corresponding |
| 54 // StringPiece type. | 65 // StringPiece type. |
| 55 template<typename CHAR> struct CharToStringPiece { | 66 template<typename CHAR> struct CharToStringPiece { |
| 56 }; | 67 }; |
| 57 template<> struct CharToStringPiece<char> { | 68 template<> struct CharToStringPiece<char> { |
| 58 typedef base::StringPiece Piece; | 69 typedef base::StringPiece Piece; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 77 void InitStandardSchemes() { | 88 void InitStandardSchemes() { |
| 78 InitSchemes(&standard_schemes, kStandardURLSchemes, kNumStandardURLSchemes); | 89 InitSchemes(&standard_schemes, kStandardURLSchemes, kNumStandardURLSchemes); |
| 79 } | 90 } |
| 80 | 91 |
| 81 // Ensures that the referrer_schemes list is initialized, does nothing if | 92 // Ensures that the referrer_schemes list is initialized, does nothing if |
| 82 // it already has values. | 93 // it already has values. |
| 83 void InitReferrerSchemes() { | 94 void InitReferrerSchemes() { |
| 84 InitSchemes(&referrer_schemes, kReferrerURLSchemes, kNumReferrerURLSchemes); | 95 InitSchemes(&referrer_schemes, kReferrerURLSchemes, kNumReferrerURLSchemes); |
| 85 } | 96 } |
| 86 | 97 |
| 98 // Ensures that the path_schemes list is initialized, does nothing if |
| 99 // it already has values. |
| 100 void InitPathSchemes() { |
| 101 InitSchemes(&path_schemes, kPathURLSchemes, kNumPathURLSchemes); |
| 102 } |
| 103 |
| 87 // Given a string and a range inside the string, compares it to the given | 104 // Given a string and a range inside the string, compares it to the given |
| 88 // lower-case |compare_to| buffer. | 105 // lower-case |compare_to| buffer. |
| 89 template<typename CHAR> | 106 template<typename CHAR> |
| 90 inline bool DoCompareSchemeComponent(const CHAR* spec, | 107 inline bool DoCompareSchemeComponent(const CHAR* spec, |
| 91 const Component& component, | 108 const Component& component, |
| 92 const char* compare_to) { | 109 const char* compare_to) { |
| 93 if (!component.is_nonempty()) | 110 if (!component.is_nonempty()) |
| 94 return compare_to[0] == 0; // When component is empty, match empty scheme. | 111 return compare_to[0] == 0; // When component is empty, match empty scheme. |
| 95 return base::LowerCaseEqualsASCII( | 112 return base::LowerCaseEqualsASCII( |
| 96 typename CharToStringPiece<CHAR>::Piece( | 113 typename CharToStringPiece<CHAR>::Piece( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 118 } | 135 } |
| 119 return false; | 136 return false; |
| 120 } | 137 } |
| 121 | 138 |
| 122 template<typename CHAR> | 139 template<typename CHAR> |
| 123 bool DoIsStandard(const CHAR* spec, const Component& scheme, SchemeType* type) { | 140 bool DoIsStandard(const CHAR* spec, const Component& scheme, SchemeType* type) { |
| 124 InitStandardSchemes(); | 141 InitStandardSchemes(); |
| 125 return DoIsInSchemes(spec, scheme, type, *standard_schemes); | 142 return DoIsInSchemes(spec, scheme, type, *standard_schemes); |
| 126 } | 143 } |
| 127 | 144 |
| 145 template <typename CHAR> |
| 146 bool DoIsPath(const CHAR* spec, const Component& scheme, SchemeType* type) { |
| 147 InitPathSchemes(); |
| 148 return DoIsInSchemes(spec, scheme, type, *path_schemes); |
| 149 } |
| 128 | 150 |
| 129 template<typename CHAR> | 151 template<typename CHAR> |
| 130 bool DoFindAndCompareScheme(const CHAR* str, | 152 bool DoFindAndCompareScheme(const CHAR* str, |
| 131 int str_len, | 153 int str_len, |
| 132 const char* compare, | 154 const char* compare, |
| 133 Component* found_scheme) { | 155 Component* found_scheme) { |
| 134 // Before extracting scheme, canonicalize the URL to remove any whitespace. | 156 // Before extracting scheme, canonicalize the URL to remove any whitespace. |
| 135 // This matches the canonicalization done in DoCanonicalize function. | 157 // This matches the canonicalization done in DoCanonicalize function. |
| 136 RawCanonOutputT<CHAR> whitespace_buffer; | 158 RawCanonOutputT<CHAR> whitespace_buffer; |
| 137 int spec_len; | 159 int spec_len; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, | 232 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, |
| 211 charset_converter, output, output_parsed); | 233 charset_converter, output, output_parsed); |
| 212 | 234 |
| 213 } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) { | 235 } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) { |
| 214 // Mailto URLs are treated like standard URLs, with only a scheme, path, | 236 // Mailto URLs are treated like standard URLs, with only a scheme, path, |
| 215 // and query. | 237 // and query. |
| 216 ParseMailtoURL(spec, spec_len, &parsed_input); | 238 ParseMailtoURL(spec, spec_len, &parsed_input); |
| 217 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output, | 239 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output, |
| 218 output_parsed); | 240 output_parsed); |
| 219 | 241 |
| 220 } else { | 242 } else if (DoIsPath(spec, scheme, &unused_scheme_type)) { |
| 221 // "Weird" URLs like data: and javascript:. | 243 // "Weird" URLs like data: and javascript:. |
| 222 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input); | 244 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input); |
| 223 success = CanonicalizePathURL(spec, spec_len, parsed_input, output, | 245 success = CanonicalizePathURL(spec, spec_len, parsed_input, output, |
| 224 output_parsed); | 246 output_parsed); |
| 247 |
| 248 } else { |
| 249 // All other urls. |
| 250 ParseNonStandardURL(spec, spec_len, &parsed_input); |
| 251 success = CanonicalizeNonStandardURL( |
| 252 spec, spec_len, parsed_input, charset_converter, output, output_parsed); |
| 225 } | 253 } |
| 226 return success; | 254 return success; |
| 227 } | 255 } |
| 228 | 256 |
| 229 template<typename CHAR> | 257 template<typename CHAR> |
| 230 bool DoResolveRelative(const char* base_spec, | 258 bool DoResolveRelative(const char* base_spec, |
| 231 int base_spec_len, | 259 int base_spec_len, |
| 232 const Parsed& base_parsed, | 260 const Parsed& base_parsed, |
| 233 const CHAR* in_relative, | 261 const CHAR* in_relative, |
| 234 int in_relative_length, | 262 int in_relative_length, |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 return DoCompareSchemeComponent(spec, component, compare_to); | 650 return DoCompareSchemeComponent(spec, component, compare_to); |
| 623 } | 651 } |
| 624 | 652 |
| 625 bool CompareSchemeComponent(const base::char16* spec, | 653 bool CompareSchemeComponent(const base::char16* spec, |
| 626 const Component& component, | 654 const Component& component, |
| 627 const char* compare_to) { | 655 const char* compare_to) { |
| 628 return DoCompareSchemeComponent(spec, component, compare_to); | 656 return DoCompareSchemeComponent(spec, component, compare_to); |
| 629 } | 657 } |
| 630 | 658 |
| 631 } // namespace url | 659 } // namespace url |
| OLD | NEW |