1 | unit UHTTPSessionFile;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UHTTPServer, syncobjs;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TFileHTTPSessionStorage }
|
---|
13 |
|
---|
14 | TFileHTTPSessionStorage = class(THTTPSessionStorage)
|
---|
15 | private
|
---|
16 | Lock: TCriticalSection;
|
---|
17 | procedure GetSessionId(HandlerData: THTTPHandlerData);
|
---|
18 | public
|
---|
19 | Directory: string;
|
---|
20 | SessionIdCookieName: string;
|
---|
21 | Sessions: TStringList;
|
---|
22 | procedure Load(HandlerData: THTTPHandlerData); override;
|
---|
23 | procedure Save(HandlerData: THTTPHandlerData); override;
|
---|
24 | constructor Create; override;
|
---|
25 | destructor Destroy; override;
|
---|
26 | end;
|
---|
27 |
|
---|
28 | implementation
|
---|
29 |
|
---|
30 | { THTTPSession }
|
---|
31 |
|
---|
32 | procedure TFileHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData);
|
---|
33 | begin
|
---|
34 | with HandlerData do
|
---|
35 | if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
|
---|
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;
|
---|
55 | end;
|
---|
56 | inherited;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TFileHTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
|
---|
60 | var
|
---|
61 | SessionFile: string;
|
---|
62 | begin
|
---|
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.');
|
---|
71 |
|
---|
72 | HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
|
---|
73 | finally
|
---|
74 | Lock.Release;
|
---|
75 | end;
|
---|
76 | inherited;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | constructor TFileHTTPSessionStorage.Create;
|
---|
80 | begin
|
---|
81 | inherited Create;
|
---|
82 | Lock := TCriticalSection.Create;
|
---|
83 | Sessions := TStringList.Create;
|
---|
84 | SessionIdCookieName := 'SessionId';
|
---|
85 | Directory := 'Session';
|
---|
86 | end;
|
---|
87 |
|
---|
88 | destructor TFileHTTPSessionStorage.Destroy;
|
---|
89 | begin
|
---|
90 | Sessions.Destroy;
|
---|
91 | Lock.Destroy;
|
---|
92 | inherited Destroy;
|
---|
93 | end;
|
---|
94 |
|
---|
95 | end.
|
---|