Substitute - a simple iRule script to simulate apache mod_substitute
Problem this snippet solves:
Sometimes it is neccessary to rewrite some stuff in delivered HTTP-Content. Here we have a simple way to search an replace these thinks.
How to use this snippet:
If you want to use these script you have to provide a data group with type string named
Substitude_<VS>
Here each line have the format
ul
Code :
when RULE_INIT { # Enable to debug Substitute translations via log messages in /var/log/ltm # (2 = verbose, 1 = essential, 0 = none) set static::SubstituteDebug 0 } when HTTP_REQUEST { set static::log_prefix "[IP::client_addr]:[TCP::local_port]:" # Disable the stream filter for all requests STREAM::disable # The name of the Data Group (aka class) we are going to use. # Parse just the virtual server name by stripping off the folders (if present) set class_name "Substitute_[URI::basename [virtual name]]" if { $static::SubstituteDebug > 1 } { log local0. "$static::log_prefix [IP::client_addr]:[TCP::client_port] -> [IP::local_addr]:[TCP::local_port]" } } when HTTP_RESPONSE { # disable stream by default STREAM::disable if { $static::SubstituteDebug > 1}{ log local0. "$static::log_prefix: disable stream per default" } # Create all essential variables for the logic below # set all variables to an empty value set regexreplace "" set regex "" set replace "" # Check whether class_name exist or not. # If it doesn't exists log an that the data group is missing an exiting if {! [class exists $class_name]} { log local0. "$log_prefix: Data group $class_name not found, exiting." return } else { # Here we know that the needed data group exists and we can go on # set id for iteration purpose set id [class startsearch $class_name] # iterate over data group list to get all regex replace pairs while {[class anymore $class_name $id]}{ # save actual line of data group in element # extract regex and replace from element set element [class nextelement $class_name $id] set regex [getfield $element " " 1] set replace [getfield $element " " 2 ] if { $static::SubstituteDebug > 1} { log local0. "$log_prefix: regex=$regex, replace=$replace" } # and build a string with all regex replace pairs set regexreplace "@$regex@$replace@ $regexreplace" } if { $static::SubstituteDebug > 1} { log local0. "$log_prefix: regexreplace=$regexreplace" } # if regexreplace is set if {$regexreplace ne ""}{ # and content-type equals text if {[HTTP::header value Content-Type] contains "text"}{ if { $static::SubstituteDebug > 1} { log local0. "$log_prefix: try STREAM::expression [string trim $regexreplace " "]" } # apply regexreplace pairs to stream STREAM::expression [string trim $regexreplace " "] if { $static::SubstituteDebug > 1}{ log local0. "$log_prefix: enable stream" } # Enable the stream filter for this response only STREAM::enable } } } }
Tested this on version:
11.5Updated Jun 06, 2023
Version 2.0Christian_Meiss
Nimbostratus
Joined May 05, 2019
No CommentsBe the first to comment