source: trunk/Demo/Packages/CoolWeb/WebServer/UHTTPSessionFile.pas

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