Changeset 88 for branches/DirectWeb/UHTTPSessionFile.pas
- Timestamp:
- Dec 18, 2009, 12:21:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DirectWeb/UHTTPSessionFile.pas
r87 r88 6 6 7 7 uses 8 Classes, SysUtils, UHTTPServer ;8 Classes, SysUtils, UHTTPServer, syncobjs; 9 9 10 type 10 type 11 11 12 { T HTTPSessionFile }12 { TFileHTTPSessionStorage } 13 13 14 THTTPSessionFile = class(THTTPSession) 14 TFileHTTPSessionStorage = class(THTTPSessionStorage) 15 private 16 Lock: TCriticalSection; 17 procedure GetSessionId(HandlerData: THTTPHandlerData); 18 public 15 19 Directory: string; 16 20 SessionIdCookieName: string; 17 Session Id: string;18 procedure Load( Request: THTTPRequest); override;19 procedure Save( Response: THTTPResponse); override;21 Sessions: TStringList; 22 procedure Load(HandlerData: THTTPHandlerData); override; 23 procedure Save(HandlerData: THTTPHandlerData); override; 20 24 constructor Create; override; 21 25 destructor Destroy; override; … … 26 30 { THTTPSession } 27 31 28 procedure T HTTPSessionFile.Load(Request: THTTPRequest);32 procedure TFileHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData); 29 33 begin 34 with HandlerData do 30 35 if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin 31 SessionId := Request.Cookies.Values[SessionIdCookieName]; 32 end else begin 33 SessionId := IntToStr(Random(1000)); 34 // TODO: Check if new id already used 36 SessionId := Request.Cookies.Values[SessionIdCookieName]; 37 end else begin 38 SessionId := IntToStr(Random(2000000000)); 39 // TODO: Check if new id already used 40 end; 41 end; 42 43 procedure TFileHTTPSessionStorage.Load(HandlerData: THTTPHandlerData); 44 var 45 SessionFile: string; 46 begin 47 GetSessionId(HandlerData); 48 try 49 Lock.Acquire; 50 SessionFile := Directory + '/' + HandlerData.SessionId; 51 if FileExists(SessionFile) then 52 HandlerData.Session.LoadFromFile(SessionFile); 53 finally 54 Lock.Release; 35 55 end; 36 37 if FileExists(Directory + '/' + SessionId) then38 Variables.LoadFromFile(Directory + '/' + SessionId);39 56 inherited; 40 57 end; 41 58 42 procedure THTTPSessionFile.Save(Response: THTTPResponse); 59 procedure TFileHTTPSessionStorage.Save(HandlerData: THTTPHandlerData); 60 var 61 SessionFile: string; 43 62 begin 44 ForceDirectories(Directory); 45 if DirectoryExists(Directory) then 46 Variables.SaveToFile(Directory + '/' + SessionId) 47 else raise Exception.Create('Can''t create session storage directory.'); 63 try 64 Lock.Acquire; 65 SessionFile := Directory + '/' + HandlerData.SessionId; 66 ForceDirectories(Directory); 67 if DirectoryExists(Directory) then begin 68 DeleteFile(SessionFile); 69 HandlerData.Session.SaveToFile(SessionFile) 70 end else raise Exception.Create('Can''t create session storage directory.'); 48 71 49 Response.Cookies.Values[SessionIdCookieName] := SessionId; 50 inherited Save(Response); 72 HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId; 73 finally 74 Lock.Release; 75 end; 76 inherited; 51 77 end; 52 78 53 constructor T HTTPSessionFile.Create;79 constructor TFileHTTPSessionStorage.Create; 54 80 begin 55 inherited; 81 inherited Create; 82 Lock := TCriticalSection.Create; 83 Sessions := TStringList.Create; 56 84 SessionIdCookieName := 'SessionId'; 57 85 Directory := 'Session'; 58 86 end; 59 87 60 destructor T HTTPSessionFile.Destroy;88 destructor TFileHTTPSessionStorage.Destroy; 61 89 begin 90 Sessions.Destroy; 91 Lock.Destroy; 62 92 inherited Destroy; 63 93 end;
Note:
See TracChangeset
for help on using the changeset viewer.