Forum Discussion

10 Replies

  • The wiki is your best friend...for this rule, we'll use subsets of the "HTTP::" command

    http://devcentral.f5.com/wiki/default.aspx/iRules.HTTP

    Here's a list of all the commands.

    http://devcentral.f5.com/wiki/default.aspx/iRules.Commands

    when HTTP_REQUEST {
        if { [string tolower [HTTP::uri]] contains "/folder" } {
           HTTP::cookie insert "name" value "value" } }
    

    That rule will insert a cookie named "name" with a value of "value" whenever the URI requested contains "/folder" You might want to replace "contains" with "starts_with" depending on your requirements. You'll also want to add an expiration or path if you need it.

    http://devcentral.f5.com/wiki/default.aspx/iRules/HTTP__cookie.html shows all the options.

    Let me know if you have any other questions.
  • @ Chris

     

     

    I did create the iRule like you recommended:

     

     

    when HTTP_REQUEST {

     

     

    if { [string tolower [HTTP::uri]] starts_with "/test" } {

     

     

    HTTP::cookie insert name "NewCookie" value "test"

     

     

    }

     

     

    }

     

     

     

    But after I request /test there is no cookie show in Firefox???

     

    Is there something more to set?

     

     

    To which domain does this cookie belong to?

     

     

     

     

    Kind regards

     

    Andrea

     

  • Posted By bigbrother on 10/20/2010 06:47 AM

     

    @ Chris

     

     

    I did create the iRule like you recommended:

     

     

    when HTTP_REQUEST {

     

     

    if { [string tolower [HTTP::uri]] starts_with "/test" } {

     

     

    HTTP::cookie insert name "NewCookie" value "test"

     

     

    }

     

     

    }

     

     

     

    But after I request /test there is no cookie show in Firefox???

     

    Is there something more to set?

     

     

    To which domain does this cookie belong to?

     

     

     

     

    Kind regards

     

    Andrea

     

     

    What's the exact URI you're testing?

     

     

    Would it be like "www.sample.com/test" ?

     

     

     

    Try "HTTP::cookie insert name "NewCookie" value "test" path /"

     

     

     

    The cookie belongs to whatever domain you're accessing. In my example, it would be for "www.sample.com"

     

  • I just had a total brain-fart. We need to do cookie insert in the response event.

    Try this:

    when HTTP_REQUEST {
        if { [string tolower [HTTP::uri]] contains "/folder" } {
           set cinsert 1 
    } else { set cinsert 0 }}
    
    when HTTP_RESPONSE {
      if { $cinsert eq 1 } {
         HTTP::cookie insert "name" value "value" path / }}
    
  • I do the same you explained...

     

     

     

     

    when HTTP_REQUEST {

     

     

     

    set uagent [string tolower [HTTP::header User-Agent]]

     

     

     

    switch -glob [HTTP::host] {

     

    www.testdom.com {

     

    if { [HTTP::uri] starts_with "/test" } {

     

     

    HTTP::cookie insert name "ClassicView" value "mobile" path /

     

     

    } else {

     

    log local0. "no cookie set"

     

    }

     

    }

     

     

     

    unset uagent

     

     

     

    }

     

     

     

    But the cookie is not set ???

     

  • Hi Chris

     

     

    okay I got it...

     

     

     

    I have to do the following:

     

     

     

    1. when HTTP_REQUEST

     

    - matches any request conditions

     

     

     

    If the request condition matches

     

     

     

    2. when HTTP_RESPONSE

     

    - set cookie

     

     

     

     

     

    ahh and you check with the variable cinsert that the RESPONSE works even if the condtition matches right?

     

     

     

    Do you have an example somewhere?

     

     

     

     

     

    Kind regards

     

    andy

     

  • Posted By bigbrother on 10/20/2010 07:20 AM

    I do the same you explained...

    when HTTP_REQUEST {

    set uagent [string tolower [HTTP::header User-Agent]]

    switch -glob [HTTP::host] {

    www.testdom.com {

    if { [HTTP::uri] starts_with "/test" } {

    HTTP::cookie insert name "ClassicView" value "mobile" path /

    } else {

    log local0. "no cookie set"

    }

    }

    unset uagent

    }

    But the cookie is not set ???

    I posted right before you...we need to do the cookie insert from the HTTP_RESPONSE event. Using it in the Request event inserts it between LTM and the pool members. I forgot my traffic flow for a second.

    The example above your post should work. I can modify yours as well.

    when HTTP_REQUEST {

    set uagent [string tolower [HTTP::header User-Agent]]

    switch -glob [HTTP::host] {

    www.testdom.com {

    if { [HTTP::uri] starts_with "/test" } {

    set cinsert 1

    } else {

    set cinsert 0

    log local0. "no cookie set"

    }

    }

    unset uagent

    }

    when HTTP_RESPONSE {

    if { $cinsert eq 1 } {

    HTTP::cookie insert name "ClassicView" value "mobile" path / }}

    May I ask how you're using the uagent variable here? Is it for something separate from the cookie logic?

  • Hi Chris

     

     

    Yes it is for something separated. Because we want also integrate in this iRule mobile device detection.

     

     

     

    Ok I will try your steps...

     

  • The cookie is set but I get following ltm error:

     

     

    Oct 20 16:39:51 local/tmm err tmm[5029]: 01220001:3: TCL error: irule_global_redirect_mobile - can't read "cinsert": no such variable while executing "if { $cinsert eq 1 } {
  • ahh okay I see...

     

     

    I have to initialize the variable globaly:

     

     

    set cinsert ""

     

     

    Now it works without any errors...

     

     

    YOU ARE THE MEN !!!

     

     

    What is your profession Chris?