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 29 matching lines...) Expand all Loading... |
40 if (!query.empty()) | 40 if (!query.empty()) |
41 query += "&"; | 41 query += "&"; |
42 | 42 |
43 query += (net::EscapeQueryParamValue(name, true) + "=" + | 43 query += (net::EscapeQueryParamValue(name, true) + "=" + |
44 net::EscapeQueryParamValue(value, true)); | 44 net::EscapeQueryParamValue(value, true)); |
45 GURL::Replacements replacements; | 45 GURL::Replacements replacements; |
46 replacements.SetQueryStr(query); | 46 replacements.SetQueryStr(query); |
47 return url.ReplaceComponents(replacements); | 47 return url.ReplaceComponents(replacements); |
48 } | 48 } |
49 | 49 |
| 50 |
50 GURL AppendOrReplaceQueryParameter(const GURL& url, | 51 GURL AppendOrReplaceQueryParameter(const GURL& url, |
51 const std::string& name, | 52 const std::string& name, |
52 const std::string& value) { | 53 const std::string& value) { |
53 bool replaced = false; | 54 bool replaced = false; |
54 std::string param_name = net::EscapeQueryParamValue(name, true); | 55 std::string param_name = net::EscapeQueryParamValue(name, true); |
55 std::string param_value = net::EscapeQueryParamValue(value, true); | 56 std::string param_value = net::EscapeQueryParamValue(value, true); |
56 | 57 |
57 const std::string input = url.query(); | 58 const std::string input = url.query(); |
58 url_parse::Component cursor(0, input.size()); | 59 url_parse::Component cursor(0, input.size()); |
59 std::string output; | 60 std::string output; |
(...skipping 23 matching lines...) Expand all Loading... |
83 if (!output.empty()) | 84 if (!output.empty()) |
84 output += "&"; | 85 output += "&"; |
85 | 86 |
86 output += (param_name + "=" + param_value); | 87 output += (param_name + "=" + param_value); |
87 } | 88 } |
88 GURL::Replacements replacements; | 89 GURL::Replacements replacements; |
89 replacements.SetQueryStr(output); | 90 replacements.SetQueryStr(output); |
90 return url.ReplaceComponents(replacements); | 91 return url.ReplaceComponents(replacements); |
91 } | 92 } |
92 | 93 |
93 bool GetValueForKeyInQuery(const GURL& url, | |
94 const std::string& search_key, | |
95 std::string* out_value) { | |
96 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | |
97 url_parse::Component key, value; | |
98 while (url_parse::ExtractQueryKeyValue( | |
99 url.spec().c_str(), &query, &key, &value)) { | |
100 if (key.is_nonempty()) { | |
101 std::string key_string = url.spec().substr(key.begin, key.len); | |
102 if (key_string == search_key) { | |
103 if (value.is_nonempty()) { | |
104 *out_value = net::UnescapeURLComponent( | |
105 url.spec().substr(value.begin, value.len), | |
106 net::UnescapeRule::SPACES | | |
107 net::UnescapeRule::URL_SPECIAL_CHARS | | |
108 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE); | |
109 } else { | |
110 *out_value = ""; | |
111 } | |
112 return true; | |
113 } | |
114 } | |
115 } | |
116 return false; | |
117 } | |
118 | |
119 } // namespace chrome_common_net | 94 } // namespace chrome_common_net |
OLD | NEW |