TidCookieManager questions (threads)

Giganews Newsgroups
Subject: TidCookieManager questions (threads)
Posted by:  dk_sz (dk_…@hotmail.com)
Date: Thu, 22 Mar 2007

Hi,

I need to share cookies between multiple threads
(I need to share session/login information across threads)

My [first solution] was to use a shared cookiemanager
(override TidHttp.SetCookieManager to avoid
multi threads AVs in notification system)

Advantages is speed and minimal locks. However, this
does not fully work (it sometimes work for a little time).

I suspect some data gets messed up, perhaps related to:
It seems like all access to raw TidCookies collection
data is not threadsafe (e.g. Items, Cookie) as opposed to
*LockCookieListByDomain / FCookieListByDomain*

OK, giving up on this I tried my second solution.
It consists of copying cookies back and forth
like this (slightly pseudo for readability):

UpdateCookieManager(const ACookieManagerUpdate, ACookieManagerShared:
TidCookieManager);
var
  I: Integer;
  E: Integer;
  O: TIdCookieRFC2109;
  N: TIdCookieRFC2109;
begin
  ACookieManagerUpdate.CookieCollection.BeginUpdate;
  ACookieManagerCS.Acquire;
  E := ACookieManagerShared.CookieCollection.Count - 1;
  for I := 0 to E do
  begin
    O := ACookieManagerShared.CookieCollection[I];
    N := ACookieManagerUpdate.CookieCollection.Add;
    N.Assign(O);
  end;
  ACookieManagerCS.Release;
  ACookieManagerUpdate.CookieCollection.EndUpdate;
  ACookieManagerUpdate.OnNewCookie := OnNewCookie_Handler;
end;

OnNewCookie_Handler(ASender: TObject; ACookie: TIdCookieRFC2109; Var
VAccept: Boolean);
var
  TmpCookieNew: TIdCookieRFC2109;
begin
  VAccept := True;
  ACookieManagerCS.Acquire;
  TmpCookieNew := ACookieManagerShared.CookieCollection.Add;
  TmpCookieNew.Assign(ACookie);
  ACookieManagerCS.Release;
end;

However, this does not work at all. Session/cookie/login is
seemingly not maintained fully (?) across two different TidHttp
... even in non-threaded (!) VCL main thread environment.

So perhaps my copying is insufficuent? Can anyone help?

I had hoped I perhaps could replace TidCookieManager
entirely with my own, but as most methods are not virtual
etc. this does not seem like a feasible solution?

best regards
Thomas Schulz

Replies