APM Portal Host Rewrite

Problem this snippet solves:

Sometimes it’s not necessary, and even redundant, for a link to an external site to go through the reverse proxy process. In the following iRules, we added the tags apm_do_not_touch, and /apm_do_not_touch to indicate that the information between those tags can be safely ignored by the rewrite. Before the client receives the data, we clean up the tags (by removing them) so that we can shield our internal work from our clients.

Code :

when REWRITE_REQUEST_DONE {
    switch "[HTTP::host][HTTP::path]" {
        "intra.fp.f5net.com/contents.php" {
            set match 1
            REWRITE::post_process 1
        }
        default {
            # Reset the value of match
            # log local0. "No match for [HTTP::host][HTTP::path]"
            set match 0
        }
    }
    if { $match == 1 } {
        if { [HTTP::version] eq "1.1" } {
            # Force downgrade to HTTP 1.0, to disallow chunched responses, but still allow keep-alive connections.
            # Since 1.1 is keep-alive by default, and 1.0 isn't,
            # we need make sure the headers reflect the keep-alive status.
            
            if { [HTTP::header is_keepalive] } {
                # Replace the connection header value with "Keep-Alive"
                HTTP::header replace "Connection" "Keep-Alive"
            }
            HTTP::version "1.0"
        }
    }
}

when HTTP_RESPONSE {
    if {$match == 1} {
        if { [HTTP::header exists "Content-Length"] and [HTTP::header "Content-Length"] <= 1048576 } {
            HTTP::collect [HTTP::header Content-Length]
            # log local0. "Collecting [HTTP::header Content-Length] bytes on the response."
        } else {
            HTTP::collect 1048576 ;# 1MB
            # log local0. "Collecting first 1 Mbytes on the response."
        }
    }
}

when HTTP_RESPONSE_DATA {
    set data [HTTP::payload]
    set start [string first {} $data]
    # Set the start and end variables to be the locations in the payload where you want to insert the tags
    set end [expr {[string first {} $data $start] + [string length ""]}]
    # We want the index at the end of the  tag

    # log local0. "Starting Index:: $start ::: Ending Index:: $end"

    HTTP::payload replace $end 0 {} 
    # Notice the length is zero because we do not want to remove any of the original code
    HTTP::payload replace $start 0 {}
    # We inserted the close tag first because we don't want to take into account how much was inserted before the close tag

    HTTP::release
}


when REWRITE_RESPONSE_DONE {
    set data [REWRITE::payload]
    # Find the tags we inserted
    set start [string first {} $data]
    set end [string last {} $data]
    # Determines the amount of characters to remove
    set length_open [string length {}]
    set length_close [string length {}]

    # log local0. "Starting Index:: $start ::: Ending Index:: $end"

    # Replace the number of characters taken up by the tags with nothing (i.e. null)
    REWRITE::payload replace $end $length_close {}
    REWRITE::payload replace $start $length_open {}
}

Tested this on version:

10.0
Published Jan 30, 2015
Version 1.0

Was this article helpful?