Forum Discussion
logging each cookie name failing in logs
The below code is throwing an error in the logs:
when HTTP_REQUEST {
set cookies [HTTP::cookie names]
foreach cookie $::cookies {
log local0. "Cookie: $::cookie"
}
}
Error in logs:
TCL error: qadpreviewdev.qad.com HTTP_RESPONSE - cant read ::cookies: no such variable while executing foreach cookie $::cookies { log local0. Cookie: $::cookie }
Any help if fully appreciated.
Thanks.
Regards,
TRX
11 Replies
- hoolio
Cirrostratus
Hi TRX,
Using a global variable ($::cookies) probably isn't what you want in this scenario as a global variable is shared across all connections. If you change $::cookies and $::cookie to $cookies and $::cookie to $cookie, the rule should work fine. You can also eliminate the intermediate variable, $cookies to save a little memory:when HTTP_REQUEST { Loop through each cookie by name foreach cookie [HTTP::cookie names] { Log the cookie name and value log local0. "Cookie name: $cookie, Cookie value: [HTTP::cookie value $cookie]" } }
Aaron - Thanks Aaron. If I wanted to delete a cookie from the client machine would I do that in the request or the response function?
Also when I display the list of cookies in the "http_response" I get the expected results in the log, but when I do a check for them using the code below, it doesn't return true and never logs:
when HTTP_RESPONSE {
foreach cookie [HTTP::cookie names] {
log local0. "Cookie name: $cookie"
}
if { ([HTTP::cookie exists "sso"]) and ([HTTP::cookie exists "portal") } {
log local0. "Both portal and sso cookies exist!!!!"
}
}
Let me know your thoughts.
Thanks.
Regards,
TRX - hoolio
Cirrostratus
If you want the client to remove the cookie, you'd need to set the cookie in a response with the expire date in the past:when HTTP_RESPONSE { HTTP::cookie insert name "my_cookie_name" value "nada" path "/" HTTP::cookie expires "my_cookie_name" 0 absolute }
Aaron - Thanks. Let me try that.
- Aaron, do you know why this example is deleting the cookie in the HTTP_REQUEST function?
http://devcentral.f5.com/wiki/default.aspx/iRules/HTTP__cookie.html
Example:
Rename a cookie by inserting a new cookie name with the same value as the original. Then remove the old cookie.
when HTTP_REQUEST {
Check if old cookie exists in request
if { [HTTP::cookie exists "old-cookie-name"] } {
Insert a new cookie with the new name and old cookie's value
HTTP::cookie insert name "new-cookie-name" value [HTTP::cookie value "old-cookie-name"]
Remove the old cookie
HTTP::cookie remove "old-cookie-name"
}
}
Let me know your thoughts.
Thanks.
Regards,
TRX - hoolio
Cirrostratus
Because there isn't a cookie rename function. So the cookie value is set with a new name and then the old cookie is removed.
Keep in mind, that any changes you make to the cookie in requests would only effect what the web application sees. Removing a cookie from the request wouldn't effect the client's cookies.
If you want to tell the client to remove an existing cookie from its cookie store, you'd need to set the cookie with an expire time in the past, as I showed a few posts up.
Aaron - So when doing this in the HTTP_RESPONSE function:
foreach cookie [HTTP::cookie names] {
log local0. "Cookie RESPONSE name: $cookie"
}
... should I see the cookies on the client machine or ONLY the cookies that the server/web app is sending to the client machine?
Please let me know.
Thanks.
Regards,
TRX - Had another question. Is the below statement true?
http://devcentral.f5.com/wiki/default.aspx/iRules/HTTP__cookie.html
HTTP::cookie insert name value [path ] [domain ] [version <0 | 1 | 2>]
Adds a cookie to the HTTP Cookie header in requests or Set-Cookie response header. The default value for the version is 0. If the cookie already exists, a second cookie will be inserted (tested in 9.2.4).
Regards,
TRX - Any thoughts on this?
- hoolio
Cirrostratus
should I see the cookies on the client machine or ONLY the cookies that the server/web app is sending to the client machine?
You'll only see the cookies that the web app has set in that response. If you want to know which cookies the client has, you'd need to look for them in HTTP_REQUEST.
Adds a cookie to the HTTP Cookie header in requests or Set-Cookie response header. The default value for the version is 0. If the cookie already exists, a second cookie will be inserted (tested in 9.2.4).
Yes, HTTP::cookie insert.... will insert a cookie in the request sent to the pool member if called in HTTP_REQUEST or in the response sent to the client if called in HTTP_RESPONSE.
Aaron
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com