1 | unit HTTPSessionFile;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, HTTPServer, syncobjs, synacode, Common, FileUtil,
|
---|
7 | LazFileUtils;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
34 | procedure Register;
|
---|
35 |
|
---|
36 |
|
---|
37 | implementation
|
---|
38 |
|
---|
39 | resourcestring
|
---|
40 | SCantCreateSessionStorageDirectory = 'Can''t create session storage directory.';
|
---|
41 |
|
---|
42 | procedure Register;
|
---|
43 | begin
|
---|
44 | RegisterComponents('CoolWeb', [THTTPSessionStorageFile]);
|
---|
45 | end;
|
---|
46 |
|
---|
47 | { THTTPSession }
|
---|
48 |
|
---|
49 | function THTTPSessionStorageFile.GetNewSessionId: string;
|
---|
50 | begin
|
---|
51 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
---|
52 | while FileExistsUTF8(Directory + DirectorySeparator + Result) do
|
---|
53 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
---|
54 | end;
|
---|
55 |
|
---|
56 | procedure THTTPSessionStorageFile.GetSessionId(HandlerData: THTTPHandlerData);
|
---|
57 | var
|
---|
58 | Value: string;
|
---|
59 | begin
|
---|
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;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | procedure THTTPSessionStorageFile.Load(HandlerData: THTTPHandlerData);
|
---|
71 | var
|
---|
72 | SessionFile: string;
|
---|
73 | begin
|
---|
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;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | procedure THTTPSessionStorageFile.Save(HandlerData: THTTPHandlerData);
|
---|
88 | var
|
---|
89 | SessionFile: string;
|
---|
90 | begin
|
---|
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;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | constructor THTTPSessionStorageFile.Create(AOwner: TComponent);
|
---|
108 | begin
|
---|
109 | inherited;
|
---|
110 | Lock := TCriticalSection.Create;
|
---|
111 | Sessions := TStringList.Create;
|
---|
112 | SessionIdCookieName := 'SessionId';
|
---|
113 | Directory := 'Session';
|
---|
114 | Timeout := 3600;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | destructor THTTPSessionStorageFile.Destroy;
|
---|
118 | begin
|
---|
119 | Sessions.Destroy;
|
---|
120 | Lock.Destroy;
|
---|
121 | inherited;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | end.
|
---|