irule pool selection based on dynamic cookie value

Hi, my scenario: i want to select a pool based on a cookie value - that works as aspected

when_HTTP_REQUEST {
if { [HTTP::cookie exists "myCookie"] } {
  switch -glob [HTTP::cookie value "myCookie"] {
    "foo" { pool foo }
    "bar" { pool bar }
    default { pool default  }
  }
} else {
    pool default 
}
}

i’m struggle at the point to set the cookie for a uri match condition

when_HTTP_REQUEST {
if { [string tolower [HTTP::uri]] equals "/foo/" } {
    psuedo code: set cookie myCookie value foo
}
if { [string tolower [HTTP::uri]] equals "/bar/" } {
    psuedo code: set cookie myCookie value bar
}
}

i know that i have to put the cookie insert in the HTTP_RESPONSE so that the browser add it but in this context is a uri match no possible have anyone a idea how i can set a cookie in the response with a condition in the request? thanks for your ideas

Cookies are set using the following syntax:

HTTP::cookie insert name  value  [path ] [domain ] [version <0 | 1 | 2>]

I’ve updated your iRule, please give it a try and let me know how you get on

when_HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] equals "/foo/" } {
        HTTP::cookie insert name "myCookie" value "foo"
    }
    if { [string tolower [HTTP::uri]] equals "/bar/" } {
        HTTP::cookie insert name "myCookie" value "bar"
    }
}

that works fine

when HTTP_REQUEST {
 Check the requested URI
switch -glob [HTTP::uri] {
    "/foo/*" 
    {
    pool foo
    persist cookie insert foo 0
    }
    default {
    pool bar
    persist cookie insert bar 0
    }

}

there is a way to add a cookie in a redirect. also a cool way to handle that

when_HTTP_REQUEST {
if { [string tolower [HTTP::uri]] equals "/foo/" } {
    set cookie [format "%s=%s; path=/; domain=%s" "cookie" "foo" ".foobar.com"]
    HTTP::respond 301 Location "/foo" "Set-Cookie" $cookie
}
if { [string tolower [HTTP::uri]] equals "/bar/" } {
    set cookie [format "%s=%s; path=/; domain=%s" "cookie" "bar" ".foobar.com"]
    HTTP::respond 301 Location "/bar" "Set-Cookie" $cookie
}
}

Hi,

you can use following code:

when CLIENT_ACCEPTED { 
    set default_pool [LB::server pool]
}

when_HTTP_REQUEST {
    set target ""
    if { [string tolower [HTTP::path]] equals "/foo/" } {
        set target "foo"
    } elseif { [string tolower [HTTP::path]] equals "/bar/" } {
        set target "bar"
    }
    if { [HTTP::cookie exists "myCookie"] && $target equals "" } {
         target is empty, get target from cookie
        set target [HTTP::cookie value "myCookie"]
    } elseif { (![HTTP::cookie exists "myCookie"] && $target ne "") || [HTTP::cookie value "myCookie"] ne $target } {
         cookie value is empty or value is wrong, inject cookie with right value is response.
        set inject_cookie $target
    }
    switch $target {
        foo {pool foo}
        bar {pool bar}
        default {pool $default_pool}
    }
}

when HTTP_RESPONSE {
    if {[info exists inject_cookie]} {
        HTTP::cookie insert name myCookie value $inject_cookie
        unset inject_cookie
    }
}