Browse Source

* Moved session cookie up in the class hierarchie, so it can be configured at the session factory level

git-svn-id: trunk@17653 -
michael 14 years ago
parent
commit
4492b9b993

+ 13 - 2
packages/fcl-web/src/base/fphttp.pp

@@ -147,6 +147,8 @@ Type
 
 
   TSessionFactory = Class(TComponent)
   TSessionFactory = Class(TComponent)
   private
   private
+    FSessionCookie: String;
+    FSessionCookiePath: String;
     FTimeOut: Integer;
     FTimeOut: Integer;
     FCleanupInterval: Integer;
     FCleanupInterval: Integer;
     FDoneCount: Integer;
     FDoneCount: Integer;
@@ -167,6 +169,10 @@ Type
     Property CleanupInterval : Integer read FCleanupInterval Write FCleanUpInterval;
     Property CleanupInterval : Integer read FCleanupInterval Write FCleanUpInterval;
     // Default timeout for sessions, in minutes.
     // Default timeout for sessions, in minutes.
     Property DefaultTimeOutMinutes : Integer Read FTimeOut Write FTimeOut;
     Property DefaultTimeOutMinutes : Integer Read FTimeOut Write FTimeOut;
+    // Default session cookie.
+    property SessionCookie : String Read FSessionCookie Write FSessionCookie;
+    // Default session cookie path
+    Property SessionCookiePath : String Read FSessionCookiePath write FSessionCookiePath;
   end;
   end;
   TSessionFactoryClass = Class of TSessionFactory;
   TSessionFactoryClass = Class of TSessionFactory;
 
 
@@ -242,8 +248,13 @@ end;
 function TSessionFactory.CreateSession(ARequest: TRequest): TCustomSession;
 function TSessionFactory.CreateSession(ARequest: TRequest): TCustomSession;
 begin
 begin
   Result:=DoCreateSession(ARequest);
   Result:=DoCreateSession(ARequest);
-  if (FTimeOut<>0) and Assigned(Result) then
-    Result.TimeoutMinutes:=FTimeOut;
+  if Assigned(Result) then
+    begin
+    if (FTimeOut<>0) then
+      Result.TimeoutMinutes:=FTimeOut;
+    Result.SessionCookie:=Self.SessionCookie;
+    Result.SessionCookiePath:=Self.SessionCookiePath;
+    end;
 end;
 end;
 
 
 procedure TSessionFactory.DoneSession(var ASession: TCustomSession);
 procedure TSessionFactory.DoneSession(var ASession: TCustomSession);

+ 33 - 3
packages/fcl-web/src/base/httpdefs.pp

@@ -32,6 +32,9 @@ interface
 uses Classes,Sysutils;
 uses Classes,Sysutils;
 
 
 const
 const
+  DefaultTimeOut = 15;
+  SFPWebSession  = 'FPWebSession'; // Cookie name for session.
+
   fieldAccept          = 'Accept';
   fieldAccept          = 'Accept';
   fieldAcceptCharset   = 'Accept-Charset';
   fieldAcceptCharset   = 'Accept-Charset';
   fieldAcceptEncoding  = 'Accept-Encoding';
   fieldAcceptEncoding  = 'Accept-Encoding';
@@ -355,9 +358,16 @@ type
 
 
   TCustomSession = Class(TComponent)
   TCustomSession = Class(TComponent)
   Private
   Private
+    FSessionCookie: String;
+    FSessionCookiePath: String;
     FTimeOut: Integer;
     FTimeOut: Integer;
   Protected
   Protected
+    // Can be overridden to provide custom behaviour.
+    procedure SetSessionCookie(const AValue: String); virtual;
+    procedure SetSessionCookiePath(const AValue: String); virtual;
+    // When called, generates a new GUID. Override to retrieve GUID from cookie/URL/...
     Function GetSessionID : String; virtual;
     Function GetSessionID : String; virtual;
+    // These must be overridden to actually store/retrieve variables.
     Function GetSessionVariable(VarName : String) : String; Virtual; abstract;
     Function GetSessionVariable(VarName : String) : String; Virtual; abstract;
     procedure SetSessionVariable(VarName : String; const AValue: String);Virtual;abstract;
     procedure SetSessionVariable(VarName : String; const AValue: String);Virtual;abstract;
   Public
   Public
@@ -368,10 +378,19 @@ type
     Procedure InitResponse(AResponse : TResponse); virtual;
     Procedure InitResponse(AResponse : TResponse); virtual;
     // Update response from session (typically, change cookie to response and write session data).
     // Update response from session (typically, change cookie to response and write session data).
     Procedure UpdateResponse(AResponse : TResponse); virtual; Abstract;
     Procedure UpdateResponse(AResponse : TResponse); virtual; Abstract;
+    // Remove variable from list of variables.
     Procedure RemoveVariable(VariableName : String); virtual; abstract;
     Procedure RemoveVariable(VariableName : String); virtual; abstract;
+    // Terminate session
     Procedure Terminate; virtual; abstract;
     Procedure Terminate; virtual; abstract;
-    Property TimeOutMinutes : Integer Read FTimeOut Write FTimeOut;
+    // Session timeout in minutes
+    Property TimeOutMinutes : Integer Read FTimeOut Write FTimeOut default 15;
+    // ID of this session.
     Property SessionID : String Read GetSessionID;
     Property SessionID : String Read GetSessionID;
+    // Name of cookie used when tracing session. (may or may not be used)
+    property SessionCookie : String Read FSessionCookie Write SetSessionCookie;
+    // Path of cookie used when tracing session. (may or may not be used)
+    Property SessionCookiePath : String Read FSessionCookiePath write SetSessionCookiePath;
+    // Variables, tracked in session.
     Property Variables[VarName : String] : String Read GetSessionVariable Write SetSessionVariable;
     Property Variables[VarName : String] : String Read GetSessionVariable Write SetSessionVariable;
   end;
   end;
 
 
@@ -1709,6 +1728,16 @@ end;
 { TCustomSession }
 { TCustomSession }
 
 
 
 
+procedure TCustomSession.SetSessionCookie(const AValue: String);
+begin
+  FSessionCookie:=AValue;
+end;
+
+procedure TCustomSession.SetSessionCookiePath(const AValue: String);
+begin
+  FSessionCookiePath:=AValue;
+end;
+
 function TCustomSession.GetSessionID: String;
 function TCustomSession.GetSessionID: String;
 
 
 Var
 Var
@@ -1722,7 +1751,7 @@ end;
 
 
 constructor TCustomSession.Create(AOwner: TComponent);
 constructor TCustomSession.Create(AOwner: TComponent);
 begin
 begin
-  FTimeOut:=15;
+  FTimeOut:=DefaultTimeOut;
   inherited Create(AOwner);
   inherited Create(AOwner);
 end;
 end;
 
 
@@ -1731,7 +1760,8 @@ begin
   // do nothing
   // do nothing
 end;
 end;
 
 
-procedure TCustomSession.InitSession(ARequest: TRequest; OnNewSession,OnExpired : TNotifyEvent);
+procedure TCustomSession.InitSession(ARequest: TRequest; OnNewSession,
+  OnExpired: TNotifyEvent);
 begin
 begin
   // Do nothing
   // Do nothing
 end;
 end;

+ 1 - 8
packages/fcl-web/src/base/iniwebsession.pp

@@ -29,8 +29,6 @@ Type
     FSessionStarted : Boolean;
     FSessionStarted : Boolean;
     FCached: Boolean;
     FCached: Boolean;
     FIniFile : TMemInifile;
     FIniFile : TMemInifile;
-    FSessionCookie: String;
-    FSessionCookiePath: String;
     FSessionDir: String;
     FSessionDir: String;
     FTerminated :Boolean;
     FTerminated :Boolean;
     SID : String;
     SID : String;
@@ -42,9 +40,7 @@ Type
     Function GetSessionVariable(VarName : String) : String; override;
     Function GetSessionVariable(VarName : String) : String; override;
     procedure SetSessionVariable(VarName : String; const AValue: String); override;
     procedure SetSessionVariable(VarName : String; const AValue: String); override;
     Property Cached : Boolean Read FCached Write FCached;
     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 FSessionDir Write FSessionDir;
-    Property SessionCookiePath : String Read FSessionCookiePath write FSessionCookiePath;
   Public
   Public
     Destructor Destroy; override;
     Destructor Destroy; override;
     Procedure Terminate; override;
     Procedure Terminate; override;
@@ -99,8 +95,6 @@ Const
   KeyLast    = 'Last';          // Last seen time of session
   KeyLast    = 'Last';          // Last seen time of session
   KeyTimeOut = 'Timeout';       // Timeout in seconds;
   KeyTimeOut = 'Timeout';       // Timeout in seconds;
 
 
-  SFPWebSession = 'FPWebSession'; // Cookie name for session.
-
 resourcestring
 resourcestring
   SErrSessionTerminated = 'No web session active: Session was terminated';
   SErrSessionTerminated = 'No web session active: Session was terminated';
   SErrNoSession         = 'No web session active: Session was not started';
   SErrNoSession         = 'No web session active: Session was not started';
@@ -297,7 +291,6 @@ begin
   FTerminated := False;
   FTerminated := False;
   // If a exception occured during a prior request FIniFile is still not freed
   // If a exception occured during a prior request FIniFile is still not freed
   if assigned(FIniFile) then FreeIniFile;
   if assigned(FIniFile) then FreeIniFile;
-
   If (SessionCookie='') then
   If (SessionCookie='') then
     SessionCookie:=SFPWebSession;
     SessionCookie:=SFPWebSession;
   S:=ARequest.CookieFields.Values[SessionCookie];
   S:=ARequest.CookieFields.Values[SessionCookie];
@@ -353,7 +346,7 @@ begin
       C.Name:=SessionCookie;
       C.Name:=SessionCookie;
       end;
       end;
     C.Value:=SID;
     C.Value:=SID;
-    C.Path:=FSessionCookiePath;
+    C.Path:=SessionCookiePath;
     end
     end
   else If FTerminated then
   else If FTerminated then
     begin
     begin