Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: net/base/url_util.cc

Issue 632843003: Add a function to escape a query part of url (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename the function to EscapeQueryParameters Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "net/base/url_util.h" 5 #include "net/base/url_util.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 std::string* out_value) { 127 std::string* out_value) {
128 for (QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { 128 for (QueryIterator it(url); !it.IsAtEnd(); it.Advance()) {
129 if (it.GetKey() == search_key) { 129 if (it.GetKey() == search_key) {
130 *out_value = it.GetUnescapedValue(); 130 *out_value = it.GetUnescapedValue();
131 return true; 131 return true;
132 } 132 }
133 } 133 }
134 return false; 134 return false;
135 } 135 }
136 136
137 GURL EscapeQueryParameters(const GURL& url) {
138 const std::string input = url.query();
139 url::Component cursor(0, input.size());
140 std::string output;
141 url::Component key_range, value_range;
142 while (url::ExtractQueryKeyValue(
143 input.data(), &cursor, &key_range, &value_range)) {
144 std::string key(input.data() + key_range.begin, key_range.len);
145 std::string value(input.data() + value_range.begin, value_range.len);
146 std::string unescaped_key = UnescapeURLComponent(
147 key,
148 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS |
149 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE);
150 std::string unescaped_value = UnescapeURLComponent(
151 value,
152 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS |
153 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE);
154 std::string param_name = EscapeQueryParamValue(unescaped_key, true);
155 std::string param_value = EscapeQueryParamValue(unescaped_value, true);
156 std::string key_value_pair = (param_name + "=" + param_value);
157 if (!output.empty())
158 output += "&";
159
160 output += key_value_pair;
161 }
162 if (output.empty())
163 return url;
164
165 GURL::Replacements replacements;
166 replacements.SetQueryStr(output);
167 return url.ReplaceComponents(replacements);
168 }
169
137 } // namespace net 170 } // namespace net
OLDNEW
« net/base/url_util.h ('K') | « net/base/url_util.h ('k') | net/base/url_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698