| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base/net_util.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 } | 78 } |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Check if the script of a language can be 'safely' mixed with | 82 // Check if the script of a language can be 'safely' mixed with |
| 83 // Latin letters in the ASCII range. | 83 // Latin letters in the ASCII range. |
| 84 bool IsCompatibleWithASCIILetters(const std::string& lang) { | 84 bool IsCompatibleWithASCIILetters(const std::string& lang) { |
| 85 // For now, just list Chinese, Japanese and Korean (positive list). | 85 // For now, just list Chinese, Japanese and Korean (positive list). |
| 86 // An alternative is negative-listing (languages using Greek and | 86 // An alternative is negative-listing (languages using Greek and |
| 87 // Cyrillic letters), but it can be more dangerous. | 87 // Cyrillic letters), but it can be more dangerous. |
| 88 return !lang.substr(0, 2).compare("zh") || | 88 return !lang.substr(0, 2).compare("zh") || !lang.substr(0, 2).compare("ja") || |
| 89 !lang.substr(0, 2).compare("ja") || | |
| 90 !lang.substr(0, 2).compare("ko"); | 89 !lang.substr(0, 2).compare("ko"); |
| 91 } | 90 } |
| 92 | 91 |
| 93 typedef std::map<std::string, icu::UnicodeSet*> LangToExemplarSetMap; | 92 typedef std::map<std::string, icu::UnicodeSet*> LangToExemplarSetMap; |
| 94 | 93 |
| 95 class LangToExemplarSet { | 94 class LangToExemplarSet { |
| 96 public: | 95 public: |
| 97 static LangToExemplarSet* GetInstance() { | 96 static LangToExemplarSet* GetInstance() { |
| 98 return Singleton<LangToExemplarSet>::get(); | 97 return Singleton<LangToExemplarSet>::get(); |
| 99 } | 98 } |
| 100 | 99 |
| 101 private: | 100 private: |
| 102 LangToExemplarSetMap map; | 101 LangToExemplarSetMap map; |
| 103 LangToExemplarSet() { } | 102 LangToExemplarSet() {} |
| 104 ~LangToExemplarSet() { | 103 ~LangToExemplarSet() { |
| 105 STLDeleteContainerPairSecondPointers(map.begin(), map.end()); | 104 STLDeleteContainerPairSecondPointers(map.begin(), map.end()); |
| 106 } | 105 } |
| 107 | 106 |
| 108 friend class Singleton<LangToExemplarSet>; | 107 friend class Singleton<LangToExemplarSet>; |
| 109 friend struct DefaultSingletonTraits<LangToExemplarSet>; | 108 friend struct DefaultSingletonTraits<LangToExemplarSet>; |
| 110 friend bool GetExemplarSetForLang(const std::string&, icu::UnicodeSet**); | 109 friend bool GetExemplarSetForLang(const std::string&, icu::UnicodeSet**); |
| 111 friend void SetExemplarSetForLang(const std::string&, icu::UnicodeSet*); | 110 friend void SetExemplarSetForLang(const std::string&, icu::UnicodeSet*); |
| 112 | 111 |
| 113 DISALLOW_COPY_AND_ASSIGN(LangToExemplarSet); | 112 DISALLOW_COPY_AND_ASSIGN(LangToExemplarSet); |
| 114 }; | 113 }; |
| 115 | 114 |
| 116 bool GetExemplarSetForLang(const std::string& lang, | 115 bool GetExemplarSetForLang(const std::string& lang, |
| 117 icu::UnicodeSet** lang_set) { | 116 icu::UnicodeSet** lang_set) { |
| 118 const LangToExemplarSetMap& map = LangToExemplarSet::GetInstance()->map; | 117 const LangToExemplarSetMap& map = LangToExemplarSet::GetInstance()->map; |
| 119 LangToExemplarSetMap::const_iterator pos = map.find(lang); | 118 LangToExemplarSetMap::const_iterator pos = map.find(lang); |
| 120 if (pos != map.end()) { | 119 if (pos != map.end()) { |
| 121 *lang_set = pos->second; | 120 *lang_set = pos->second; |
| 122 return true; | 121 return true; |
| 123 } | 122 } |
| 124 return false; | 123 return false; |
| 125 } | 124 } |
| 126 | 125 |
| 127 void SetExemplarSetForLang(const std::string& lang, | 126 void SetExemplarSetForLang(const std::string& lang, icu::UnicodeSet* lang_set) { |
| 128 icu::UnicodeSet* lang_set) { | |
| 129 LangToExemplarSetMap& map = LangToExemplarSet::GetInstance()->map; | 127 LangToExemplarSetMap& map = LangToExemplarSet::GetInstance()->map; |
| 130 map.insert(std::make_pair(lang, lang_set)); | 128 map.insert(std::make_pair(lang, lang_set)); |
| 131 } | 129 } |
| 132 | 130 |
| 133 static base::LazyInstance<base::Lock>::Leaky | 131 static base::LazyInstance<base::Lock>::Leaky g_lang_set_lock = |
| 134 g_lang_set_lock = LAZY_INSTANCE_INITIALIZER; | 132 LAZY_INSTANCE_INITIALIZER; |
| 135 | 133 |
| 136 // Returns true if all the characters in component_characters are used by | 134 // Returns true if all the characters in component_characters are used by |
| 137 // the language |lang|. | 135 // the language |lang|. |
| 138 bool IsComponentCoveredByLang(const icu::UnicodeSet& component_characters, | 136 bool IsComponentCoveredByLang(const icu::UnicodeSet& component_characters, |
| 139 const std::string& lang) { | 137 const std::string& lang) { |
| 140 CR_DEFINE_STATIC_LOCAL( | 138 CR_DEFINE_STATIC_LOCAL(const icu::UnicodeSet, kASCIILetters, ('a', 'z')); |
| 141 const icu::UnicodeSet, kASCIILetters, ('a', 'z')); | |
| 142 icu::UnicodeSet* lang_set = NULL; | 139 icu::UnicodeSet* lang_set = NULL; |
| 143 // We're called from both the UI thread and the history thread. | 140 // We're called from both the UI thread and the history thread. |
| 144 { | 141 { |
| 145 base::AutoLock lock(g_lang_set_lock.Get()); | 142 base::AutoLock lock(g_lang_set_lock.Get()); |
| 146 if (!GetExemplarSetForLang(lang, &lang_set)) { | 143 if (!GetExemplarSetForLang(lang, &lang_set)) { |
| 147 UErrorCode status = U_ZERO_ERROR; | 144 UErrorCode status = U_ZERO_ERROR; |
| 148 ULocaleData* uld = ulocdata_open(lang.c_str(), &status); | 145 ULocaleData* uld = ulocdata_open(lang.c_str(), &status); |
| 149 // TODO(jungshik) Turn this check on when the ICU data file is | 146 // TODO(jungshik) Turn this check on when the ICU data file is |
| 150 // rebuilt with the minimal subset of locale data for languages | 147 // rebuilt with the minimal subset of locale data for languages |
| 151 // to which Chrome is not localized but which we offer in the list | 148 // to which Chrome is not localized but which we offer in the list |
| 152 // of languages selectable for Accept-Languages. With the rebuilt ICU | 149 // of languages selectable for Accept-Languages. With the rebuilt ICU |
| 153 // data, ulocdata_open never should fall back to the default locale. | 150 // data, ulocdata_open never should fall back to the default locale. |
| 154 // (issue 2078) | 151 // (issue 2078) |
| 155 // DCHECK(U_SUCCESS(status) && status != U_USING_DEFAULT_WARNING); | 152 // DCHECK(U_SUCCESS(status) && status != U_USING_DEFAULT_WARNING); |
| 156 if (U_SUCCESS(status) && status != U_USING_DEFAULT_WARNING) { | 153 if (U_SUCCESS(status) && status != U_USING_DEFAULT_WARNING) { |
| 157 lang_set = reinterpret_cast<icu::UnicodeSet *>( | 154 lang_set = reinterpret_cast<icu::UnicodeSet*>(ulocdata_getExemplarSet( |
| 158 ulocdata_getExemplarSet(uld, NULL, 0, | 155 uld, NULL, 0, ULOCDATA_ES_STANDARD, &status)); |
| 159 ULOCDATA_ES_STANDARD, &status)); | |
| 160 // If |lang| is compatible with ASCII Latin letters, add them. | 156 // If |lang| is compatible with ASCII Latin letters, add them. |
| 161 if (IsCompatibleWithASCIILetters(lang)) | 157 if (IsCompatibleWithASCIILetters(lang)) |
| 162 lang_set->addAll(kASCIILetters); | 158 lang_set->addAll(kASCIILetters); |
| 163 } else { | 159 } else { |
| 164 lang_set = new icu::UnicodeSet(1, 0); | 160 lang_set = new icu::UnicodeSet(1, 0); |
| 165 } | 161 } |
| 166 lang_set->freeze(); | 162 lang_set->freeze(); |
| 167 SetExemplarSetForLang(lang, lang_set); | 163 SetExemplarSetForLang(lang, lang_set); |
| 168 ulocdata_close(uld); | 164 ulocdata_close(uld); |
| 169 } | 165 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 181 // TODO(jungshik) : Check if there's any character inappropriate | 177 // TODO(jungshik) : Check if there's any character inappropriate |
| 182 // (although allowed) for domain names. | 178 // (although allowed) for domain names. |
| 183 // See http://www.unicode.org/reports/tr39/#IDN_Security_Profiles and | 179 // See http://www.unicode.org/reports/tr39/#IDN_Security_Profiles and |
| 184 // http://www.unicode.org/reports/tr39/data/xidmodifications.txt | 180 // http://www.unicode.org/reports/tr39/data/xidmodifications.txt |
| 185 // For now, we borrow the list from Mozilla and tweaked it slightly. | 181 // For now, we borrow the list from Mozilla and tweaked it slightly. |
| 186 // (e.g. Characters like U+00A0, U+3000, U+3002 are omitted because | 182 // (e.g. Characters like U+00A0, U+3000, U+3002 are omitted because |
| 187 // they're gonna be canonicalized to U+0020 and full stop before | 183 // they're gonna be canonicalized to U+0020 and full stop before |
| 188 // reaching here.) | 184 // reaching here.) |
| 189 // The original list is available at | 185 // The original list is available at |
| 190 // http://kb.mozillazine.org/Network.IDN.blacklist_chars and | 186 // http://kb.mozillazine.org/Network.IDN.blacklist_chars and |
| 191 // at http://mxr.mozilla.org/seamonkey/source/modules/libpref/src/init/all.js#
703 | 187 // at |
| 188 // http://mxr.mozilla.org/seamonkey/source/modules/libpref/src/init/all.js#703 |
| 192 | 189 |
| 193 UErrorCode status = U_ZERO_ERROR; | 190 UErrorCode status = U_ZERO_ERROR; |
| 194 #ifdef U_WCHAR_IS_UTF16 | 191 #ifdef U_WCHAR_IS_UTF16 |
| 195 icu::UnicodeSet dangerous_characters(icu::UnicodeString( | 192 icu::UnicodeSet dangerous_characters( |
| 196 L"[[\\ \u00ad\u00bc\u00bd\u01c3\u0337\u0338" | 193 icu::UnicodeString( |
| 197 L"\u05c3\u05f4\u06d4\u0702\u115f\u1160][\u2000-\u200b]" | 194 L"[[\\ \u00ad\u00bc\u00bd\u01c3\u0337\u0338" |
| 198 L"[\u2024\u2027\u2028\u2029\u2039\u203a\u2044\u205f]" | 195 L"\u05c3\u05f4\u06d4\u0702\u115f\u1160][\u2000-\u200b]" |
| 199 L"[\u2154-\u2156][\u2159-\u215b][\u215f\u2215\u23ae" | 196 L"[\u2024\u2027\u2028\u2029\u2039\u203a\u2044\u205f]" |
| 200 L"\u29f6\u29f8\u2afb\u2afd][\u2ff0-\u2ffb][\u3014" | 197 L"[\u2154-\u2156][\u2159-\u215b][\u215f\u2215\u23ae" |
| 201 L"\u3015\u3033\u3164\u321d\u321e\u33ae\u33af\u33c6\u33df\ufe14" | 198 L"\u29f6\u29f8\u2afb\u2afd][\u2ff0-\u2ffb][\u3014" |
| 202 L"\ufe15\ufe3f\ufe5d\ufe5e\ufeff\uff0e\uff06\uff61\uffa0\ufff9]" | 199 L"\u3015\u3033\u3164\u321d\u321e\u33ae\u33af\u33c6\u33df\ufe14" |
| 203 L"[\ufffa-\ufffd]]"), status); | 200 L"\ufe15\ufe3f\ufe5d\ufe5e\ufeff\uff0e\uff06\uff61\uffa0\ufff9]" |
| 201 L"[\ufffa-\ufffd]]"), |
| 202 status); |
| 204 DCHECK(U_SUCCESS(status)); | 203 DCHECK(U_SUCCESS(status)); |
| 205 icu::RegexMatcher dangerous_patterns(icu::UnicodeString( | 204 icu::RegexMatcher dangerous_patterns( |
| 206 // Lone katakana no, so, or n | 205 icu::UnicodeString( |
| 207 L"[^\\p{Katakana}][\u30ce\u30f3\u30bd][^\\p{Katakana}]" | 206 // Lone katakana no, so, or n |
| 208 // Repeating Japanese accent characters | 207 L"[^\\p{Katakana}][\u30ce\u30f3\u30bd][^\\p{Katakana}]" |
| 209 L"|[\u3099\u309a\u309b\u309c][\u3099\u309a\u309b\u309c]"), | 208 // Repeating Japanese accent characters |
| 210 0, status); | 209 L"|[\u3099\u309a\u309b\u309c][\u3099\u309a\u309b\u309c]"), |
| 210 0, |
| 211 status); |
| 211 #else | 212 #else |
| 212 icu::UnicodeSet dangerous_characters(icu::UnicodeString( | 213 icu::UnicodeSet dangerous_characters( |
| 213 "[[\\u0020\\u00ad\\u00bc\\u00bd\\u01c3\\u0337\\u0338" | 214 icu::UnicodeString( |
| 214 "\\u05c3\\u05f4\\u06d4\\u0702\\u115f\\u1160][\\u2000-\\u200b]" | 215 "[[\\u0020\\u00ad\\u00bc\\u00bd\\u01c3\\u0337\\u0338" |
| 215 "[\\u2024\\u2027\\u2028\\u2029\\u2039\\u203a\\u2044\\u205f]" | 216 "\\u05c3\\u05f4\\u06d4\\u0702\\u115f\\u1160][\\u2000-\\u200b]" |
| 216 "[\\u2154-\\u2156][\\u2159-\\u215b][\\u215f\\u2215\\u23ae" | 217 "[\\u2024\\u2027\\u2028\\u2029\\u2039\\u203a\\u2044\\u205f]" |
| 217 "\\u29f6\\u29f8\\u2afb\\u2afd][\\u2ff0-\\u2ffb][\\u3014" | 218 "[\\u2154-\\u2156][\\u2159-\\u215b][\\u215f\\u2215\\u23ae" |
| 218 "\\u3015\\u3033\\u3164\\u321d\\u321e\\u33ae\\u33af\\u33c6\\u33df\\ufe14" | 219 "\\u29f6\\u29f8\\u2afb\\u2afd][\\u2ff0-\\u2ffb][\\u3014" |
| 219 "\\ufe15\\ufe3f\\ufe5d\\ufe5e\\ufeff\\uff0e\\uff06\\uff61\\uffa0\\ufff9]" | 220 "\\u3015\\u3033\\u3164\\u321d\\u321e\\u33ae\\u33af\\u33c6\\u33df\\ufe" |
| 220 "[\\ufffa-\\ufffd]]", -1, US_INV), status); | 221 "14" |
| 222 "\\ufe15\\ufe3f\\ufe5d\\ufe5e\\ufeff\\uff0e\\uff06\\uff61\\uffa0\\uff" |
| 223 "f9]" |
| 224 "[\\ufffa-\\ufffd]]", |
| 225 -1, |
| 226 US_INV), |
| 227 status); |
| 221 DCHECK(U_SUCCESS(status)); | 228 DCHECK(U_SUCCESS(status)); |
| 222 icu::RegexMatcher dangerous_patterns(icu::UnicodeString( | 229 icu::RegexMatcher dangerous_patterns( |
| 223 // Lone katakana no, so, or n | 230 icu::UnicodeString( |
| 224 "[^\\p{Katakana}][\\u30ce\\u30f3\u30bd][^\\p{Katakana}]" | 231 // Lone katakana no, so, or n |
| 225 // Repeating Japanese accent characters | 232 "[^\\p{Katakana}][\\u30ce\\u30f3\u30bd][^\\p{Katakana}]" |
| 226 "|[\\u3099\\u309a\\u309b\\u309c][\\u3099\\u309a\\u309b\\u309c]"), | 233 // Repeating Japanese accent characters |
| 227 0, status); | 234 "|[\\u3099\\u309a\\u309b\\u309c][\\u3099\\u309a\\u309b\\u309c]"), |
| 235 0, |
| 236 status); |
| 228 #endif | 237 #endif |
| 229 DCHECK(U_SUCCESS(status)); | 238 DCHECK(U_SUCCESS(status)); |
| 230 icu::UnicodeSet component_characters; | 239 icu::UnicodeSet component_characters; |
| 231 icu::UnicodeString component_string(str, str_len); | 240 icu::UnicodeString component_string(str, str_len); |
| 232 component_characters.addAll(component_string); | 241 component_characters.addAll(component_string); |
| 233 if (dangerous_characters.containsSome(component_characters)) | 242 if (dangerous_characters.containsSome(component_characters)) |
| 234 return false; | 243 return false; |
| 235 | 244 |
| 236 DCHECK(U_SUCCESS(status)); | 245 DCHECK(U_SUCCESS(status)); |
| 237 dangerous_patterns.reset(component_string); | 246 dangerous_patterns.reset(component_string); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // TODO(jungshik): Change options as different parties (browsers, | 302 // TODO(jungshik): Change options as different parties (browsers, |
| 294 // registrars, search engines) converge toward a consensus. | 303 // registrars, search engines) converge toward a consensus. |
| 295 value = uidna_openUTS46(UIDNA_CHECK_BIDI, &err); | 304 value = uidna_openUTS46(UIDNA_CHECK_BIDI, &err); |
| 296 if (U_FAILURE(err)) | 305 if (U_FAILURE(err)) |
| 297 value = NULL; | 306 value = NULL; |
| 298 } | 307 } |
| 299 | 308 |
| 300 UIDNA* value; | 309 UIDNA* value; |
| 301 }; | 310 }; |
| 302 | 311 |
| 303 static base::LazyInstance<UIDNAWrapper>::Leaky | 312 static base::LazyInstance<UIDNAWrapper>::Leaky g_uidna = |
| 304 g_uidna = LAZY_INSTANCE_INITIALIZER; | 313 LAZY_INSTANCE_INITIALIZER; |
| 305 | 314 |
| 306 // Converts one component of a host (between dots) to IDN if safe. The result | 315 // Converts one component of a host (between dots) to IDN if safe. The result |
| 307 // will be APPENDED to the given output string and will be the same as the input | 316 // will be APPENDED to the given output string and will be the same as the input |
| 308 // if it is not IDN or the IDN is unsafe to display. Returns whether any | 317 // if it is not IDN or the IDN is unsafe to display. Returns whether any |
| 309 // conversion was performed. | 318 // conversion was performed. |
| 310 bool IDNToUnicodeOneComponent(const base::char16* comp, | 319 bool IDNToUnicodeOneComponent(const base::char16* comp, |
| 311 size_t comp_len, | 320 size_t comp_len, |
| 312 const std::string& languages, | 321 const std::string& languages, |
| 313 base::string16* out) { | 322 base::string16* out) { |
| 314 DCHECK(out); | 323 DCHECK(out); |
| 315 if (comp_len == 0) | 324 if (comp_len == 0) |
| 316 return false; | 325 return false; |
| 317 | 326 |
| 318 // Only transform if the input can be an IDN component. | 327 // Only transform if the input can be an IDN component. |
| 319 static const base::char16 kIdnPrefix[] = {'x', 'n', '-', '-'}; | 328 static const base::char16 kIdnPrefix[] = {'x', 'n', '-', '-'}; |
| 320 if ((comp_len > arraysize(kIdnPrefix)) && | 329 if ((comp_len > arraysize(kIdnPrefix)) && |
| 321 !memcmp(comp, kIdnPrefix, arraysize(kIdnPrefix) * sizeof(base::char16))) { | 330 !memcmp(comp, kIdnPrefix, arraysize(kIdnPrefix) * sizeof(base::char16))) { |
| 322 UIDNA* uidna = g_uidna.Get().value; | 331 UIDNA* uidna = g_uidna.Get().value; |
| 323 DCHECK(uidna != NULL); | 332 DCHECK(uidna != NULL); |
| 324 size_t original_length = out->length(); | 333 size_t original_length = out->length(); |
| 325 int output_length = 64; | 334 int output_length = 64; |
| 326 UIDNAInfo info = UIDNA_INFO_INITIALIZER; | 335 UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
| 327 UErrorCode status; | 336 UErrorCode status; |
| 328 do { | 337 do { |
| 329 out->resize(original_length + output_length); | 338 out->resize(original_length + output_length); |
| 330 status = U_ZERO_ERROR; | 339 status = U_ZERO_ERROR; |
| 331 // This returns the actual length required. If this is more than 64 | 340 // This returns the actual length required. If this is more than 64 |
| 332 // code units, |status| will be U_BUFFER_OVERFLOW_ERROR and we'll try | 341 // code units, |status| will be U_BUFFER_OVERFLOW_ERROR and we'll try |
| 333 // the conversion again, but with a sufficiently large buffer. | 342 // the conversion again, but with a sufficiently large buffer. |
| 334 output_length = uidna_labelToUnicode( | 343 output_length = uidna_labelToUnicode(uidna, |
| 335 uidna, comp, static_cast<int32_t>(comp_len), &(*out)[original_length], | 344 comp, |
| 336 output_length, &info, &status); | 345 static_cast<int32_t>(comp_len), |
| 346 &(*out)[original_length], |
| 347 output_length, |
| 348 &info, |
| 349 &status); |
| 337 } while ((status == U_BUFFER_OVERFLOW_ERROR && info.errors == 0)); | 350 } while ((status == U_BUFFER_OVERFLOW_ERROR && info.errors == 0)); |
| 338 | 351 |
| 339 if (U_SUCCESS(status) && info.errors == 0) { | 352 if (U_SUCCESS(status) && info.errors == 0) { |
| 340 // Converted successfully. Ensure that the converted component | 353 // Converted successfully. Ensure that the converted component |
| 341 // can be safely displayed to the user. | 354 // can be safely displayed to the user. |
| 342 out->resize(original_length + output_length); | 355 out->resize(original_length + output_length); |
| 343 if (IsIDNComponentSafe(out->data() + original_length, output_length, | 356 if (IsIDNComponentSafe( |
| 344 languages)) | 357 out->data() + original_length, output_length, languages)) |
| 345 return true; | 358 return true; |
| 346 } | 359 } |
| 347 | 360 |
| 348 // Something went wrong. Revert to original string. | 361 // Something went wrong. Revert to original string. |
| 349 out->resize(original_length); | 362 out->resize(original_length); |
| 350 } | 363 } |
| 351 | 364 |
| 352 // We get here with no IDN or on error, in which case we just append the | 365 // We get here with no IDN or on error, in which case we just append the |
| 353 // literal input. | 366 // literal input. |
| 354 out->append(comp, comp_len); | 367 out->append(comp, comp_len); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 382 component_start = component_end + 1) { | 395 component_start = component_end + 1) { |
| 383 // Find the end of the component. | 396 // Find the end of the component. |
| 384 component_end = input16.find('.', component_start); | 397 component_end = input16.find('.', component_start); |
| 385 if (component_end == base::string16::npos) | 398 if (component_end == base::string16::npos) |
| 386 component_end = input16.length(); // For getting the last component. | 399 component_end = input16.length(); // For getting the last component. |
| 387 size_t component_length = component_end - component_start; | 400 size_t component_length = component_end - component_start; |
| 388 size_t new_component_start = out16.length(); | 401 size_t new_component_start = out16.length(); |
| 389 bool converted_idn = false; | 402 bool converted_idn = false; |
| 390 if (component_end > component_start) { | 403 if (component_end > component_start) { |
| 391 // Add the substring that we just found. | 404 // Add the substring that we just found. |
| 392 converted_idn = IDNToUnicodeOneComponent( | 405 converted_idn = |
| 393 input16.data() + component_start, component_length, languages, | 406 IDNToUnicodeOneComponent(input16.data() + component_start, |
| 394 &out16); | 407 component_length, |
| 408 languages, |
| 409 &out16); |
| 395 } | 410 } |
| 396 size_t new_component_length = out16.length() - new_component_start; | 411 size_t new_component_length = out16.length() - new_component_start; |
| 397 | 412 |
| 398 if (converted_idn && adjustments) { | 413 if (converted_idn && adjustments) { |
| 399 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 414 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
| 400 component_start, component_length, new_component_length)); | 415 component_start, component_length, new_component_length)); |
| 401 } | 416 } |
| 402 | 417 |
| 403 // Need to add the dot we just found (if we found one). | 418 // Need to add the dot we just found (if we found one). |
| 404 if (component_end < input16.length()) | 419 if (component_end < input16.length()) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 url::Parsed* new_parsed, | 452 url::Parsed* new_parsed, |
| 438 size_t* prefix_end, | 453 size_t* prefix_end, |
| 439 base::OffsetAdjuster::Adjustments* adjustments) { | 454 base::OffsetAdjuster::Adjustments* adjustments) { |
| 440 DCHECK(new_parsed); | 455 DCHECK(new_parsed); |
| 441 const char kViewSource[] = "view-source:"; | 456 const char kViewSource[] = "view-source:"; |
| 442 const size_t kViewSourceLength = arraysize(kViewSource) - 1; | 457 const size_t kViewSourceLength = arraysize(kViewSource) - 1; |
| 443 | 458 |
| 444 // Format the underlying URL and record adjustments. | 459 // Format the underlying URL and record adjustments. |
| 445 const std::string& url_str(url.possibly_invalid_spec()); | 460 const std::string& url_str(url.possibly_invalid_spec()); |
| 446 adjustments->clear(); | 461 adjustments->clear(); |
| 447 base::string16 result(base::ASCIIToUTF16(kViewSource) + | 462 base::string16 result( |
| 463 base::ASCIIToUTF16(kViewSource) + |
| 448 FormatUrlWithAdjustments(GURL(url_str.substr(kViewSourceLength)), | 464 FormatUrlWithAdjustments(GURL(url_str.substr(kViewSourceLength)), |
| 449 languages, format_types, unescape_rules, | 465 languages, |
| 450 new_parsed, prefix_end, adjustments)); | 466 format_types, |
| 467 unescape_rules, |
| 468 new_parsed, |
| 469 prefix_end, |
| 470 adjustments)); |
| 451 // Revise |adjustments| by shifting to the offsets to prefix that the above | 471 // Revise |adjustments| by shifting to the offsets to prefix that the above |
| 452 // call to FormatUrl didn't get to see. | 472 // call to FormatUrl didn't get to see. |
| 453 for (base::OffsetAdjuster::Adjustments::iterator it = adjustments->begin(); | 473 for (base::OffsetAdjuster::Adjustments::iterator it = adjustments->begin(); |
| 454 it != adjustments->end(); ++it) | 474 it != adjustments->end(); |
| 475 ++it) |
| 455 it->original_offset += kViewSourceLength; | 476 it->original_offset += kViewSourceLength; |
| 456 | 477 |
| 457 // Adjust positions of the parsed components. | 478 // Adjust positions of the parsed components. |
| 458 if (new_parsed->scheme.is_nonempty()) { | 479 if (new_parsed->scheme.is_nonempty()) { |
| 459 // Assume "view-source:real-scheme" as a scheme. | 480 // Assume "view-source:real-scheme" as a scheme. |
| 460 new_parsed->scheme.len += kViewSourceLength; | 481 new_parsed->scheme.len += kViewSourceLength; |
| 461 } else { | 482 } else { |
| 462 new_parsed->scheme.begin = 0; | 483 new_parsed->scheme.begin = 0; |
| 463 new_parsed->scheme.len = kViewSourceLength - 1; | 484 new_parsed->scheme.len = kViewSourceLength - 1; |
| 464 } | 485 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 480 base::OffsetAdjuster::Adjustments* adjustments) const = 0; | 501 base::OffsetAdjuster::Adjustments* adjustments) const = 0; |
| 481 | 502 |
| 482 // NOTE: No DISALLOW_COPY_AND_ASSIGN here, since gcc < 4.3.0 requires an | 503 // NOTE: No DISALLOW_COPY_AND_ASSIGN here, since gcc < 4.3.0 requires an |
| 483 // accessible copy constructor in order to call AppendFormattedComponent() | 504 // accessible copy constructor in order to call AppendFormattedComponent() |
| 484 // with an inline temporary (see http://gcc.gnu.org/bugs/#cxx%5Frvalbind ). | 505 // with an inline temporary (see http://gcc.gnu.org/bugs/#cxx%5Frvalbind ). |
| 485 }; | 506 }; |
| 486 | 507 |
| 487 class HostComponentTransform : public AppendComponentTransform { | 508 class HostComponentTransform : public AppendComponentTransform { |
| 488 public: | 509 public: |
| 489 explicit HostComponentTransform(const std::string& languages) | 510 explicit HostComponentTransform(const std::string& languages) |
| 490 : languages_(languages) { | 511 : languages_(languages) {} |
| 491 } | |
| 492 | 512 |
| 493 private: | 513 private: |
| 494 virtual base::string16 Execute( | 514 virtual base::string16 Execute( |
| 495 const std::string& component_text, | 515 const std::string& component_text, |
| 496 base::OffsetAdjuster::Adjustments* adjustments) const override { | 516 base::OffsetAdjuster::Adjustments* adjustments) const override { |
| 497 return IDNToUnicodeWithAdjustments(component_text, languages_, | 517 return IDNToUnicodeWithAdjustments(component_text, languages_, adjustments); |
| 498 adjustments); | |
| 499 } | 518 } |
| 500 | 519 |
| 501 const std::string& languages_; | 520 const std::string& languages_; |
| 502 }; | 521 }; |
| 503 | 522 |
| 504 class NonHostComponentTransform : public AppendComponentTransform { | 523 class NonHostComponentTransform : public AppendComponentTransform { |
| 505 public: | 524 public: |
| 506 explicit NonHostComponentTransform(UnescapeRule::Type unescape_rules) | 525 explicit NonHostComponentTransform(UnescapeRule::Type unescape_rules) |
| 507 : unescape_rules_(unescape_rules) { | 526 : unescape_rules_(unescape_rules) {} |
| 508 } | |
| 509 | 527 |
| 510 private: | 528 private: |
| 511 virtual base::string16 Execute( | 529 virtual base::string16 Execute( |
| 512 const std::string& component_text, | 530 const std::string& component_text, |
| 513 base::OffsetAdjuster::Adjustments* adjustments) const override { | 531 base::OffsetAdjuster::Adjustments* adjustments) const override { |
| 514 return (unescape_rules_ == UnescapeRule::NONE) ? | 532 return (unescape_rules_ == UnescapeRule::NONE) |
| 515 base::UTF8ToUTF16WithAdjustments(component_text, adjustments) : | 533 ? base::UTF8ToUTF16WithAdjustments(component_text, adjustments) |
| 516 UnescapeAndDecodeUTF8URLComponentWithAdjustments(component_text, | 534 : UnescapeAndDecodeUTF8URLComponentWithAdjustments( |
| 517 unescape_rules_, adjustments); | 535 component_text, unescape_rules_, adjustments); |
| 518 } | 536 } |
| 519 | 537 |
| 520 const UnescapeRule::Type unescape_rules_; | 538 const UnescapeRule::Type unescape_rules_; |
| 521 }; | 539 }; |
| 522 | 540 |
| 523 // Transforms the portion of |spec| covered by |original_component| according to | 541 // Transforms the portion of |spec| covered by |original_component| according to |
| 524 // |transform|. Appends the result to |output|. If |output_component| is | 542 // |transform|. Appends the result to |output|. If |output_component| is |
| 525 // non-NULL, its start and length are set to the transformed component's new | 543 // non-NULL, its start and length are set to the transformed component's new |
| 526 // start and length. If |adjustments| is non-NULL, appends adjustments (if | 544 // start and length. If |adjustments| is non-NULL, appends adjustments (if |
| 527 // any) that reflect the transformation the original component underwent to | 545 // any) that reflect the transformation the original component underwent to |
| 528 // become the transformed value appended to |output|. | 546 // become the transformed value appended to |output|. |
| 529 void AppendFormattedComponent(const std::string& spec, | 547 void AppendFormattedComponent(const std::string& spec, |
| 530 const url::Component& original_component, | 548 const url::Component& original_component, |
| 531 const AppendComponentTransform& transform, | 549 const AppendComponentTransform& transform, |
| 532 base::string16* output, | 550 base::string16* output, |
| 533 url::Component* output_component, | 551 url::Component* output_component, |
| 534 base::OffsetAdjuster::Adjustments* adjustments) { | 552 base::OffsetAdjuster::Adjustments* adjustments) { |
| 535 DCHECK(output); | 553 DCHECK(output); |
| 536 if (original_component.is_nonempty()) { | 554 if (original_component.is_nonempty()) { |
| 537 size_t original_component_begin = | 555 size_t original_component_begin = |
| 538 static_cast<size_t>(original_component.begin); | 556 static_cast<size_t>(original_component.begin); |
| 539 size_t output_component_begin = output->length(); | 557 size_t output_component_begin = output->length(); |
| 540 std::string component_str(spec, original_component_begin, | 558 std::string component_str(spec, |
| 559 original_component_begin, |
| 541 static_cast<size_t>(original_component.len)); | 560 static_cast<size_t>(original_component.len)); |
| 542 | 561 |
| 543 // Transform |component_str| and modify |adjustments| appropriately. | 562 // Transform |component_str| and modify |adjustments| appropriately. |
| 544 base::OffsetAdjuster::Adjustments component_transform_adjustments; | 563 base::OffsetAdjuster::Adjustments component_transform_adjustments; |
| 545 output->append( | 564 output->append( |
| 546 transform.Execute(component_str, &component_transform_adjustments)); | 565 transform.Execute(component_str, &component_transform_adjustments)); |
| 547 | 566 |
| 548 // Shift all the adjustments made for this component so the offsets are | 567 // Shift all the adjustments made for this component so the offsets are |
| 549 // valid for the original string and add them to |adjustments|. | 568 // valid for the original string and add them to |adjustments|. |
| 550 for (base::OffsetAdjuster::Adjustments::iterator comp_iter = | 569 for (base::OffsetAdjuster::Adjustments::iterator comp_iter = |
| 551 component_transform_adjustments.begin(); | 570 component_transform_adjustments.begin(); |
| 552 comp_iter != component_transform_adjustments.end(); ++comp_iter) | 571 comp_iter != component_transform_adjustments.end(); |
| 572 ++comp_iter) |
| 553 comp_iter->original_offset += original_component_begin; | 573 comp_iter->original_offset += original_component_begin; |
| 554 if (adjustments) { | 574 if (adjustments) { |
| 555 adjustments->insert(adjustments->end(), | 575 adjustments->insert(adjustments->end(), |
| 556 component_transform_adjustments.begin(), | 576 component_transform_adjustments.begin(), |
| 557 component_transform_adjustments.end()); | 577 component_transform_adjustments.end()); |
| 558 } | 578 } |
| 559 | 579 |
| 560 // Set positions of the parsed component. | 580 // Set positions of the parsed component. |
| 561 if (output_component) { | 581 if (output_component) { |
| 562 output_component->begin = static_cast<int>(output_component_begin); | 582 output_component->begin = static_cast<int>(output_component_begin); |
| 563 output_component->len = | 583 output_component->len = |
| 564 static_cast<int>(output->length() - output_component_begin); | 584 static_cast<int>(output->length() - output_component_begin); |
| 565 } | 585 } |
| 566 } else if (output_component) { | 586 } else if (output_component) { |
| 567 output_component->reset(); | 587 output_component->reset(); |
| 568 } | 588 } |
| 569 } | 589 } |
| 570 | 590 |
| 571 } // namespace | 591 } // namespace |
| 572 | 592 |
| 573 const FormatUrlType kFormatUrlOmitNothing = 0; | 593 const FormatUrlType kFormatUrlOmitNothing = 0; |
| 574 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; | 594 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; |
| 575 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; | 595 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; |
| 576 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; | 596 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; |
| 577 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | | 597 const FormatUrlType kFormatUrlOmitAll = |
| 578 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; | 598 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP | |
| 599 kFormatUrlOmitTrailingSlashOnBareHostname; |
| 579 | 600 |
| 580 base::string16 IDNToUnicode(const std::string& host, | 601 base::string16 IDNToUnicode(const std::string& host, |
| 581 const std::string& languages) { | 602 const std::string& languages) { |
| 582 return IDNToUnicodeWithAdjustments(host, languages, NULL); | 603 return IDNToUnicodeWithAdjustments(host, languages, NULL); |
| 583 } | 604 } |
| 584 | 605 |
| 585 std::string GetDirectoryListingEntry(const base::string16& name, | 606 std::string GetDirectoryListingEntry(const base::string16& name, |
| 586 const std::string& raw_bytes, | 607 const std::string& raw_bytes, |
| 587 bool is_dir, | 608 bool is_dir, |
| 588 int64 size, | 609 int64 size, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 619 | 640 |
| 620 result.append(");</script>\n"); | 641 result.append(");</script>\n"); |
| 621 | 642 |
| 622 return result; | 643 return result; |
| 623 } | 644 } |
| 624 | 645 |
| 625 void AppendFormattedHost(const GURL& url, | 646 void AppendFormattedHost(const GURL& url, |
| 626 const std::string& languages, | 647 const std::string& languages, |
| 627 base::string16* output) { | 648 base::string16* output) { |
| 628 AppendFormattedComponent(url.possibly_invalid_spec(), | 649 AppendFormattedComponent(url.possibly_invalid_spec(), |
| 629 url.parsed_for_possibly_invalid_spec().host, | 650 url.parsed_for_possibly_invalid_spec().host, |
| 630 HostComponentTransform(languages), output, NULL, NULL); | 651 HostComponentTransform(languages), |
| 652 output, |
| 653 NULL, |
| 654 NULL); |
| 631 } | 655 } |
| 632 | 656 |
| 633 base::string16 FormatUrlWithOffsets( | 657 base::string16 FormatUrlWithOffsets( |
| 634 const GURL& url, | 658 const GURL& url, |
| 635 const std::string& languages, | 659 const std::string& languages, |
| 636 FormatUrlTypes format_types, | 660 FormatUrlTypes format_types, |
| 637 UnescapeRule::Type unescape_rules, | 661 UnescapeRule::Type unescape_rules, |
| 638 url::Parsed* new_parsed, | 662 url::Parsed* new_parsed, |
| 639 size_t* prefix_end, | 663 size_t* prefix_end, |
| 640 std::vector<size_t>* offsets_for_adjustment) { | 664 std::vector<size_t>* offsets_for_adjustment) { |
| 641 base::OffsetAdjuster::Adjustments adjustments; | 665 base::OffsetAdjuster::Adjustments adjustments; |
| 642 const base::string16& format_url_return_value = | 666 const base::string16& format_url_return_value = |
| 643 FormatUrlWithAdjustments(url, languages, format_types, unescape_rules, | 667 FormatUrlWithAdjustments(url, |
| 644 new_parsed, prefix_end, &adjustments); | 668 languages, |
| 669 format_types, |
| 670 unescape_rules, |
| 671 new_parsed, |
| 672 prefix_end, |
| 673 &adjustments); |
| 645 base::OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment); | 674 base::OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment); |
| 646 if (offsets_for_adjustment) { | 675 if (offsets_for_adjustment) { |
| 647 std::for_each( | 676 std::for_each( |
| 648 offsets_for_adjustment->begin(), | 677 offsets_for_adjustment->begin(), |
| 649 offsets_for_adjustment->end(), | 678 offsets_for_adjustment->end(), |
| 650 base::LimitOffset<std::string>(format_url_return_value.length())); | 679 base::LimitOffset<std::string>(format_url_return_value.length())); |
| 651 } | 680 } |
| 652 return format_url_return_value; | 681 return format_url_return_value; |
| 653 } | 682 } |
| 654 | 683 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 668 else | 697 else |
| 669 *new_parsed = url::Parsed(); | 698 *new_parsed = url::Parsed(); |
| 670 | 699 |
| 671 // Special handling for view-source:. Don't use content::kViewSourceScheme | 700 // Special handling for view-source:. Don't use content::kViewSourceScheme |
| 672 // because this library shouldn't depend on chrome. | 701 // because this library shouldn't depend on chrome. |
| 673 const char* const kViewSource = "view-source"; | 702 const char* const kViewSource = "view-source"; |
| 674 // Reject "view-source:view-source:..." to avoid deep recursion. | 703 // Reject "view-source:view-source:..." to avoid deep recursion. |
| 675 const char* const kViewSourceTwice = "view-source:view-source:"; | 704 const char* const kViewSourceTwice = "view-source:view-source:"; |
| 676 if (url.SchemeIs(kViewSource) && | 705 if (url.SchemeIs(kViewSource) && |
| 677 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { | 706 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { |
| 678 return FormatViewSourceUrl(url, languages, format_types, | 707 return FormatViewSourceUrl(url, |
| 679 unescape_rules, new_parsed, prefix_end, | 708 languages, |
| 709 format_types, |
| 710 unescape_rules, |
| 711 new_parsed, |
| 712 prefix_end, |
| 680 adjustments); | 713 adjustments); |
| 681 } | 714 } |
| 682 | 715 |
| 683 // We handle both valid and invalid URLs (this will give us the spec | 716 // We handle both valid and invalid URLs (this will give us the spec |
| 684 // regardless of validity). | 717 // regardless of validity). |
| 685 const std::string& spec = url.possibly_invalid_spec(); | 718 const std::string& spec = url.possibly_invalid_spec(); |
| 686 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); | 719 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
| 687 | 720 |
| 688 // Scheme & separators. These are ASCII. | 721 // Scheme & separators. These are ASCII. |
| 689 base::string16 url_string; | 722 base::string16 url_string; |
| 690 url_string.insert( | 723 url_string.insert( |
| 691 url_string.end(), spec.begin(), | 724 url_string.end(), |
| 725 spec.begin(), |
| 692 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); | 726 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); |
| 693 const char kHTTP[] = "http://"; | 727 const char kHTTP[] = "http://"; |
| 694 const char kFTP[] = "ftp."; | 728 const char kFTP[] = "ftp."; |
| 695 // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This | 729 // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This |
| 696 // means that if we trim "http://" off a URL whose host starts with "ftp." and | 730 // means that if we trim "http://" off a URL whose host starts with "ftp." and |
| 697 // the user inputs this into any field subject to fixup (which is basically | 731 // the user inputs this into any field subject to fixup (which is basically |
| 698 // all input fields), the meaning would be changed. (In fact, often the | 732 // all input fields), the meaning would be changed. (In fact, often the |
| 699 // formatted URL is directly pre-filled into an input field.) For this reason | 733 // formatted URL is directly pre-filled into an input field.) For this reason |
| 700 // we avoid stripping "http://" in this case. | 734 // we avoid stripping "http://" in this case. |
| 701 bool omit_http = (format_types & kFormatUrlOmitHTTP) && | 735 bool omit_http = (format_types & kFormatUrlOmitHTTP) && |
| 702 EqualsASCII(url_string, kHTTP) && | 736 EqualsASCII(url_string, kHTTP) && |
| 703 !StartsWithASCII(url.host(), kFTP, true); | 737 !StartsWithASCII(url.host(), kFTP, true); |
| 704 new_parsed->scheme = parsed.scheme; | 738 new_parsed->scheme = parsed.scheme; |
| 705 | 739 |
| 706 // Username & password. | 740 // Username & password. |
| 707 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { | 741 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { |
| 708 // Remove the username and password fields. We don't want to display those | 742 // Remove the username and password fields. We don't want to display those |
| 709 // to the user since they can be used for attacks, | 743 // to the user since they can be used for attacks, |
| 710 // e.g. "http://google.com:search@evil.ru/" | 744 // e.g. "http://google.com:search@evil.ru/" |
| 711 new_parsed->username.reset(); | 745 new_parsed->username.reset(); |
| 712 new_parsed->password.reset(); | 746 new_parsed->password.reset(); |
| 713 // Update the adjustments based on removed username and/or password. | 747 // Update the adjustments based on removed username and/or password. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 724 parsed.username.is_nonempty() ? &parsed.username : &parsed.password; | 758 parsed.username.is_nonempty() ? &parsed.username : &parsed.password; |
| 725 // The seeming off-by-one is to account for the '@' after the | 759 // The seeming off-by-one is to account for the '@' after the |
| 726 // username/password. | 760 // username/password. |
| 727 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 761 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
| 728 static_cast<size_t>(nonempty_component->begin), | 762 static_cast<size_t>(nonempty_component->begin), |
| 729 static_cast<size_t>(nonempty_component->len + 1), | 763 static_cast<size_t>(nonempty_component->len + 1), |
| 730 0)); | 764 0)); |
| 731 } | 765 } |
| 732 } | 766 } |
| 733 } else { | 767 } else { |
| 734 AppendFormattedComponent(spec, parsed.username, | 768 AppendFormattedComponent(spec, |
| 769 parsed.username, |
| 735 NonHostComponentTransform(unescape_rules), | 770 NonHostComponentTransform(unescape_rules), |
| 736 &url_string, &new_parsed->username, adjustments); | 771 &url_string, |
| 772 &new_parsed->username, |
| 773 adjustments); |
| 737 if (parsed.password.is_valid()) | 774 if (parsed.password.is_valid()) |
| 738 url_string.push_back(':'); | 775 url_string.push_back(':'); |
| 739 AppendFormattedComponent(spec, parsed.password, | 776 AppendFormattedComponent(spec, |
| 777 parsed.password, |
| 740 NonHostComponentTransform(unescape_rules), | 778 NonHostComponentTransform(unescape_rules), |
| 741 &url_string, &new_parsed->password, adjustments); | 779 &url_string, |
| 780 &new_parsed->password, |
| 781 adjustments); |
| 742 if (parsed.username.is_valid() || parsed.password.is_valid()) | 782 if (parsed.username.is_valid() || parsed.password.is_valid()) |
| 743 url_string.push_back('@'); | 783 url_string.push_back('@'); |
| 744 } | 784 } |
| 745 if (prefix_end) | 785 if (prefix_end) |
| 746 *prefix_end = static_cast<size_t>(url_string.length()); | 786 *prefix_end = static_cast<size_t>(url_string.length()); |
| 747 | 787 |
| 748 // Host. | 788 // Host. |
| 749 AppendFormattedComponent(spec, parsed.host, HostComponentTransform(languages), | 789 AppendFormattedComponent(spec, |
| 750 &url_string, &new_parsed->host, adjustments); | 790 parsed.host, |
| 791 HostComponentTransform(languages), |
| 792 &url_string, |
| 793 &new_parsed->host, |
| 794 adjustments); |
| 751 | 795 |
| 752 // Port. | 796 // Port. |
| 753 if (parsed.port.is_nonempty()) { | 797 if (parsed.port.is_nonempty()) { |
| 754 url_string.push_back(':'); | 798 url_string.push_back(':'); |
| 755 new_parsed->port.begin = url_string.length(); | 799 new_parsed->port.begin = url_string.length(); |
| 756 url_string.insert(url_string.end(), | 800 url_string.insert(url_string.end(), |
| 757 spec.begin() + parsed.port.begin, | 801 spec.begin() + parsed.port.begin, |
| 758 spec.begin() + parsed.port.end()); | 802 spec.begin() + parsed.port.end()); |
| 759 new_parsed->port.len = url_string.length() - new_parsed->port.begin; | 803 new_parsed->port.len = url_string.length() - new_parsed->port.begin; |
| 760 } else { | 804 } else { |
| 761 new_parsed->port.reset(); | 805 new_parsed->port.reset(); |
| 762 } | 806 } |
| 763 | 807 |
| 764 // Path & query. Both get the same general unescape & convert treatment. | 808 // Path & query. Both get the same general unescape & convert treatment. |
| 765 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) || | 809 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) || |
| 766 !CanStripTrailingSlash(url)) { | 810 !CanStripTrailingSlash(url)) { |
| 767 AppendFormattedComponent(spec, parsed.path, | 811 AppendFormattedComponent(spec, |
| 812 parsed.path, |
| 768 NonHostComponentTransform(unescape_rules), | 813 NonHostComponentTransform(unescape_rules), |
| 769 &url_string, &new_parsed->path, adjustments); | 814 &url_string, |
| 815 &new_parsed->path, |
| 816 adjustments); |
| 770 } else { | 817 } else { |
| 771 if (parsed.path.len > 0) { | 818 if (parsed.path.len > 0) { |
| 772 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 819 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
| 773 parsed.path.begin, parsed.path.len, 0)); | 820 parsed.path.begin, parsed.path.len, 0)); |
| 774 } | 821 } |
| 775 } | 822 } |
| 776 if (parsed.query.is_valid()) | 823 if (parsed.query.is_valid()) |
| 777 url_string.push_back('?'); | 824 url_string.push_back('?'); |
| 778 AppendFormattedComponent(spec, parsed.query, | 825 AppendFormattedComponent(spec, |
| 826 parsed.query, |
| 779 NonHostComponentTransform(unescape_rules), | 827 NonHostComponentTransform(unescape_rules), |
| 780 &url_string, &new_parsed->query, adjustments); | 828 &url_string, |
| 829 &new_parsed->query, |
| 830 adjustments); |
| 781 | 831 |
| 782 // Ref. This is valid, unescaped UTF-8, so we can just convert. | 832 // Ref. This is valid, unescaped UTF-8, so we can just convert. |
| 783 if (parsed.ref.is_valid()) | 833 if (parsed.ref.is_valid()) |
| 784 url_string.push_back('#'); | 834 url_string.push_back('#'); |
| 785 AppendFormattedComponent(spec, parsed.ref, | 835 AppendFormattedComponent(spec, |
| 836 parsed.ref, |
| 786 NonHostComponentTransform(UnescapeRule::NONE), | 837 NonHostComponentTransform(UnescapeRule::NONE), |
| 787 &url_string, &new_parsed->ref, adjustments); | 838 &url_string, |
| 839 &new_parsed->ref, |
| 840 adjustments); |
| 788 | 841 |
| 789 // If we need to strip out http do it after the fact. | 842 // If we need to strip out http do it after the fact. |
| 790 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { | 843 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { |
| 791 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 844 const size_t kHTTPSize = arraysize(kHTTP) - 1; |
| 792 url_string = url_string.substr(kHTTPSize); | 845 url_string = url_string.substr(kHTTPSize); |
| 793 // Because offsets in the |adjustments| are already calculated with respect | 846 // Because offsets in the |adjustments| are already calculated with respect |
| 794 // to the string with the http:// prefix in it, those offsets remain correct | 847 // to the string with the http:// prefix in it, those offsets remain correct |
| 795 // after stripping the prefix. The only thing necessary is to add an | 848 // after stripping the prefix. The only thing necessary is to add an |
| 796 // adjustment to reflect the stripped prefix. | 849 // adjustment to reflect the stripped prefix. |
| 797 adjustments->insert(adjustments->begin(), | 850 adjustments->insert(adjustments->begin(), |
| 798 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); | 851 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); |
| 799 | 852 |
| 800 if (prefix_end) | 853 if (prefix_end) |
| 801 *prefix_end -= kHTTPSize; | 854 *prefix_end -= kHTTPSize; |
| 802 | 855 |
| 803 // Adjust new_parsed. | 856 // Adjust new_parsed. |
| 804 DCHECK(new_parsed->scheme.is_valid()); | 857 DCHECK(new_parsed->scheme.is_valid()); |
| 805 int delta = -(new_parsed->scheme.len + 3); // +3 for ://. | 858 int delta = -(new_parsed->scheme.len + 3); // +3 for ://. |
| 806 new_parsed->scheme.reset(); | 859 new_parsed->scheme.reset(); |
| 807 AdjustAllComponentsButScheme(delta, new_parsed); | 860 AdjustAllComponentsButScheme(delta, new_parsed); |
| 808 } | 861 } |
| 809 | 862 |
| 810 return url_string; | 863 return url_string; |
| 811 } | 864 } |
| 812 | 865 |
| 813 base::string16 FormatUrl(const GURL& url, | 866 base::string16 FormatUrl(const GURL& url, |
| 814 const std::string& languages, | 867 const std::string& languages, |
| 815 FormatUrlTypes format_types, | 868 FormatUrlTypes format_types, |
| 816 UnescapeRule::Type unescape_rules, | 869 UnescapeRule::Type unescape_rules, |
| 817 url::Parsed* new_parsed, | 870 url::Parsed* new_parsed, |
| 818 size_t* prefix_end, | 871 size_t* prefix_end, |
| 819 size_t* offset_for_adjustment) { | 872 size_t* offset_for_adjustment) { |
| 820 Offsets offsets; | 873 Offsets offsets; |
| 821 if (offset_for_adjustment) | 874 if (offset_for_adjustment) |
| 822 offsets.push_back(*offset_for_adjustment); | 875 offsets.push_back(*offset_for_adjustment); |
| 823 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, | 876 base::string16 result = FormatUrlWithOffsets(url, |
| 824 unescape_rules, new_parsed, prefix_end, &offsets); | 877 languages, |
| 878 format_types, |
| 879 unescape_rules, |
| 880 new_parsed, |
| 881 prefix_end, |
| 882 &offsets); |
| 825 if (offset_for_adjustment) | 883 if (offset_for_adjustment) |
| 826 *offset_for_adjustment = offsets[0]; | 884 *offset_for_adjustment = offsets[0]; |
| 827 return result; | 885 return result; |
| 828 } | 886 } |
| 829 | 887 |
| 830 } // namespace net | 888 } // namespace net |
| OLD | NEW |