summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authoreroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-04 02:33:58 +0000
committereroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-04 02:33:58 +0000
commit7ec7c1899afc8537bb25ce373aa2e136aa7788dc (patch)
treec108e17742ec31aef01eef3824d1388b8a3f8937 /net/base
parent5cd1f8f498abd2ed8d0bd545b224b95918cbec72 (diff)
downloadchromium_src-7ec7c1899afc8537bb25ce373aa2e136aa7788dc.zip
chromium_src-7ec7c1899afc8537bb25ce373aa2e136aa7788dc.tar.gz
chromium_src-7ec7c1899afc8537bb25ce373aa2e136aa7788dc.tar.bz2
Split out HttpUtil::SpecForRequest() into a more generic function of net_util.h.
This was a TODO, since that function is useful outside of HTTP. In the process, I uncovered some test cases in proxy_service that are passing in invalid URLs (by virtue of the extra DCHECK). This doesn't make much sense to me to support that, so I have changed them. Review URL: http://codereview.chromium.org/160558 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22359 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/net_util.cc9
-rw-r--r--net/base/net_util.h5
-rw-r--r--net/base/net_util_unittest.cc44
3 files changed, 58 insertions, 0 deletions
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 7afd4c5..492f7a8 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -1297,4 +1297,13 @@ std::wstring FormatUrl(const GURL& url,
return url_string;
}
+GURL SimplifyUrlForRequest(const GURL& url) {
+ DCHECK(url.is_valid());
+ GURL::Replacements replacements;
+ replacements.ClearUsername();
+ replacements.ClearPassword();
+ replacements.ClearRef();
+ return url.ReplaceComponents(replacements);
+}
+
} // namespace net
diff --git a/net/base/net_util.h b/net/base/net_util.h
index 4320e1c..0e247b8 100644
--- a/net/base/net_util.h
+++ b/net/base/net_util.h
@@ -225,6 +225,11 @@ inline std::wstring FormatUrl(const GURL& url, const std::wstring& languages) {
return FormatUrl(url, languages, true, UnescapeRule::SPACES, NULL, NULL);
}
+// Strip the portions of |url| that aren't core to the network request.
+// - user name / password
+// - reference section
+GURL SimplifyUrlForRequest(const GURL& url);
+
} // namespace net
#endif // NET_BASE_NET_UTIL_H__
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index feb4be7..726529e 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -1269,3 +1269,47 @@ TEST(NetUtilTest, FormatUrlParsed) {
EXPECT_EQ(L"query", formatted.substr(parsed.query.begin, parsed.query.len));
EXPECT_EQ(L"ref", formatted.substr(parsed.ref.begin, parsed.ref.len));
}
+
+TEST(NetUtilTest, SimplifyUrlForRequest) {
+ struct {
+ const char* input_url;
+ const char* expected_simplified_url;
+ } tests[] = {
+ {
+ // Reference section should be stripped.
+ "http://www.google.com:78/foobar?query=1#hash",
+ "http://www.google.com:78/foobar?query=1",
+ },
+ {
+ // Reference section can itself contain #.
+ "http://192.168.0.1?query=1#hash#10#11#13#14",
+ "http://192.168.0.1?query=1",
+ },
+ { // Strip username/password.
+ "http://user:pass@google.com",
+ "http://google.com/",
+ },
+ { // Strip both the reference and the username/password.
+ "http://user:pass@google.com:80/sup?yo#X#X",
+ "http://google.com/sup?yo",
+ },
+ { // Try an HTTPS URL -- strip both the reference and the username/password.
+ "https://user:pass@google.com:80/sup?yo#X#X",
+ "https://google.com:80/sup?yo",
+ },
+ { // Try an FTP URL -- strip both the reference and the username/password.
+ "ftp://user:pass@google.com:80/sup?yo#X#X",
+ "ftp://google.com:80/sup?yo",
+ },
+ { // Try an standard URL with unknow scheme.
+ "foobar://user:pass@google.com:80/sup?yo#X#X",
+ "foobar://google.com:80/sup?yo",
+ },
+ };
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ SCOPED_TRACE(StringPrintf("Test[%d]: %s", i, tests[i].input_url));
+ GURL input_url(GURL(tests[i].input_url));
+ GURL expected_url(GURL(tests[i].expected_simplified_url));
+ EXPECT_EQ(expected_url, net::SimplifyUrlForRequest(input_url));
+ }
+}