Forum Discussion

Javon_Simons_47's avatar
Javon_Simons_47
Icon for Nimbostratus rankNimbostratus
Jan 06, 2009

irule User Agent rewrite

IE problem. It seems that the more .NET updates that you install, the longer your user agent becomes in your registry. I was wondering if this irule would rewrite, IE shorten the user agent:

 

 

when HTTP_REQUEST {

 

change host header

 

if { [HTTP::header "User-Agent"] contains " .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729 " }

 

{

 

HTTP::header replace "; .NET CLR 1.1.4322" " "

 

HTTP::header replace "; .NET CLR 3.0.04506.648" " "

 

HTTP::header replace "; .NET CLR 3.0.04506.30" " "

 

 

}

 

}

 

2 Replies

  • Out of curiosity, why are you trying to trim down the length of the User-Agent header? Does your app not handle the long header value? Could you just truncate it or replace it with a default value if the User-Agent header is over a certain length and contains ".net clr"?

    If not, you could use a regex to replace .net clr followed by the version string.

    'HTTP::header replace' expects the header name and the new value for the header in the following format: 'HTTP::header replace header_name new_value'.

      
      when HTTP_REQUEST {  
        
          Rewrite the User-Agent header value if it's over 100 characters and contains .NET CLR  
         if { [string length [HTTP::header "User-Agent"]] > 100 && [string tolower [HTTP::header "User-Agent"]] contains " .net clr"}{  
        
             Replace the User-Agent header with a default value  
           HTTP::header replace "User-Agent" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; )"  
        
             A more precise option (but also more resource intensive)  
             Replace the .NET CLR strings in the User-Agent header with nothing, using a regex  
            HTTP::header replace "User-Agent" [regsub -all -nocase {\.net clr (?:\d{1,5}\.?){1,4};? ?} [HTTP::header "User-Agent"] ""]  
         }  
      }   
      

    Here is a description of the regex:

    \.net clr (?:\d{1,5}\.?){1,4};? ?

    Match the character "." literally «\.»

    Match the characters "net clr " literally «net clr »

    Match the regular expression below «(?:\d{1,5}\.?){1,4}»

    Between one and 4 times, as many times as possible, giving back as needed (greedy) «{1,4}»

    Match a single digit 0..9 «\d{1,5}»

    Between one and 5 times, as many times as possible, giving back as needed (greedy) «{1,5}»

    Match the character "." literally «\.?»

    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

    Match the character ";" literally «;?»

    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

    Match the character " " literally « ?»

    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

    Aaron
  • Thank you for replying. Yes, the app is having trouble with the long string. It causes DOS of the website in IE. Your first option looks like the way I should go. Thank you.