Browse Source

* improved session management:
- GlobalSessionDir is now always used, even when creating descendents of TWebIniSession.
- Explicitly Free session. (Prepares for persistent session info for fastcgi apps)

git-svn-id: trunk@16069 -

michael 15 years ago
parent
commit
54411b9180
2 changed files with 28 additions and 14 deletions
  1. 1 5
      packages/fcl-web/src/base/fpweb.pp
  2. 27 9
      packages/fcl-web/src/base/websession.pp

+ 1 - 5
packages/fcl-web/src/base/fpweb.pp

@@ -465,11 +465,7 @@ begin
   FRequest := Nil;
   FResponse := Nil;
   // Clean up session for the case the webmodule is used again
-  if assigned(Session) then
-    begin
-    Session.Free;
-    Session := nil;
-    end;
+  DoneSession;
 {$ifdef cgidebug}
   SendMethodExit('WebModule('+Name+').handlerequest');
 {$endif cgidebug}

+ 27 - 9
packages/fcl-web/src/base/websession.pp

@@ -36,7 +36,9 @@ Type
     Procedure CheckSession(ARequest : TRequest);
     Procedure InitSession(AResponse : TResponse);
     Procedure UpdateSession(AResponse : TResponse);
+    Procedure DoneSession; virtual;
   Public
+    destructor destroy; override;
     Procedure Notification(AComponent : TComponent;Operation : TOperation); override;
     Procedure Loaded; Override;
     Property CreateSession : Boolean Read FCreateSession Write FCreateSession;
@@ -59,6 +61,7 @@ Type
     SID : String;
   private
     procedure FreeIniFile;
+    function GetSessionDir: String;
   Protected
     Procedure CheckSession;
     Function GetSessionID : String; override;
@@ -66,7 +69,7 @@ Type
     procedure SetSessionVariable(VarName : String; const AValue: String); override;
     Property Cached : Boolean Read FCached Write FCached;
     property SessionCookie : String Read FSessionCookie Write FSessionCookie;
-    Property SessionDir : String Read FSessionDir Write FSessionDir;
+    Property SessionDir : String Read GetSessionDir Write FSessionDir;
     Property SessionCookiePath : String Read FSessionCookiePath write FSessionCookiePath;
   Public
     Destructor Destroy; override;
@@ -107,7 +110,7 @@ Const
   SData      = 'Data';
 
   KeyStart   = 'Start';         // Start time of session
-  KeyLast    = 'Start';         // Last seen time of session
+  KeyLast    = 'Last';          // Last seen time of session
   KeyTimeOut = 'Timeout';       // Timeout in seconds;
 
   SFPWebSession = 'FPWebSession'; // Cookie name for session.
@@ -118,9 +121,6 @@ resourcestring
 
 Function GetDefaultSession : TCustomSession;
 
-Var
-  W : TFPWebSession;
-
 begin
 {$ifdef cgidebug}SendMethodEnter('GetDefaultSession');{$endif}
   Result:=Nil;
@@ -134,9 +134,7 @@ begin
   if (Result=Nil) then
     begin
     {$ifdef cgidebug}Senddebug('Creating iniwebsession');{$endif}
-    W:=TFPWebSession.Create(Nil);
-    W.SessionDir:=GlobalSessionDir;
-    Result:=W;
+    Result:=TFPWebSession.Create(Nil);
     end;
 {$ifdef cgidebug}SendMethodExit('GetDefaultSession');{$endif}
 end;
@@ -157,6 +155,13 @@ begin
   FreeAndNil(FIniFile);
 end;
 
+function TIniWebSession.GetSessionDir: String;
+begin
+  Result:=FSessionDir;
+  If (Result='') then
+    Result:=GlobalSessionDir;
+end;
+
 Procedure TIniWebSession.CheckSession;
 
 begin
@@ -234,7 +239,7 @@ begin
     L:=Finifile.ReadDateTime(SSession,KeyLast,0);
 {$ifdef cgidebug}
     If (L=0) then
-    SendDebug('No datetime in inifile');
+    SendDebug('No datetime in inifile (or not valid datetime : '+Finifile.ReadString(SSession,KeyLast,''));
 {$endif}
     T:=FIniFile.ReadInteger(SSession,KeyTimeOut,Self.TimeOutMinutes);
 {$ifdef cgidebug}SendDebug('Timeout :'+IntToStr(t));{$endif}
@@ -374,6 +379,19 @@ begin
     FSession.UpdateResponse(AResponse);
 end;
 
+procedure TSessionHTTPModule.DoneSession;
+begin
+  FreeAndNil(FSession);
+end;
+
+destructor TSessionHTTPModule.destroy;
+begin
+  // Prevent memory leaks.
+  If Assigned(FSession) then
+    DoneSession;
+  inherited destroy;
+end;
+
 procedure TSessionHTTPModule.Notification(AComponent: TComponent;
   Operation: TOperation);
 begin