diff options
Diffstat (limited to 'chrome/common/net')
-rw-r--r-- | chrome/common/net/url_util.cc | 86 | ||||
-rw-r--r-- | chrome/common/net/url_util.h | 38 | ||||
-rw-r--r-- | chrome/common/net/url_util_unittest.cc | 102 |
3 files changed, 0 insertions, 226 deletions
diff --git a/chrome/common/net/url_util.cc b/chrome/common/net/url_util.cc index 9d58317..2062d14 100644 --- a/chrome/common/net/url_util.cc +++ b/chrome/common/net/url_util.cc @@ -4,11 +4,9 @@ #include "chrome/common/net/url_util.h" -#include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/common/url_constants.h" #include "googleurl/src/gurl.h" -#include "googleurl/src/url_parse.h" #include "net/base/escape.h" #include "net/base/net_util.h" #include "ui/base/clipboard/scoped_clipboard_writer.h" @@ -32,88 +30,4 @@ void WriteURLToClipboard(const GURL& url, scw.WriteURL(text); } -GURL AppendQueryParameter(const GURL& url, - const std::string& name, - const std::string& value) { - std::string query(url.query()); - - if (!query.empty()) - query += "&"; - - query += (net::EscapeQueryParamValue(name, true) + "=" + - net::EscapeQueryParamValue(value, true)); - GURL::Replacements replacements; - replacements.SetQueryStr(query); - return url.ReplaceComponents(replacements); -} - -GURL AppendOrReplaceQueryParameter(const GURL& url, - const std::string& name, - const std::string& value) { - bool replaced = false; - std::string param_name = net::EscapeQueryParamValue(name, true); - std::string param_value = net::EscapeQueryParamValue(value, true); - - const std::string input = url.query(); - url_parse::Component cursor(0, input.size()); - std::string output; - url_parse::Component key_range, value_range; - while (url_parse::ExtractQueryKeyValue( - input.data(), &cursor, &key_range, &value_range)) { - const base::StringPiece key( - input.data() + key_range.begin, key_range.len); - const base::StringPiece value( - input.data() + value_range.begin, value_range.len); - std::string key_value_pair; - // Check |replaced| as only the first pair should be replaced. - if (!replaced && key == param_name) { - replaced = true; - key_value_pair = (param_name + "=" + param_value); - } else { - key_value_pair.assign(input.data(), - key_range.begin, - value_range.end() - key_range.begin); - } - if (!output.empty()) - output += "&"; - - output += key_value_pair; - } - if (!replaced) { - if (!output.empty()) - output += "&"; - - output += (param_name + "=" + param_value); - } - GURL::Replacements replacements; - replacements.SetQueryStr(output); - return url.ReplaceComponents(replacements); -} - -bool GetValueForKeyInQuery(const GURL& url, - const std::string& search_key, - std::string* out_value) { - url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; - url_parse::Component key, value; - while (url_parse::ExtractQueryKeyValue( - url.spec().c_str(), &query, &key, &value)) { - if (key.is_nonempty()) { - std::string key_string = url.spec().substr(key.begin, key.len); - if (key_string == search_key) { - if (value.is_nonempty()) { - *out_value = net::UnescapeURLComponent( - url.spec().substr(value.begin, value.len), - net::UnescapeRule::SPACES | - net::UnescapeRule::URL_SPECIAL_CHARS | - net::UnescapeRule::REPLACE_PLUS_WITH_SPACE); - } else { - *out_value = ""; - } - return true; - } - } - } - return false; -} - } // namespace chrome_common_net diff --git a/chrome/common/net/url_util.h b/chrome/common/net/url_util.h index f6bf296..053b09b 100644 --- a/chrome/common/net/url_util.h +++ b/chrome/common/net/url_util.h @@ -20,44 +20,6 @@ void WriteURLToClipboard(const GURL& url, const std::string& languages, ui::Clipboard *clipboard); -// Returns a new GURL by appending the given query parameter name and the -// value. Unsafe characters in the name and the value are escaped like -// %XX%XX. The original query component is preserved if it's present. -// -// Examples: -// -// AppendQueryParameter(GURL("http://example.com"), "name", "value").spec() -// => "http://example.com?name=value" -// AppendQueryParameter(GURL("http://example.com?x=y"), "name", "value").spec() -// => "http://example.com?x=y&name=value" -GURL AppendQueryParameter(const GURL& url, - const std::string& name, - const std::string& value); - -// Returns a new GURL by appending or replacing the given query parameter name -// and the value. If |name| appears more than once, only the first name-value -// pair is replaced. Unsafe characters in the name and the value are escaped -// like %XX%XX. The original query component is preserved if it's present. -// -// Examples: -// -// AppendOrReplaceQueryParameter( -// GURL("http://example.com"), "name", "new").spec() -// => "http://example.com?name=value" -// AppendOrReplaceQueryParameter( -// GURL("http://example.com?x=y&name=old"), "name", "new").spec() -// => "http://example.com?x=y&name=new" -GURL AppendOrReplaceQueryParameter(const GURL& url, - const std::string& name, - const std::string& value); - -// Looks for |search_key| in the query portion of |url|. Returns true if the -// key is found and sets |out_value| to the unescaped value for the key. -// Returns false if the key is not found. -bool GetValueForKeyInQuery(const GURL& url, - const std::string& search_key, - std::string* out_value); - } // namespace chrome_common_net #endif // CHROME_COMMON_NET_URL_UTIL_H_ diff --git a/chrome/common/net/url_util_unittest.cc b/chrome/common/net/url_util_unittest.cc deleted file mode 100644 index 1d7fdda..0000000 --- a/chrome/common/net/url_util_unittest.cc +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/common/net/url_util.h" - -#include "googleurl/src/gurl.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace chrome_common_net { - -TEST(UrlUtilTest, AppendQueryParameter) { - // Appending a name-value pair to a URL without a query component. - EXPECT_EQ("http://example.com/path?name=value", - AppendQueryParameter(GURL("http://example.com/path"), - "name", "value").spec()); - - // Appending a name-value pair to a URL with a query component. - // The original component should be preserved, and the new pair should be - // appended with '&'. - EXPECT_EQ("http://example.com/path?existing=one&name=value", - AppendQueryParameter(GURL("http://example.com/path?existing=one"), - "name", "value").spec()); - - // Appending a name-value pair with unsafe characters included. The - // unsafe characters should be escaped. - EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", - AppendQueryParameter(GURL("http://example.com/path?existing=one"), - "na me", "v.alue=").spec()); - -} - -TEST(UrlUtilTest, AppendOrReplaceQueryParameter) { - // Appending a name-value pair to a URL without a query component. - EXPECT_EQ("http://example.com/path?name=value", - AppendOrReplaceQueryParameter(GURL("http://example.com/path"), - "name", "value").spec()); - - // Appending a name-value pair to a URL with a query component. - // The original component should be preserved, and the new pair should be - // appended with '&'. - EXPECT_EQ("http://example.com/path?existing=one&name=value", - AppendOrReplaceQueryParameter( - GURL("http://example.com/path?existing=one"), - "name", "value").spec()); - - // Appending a name-value pair with unsafe characters included. The - // unsafe characters should be escaped. - EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", - AppendOrReplaceQueryParameter( - GURL("http://example.com/path?existing=one"), - "na me", "v.alue=").spec()); - - // Replace value of an existing paramater. - EXPECT_EQ("http://example.com/path?existing=one&name=new", - AppendOrReplaceQueryParameter( - GURL("http://example.com/path?existing=one&name=old"), - "name", "new").spec()); - - // Replace a name-value pair with unsafe characters included. The - // unsafe characters should be escaped. - EXPECT_EQ("http://example.com/path?na+me=n.ew%3D&existing=one", - AppendOrReplaceQueryParameter( - GURL("http://example.com/path?na+me=old&existing=one"), - "na me", "n.ew=").spec()); - - // Replace the value of first parameter with this name only. - EXPECT_EQ("http://example.com/path?name=new&existing=one&name=old", - AppendOrReplaceQueryParameter( - GURL("http://example.com/path?name=old&existing=one&name=old"), - "name", "new").spec()); - - // Preserve the content of the original params regarless of our failure to - // interpret them correctly. - EXPECT_EQ("http://example.com/path?bar&name=new&left=&" - "=right&=&&name=again", - AppendOrReplaceQueryParameter( - GURL("http://example.com/path?bar&name=old&left=&" - "=right&=&&name=again"), - "name", "new").spec()); -} - -TEST(BrowserUrlUtilTest, GetValueForKeyInQuery) { - GURL url("http://example.com/path?name=value&boolParam&" - "url=http://test.com/q?n1%3Dv1%26n2"); - std::string value; - - // False when getting a non-existent query param. - EXPECT_FALSE(GetValueForKeyInQuery(url, "non-exist", &value)); - - // True when query param exist. - EXPECT_TRUE(GetValueForKeyInQuery(url, "name", &value)); - EXPECT_EQ("value", value); - - EXPECT_TRUE(GetValueForKeyInQuery(url, "boolParam", &value)); - EXPECT_EQ("", value); - - EXPECT_TRUE(GetValueForKeyInQuery(url, "url", &value)); - EXPECT_EQ("http://test.com/q?n1=v1&n2", value); -} - -} // namespace chrome_common_net. |