Forum Discussion
Russell_McGinni
Nimbostratus
Sep 16, 2009iRule to replace http response data causes havoc
We have a live and beta VIP setup in F5 and in order to keep users in the environment they started in, we would like to use an iRule - we already use an iRule to inject a javascript include on most html pages and thought we could piggy back on that.
However when we included code to replace "www" with "beta" in our urls our F5's started failing over - not instantly - but more frequently when traffic peeked, less frequently during low traffic usage.
Any ideas what we are doing wrong ?
Many thanks in advance
Russell
when RULE_INIT {
Text to inject at the end of the stream
set ::InjectionText ""
set ::InjectText 0
set ::uri ""
}
when HTTP_REQUEST {
Save the URI so we can check it in the response
set ::uri [string tolower [HTTP::uri]]
set ::InjectText 0
}
when HTTP_RESPONSE {
if { [HTTP::status] eq "200" } {
set replace 0
set ctype [HTTP::header Content-Type]
First check to see if we are dealing with html or js content
switch -glob $ctype {
"*html*" -
"*javascript*" -
"*xml*" {
set replace 1
}
}
Incase there is no content type set on an htm(l) or js file
switch -glob $::uri {
"*.htm*" -
"*.js" {
set replace 1
}
}
If we have html content, make sure we add the Injection text later
if { $ctype starts_with "text/html" } {
set ::InjectText 1
}
But dont add the Injection text if we are dealing with certain files
That have a content type of text/html
switch -glob $::uri {
"*.ashx*" -
"*.asmx*" -
"*.axd*" -
"*.js*" -
"/members/login/*" -
"/members/uploader*" {
set ::InjectText 0
}
}
Trigger the event for HTTP_RESPONSE_DATA if needed
if { $replace == 1 } {
log local0. "Replace value: $replace"
if { [HTTP::header exists "Content-Length"] } {
set content_length [HTTP::header "Content-Length"]
} else {
set content_length 2048000
}
if { $content_length > 0 && $content_length < 2048001} {
HTTP::collect $content_length
} else {
log local0. "Http content greater than 2mb (actual: $content_length) for: $::uri"
}
}
}
}
when HTTP_RESPONSE_DATA {
First check to see if we have any text to inject
if { $::InjectText == 1 } {
set idx [string last "" [HTTP::payload]]
if { -1 == $idx } {
set idx [string last "" [HTTP::payload]]
}
if { -1 == $idx } {
set offset [HTTP::payload length]
log local0.debug "Got here but NOT injecting text into $::uri"
} else {
set offset $idx
HTTP::payload replace $offset 0 $::InjectionText
log local0.debug "Injecting text into $::uri"
}
}
set clen [string length [HTTP::payload]]
set old_name "www.mydomain.com"
set new_name "beta.mydomain.com"
set old_name_len [string length $old_name]
set new_name_len [string length $new_name]
set last [string last $old_name [HTTP::payload $clen]]
for {set old_index 0} {$old_index < $last} {incr old_index $new_name_len}{
set old_index [string first $old_name [HTTP::payload $clen] $old_index]
HTTP::payload replace $old_index $old_name_len $new_name
}
set clen [string length [HTTP::payload]]
set old_name "www.myotherdomain.org"
set new_name "beta.myotherdomain.org"
set old_name_len [string length $old_name]
set new_name_len [string length $new_name]
set last [string last $old_name [HTTP::payload $clen]]
for {set old_index 0} {$old_index < $last} {incr old_index $new_name_len} {
set old_index [string first $old_name [HTTP::payload $clen] $old_index]
HTTP::payload replace $old_index $old_name_len $new_name
}
HTTP::release
}
- hoolio
Cirrostratus
Hi,Trigger collection for up to 1MB of data if {([HTTP::header exists "Content-Length"]) && ([HTTP::header "Content-Length"] <= 1024000)}{ set content_length [HTTP::header "Content-Length"] } else { set content_length 1024000 } if { [info exists content_length] } { HTTP::collect $content_length if {$::rule_debug}{log local0. "[IP::client_addr]:[TCP::client_port]: Collecting $content_length"} }
- Russell_McGinni
Nimbostratus
Thanks Aaron for the prompt and helpful reply... - Russell_McGinni
Nimbostratus
Thanks Aaron, is my assumption correct, that if I do not "collect" for payloads greater than a limit (2Mb in my example), the HTTP_RESPONSE_DATA event isnt triggered - and therefore no payload replacement takes place ? - hoolio
Cirrostratus
Hi Russell,
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects