Forum Discussion

Pierce_Fortin's avatar
Pierce_Fortin
Icon for Nimbostratus rankNimbostratus
Dec 12, 2017

iRule replace substring in cookie name

Hi,

I need to write an iRule which will rewrite part of the cookie name and the cookie path. For example:

Set-Cookie: ASP.NET_SessionId=f3izvd1kvrlsfy0wc2ui1t5k;Path=/ needs to become:

Set-Cookie: SpecialName-SessionID=f3izvd1kvrlsfy0wc2ui1t5k;Path=/foo-bar/

 when HTTP_RESPONSE {    
    set cookies [HTTP::cookie names]    
    foreach cookie $cookies {        
        set cookie_value [HTTP::cookie $cookie]        
        set cookie_path [string map -nocase {"/" "/FOO-BAR/"} 
        [HTTP::cookie path $cookie]]
        set cookie_name [string map -nocase {"ASP.NET_SessionID" "Special-Name-"} 
        [HTTP::cookie path $cookie]]        
        HTTP::cookie remove $cookie        
        HTTP::cookie insert name $cookie value $cookie_value path $cookie_path    
  }

  }

The first part (rewriting the path) works but rewriting the cookie name fails, I think it's because its expecting to match on the full string but I need to match on a substring.

1 Reply

  • I wrongly understood how cookies were formatted. This works for me:

     when HTTP_RESPONSE {    
        set cookies [HTTP::cookie names]    
        foreach cookie $cookies {        
            set cookie_value [HTTP::cookie $cookie]        
            set cookie_path [string map -nocase {"/" "/FOO-BAR/"} 
            [HTTP::cookie path $cookie]]
            HTTP::cookie remove $cookie        
            HTTP::cookie insert name Special-Name value $cookie_value path $cookie_path    
      }
    
      }