PowerShellDon_1
Dec 04, 2015Nimbostratus
iRule - Adding a cookie based on Geo-IP , without breaking everything
Hi all, First post on DevCentral, i've done a handful of iRules but not great at them.
I have a case where we have a website with multiple language/country versions. I have been asked to be able to change a resource on the site based upon the clients US State. At the moment, we are just targeting Florida. The logic on the server side is setup and works, so if cookie "uslocation" has value "florida" the logo changes.
The problem is, the logo wont change without me adding the cookie to the HTTP_RESPONSE And i suspect i'm not doing the HTTP_RESPONSE correctly, because it fails to load everything that isnt in the /usa/ path, and if the cookie isn't present, the connection is dropped.
when HTTP_REQUEST {
if { [string tolower [HTTP::host]] equals "mywebsite.com" && [HTTP::path] eq "/usa" }{
Parse the client IP from the header supplied
set client_ip [HTTP::header value "X-Forwarded-For"]
log local0. " Staging - XFF ... incoming connection from $client_ip "
if { $client_ip eq "" }{
The header was empty/did not exist, so use the actual client IP
log local0. "Staging - XFF Header was empty so using actual IP - $client_ip"
set client_ip [IP::client_addr]
}
set state [string tolower [whereis $client_ip state]]
log local0. " Staging - incoming connection from $client_ip detected state as $state"
if { $state eq "florida" } {
log local0. " Staging - .. $state.. "
set cookie_insertion 1
HTTP::cookie insert name "uslocation" value $state path "/usa" domain "mywebsite.com" }
log local0. "Cookies = [HTTP::cookie uslocation] "
}
else {
pool POOL-STAGING-HTTP
}
}
when HTTP_RESPONSE {
if { $cookie_insertion > 0 }{
log local0. " RESPONSE - Adding cookie to response Staging - ... $state.. "
HTTP::cookie insert name "uslocation" value $state path "/usa" domain "mywebsite.com"
log local0. "RESPONSE - Cookies = [HTTP::cookie uslocation] " }
}
Any help would be hugely appreciated