Forum Discussion
hooleylist
Jul 27, 2009Cirrostratus
Hi,
The format for string match should be:
% string match
wrong args: should be "string match ?-nocase? pattern string"
And you shouldn't escape periods in a string comparison. They do not have a special meaning of "any character" like they do in regex.
The downside to a string match is that you can't use a wildcard for number of times a character class is repeated. A * will match any character zero to any number of times.
So you can check if the hostname matches x.y.example.com using:
string match -nocase {[a-z0-9]*.[a-z0-9]*.example.com} [HTTP::host]
But scan (Click here) will give you the ability to match a character class any number of times and the ability to save matches. Here are a few examples:
scan returns a count of the number of matches assuming you provide variables to save the matches in
% scan test1.test2.example.com {%[a-z0-9].%[a-z0-9].%[a-z0-9].%[a-z0-9]} a b domain tld
4
% puts "$a $b $domain $tld"
test1 test2 example com
Or:
% scan test1.test2.example.com {%[a-z0-9].%[a-z0-9].example.com} a b
2
% puts "$a $b"
test1 test2
So, you could try something like this:
when HTTP_REQUEST {
Check if host header matches pattern, a.b.example.com
if {[scan [string tolower [HTTP::host]] {%[a-z0-9].%[a-z0-9].example.com} a b] == 2}{
log local0. "[IP::client_addr]:[TCP::client_port]: Matched host [HTTP::host] with $a $b"
HTTP::redirect "https://application.store.com/app/jsp/initialGMCLanding.jsp?id=[HTTP::host]"
} elseif {[string match -nocase {[a-z0-9]*.example.com} [HTTP::host]]}{
Redirect to default location
HTTP::redirect "https://application.store.com/app/jsp/initialGMCLanding.jsp"
} else {
Take some default action?
}
}
Aaron