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

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 3.7 KB
Line 
1unit UHTTPSessionMySQL;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UHTTPServer, syncobjs, synacode, UCommon, USqlDatabase;
9
10type
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
37procedure Register;
38
39implementation
40
41procedure Register;
42begin
43 RegisterComponents('CoolWeb', [THTTPSessionStorageMySQL]);
44end;
45
46
47{ THTTPSession }
48
49function THTTPSessionStorageMySQL.GetNewSessionId: string;
50var
51 DbRows: TDbRows;
52 Found: Boolean;
53begin
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;
65end;
66
67procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData);
68begin
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;
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].Values['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.Values[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 Destroy;
141end;
142
143end.
Note: See TracBrowser for help on using the repository browser.