1 | unit UHTTPSessionMySQL;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UHTTPServer, syncobjs, synacode, UCommon, USqlDatabase;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TFileHTTPSessionStorage }
|
---|
13 |
|
---|
14 | TMySQLHTTPSessionStorage = class(THTTPSessionStorage)
|
---|
15 | private
|
---|
16 | Lock: TCriticalSection;
|
---|
17 | function GetNewSessionId: string;
|
---|
18 | procedure GetSessionId(HandlerData: THTTPHandlerData);
|
---|
19 | public
|
---|
20 | Timeout: Integer; // in seconds
|
---|
21 | SqlDatabase: TSqlDatabase;
|
---|
22 | SessionIdCookieName: string;
|
---|
23 | Sessions: TStringList;
|
---|
24 | procedure Load(HandlerData: THTTPHandlerData); override;
|
---|
25 | procedure Save(HandlerData: THTTPHandlerData); override;
|
---|
26 | constructor Create; override;
|
---|
27 | destructor Destroy; override;
|
---|
28 | end;
|
---|
29 |
|
---|
30 | implementation
|
---|
31 |
|
---|
32 | { THTTPSession }
|
---|
33 |
|
---|
34 | function TMySQLHTTPSessionStorage.GetNewSessionId: string;
|
---|
35 | var
|
---|
36 | DbRows: TDbRows;
|
---|
37 | begin
|
---|
38 | DbRows := nil;
|
---|
39 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
---|
40 | repeat
|
---|
41 | if Assigned(DbRows) then DbRows.Destroy;
|
---|
42 | DbRows := SqlDatabase.Query('SELECT * FROM Session WHERE Identification="' +
|
---|
43 | Result + '"');
|
---|
44 | if DbRows.Count > 0 then Result := BinToHexString(SHA1(FloatToStr(Now)));
|
---|
45 | until DbRows.Count > 0;
|
---|
46 | DbRows.Destroy;
|
---|
47 | end;
|
---|
48 |
|
---|
49 | procedure TMySQLHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData);
|
---|
50 | begin
|
---|
51 | with HandlerData do begin
|
---|
52 | if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
|
---|
53 | SessionId := Request.Cookies.Values[SessionIdCookieName];
|
---|
54 | end else begin
|
---|
55 | SessionId := GetNewSessionId;
|
---|
56 | Response.Cookies.Values[SessionIdCookieName] := SessionId;
|
---|
57 | end;
|
---|
58 | end;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | procedure TMySQLHTTPSessionStorage.Load(HandlerData: THTTPHandlerData);
|
---|
62 | var
|
---|
63 | DbRows: TDbRows;
|
---|
64 | begin
|
---|
65 | GetSessionId(HandlerData);
|
---|
66 | try
|
---|
67 | Lock.Acquire;
|
---|
68 | DbRows := SqlDatabase.Query('SELECT * FROM Session WHERE Identification="' +
|
---|
69 | HandlerData.SessionId + '"');
|
---|
70 | if DbRows.Count > 0 then begin
|
---|
71 | HandlerData.Session.Text := DbRows[0].Values['Variables'];
|
---|
72 | end else begin
|
---|
73 | HandlerData.SessionId := GetNewSessionId;
|
---|
74 | end;
|
---|
75 | DbRows.Destroy;
|
---|
76 | finally
|
---|
77 | Lock.Release;
|
---|
78 | end;
|
---|
79 | inherited;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TMySQLHTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
|
---|
83 | var
|
---|
84 | DbRows: TDbRows;
|
---|
85 | DbRows2: TDbRows;
|
---|
86 | begin
|
---|
87 | try
|
---|
88 | Lock.Acquire;
|
---|
89 | DbRows := SqlDatabase.Query('SELECT * FROM Session WHERE Identification="' +
|
---|
90 | HandlerData.SessionId + '"');
|
---|
91 | if DbRows.Count > 0 then
|
---|
92 | DbRows2 := SqlDatabase.Query('UPDATE Session SET Variables="' + HandlerData.Session.Text
|
---|
93 | + '" WHERE Identification="' + HandlerData.SessionId + '"')
|
---|
94 | else DbRows2 := SqlDatabase.Query('REPLACE Session SET Variables="' + HandlerData.Session.Text
|
---|
95 | + '" WHERE Identification="' + HandlerData.SessionId + '"');
|
---|
96 | DbRows2.Destroy;
|
---|
97 | DbRows.Destroy;
|
---|
98 | HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
|
---|
99 | finally
|
---|
100 | Lock.Release;
|
---|
101 | end;
|
---|
102 | inherited;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | constructor TMySQLHTTPSessionStorage.Create;
|
---|
106 | begin
|
---|
107 | inherited Create;
|
---|
108 | Lock := TCriticalSection.Create;
|
---|
109 | Sessions := TStringList.Create;
|
---|
110 | SessionIdCookieName := 'SessionId';
|
---|
111 | SqlDatabase := TSqlDatabase.Create;
|
---|
112 | Timeout := 3600;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | destructor TMySQLHTTPSessionStorage.Destroy;
|
---|
116 | begin
|
---|
117 | SqlDatabase.Destroy;
|
---|
118 | Sessions.Destroy;
|
---|
119 | Lock.Destroy;
|
---|
120 | inherited Destroy;
|
---|
121 | end;
|
---|
122 |
|
---|
123 | end.
|
---|