Forum Discussion

skfads_167852's avatar
skfads_167852
Icon for Nimbostratus rankNimbostratus
Aug 19, 2015

Need help with understanding this iRule

I have very limited or no knowledge about iRule. It would be of great help if anyone can be able to break down this iRule below.

when RULE_INIT { set static::New_netsector_response_body { "" ":80====nothing" } }

when HTTP_RESPONSE { STREAM::disable

if {[llength $static::New_netsector_response_body] > 0} { set expression "" foreach New_netsector_request_rewriteRecord $static::New_netsector_response_body { set New_netsector_request_find [getfield $New_netsector_request_rewriteRecord "====" 1] set New_netsector_request_replace [getfield $New_netsector_request_rewriteRecord "====" 2] if {$New_netsector_request_replace == "nothing"} { set New_netsector_request_replace "" }

        set expression "$expression@$New_netsector_request_find@$New_netsector_request_replace@"
    }
    if {[HTTP::header Content-Type] contains "text" or [HTTP::header Content-Type] contains "xml" or [HTTP::header Content-Type] contains "java"}
    {
        if { [catch
        {
            STREAM::expression $expression
            STREAM::enable
        } result] }
        {
               log local0. "fffffffffff $result"
        }
    }
}

}

4 Replies

  • Hopefully this will help you understand what's happening.

    when RULE_INIT { 
         Create a static variable to hold the find/replace values you want to check
        set static::New_netsector_response_body { "http://====https://" ":80====nothing" } 
    }
    when HTTP_RESPONSE { 
         Disable the stream rewriter (to be enabled only if you need to rewrite something)
        STREAM::disable
    
         Check that the list in RULE_INIT has items
        if {[llength $static::New_netsector_response_body] > 0}
        {
             Default the expression to nothing
            set expression ""
    
             Loop through each of the list items
            foreach New_netsector_request_rewriteRecord $static::New_netsector_response_body
            {
                 Get the first part of the list item (before the "====") as the "find"
                set New_netsector_request_find [getfield $New_netsector_request_rewriteRecord "====" 1]
                 Get the last part of the list item (after the "====") as the "replace"
                set New_netsector_request_replace [getfield $New_netsector_request_rewriteRecord "====" 2]
    
                 If the "replace" value is "nothing" then default replace to blank
                if {$New_netsector_request_replace == "nothing"} {
                    set New_netsector_request_replace ""
                }
    
                 Append the find/replace values to the expression string
                set expression "$expression@$New_netsector_request_find@$New_netsector_request_replace@"
            }
    
             Check the response to only try to rewrite these specific content-types
            if {[HTTP::header Content-Type] contains "text" or [HTTP::header Content-Type] contains "xml" or [HTTP::header Content-Type] contains "java"}
            {
                 Try to set and enable the stream rewriter expression (catching an error if it occurs so it doesn't blow up the iRule)
                if { [catch {
                    STREAM::expression $expression
                    STREAM::enable
                } result] }
                {
                     Log the error message
                       log local0. "fffffffffff $result"
                }
            }
        }
    }
    

    On a separate note, this seems really inefficient, and it could be rewritten to remove a bit. I'd recommend just setting the expression in the static variable and using that... like this (not tested, so may contain a syntax error somewhere)

    when RULE_INIT { 
         Create a static variable to hold the expression
        set static::New_netsector_expression { @http://@https://@ @:80@@ } 
    }
    when HTTP_RESPONSE { 
         Disable the stream rewriter (to be enabled only if you need to rewrite something)
        STREAM::disable
    
         Check that the list in RULE_INIT has items
        if {not($static::New_netsector_response_body equals "")} {        
             Check the response to only try to rewrite these specific content-types
            switch -glob [HTTP::header value Content-Type] {
                "*text*" -
                "*xml*" - 
                "java" {
                     Try to set and enable the stream rewriter expression (catching an error if it occurs so it doesn't blow up the iRule)
                    if { [catch {
                                STREAM::expression $expression
                                STREAM::enable
                            } result] } {
                         Log the error message
                           log local0. "fffffffffff $result"
                    }
                }
            }
        }
    }
    
  • Thanks Michael...

     

    The explanation helps a lot.

     

    The problem with my irule is that we have set the in out webpage to "font-size:80px" and this irule is rewiring ":80" to "" which results in deformed webpage.

     

    • Michael_Jenkins's avatar
      Michael_Jenkins
      Icon for Cirrostratus rankCirrostratus
      I think you could do something like this then as your expression "@:80(?!px)@@" for the port. Since the expression allows regex, you can use that to handle the lookahead to make sure the :80 isn't followed by "px"
    • Michael_Jenkins's avatar
      Michael_Jenkins
      Icon for Cirrostratus rankCirrostratus
      There's a few more examples here: https://clouddocs.f5.com/api/irules/STREAM__expression.html that may help guide a little bit.