Forum Discussion

maximillean_953's avatar
maximillean_953
Icon for Nimbostratus rankNimbostratus
Apr 07, 2014

Example Irule

Hi, I try to translate a content switching rule from netscaler to F5 Irule for migration which still keeps going on.

I need a rule that if first 2 path of uri /cookies/set/xyz

then rule does 302 temp redirect to www.123.com with 3rd path string pasted to Set-cookie header member as Set-Cookie:TESTROUTER=xyz; and the rest mentioned below example.

curl -I 10.35.72.172/cookies/set/xyz

HTTP/1.1 302 Temporary Redirect
Location: http://www.123.com
Set-Cookie:TESTROUTER=xyz; path=/; expires=Tue, 08 Apr 2014 12:38:25 GMT; domain=.123.com

Any idea how can i do this? I try to do couple things but did not succeed yet

7 Replies

  • Give this a try (untested):

    when HTTP_REQUEST {
      if { [string tolower [HTTP::path]] starts_with "/cookies/set/" } {
        set ckname "TESTROUTER"
        set ckvalue [getfield [HTTP::path] "/" 3]
        set cookie [format "%s=%s; path=/; domain=%s" $ckname $ckvalue ".123.com"]
        HTTP::respond 302 Location [HTTP::host] "Set-Cookie" $cookie
      }
    }
    
  • Thanks Jason works perfectly except that cookie header doesnot have expires which includes in example i post above. How sould i change set cookie line formatting to include expires date with it?

     

  • John_Alam_45640's avatar
    John_Alam_45640
    Historic F5 Account

    This is untested.

    Also see this page: https://devcentral.f5.com/wiki/iRules.HTTP__respond.ashx
    when HTTP_REQUEST {
        if { [HTTP::uri] starts_with "/cookies/set" } {
            set fmt "%d %b %Y %H:%M:%S %Z"
            set expiry [clock format [clock seconds] -format $fmt]
            set domain ".[getfield [HTTP::host] . 2].[getfield [HTTP::host] . 3]"
            set testrouter [string trim [URI::path [HTTP::uri] 3] "/"]
    
            HTTP::respond 302 -version 1.1 Set-Cookie "TESTROUTER=$testrouter; path=/; expires=$expiry; domain=$domain" Location "http://www.123.com"
        }
    }
    
  • OK I solved it with

    expires=[clock format [expr ([clock seconds]+86400)] -format "%a, %d %h %Y %T GMT" -gmt true];
    

    So new one is ;

    when HTTP_REQUEST {
      if { [string tolower [HTTP::path]] starts_with "/cookies/set/" } {
        set ckname "TESTROUTER"
        set ckvalue [getfield [HTTP::path] "/" 4]
        set cookie [format "%s=%s; path=/; expires=[clock format [expr ([clock seconds]+86400)] -format "%a, %d %h %Y %T GMT" -gmt true]; domain=%s" $ckname $ckvalue ".123.com"]
        HTTP::respond 302 Location [HTTP::host] "Set-Cookie" $cookie
      }
    }
    
  • this one above is tested and works perfectly same output. As others only thing left is one respond with http 1.1 f5 respond with http 1.0

    Netscaler One
    curl -I 10.35.72.172/cookies/set/xyz                              
    HTTP/1.1 302 Temporary Redirect
    Location: http://www.123.com
    Set-Cookie:TESTROUTER=xyz; path=/; expires=Tue, 08 Apr 2014 13:43:25 GMT; domain=.123.com
    
    F5 One
    curl -H "Host:www.123.com" -I 10.35.78.76/cookies/set/xyz
    

    HTTP/1.0 302 Found Location: www.gittigidiyor.com Set-Cookie: TESTROUTER=xyz; path=/; expires=Tue, 08 Apr 2014 13:43:52 GMT; domain=.123.com Server: BigIP Connection: Keep-Alive Content-Length: 0