Commit 518ef5cc authored by mbelshe@chromium.org's avatar mbelshe@chromium.org

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
parent 6a9c55c9
...@@ -28,8 +28,18 @@ bool HostMappingRules::RewriteHost(HostPortPair* host_port) const { ...@@ -28,8 +28,18 @@ bool HostMappingRules::RewriteHost(HostPortPair* host_port) const {
it != map_rules_.end(); ++it) { it != map_rules_.end(); ++it) {
const MapRule& rule = *it; const MapRule& rule = *it;
if (!MatchPatternASCII(host_port->host(), rule.hostname_pattern)) // The rule's hostname_pattern will be something like:
continue; // This rule doesn't apply. // 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); host_port->set_host(rule.replacement_hostname);
if (rule.replacement_port != -1) if (rule.replacement_port != -1)
......
...@@ -37,6 +37,36 @@ TEST(HostMappingRulesTest, SetRulesFromString) { ...@@ -37,6 +37,36 @@ TEST(HostMappingRulesTest, SetRulesFromString) {
EXPECT_EQ(666u, host_port.port()); 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). // Parsing bad rules should silently discard the rule (and never crash).
TEST(HostMappingRulesTest, ParseInvalidRules) { TEST(HostMappingRulesTest, ParseInvalidRules) {
HostMappingRules rules; HostMappingRules rules;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment