1 | unit HTTPSessionMySQL;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, HTTPServer, syncobjs, synacode, Common, SqlDatabase;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TFileHTTPSessionStorage }
|
---|
11 |
|
---|
12 | { THTTPSessionStorageMySQL }
|
---|
13 |
|
---|
14 | THTTPSessionStorageMySQL = class(THTTPSessionStorage)
|
---|
15 | private
|
---|
16 | FSessionIdCookieName: string;
|
---|
17 | FDatabase: TSqlDatabase;
|
---|
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 Database: TSqlDatabase read FDatabase write FDatabase;
|
---|
30 | property Timeout: Integer read FTimeout write FTimeout; // in seconds
|
---|
31 | property SessionIdCookieName: string read FSessionIdCookieName
|
---|
32 | write FSessionIdCookieName;
|
---|
33 | end;
|
---|
34 |
|
---|
35 | procedure Register;
|
---|
36 |
|
---|
37 |
|
---|
38 | implementation
|
---|
39 |
|
---|
40 | procedure Register;
|
---|
41 | begin
|
---|
42 | RegisterComponents('CoolWeb', [THTTPSessionStorageMySQL]);
|
---|
43 | end;
|
---|
44 |
|
---|
45 | { THTTPSession }
|
---|
46 |
|
---|
47 | function THTTPSessionStorageMySQL.GetNewSessionId: string;
|
---|
48 | var
|
---|
49 | DbRows: TDbRows;
|
---|
50 | Found: Boolean;
|
---|
51 | begin
|
---|
52 | repeat
|
---|
53 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
---|
54 | try
|
---|
55 | DbRows := TDbRows.Create;
|
---|
56 | Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
|
---|
57 | Result + '"');
|
---|
58 | Found := DbRows.Count > 0;
|
---|
59 | finally
|
---|
60 | DbRows.Free;
|
---|
61 | end;
|
---|
62 | until not Found;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData);
|
---|
66 | var
|
---|
67 | Value: string;
|
---|
68 | begin
|
---|
69 | with HandlerData do begin
|
---|
70 | if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin
|
---|
71 | SessionId := Value;
|
---|
72 | end else begin
|
---|
73 | SessionId := GetNewSessionId;
|
---|
74 | end;
|
---|
75 | end;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | procedure THTTPSessionStorageMySQL.Load(HandlerData: THTTPHandlerData);
|
---|
79 | var
|
---|
80 | DbRows: TDbRows;
|
---|
81 | begin
|
---|
82 | GetSessionId(HandlerData);
|
---|
83 | try
|
---|
84 | Lock.Acquire;
|
---|
85 | DbRows := TDbRows.Create;
|
---|
86 | Database.Query(DbRows, 'DELETE FROM `HTTPSession` WHERE `Time` < DATE_SUB(NOW(), INTERVAL ' +
|
---|
87 | IntToStr(Timeout) +' SECOND)');
|
---|
88 | Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
|
---|
89 | HandlerData.SessionId + '"');
|
---|
90 | if DbRows.Count > 0 then begin
|
---|
91 | HandlerData.Session.Text := DbRows[0].Items['Variables'];
|
---|
92 | end else begin
|
---|
93 | HandlerData.SessionId := GetNewSessionId;
|
---|
94 | end;
|
---|
95 | finally
|
---|
96 | DbRows.Free;
|
---|
97 | Lock.Release;
|
---|
98 | end;
|
---|
99 | inherited;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure THTTPSessionStorageMySQL.Save(HandlerData: THTTPHandlerData);
|
---|
103 | var
|
---|
104 | DbRows: TDbRows;
|
---|
105 | DbRows2: TDbRows;
|
---|
106 | begin
|
---|
107 | try
|
---|
108 | Lock.Acquire;
|
---|
109 | DbRows := TDbRows.Create;
|
---|
110 | DbRows2 := TDbRows.Create;
|
---|
111 | Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
|
---|
112 | HandlerData.SessionId + '"');
|
---|
113 | if DbRows.Count > 0 then
|
---|
114 | Database.Query(DbRows2, 'UPDATE `HTTPSession` SET `Variables`="' + HandlerData.Session.Text
|
---|
115 | + '", `Time` = NOW() WHERE `Identification`="' + HandlerData.SessionId + '"')
|
---|
116 | else Database.Query(DbRows2, 'INSERT INTO `HTTPSession` (`Time`, `Variables`, `Identification`) VALUES (' +
|
---|
117 | 'NOW(), "' + HandlerData.Session.Text + '", "' + HandlerData.SessionId + '")');
|
---|
118 | HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId;
|
---|
119 | finally
|
---|
120 | DbRows2.Free;
|
---|
121 | DbRows.Free;
|
---|
122 | Lock.Release;
|
---|
123 | end;
|
---|
124 | inherited;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | constructor THTTPSessionStorageMySQL.Create(AOwner: TComponent);
|
---|
128 | begin
|
---|
129 | inherited;
|
---|
130 | Lock := TCriticalSection.Create;
|
---|
131 | Sessions := TStringList.Create;
|
---|
132 | SessionIdCookieName := 'SessionId';
|
---|
133 | Timeout := 3 * 3600;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | destructor THTTPSessionStorageMySQL.Destroy;
|
---|
137 | begin
|
---|
138 | Sessions.Free;
|
---|
139 | Lock.Free;
|
---|
140 | inherited;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | end.
|
---|