Replace HTTP refresh with HTTP Redirect

Problem this snippet solves:

Intercepts pages with HTTP REFRESH and re-writes as a proper 302 redirect response.

Special handling or an alternate approach may be required to preserve any cookies or other custom headers returned by the server.

Code :

# iRule to convert meta refresh statements to 302s

when HTTP_REQUEST {
  set meta "meta HTTP-EQUIV=\"REFRESH\""
  # Don't allow data to be chunked
  if { [HTTP::version] eq "1.1" } {
    if { [HTTP::header is_keepalive] } {
      HTTP::header replace "Connection" "Keep-Alive"
    }
     HTTP::version "1.0"
  }
}
when HTTP_RESPONSE {
  # grab the response
  if { [HTTP::header exists "Content-Length"] } {
     set content_length [HTTP::header "Content-Length"]
  } else {
     set content_length 1000000
  }
  if { $content_length > 0 } {
     HTTP::collect $content_length
  }
}
when HTTP_RESPONSE_DATA {
  # look for Meta Refresh
  if {[string tolower [HTTP::payload]] contains $meta }{
    # look for refresh URL
    set urlresponse [findstr [string tolower [HTTP::payload]] "URL=" 4 {">}]
    if { $urlresponse != "" } {
      # pull URL out of URL Tag in Meta Refresh statement
      # respond with 302
      HTTP::respond 302 Location $urlresponse
    }
  }
}
Published Mar 18, 2015
Version 1.0
No CommentsBe the first to comment