source: trunk/Packages/CoolWeb/WebServer/HTTPSessionFile.pas

Last change on this file was 151, checked in by chronos, 9 months ago
File size: 3.1 KB
Line 
1unit HTTPSessionFile;
2
3interface
4
5uses
6 Classes, SysUtils, HTTPServer, syncobjs, synacode, Common, FileUtil,
7 LazFileUtils;
8
9type
10
11 { THTTPSessionStorageFile }
12
13 THTTPSessionStorageFile = class(THTTPSessionStorage)
14 private
15 FDirectory: string;
16 FSessionIdCookieName: string;
17 FTimeout: Integer;
18 Lock: TCriticalSection;
19 function GetNewSessionId: string;
20 procedure GetSessionId(HandlerData: THTTPHandlerData);
21 public
22 Sessions: TStringList;
23 procedure Load(HandlerData: THTTPHandlerData); override;
24 procedure Save(HandlerData: THTTPHandlerData); override;
25 constructor Create(AOwner: TComponent); override;
26 destructor Destroy; override;
27 published
28 property Timeout: Integer read FTimeout write FTimeout; // in seconds
29 property Directory: string read FDirectory write FDirectory;
30 property SessionIdCookieName: string read FSessionIdCookieName
31 write FSessionIdCookieName;
32 end;
33
34procedure Register;
35
36
37implementation
38
39resourcestring
40 SCantCreateSessionStorageDirectory = 'Can''t create session storage directory.';
41
42procedure Register;
43begin
44 RegisterComponents('CoolWeb', [THTTPSessionStorageFile]);
45end;
46
47{ THTTPSession }
48
49function THTTPSessionStorageFile.GetNewSessionId: string;
50begin
51 Result := BinToHexString(SHA1(FloatToStr(Now)));
52 while FileExistsUTF8(Directory + DirectorySeparator + Result) do
53 Result := BinToHexString(SHA1(FloatToStr(Now)));
54end;
55
56procedure THTTPSessionStorageFile.GetSessionId(HandlerData: THTTPHandlerData);
57var
58 Value: string;
59begin
60 with HandlerData do begin
61 if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin
62 SessionId := Value;
63 end else begin
64 SessionId := GetNewSessionId;
65 Response.Cookies.Items[SessionIdCookieName] := SessionId;
66 end;
67 end;
68end;
69
70procedure THTTPSessionStorageFile.Load(HandlerData: THTTPHandlerData);
71var
72 SessionFile: string;
73begin
74 GetSessionId(HandlerData);
75 try
76 Lock.Acquire;
77 SessionFile := Directory + DirectorySeparator + HandlerData.SessionId;
78 if FileExistsUTF8(SessionFile) then
79 HandlerData.Session.LoadFromFile(SessionFile)
80 else HandlerData.SessionId := GetNewSessionId;
81 finally
82 Lock.Release;
83 end;
84 inherited;
85end;
86
87procedure THTTPSessionStorageFile.Save(HandlerData: THTTPHandlerData);
88var
89 SessionFile: string;
90begin
91 try
92 Lock.Acquire;
93 SessionFile := Directory + DirectorySeparator + HandlerData.SessionId;
94 ForceDirectories(Directory);
95 if DirectoryExistsUTF8(Directory) then begin
96 DeleteFile(SessionFile);
97 HandlerData.Session.SaveToFile(SessionFile)
98 end else raise Exception.Create(SCantCreateSessionStorageDirectory);
99
100 HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId;
101 finally
102 Lock.Release;
103 end;
104 inherited;
105end;
106
107constructor THTTPSessionStorageFile.Create(AOwner: TComponent);
108begin
109 inherited;
110 Lock := TCriticalSection.Create;
111 Sessions := TStringList.Create;
112 SessionIdCookieName := 'SessionId';
113 Directory := 'Session';
114 Timeout := 3600;
115end;
116
117destructor THTTPSessionStorageFile.Destroy;
118begin
119 Sessions.Destroy;
120 Lock.Destroy;
121 inherited;
122end;
123
124end.
Note: See TracBrowser for help on using the repository browser.