Forum Discussion

Greg_Ryan_33844's avatar
Greg_Ryan_33844
Icon for Nimbostratus rankNimbostratus
Jul 20, 2010

iRule to set a cookie based on url

Hi, this is one of my first iRules and I am completely at a loss. What I am trying to do is set a cookie based on a 'contains' in the url. Once the cookie is set then I want to switch the pool based on the cookie value. I can get the second part to work, what I can't seem to do is to set a cookie based on an if statement. Has anyone done anything like this?

 

 

when HTTP_REQUEST {

 

 

if { [HTTP::uri] contains "newAdmin=1" } {

 

set newadmin_cookie "1"

 

pool 81_pool

 

}

 

elseif { [HTTP::uri] contains "newAdmin=0" } {

 

set newadmin_cookie "0"

 

pool server_pool

 

}

 

if { ([HTTP::cookie value "newadmin"] equals "1") } {

 

pool 81_pool

 

}

 

elseif { ([HTTP::cookie value "newadmin"] equals "0") } {

 

pool server_pool

 

}

 

 

}

 

 

when HTTP_RESPONSE {

 

HTTP::cookie insert name "newadmin" value $newadmin_cookie path "/"

 

}

 

1 Reply

  • Hi Greg,

    That looks like a very good start. I assume newAdmin is a parameter name in the query string that you want to check the value for? You could use a switch statement to check the parameter value. If that's not set to 0 or 1, you could use another switch statement to check the newadmin cookie value.

    Do you want to do something if there isn't a match on the parameter or cookie values? If so you can put that in the default switch case at the end of the iRule.

    when HTTP_REQUEST {
        Initialize the newadmin cookie value to something other than 0 or 1
       set newadmin_cookie undefined
       log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]"
        Check the query string for a parameter named newAdmin
        Use a workaround described in http ://devcentral.f5.com/wiki/default.aspx/iRules/uri__query
       switch [URI::query "?&[HTTP::query] &newAdmin] {
          "0" {
              newAdmin parameter set to 0
             set newadmin_cookie "0"
             pool server_pool
             log local0. "[IP::client_addr]:[TCP::client_port]: newAdmin param match for 0, using server_pool"
          }
          "1" {
              newAdmin parameter set to 1
             set newadmin_cookie "1"
             pool 81_pool
             log local0. "[IP::client_addr]:[TCP::client_port]: newAdmin param match for 1, using 81_pool"
          }
          default {
              newAdmin parameter not set to 0 or 1
              Check for a newadmin cookie
             switch [HTTP::cookie value "newadmin"] {
                "0" {
                    newAdmin cookie set to 0
                   set newadmin_cookie "0"
                   pool server_pool
                   log local0. "[IP::client_addr]:[TCP::client_port]: newAdmin cookie match for 0, using server_pool"
                }
                "1" {
                    newAdmin cookie set to 1
                   set newadmin_cookie "1"
                   pool 81_pool
                   log local0. "[IP::client_addr]:[TCP::client_port]: newAdmin cookie match for 1, using 81_pool"
                }
                default {
                    No match for newAdmin parameter or cookie. Take some default action?
                   log local0. "[IP::client_addr]:[TCP::client_port]: No match on newAdmin parameter or cookie"
                }
             }
          }
       }
    }
    when HTTP_RESPONSE {
       HTTP::cookie insert name "newadmin" value $newadmin_cookie path "/"
    }

    Aaron