summaryrefslogtreecommitdiffstats
path: root/url
diff options
context:
space:
mode:
authorppi@chromium.org <ppi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-20 20:27:25 +0000
committerppi@chromium.org <ppi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-20 20:27:25 +0000
commit6b775ee9071613f5a56d7c681841db136c9d4879 (patch)
tree5e11d1cf6f9ba452ef90e18fc41eab9266f62124 /url
parent5c2f32e78d718586f3024935b90664385feb91b4 (diff)
downloadchromium_src-6b775ee9071613f5a56d7c681841db136c9d4879.zip
chromium_src-6b775ee9071613f5a56d7c681841db136c9d4879.tar.gz
chromium_src-6b775ee9071613f5a56d7c681841db136c9d4879.tar.bz2
Move referrer stripping into GURL::GetAsReferrer().
This patch moves the code that removes username, password and ref parts of the url from http referrers into GURL::GetAsReferrer(). BUG=340295 Review URL: https://codereview.chromium.org/185133005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258392 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'url')
-rw-r--r--url/gurl.cc12
-rw-r--r--url/gurl.h5
-rw-r--r--url/gurl_unittest.cc19
3 files changed, 36 insertions, 0 deletions
diff --git a/url/gurl.cc b/url/gurl.cc
index 8eb2d61..77b8def2 100644
--- a/url/gurl.cc
+++ b/url/gurl.cc
@@ -320,6 +320,18 @@ GURL GURL::GetOrigin() const {
return ReplaceComponents(replacements);
}
+GURL GURL::GetAsReferrer() const {
+ if (!is_valid_ ||
+ (!has_ref() && !has_username() && !has_password()))
+ return GURL(*this);
+
+ url_canon::Replacements<char> replacements;
+ replacements.ClearRef();
+ replacements.ClearUsername();
+ replacements.ClearPassword();
+ return ReplaceComponents(replacements);
+}
+
GURL GURL::GetWithEmptyPath() const {
// This doesn't make sense for invalid or nonstandard URLs, so return
// the empty URL.
diff --git a/url/gurl.h b/url/gurl.h
index dc88fec25..28897d1 100644
--- a/url/gurl.h
+++ b/url/gurl.h
@@ -195,6 +195,11 @@ class URL_EXPORT GURL {
// will be the empty URL.
GURL GetOrigin() const;
+ // A helper function to return a GURL stripped from the elements that are not
+ // supposed to be sent as HTTP referrer: username, password and ref fragment.
+ // For invalid URLs the original URL will be returned.
+ GURL GetAsReferrer() const;
+
// Returns true if the scheme for the current URL is a known "standard"
// scheme. Standard schemes have an authority and a path section. This
// includes file: and filesystem:, which some callers may want to filter out
diff --git a/url/gurl_unittest.cc b/url/gurl_unittest.cc
index c2d86e9..738f2cc 100644
--- a/url/gurl_unittest.cc
+++ b/url/gurl_unittest.cc
@@ -287,6 +287,25 @@ TEST(GURLTest, GetOrigin) {
}
}
+TEST(GURLTest, GetAsReferrer) {
+ struct TestCase {
+ const char* input;
+ const char* expected;
+ } cases[] = {
+ {"http://www.google.com", "http://www.google.com/"},
+ {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/blah"},
+ {"http://user@www.google.com", "http://www.google.com/"},
+ {"http://:pass@www.google.com", "http://www.google.com/"},
+ {"http://:@www.google.com", "http://www.google.com/"},
+ {"http://www.google.com/temp/foo?q#b", "http://www.google.com/temp/foo?q"},
+ };
+ for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
+ GURL url(cases[i].input);
+ GURL origin = url.GetAsReferrer();
+ EXPECT_EQ(cases[i].expected, origin.spec());
+ }
+}
+
TEST(GURLTest, GetWithEmptyPath) {
struct TestCase {
const char* input;