Devonion_22138
May 14, 2012Nimbostratus
Redirect to test server and rewrite returned links
I'm trying to write an irule so that I can use a cloned live webserver to test a PHP upgrade.
irule basically changes host header from www.dev.ourserver.com to www.ourserver.com and this part works fine if I only include the HTTP_REQUEST section.
However, when I try to add the HTTP_RESPONSE and HTTP_RESPONSE_DATA sections, I start getting "server hangup"s.
Any help would be most appreciated.
My irule is as follows
when HTTP_REQUEST {
set DEBUG 1
if { [HTTP::version] equals "1.1" } {
if { [HTTP::header is_keepalive] } {
HTTP::header replace "Connection" "Keep-Alive"
HTTP::header remove Accept-Encoding
}
HTTP::version "1.0"
set collected 0
}
if { [HTTP::host] equals "devserverurl"} {
HTTP::header replace "Host" "serverurl"
if { $DEBUG } { log local0. "irule used by IP: [IP::client_addr] - requesting [HTTP::host][HTTP::uri]" }
}
}
when HTTP_RESPONSE {
Get the content length so we can request the data to be
processed in the HTTP_RESPONSE_DATA event.
set DEBUG 1
if { [HTTP::header exists "Content-Length"] } {
set content_length [HTTP::header "Content-Length"]
} else {
set content_length 0
}
content_length of 0 indicates chunked data (of unknown size)
if { $content_length > 0 && $content_length < 1024001 } {
set collect_length $content_length
} else {
set collect_length 1024000
}
if { $DEBUG } {
log local0. "[IP::client_addr]:[TCP::client_port]: Content Length:: $content_length Collect Length: $collect_length"
}
if { $collect_length > 0 } {
HTTP::collect $collect_length
}
}
when HTTP_RESPONSE_DATA {
set DEBUG 1
regsub -all "serverurl" [HTTP::payload] "devserverurl" newdata
if { $DEBUG } {
log local0. "PAYLOAD before replace: [HTTP::payload]"
}
HTTP::payload replace 0 $content_length $newdata
if { $DEBUG } {
log local0. "PAYLOAD after replace: [HTTP::payload]"
}
HTTP::release
if { $content_length > 0 } {
for unchunked data, calculate the remaing length & re-collect if necessary
the HTTP_RESPONSE_DATA event will be triggered again when each collection is complete
set collect [expr {$collected + $collect_length}]
set remaining [expr {$content_length - $collected}]
if { $remaining > 0 } {
if { $remaining < $collect_length } {
set collect_length $remaining
}
HTTP::collect $collect_length
}
} else {
chunked responses data, continue collecting in 1MB chunks. Watch for hanging responses here.
The HTTP_RESPONSE_DATA event will be triggered again when each ccollection is complete
HTTP::collect $collect_length
}
}
Many Thanks