Forum Discussion

Dana_Knudson_24's avatar
Dana_Knudson_24
Icon for Nimbostratus rankNimbostratus
Feb 16, 2016

Update web page with custom script?

We have a customer who currently has a Stingray that runs a script to update the web page with an overlay with information about the server presenting the page.

 

We need to be able to replicate this process on the LTM. The script seems to be just HTML code. Would I create an iRule to apply the script to the traffic? I haven't had much experience with iRules.

 

2 Replies

  • Hi Dana, iRules can definitely be used to either inject HTML code/script into a response from a backend server (pool member) or to reply back with that script. If the script needs to be maintained on the F5 device then it is best to upload it and store it on BIG-IP as an iFile. There is a very efficient way of performing content injection/manipulation using Stream Profiles and iRules.

     

    Not sure if this is relevant to your case, but here is an example of an iRule which shows how a script for page load performance monitoring from Gomez Networks can be injected: https://devcentral.f5.com/codeshare/gomez-injection

     

    Hope this helps,

     

    Sam

     

  • Hi Dana,

    we are using the iRule below to inject Google-Analytic code into HTML content. It shouldn't be that difficult to adopt our rather simple iRule for your own solution...

     

    when HTTP_RESPONSE {
        STREAM::disable
        if { [string tolower [HTTP::path]] ends_with ".aspx" } then {
            set Insert_GA_Code "XYZ-YOUR_GA_ID-XYZ"
    
             Note: Instead of removing the "Accept-Encoding" header, you could also use a compression profile on your virtual server
                   and make sure the "Keep Accept Encoding" is disabled. This would allow you to still compress the content before 
                   sending it to the client.
            HTTP::header remove "Accept-Encoding"
        }
    }
    when HTTP_RESPONSE {
        if { [info exists Insert_GA_Code] } then {
             if { $debug } { log -noname local0. "$log_prefix Need to inject  GA Tracking Code for the requested ressource." }
            if { [HTTP::header value Content-Type] eq "text/html" {
                 if { $debug } { log -noname local0. "$log_prefix The \"Content-Type\" Headers containing \"text/html\". Preparing the XYZ specific GA Tracking Code for HTTP Response Injection." }
                 if { $debug } { log -noname local0. "$log_prefix Generating the XYZ specific GA Tracking Code." }
                set ga_code_snipped "
    
    "
                 if { $debug } { log -noname local0. "$log_prefix Setting up the SEARCH/REPLACE expression for GA-Code injection." }
                STREAM::expression "@@$ga_code_snipped@"
                 if { $debug } { log -noname local0. "$log_prefix Reenabling Content Streaming Profile." }
                STREAM::enable
                unset -nocomplain Insert_GA_Code
                unset -nocomplain ga_code_snipped
            }
        }
    }
    

     

    Note: We inject the code Google code right before , depending on your requirement right after or even other locations could also be used.

    Note: Our JScript code is escaped to make it TCL friedly while allowing the $Insert_GA_Code variable substitution. If you need dynamic content within your injected code, then you have replace every instance of \=\\ (your first escape!), " = \" , [ = \[ , ] = \] , { = \{ , } = \} using a text editor of your choice. If you don't need any kind of $variable substitution within the code, then just wrap the code into base64 and use set ga_code_snipped [b64decode YOUR/BASE64/DATA==] to fetch the code or even use a $static::variable to store and load the [b64decode YOUR/BASE64/DATA==] data.

    Cheers, Kai