Forum Discussion
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
- Michael_Jenkins
Cirrostratus
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" } } } } } - skfads_167852
Nimbostratus
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
Cirrostratus
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
Cirrostratus
There's a few more examples here: https://clouddocs.f5.com/api/irules/STREAM__expression.html that may help guide a little bit.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
