Forum Discussion
Getting cookie value and using switch
Greetings, I am trying to write an iRule that provides the client with a cookie that we can use to dictate behavior and possibly redirect the user if needed. This is going to be used so we can ensure persistence between sites even if the client DNS server points them to a different data center, while both data centers are available, and they already have an active session. So far I only have a rule to test the cookie portion, but once I get this working the iRule will start to grow and, if there is interest, I can provide the end solution. Here is what I have so far
when HTTP_REQUEST {
if {not [HTTP::cookie exists "Data_Locale"]}
{set need_cookie 1}
set cookie_local [HTTP::cookie value "Data_Locale"]
switch $cookie_local
{
"www1"
{set need_cookie 0}
}
}
when HTTP_RESPONSE {
switch $need_cookie
{
"0"
{HTTP::cookie expires "Data_Locale" 1200 relative}
"1"
{HTTP::cookie insert name "Data_Locale" value "www1"}
}
}
I am able to get the cookie insert to work, and that seems to go without issue, however, for some reason, the cookie expires does not appear to be working. Even when the client has the cookie, for some reason it will not set the expiration. Ideas on what I am missing or where I went wrong? I need it to set the expire and update with every request.
As a note, we are using GTM to balance between sites, and it seems that 1-4% of users end up jumping sites at some point during their session. This is an attempt to correct that.
11 Replies
Hello,
You can try first to log need_cookie variable.
log local0. "need_cookie : $need_cookie"
maybe your variable is not initialized.
I think you should have a default {} in your switch commands
- What_Lies_Bene1
Cirrostratus
Here's the rule with the correct braces and formatted better for readability;
when HTTP_REQUEST { if { not [HTTP::cookie exists "Data_Locale"] } { set need_cookie 1 } set cookie_local [HTTP::cookie value "Data_Locale"] switch $cookie_local { "www1" { set need_cookie 0 } } } } when HTTP_RESPONSE { switch $need_cookie { "0" { HTTP::cookie expires "Data_Locale" 1200 relative } "1" { HTTP::cookie insert name "Data_Locale" value "www1" } } } - HyperSl4ck3r_16
Nimbostratus
I took into account the possibility of the variable not being initialized as well as not having a default switch. I re-wrote the rule as follows.
when HTTP_REQUEST { set cookie_local [HTTP::cookie value Data_Locale] switch $cookie_local { www1 { set need_cookie 0 } default { set need_cookie 1 } } } when HTTP_RESPONSE { switch $need_cookie { 0 { HTTP::cookie expires Data_Locale 1200 relative } 1 { HTTP::cookie insert name Data_Locale value www1 } } }I am again running into the issue. It creates the cookie, but it will still not update the cookie upon a request. Is it something in my set variable that is causing issues?
- What_Lies_Bene1
Cirrostratus
Think I missed something;
when HTTP_REQUEST { if { not [HTTP::cookie exists "Data_Locale"] } { set need_cookie 1 } else { set cookie_local [HTTP::cookie value "Data_Locale"] switch $cookie_local { "www1" { set need_cookie 0 } } } } when HTTP_RESPONSE { switch $need_cookie { "0" { HTTP::cookie expires "Data_Locale" 1200 relative } "1" { HTTP::cookie insert name "Data_Locale" value "www1" } } } - What_Lies_Bene1
Cirrostratus
OK, so removed else, put in a 'return' so that when the 'if' statement matches it exits after the variable is set. What do you want to do if there is no match with the if or switch?
when HTTP_REQUEST { if { not [HTTP::cookie exists "Data_Locale"] } { set need_cookie 1 return } set cookie_local [HTTP::cookie value "Data_Locale"] switch $cookie_local { "www1" { set need_cookie 0 } } } - What_Lies_Bene1
Cirrostratus
Hmmm. The wiki states 'Applies to Version 0 cookies only' - no real idea what that means but perhaps a cause?
- What_Lies_Bene1
Cirrostratus
I think you need to modify your cookie insert code and include a version perhaps?:
HTTP::cookie insert name value [path ] [domain ] [version <0 | 1 | 2>] - What_Lies_Bene1
Cirrostratus
If that's the case I'd suggest you use HTTP::header instead to insert and update the cookie.
- What_Lies_Bene1
Cirrostratus
Hmmm, v11 should really be just fine. Perhaps its because you don't set an expiry when you first create the cookie?
I'm thinking, why not just insert/replace the cookie on every response?
- HyperSl4ck3r_16
Nimbostratus
It looks like after some testing, simply insert/replace the cookie gets the desired result. Here is the finished rule that demonstrates it operates, and that the switches are working properly.
when HTTP_REQUEST { if {not [HTTP::cookie exists Data_Locale]} {set need_cookie 1} set cookie_local [HTTP::cookie value Data_Locale] switch $cookie_local { www1 { set need_cookie 0 } www2 { set need_cookie 3 } www3 { set need_cookie 1 } } } when HTTP_RESPONSE { switch $need_cookie { 0 { HTTP::cookie insert name Data_Locale value www2 HTTP::cookie expires Data_Locale 1200 relative } 1 { HTTP::cookie insert name Data_Locale value www1 HTTP::cookie expires Data_Locale 1200 relative } 3 { HTTP::cookie insert name Data_Locale value www3 HTTP::cookie expires Data_Locale 1200 relative } } }- What_Lies_Bene1
Cirrostratus
Excellent.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com