Forum Discussion

Nick_Matthews's avatar
Nick_Matthews
Icon for Altostratus rankAltostratus
Aug 21, 2008

Search and Replace with iRule

Hi,

 

 

I am hoping someone can help.

 

 

What I want to do is search in the header for a specific user agent and then find and replace a specific word in the html. Can some one give me some pointers on how I might do this.

 

 

This is what I have so far:

 

 

when HTTP_REQUEST {

 

if { [HTTP::header "User-Agent"] contains "xxx"}

 

{

 

 

How do I now search for the text and replace it?

 

 

Any help would be appreciated.

5 Replies

  • OK,

     

     

    I have looked into this again and found that using Stream might be the best option, so what I have so far is:

     

     

    when HTTP_RESPONSE {

     

     

    Disable the stream filter by default

     

    STREAM::disable

     

     

    Check if response type is text

     

    if {[HTTP::header value Content-Type] contains "text" and [HTTP::header "User-Agent"] contains "***"}{

     

     

    Replace

     

    STREAM::expression "@123@xyz@ @456@xyz@"

     

     

    Enable the stream filter for this response only

     

    STREAM::enable

     

    }

     

    }

     

     

    However I also need to check the uri as I only want to replace on certain areas. How can I do this? Can I use HTTP_REQUEST with the HTTP_RESPONSE?
  • Hi,

    You can try to use the option string map

    if you want to replace for example the directory "admin" by "secret"

     
     when HTTP_REQUEST { 
        if {[HTTP::uri] contains "/admin/"} { 
             HTTP::uri [string map {"/admin/" "/secret/"} [HTTP::uri]] 
        } 
     } 
     

    HTH

  • Hi,

     

     

    Thanks for replying, however I am looking to replace the text in the content rather than the URI. I just need to only run the code if the user is requesting a specific area of the site. I.e if someone were to access "http://www.mydomain.com/atoz/ I would want the code to run and replace all the /images/ references to http://www.mydomain.com/images/ but I wouldn't want this to happen on the rest of the site.
  • Sorry i missed this part then you can try something like this:

       
       when HTTP_REQUEST {    
           set replace_content 0   
           if {[HTTP::uri] contains "/atoz/"} {    
               set replace_content 1    
           }    
        }    
           
       when HTTP_RESPONSE {   
         if {$replace_content equals "1"} {   
              Disable the stream filter by default   
             STREAM::disable   
          
             Check if response type is text   
            if {[HTTP::header value Content-Type] contains "text" and [HTTP::header "User-Agent"] contains "***"}{   
                  Replace   
                 STREAM::expression "@123@xyz@ @456@xyz@"   
                  Enable the stream filter for this response only   
                 STREAM::enable   
             }   
         }   
       }    
       

    the variable replace_content can be shared as a local variable among all the events you want. This variable will be available for this TCP connection only