Delay in reading cookie using NSHttpCookieStorage
Delay in reading cookie using NSHttpCookieStorage
- Subject: Delay in reading cookie using NSHttpCookieStorage
- From: Hirendra Rathor <email@hidden>
- Date: Tue, 05 Jul 2011 18:27:25 +0530
- Acceptlanguage: en-US
- Thread-topic: Delay in reading cookie using NSHttpCookieStorage
I have written a Cocoa application that uses NSHttpCookieStorage class to read cookie set in the Safari browser. I see that if the time gap between setting the cookie by Safari and the application querying for the cookie is couple of seconds, then the cookie returned by NSHttpCookieStorage is stale. Here is the sequence of steps happening.
1. I browse to a URL hosted by my web server. The web server sets a cookie (say the content is Cookie-A) and sends it to the browser.
2. Browser receives the cookie and makes some more HTTP requests to the web server.
3. At some point, browser decides to launch my Cocoa application. This is done by loading an applet which in turn launches the application. Say that the contents of the cookie at this instant is Cookie-B. It changes because of the processing happening on server in step 2 above.
4. The application reads the cookie using NSHttpCookieStorage and makes another HTTP request to the web server.
I see that the application usually gets the content of cookie as Cookie-A rather than Cookie-B. To troubleshoot, I put an 'alert' dialog box in the javascript just before launching the application. I could see value emitted as Cookie-B while the application received Cookie-A. While the dialog box was displayed, I peeked into ~/Library/Cookies/Cookies.plist and found that the value of the cookie was Cookie-A.
I experimented further and waited for few seconds after the 'alert' dialog box was displayed. Since I did not dismiss the dialog box, my application was also not launched. After a while I saw the content of the cookie in Cookies.plist changed to Cookie-B. I dismissed the dialog and let the application launch. I saw the application receiving correct value of the cookie.
Why does it take this long for the cookie value to change in Cookies.plist file? Is this delay also responsible for application to receive stale cookie or is it just coincidental?
My application has been created using XCode 3.2.6 on Mac OS X 10.6.8. The main function calls NSApplicationMain() while the processing to query for the cookie happens on another thread. Could I be missing some wiring in the code for things to work properly? I have used shareHTTPCookieStorage method to get the singleton instance of NSHTTPCookieStorage and cookiesForURL method to get the cookie.
I am new to Cocoa and Mac programming. I did my best to search for these answers in this mailing list but didn't quite find it. If this discussion has been answered earlier (may be in another forum), would appreciate if someone could point me to it.
Many thanks,
Hirendra
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden