Forum Discussion
hooleylist
Apr 08, 2010Cirrostratus
Hi Brett,
I tried a regex, but it was about 10x less efficient than a couple of string and list operations:
Simple perf test:
% time {set host_list [split $host "."]; lindex $host_list [expr {[llength $host_list] -3}]} 1000
13 microseconds per iteration
% time {regexp -inline -nocase {(?:[^.]+\.){0,}([^.]+)\.mydomain\.com} test.mydomain.com} 1000
133 microseconds per iteration
when HTTP_REQUEST {
log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]"
Split the host header value on periods into a list
set host_list [split [HTTP::host] "."]
log local0. "[IP::client_addr]:[TCP::client_port]: Parsed host list $host_list"
Check if there were more than three fields
if {[llength $host_list] > 3}{
Send a redirect with the third to last field from the requested host prepended to .mydomain.com
HTTP::respond 301 Location "https://[lindex $host_list [expr {[llength $host_list] -3}]].mydomain.com"
log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting to\
https://[lindex $host_list [expr {[llength $host_list] -3}]].mydomain.com"
}
}
Aaron