summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-21 02:12:42 +0000
committermbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-21 02:12:42 +0000
commit518ef5cc41db9ed09acb4e9df7ded8ab4f01e8f5 (patch)
tree6cc83412273487716d74302450870051e565357a
parent6a9c55c96041b2b1bc51f220d0f6d5765a66f792 (diff)
downloadchromium_src-518ef5cc41db9ed09acb4e9df7ded8ab4f01e8f5.zip
chromium_src-518ef5cc41db9ed09acb4e9df7ded8ab4f01e8f5.tar.gz
chromium_src-518ef5cc41db9ed09acb4e9df7ded8ab4f01e8f5.tar.bz2
Add a feature to the HostResolverRules to be able to match rules only
for a specific port. This allows me to alter resolutions for certain ports like send :80 lookups to one location and :443 lookups to a different location. This enables me to mock out support for AlternateProtocol. BUG=none TEST=HostMappingRulesTest.PortSpecificMatching Review URL: http://codereview.chromium.org/3177026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56970 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--net/base/host_mapping_rules.cc14
-rw-r--r--net/base/host_mapping_rules_unittest.cc30
2 files changed, 42 insertions, 2 deletions
diff --git a/net/base/host_mapping_rules.cc b/net/base/host_mapping_rules.cc
index ab560a7..56b9694 100644
--- a/net/base/host_mapping_rules.cc
+++ b/net/base/host_mapping_rules.cc
@@ -28,8 +28,18 @@ bool HostMappingRules::RewriteHost(HostPortPair* host_port) const {
it != map_rules_.end(); ++it) {
const MapRule& rule = *it;
- if (!MatchPatternASCII(host_port->host(), rule.hostname_pattern))
- continue; // This rule doesn't apply.
+ // The rule's hostname_pattern will be something like:
+ // www.foo.com
+ // *.foo.com
+ // www.foo.com:1234
+ // *.foo.com:1234
+ // First, we'll check for a match just on hostname.
+ // If that fails, we'll check for a match with both hostname and port.
+ if (!MatchPatternASCII(host_port->host(), rule.hostname_pattern)) {
+ std::string host_port_string = host_port->ToString();
+ if (!MatchPatternASCII(host_port_string, rule.hostname_pattern))
+ continue; // This rule doesn't apply.
+ }
host_port->set_host(rule.replacement_hostname);
if (rule.replacement_port != -1)
diff --git a/net/base/host_mapping_rules_unittest.cc b/net/base/host_mapping_rules_unittest.cc
index 09e7f45..9ecd4b7 100644
--- a/net/base/host_mapping_rules_unittest.cc
+++ b/net/base/host_mapping_rules_unittest.cc
@@ -37,6 +37,36 @@ TEST(HostMappingRulesTest, SetRulesFromString) {
EXPECT_EQ(666u, host_port.port());
}
+TEST(HostMappingRulesTest, PortSpecificMatching) {
+ HostMappingRules rules;
+ rules.SetRulesFromString(
+ "map *.com:80 baz:111 , map *.com:443 blat:333, EXCLUDE *.foo.com");
+
+ // No match
+ HostPortPair host_port("test.com", 1234);
+ EXPECT_FALSE(rules.RewriteHost(&host_port));
+ EXPECT_EQ("test.com", host_port.host());
+ EXPECT_EQ(1234u, host_port.port());
+
+ // Match port 80
+ host_port = HostPortPair("crack.com", 80);
+ EXPECT_TRUE(rules.RewriteHost(&host_port));
+ EXPECT_EQ("baz", host_port.host());
+ EXPECT_EQ(111u, host_port.port());
+
+ // Match port 443
+ host_port = HostPortPair("wtf.com", 443);
+ EXPECT_TRUE(rules.RewriteHost(&host_port));
+ EXPECT_EQ("blat", host_port.host());
+ EXPECT_EQ(333u, host_port.port());
+
+ // Match port 443, but excluded.
+ host_port = HostPortPair("wtf.foo.com", 443);
+ EXPECT_FALSE(rules.RewriteHost(&host_port));
+ EXPECT_EQ("wtf.foo.com", host_port.host());
+ EXPECT_EQ(443u, host_port.port());
+}
+
// Parsing bad rules should silently discard the rule (and never crash).
TEST(HostMappingRulesTest, ParseInvalidRules) {
HostMappingRules rules;