Forum Discussion
Todd_Behrens_20
Nimbostratus
Dec 14, 2017Insert JavaScript If URL path contains word
Hi Everyone, im kind of newbie when it comes to irules and was hoping someone could help me out. I'm looking to insert a javascript into the header of http response only if a certain word is in the U...
Stanislas_Piro2
Cumulonimbus
Dec 14, 2017you can try this code:
In your code, you don't server side http compression. and the stream expression variable is created on every response instead of create it only when the irule is loaded.
when RULE_INIT {
Insert Token Javascript
set jscript {}
Create the stream expression list and insert in this list all rewrite expressions.
set static::stream_expression [list]
lappend static::stream_expression "@@$jscript@"
lappend static::stream_expression "@@$jscript@"
create the extension list to not rewrite
set static::no_rewrite_ext [list ashx asmx axd js]
}
when HTTP_REQUEST {
Check if the word is included in URI and if request extension support javascript insertion.
if {[HTTP::uri] contains "/Anakin" && [lsearch $static::no_rewrite_ext [getfield [URI::basename [HTTP::uri]] "." 2]] == -1 } {
set insertJscript 1
HTTP::header remove "Accept-Encoding"
} else {
set insertJscript 0
}
Disable the stream filter for client requests as we are only interested in the server response
STREAM::disable
}
when HTTP_RESPONSE {
if {$insertJscript && [HTTP::status] == 200 && [HTTP::header value Content-Type] contains "text" && !([HTTP::payload] contains "META HTTP-EQUIV=\"Refresh\"")} {
STREAM::expression $static::stream_expression
STREAM::enable
}
}
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects