Greg_34053
Aug 02, 2011Nimbostratus
detecting redirection
Hi,
I have an irule that sends the clients IP address to the back end web server via a cookie. The cookie is not sent to the user. The rule worked fine unless an earlier rule had called HTTP::redirect then modifying the Cookie header caused the irule to abort. I tried using HTTP::is_redirect but it does not detect the imminent redirect. I have used catch commands have been used to let things keep working. Obviously if this rule is first in the list then there is not an issue.
I have a little more boundary checking to do: If I remove a CIP cookie and that was the last cookie then I need to know if the Cookie header still exists to know whether to add the CIP cookie to it or whether to create a new header. In the case of the former, I at least need to remove the ";" separator as there is no preceding cookie. I also have yet to test the insert new cookie header code.
when HTTP_REQUEST {
set my_cookie_name "CIP"
set client_ip_addr [IP::client_addr]
Check if a cookie header exists
if { [HTTP::header exists Cookie] } then {
while { [HTTP::cookie exists $my_cookie_name] } {
HTTP::cookie remove $my_cookie_name
}
set new_Cookie_header "[HTTP::header value Cookie]; $my_cookie_name=$client_ip_addr"
catch {HTTP::header replace Cookie $new_Cookie_header} dont_care
}
else {
set new_Cookie_header "$my_cookie_name=$client_ip_addr"
catch {HTTP::header insert Cookie $new_Cookie_header} dont_care
}
}
Author: Greg Palmer 2011/08/02
Adds client IP to the cookie header if it can - catches error and continues otherwise
NB Putting this rule first in the list will allow it to allows add the cookie and hence no error to catch
Looks for Cookie header
if found removes all cookies whose name is CIP and modifies the cookie header to contain the required Cookie
if not found it adds a cookie header with the cookie
Regards,
Greg