Forum Discussion

Greg_34053's avatar
Greg_34053
Icon for Nimbostratus rankNimbostratus
Aug 02, 2011

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

 

  • I tested the boundary conditions and found that when you delete the last cookie that a Cookie header still exists so the code has been altered to put correct syntax for the first cookie in the header (no preceding "; ")

     

    Also tested when no cookies presented and it added the Cookie header correctly and my diagnostic irule that followed and the web server in the pool both showed the cookie correctly.

     

     

    All that is left is "How can I tell if a redirect is happening due to an earlier irule?" Then I could probably get rid of the catch.

     

     

     

    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

     

    }

     

    if { [HTTP::cookie count] > 0 } then {

     

    set new_Cookie_header "[HTTP::header value Cookie]; $my_cookie_name=$client_ip_addr"

     

    } else {

     

    set new_Cookie_header "$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