source: Network/HTTP/UHTTPSessionMySQL.pas

Last change on this file was 35, checked in by george, 14 years ago
  • Přidáno: Třídy pro běh web serveru, asociativní pole, dávkový přenos paketů.
File size: 3.3 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 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
30implementation
31
32{ THTTPSession }
33
34function TMySQLHTTPSessionStorage.GetNewSessionId: string;
35var
36 DbRows: TDbRows;
37begin
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;
47end;
48
49procedure TMySQLHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData);
50begin
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;
59end;
60
61procedure TMySQLHTTPSessionStorage.Load(HandlerData: THTTPHandlerData);
62var
63 DbRows: TDbRows;
64begin
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;
80end;
81
82procedure TMySQLHTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
83var
84 DbRows: TDbRows;
85 DbRows2: TDbRows;
86begin
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;
103end;
104
105constructor TMySQLHTTPSessionStorage.Create;
106begin
107 inherited Create;
108 Lock := TCriticalSection.Create;
109 Sessions := TStringList.Create;
110 SessionIdCookieName := 'SessionId';
111 SqlDatabase := TSqlDatabase.Create;
112 Timeout := 3600;
113end;
114
115destructor TMySQLHTTPSessionStorage.Destroy;
116begin
117 SqlDatabase.Destroy;
118 Sessions.Destroy;
119 Lock.Destroy;
120 inherited Destroy;
121end;
122
123end.
Note: See TracBrowser for help on using the repository browser.