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 | { THTTPSessionStorageMySQL }
|
---|
15 |
|
---|
16 | THTTPSessionStorageMySQL = class(THTTPSessionStorage)
|
---|
17 | private
|
---|
18 | FSessionIdCookieName: string;
|
---|
19 | FDatabase: TSqlDatabase;
|
---|
20 | FTimeout: Integer;
|
---|
21 | Lock: TCriticalSection;
|
---|
22 | function GetNewSessionId: string;
|
---|
23 | procedure GetSessionId(HandlerData: THTTPHandlerData);
|
---|
24 | public
|
---|
25 | Sessions: TStringList;
|
---|
26 | procedure Load(HandlerData: THTTPHandlerData); override;
|
---|
27 | procedure Save(HandlerData: THTTPHandlerData); override;
|
---|
28 | constructor Create(AOwner: TComponent); override;
|
---|
29 | destructor Destroy; override;
|
---|
30 | published
|
---|
31 | property Database: TSqlDatabase read FDatabase write FDatabase;
|
---|
32 | property Timeout: Integer read FTimeout write FTimeout; // in seconds
|
---|
33 | property SessionIdCookieName: string read FSessionIdCookieName
|
---|
34 | write FSessionIdCookieName;
|
---|
35 | end;
|
---|
36 |
|
---|
37 | procedure Register;
|
---|
38 |
|
---|
39 | implementation
|
---|
40 |
|
---|
41 | procedure Register;
|
---|
42 | begin
|
---|
43 | RegisterComponents('CoolWeb', [THTTPSessionStorageMySQL]);
|
---|
44 | end;
|
---|
45 |
|
---|
46 |
|
---|
47 | { THTTPSession }
|
---|
48 |
|
---|
49 | function THTTPSessionStorageMySQL.GetNewSessionId: string;
|
---|
50 | var
|
---|
51 | DbRows: TDbRows;
|
---|
52 | Found: Boolean;
|
---|
53 | begin
|
---|
54 | repeat
|
---|
55 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
---|
56 | try
|
---|
57 | DbRows := TDbRows.Create;
|
---|
58 | Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
|
---|
59 | Result + '"');
|
---|
60 | Found := DbRows.Count > 0;
|
---|
61 | finally
|
---|
62 | DbRows.Free;
|
---|
63 | end;
|
---|
64 | until not Found;
|
---|
65 | end;
|
---|
66 |
|
---|
67 | procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData);
|
---|
68 | begin
|
---|
69 | with HandlerData do begin
|
---|
70 | if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
|
---|
71 | SessionId := Request.Cookies.Values[SessionIdCookieName];
|
---|
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].Values['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.Values[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 Destroy;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | end.
|
---|