| 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 "chrome/common/net/url_util.h" | 5 #include "chrome/common/net/url_util.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // may not encode non-ASCII characters in UTF-8. See crbug.com/2820. | 25 // may not encode non-ASCII characters in UTF-8. See crbug.com/2820. |
| 26 string16 text = url.SchemeIs(chrome::kMailToScheme) ? | 26 string16 text = url.SchemeIs(chrome::kMailToScheme) ? |
| 27 ASCIIToUTF16(url.path()) : | 27 ASCIIToUTF16(url.path()) : |
| 28 net::FormatUrl(url, languages, net::kFormatUrlOmitNothing, | 28 net::FormatUrl(url, languages, net::kFormatUrlOmitNothing, |
| 29 net::UnescapeRule::NONE, NULL, NULL, NULL); | 29 net::UnescapeRule::NONE, NULL, NULL, NULL); |
| 30 | 30 |
| 31 ui::ScopedClipboardWriter scw(clipboard, ui::Clipboard::BUFFER_STANDARD); | 31 ui::ScopedClipboardWriter scw(clipboard, ui::Clipboard::BUFFER_STANDARD); |
| 32 scw.WriteURL(text); | 32 scw.WriteURL(text); |
| 33 } | 33 } |
| 34 | 34 |
| 35 GURL AppendQueryParameter(const GURL& url, | |
| 36 const std::string& name, | |
| 37 const std::string& value) { | |
| 38 std::string query(url.query()); | |
| 39 | |
| 40 if (!query.empty()) | |
| 41 query += "&"; | |
| 42 | |
| 43 query += (net::EscapeQueryParamValue(name, true) + "=" + | |
| 44 net::EscapeQueryParamValue(value, true)); | |
| 45 GURL::Replacements replacements; | |
| 46 replacements.SetQueryStr(query); | |
| 47 return url.ReplaceComponents(replacements); | |
| 48 } | |
| 49 | |
| 50 GURL AppendOrReplaceQueryParameter(const GURL& url, | |
| 51 const std::string& name, | |
| 52 const std::string& value) { | |
| 53 bool replaced = false; | |
| 54 std::string param_name = net::EscapeQueryParamValue(name, true); | |
| 55 std::string param_value = net::EscapeQueryParamValue(value, true); | |
| 56 | |
| 57 const std::string input = url.query(); | |
| 58 url_parse::Component cursor(0, input.size()); | |
| 59 std::string output; | |
| 60 url_parse::Component key_range, value_range; | |
| 61 while (url_parse::ExtractQueryKeyValue( | |
| 62 input.data(), &cursor, &key_range, &value_range)) { | |
| 63 const base::StringPiece key( | |
| 64 input.data() + key_range.begin, key_range.len); | |
| 65 const base::StringPiece value( | |
| 66 input.data() + value_range.begin, value_range.len); | |
| 67 std::string key_value_pair; | |
| 68 // Check |replaced| as only the first pair should be replaced. | |
| 69 if (!replaced && key == param_name) { | |
| 70 replaced = true; | |
| 71 key_value_pair = (param_name + "=" + param_value); | |
| 72 } else { | |
| 73 key_value_pair.assign(input.data(), | |
| 74 key_range.begin, | |
| 75 value_range.end() - key_range.begin); | |
| 76 } | |
| 77 if (!output.empty()) | |
| 78 output += "&"; | |
| 79 | |
| 80 output += key_value_pair; | |
| 81 } | |
| 82 if (!replaced) { | |
| 83 if (!output.empty()) | |
| 84 output += "&"; | |
| 85 | |
| 86 output += (param_name + "=" + param_value); | |
| 87 } | |
| 88 GURL::Replacements replacements; | |
| 89 replacements.SetQueryStr(output); | |
| 90 return url.ReplaceComponents(replacements); | |
| 91 } | |
| 92 | |
| 93 bool GetValueForKeyInQuery(const GURL& url, | 35 bool GetValueForKeyInQuery(const GURL& url, |
| 94 const std::string& search_key, | 36 const std::string& search_key, |
| 95 std::string* out_value) { | 37 std::string* out_value) { |
| 96 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | 38 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; |
| 97 url_parse::Component key, value; | 39 url_parse::Component key, value; |
| 98 while (url_parse::ExtractQueryKeyValue( | 40 while (url_parse::ExtractQueryKeyValue( |
| 99 url.spec().c_str(), &query, &key, &value)) { | 41 url.spec().c_str(), &query, &key, &value)) { |
| 100 if (key.is_nonempty()) { | 42 if (key.is_nonempty()) { |
| 101 std::string key_string = url.spec().substr(key.begin, key.len); | 43 std::string key_string = url.spec().substr(key.begin, key.len); |
| 102 if (key_string == search_key) { | 44 if (key_string == search_key) { |
| 103 if (value.is_nonempty()) { | 45 if (value.is_nonempty()) { |
| 104 *out_value = net::UnescapeURLComponent( | 46 *out_value = net::UnescapeURLComponent( |
| 105 url.spec().substr(value.begin, value.len), | 47 url.spec().substr(value.begin, value.len), |
| 106 net::UnescapeRule::SPACES | | 48 net::UnescapeRule::SPACES | |
| 107 net::UnescapeRule::URL_SPECIAL_CHARS | | 49 net::UnescapeRule::URL_SPECIAL_CHARS | |
| 108 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE); | 50 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
| 109 } else { | 51 } else { |
| 110 *out_value = ""; | 52 *out_value = ""; |
| 111 } | 53 } |
| 112 return true; | 54 return true; |
| 113 } | 55 } |
| 114 } | 56 } |
| 115 } | 57 } |
| 116 return false; | 58 return false; |
| 117 } | 59 } |
| 118 | 60 |
| 119 } // namespace chrome_common_net | 61 } // namespace chrome_common_net |
| OLD | NEW |