source: trunk/Packages/CoolWeb/WebServer/HTTPSessionMySQL.pas

Last change on this file was 151, checked in by chronos, 5 months ago
File size: 3.7 KB
Line 
1unit HTTPSessionMySQL;
2
3interface
4
5uses
6 Classes, SysUtils, HTTPServer, syncobjs, synacode, Common, SqlDatabase;
7
8type
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
35procedure Register;
36
37
38implementation
39
40procedure Register;
41begin
42 RegisterComponents('CoolWeb', [THTTPSessionStorageMySQL]);
43end;
44
45{ THTTPSession }
46
47function THTTPSessionStorageMySQL.GetNewSessionId: string;
48var
49 DbRows: TDbRows;
50 Found: Boolean;
51begin
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;
63end;
64
65procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData);
66var
67 Value: string;
68begin
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;
76end;
77
78procedure THTTPSessionStorageMySQL.Load(HandlerData: THTTPHandlerData);
79var
80 DbRows: TDbRows;
81begin
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;
100end;
101
102procedure THTTPSessionStorageMySQL.Save(HandlerData: THTTPHandlerData);
103var
104 DbRows: TDbRows;
105 DbRows2: TDbRows;
106begin
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;
125end;
126
127constructor THTTPSessionStorageMySQL.Create(AOwner: TComponent);
128begin
129 inherited;
130 Lock := TCriticalSection.Create;
131 Sessions := TStringList.Create;
132 SessionIdCookieName := 'SessionId';
133 Timeout := 3 * 3600;
134end;
135
136destructor THTTPSessionStorageMySQL.Destroy;
137begin
138 Sessions.Free;
139 Lock.Free;
140 inherited;
141end;
142
143end.
Note: See TracBrowser for help on using the repository browser.