source: Network/HTTP/UHTTPSessionFile.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: 2.7 KB
Line 
1unit UHTTPSessionFile;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UHTTPServer, syncobjs, synacode, UCommon;
9
10type
11
12 { TFileHTTPSessionStorage }
13
14 TFileHTTPSessionStorage = class(THTTPSessionStorage)
15 private
16 Lock: TCriticalSection;
17 function GetNewSessionId: string;
18 procedure GetSessionId(HandlerData: THTTPHandlerData);
19 public
20 Timeout: Integer; // in seconds
21 Directory: string;
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 TFileHTTPSessionStorage.GetNewSessionId: string;
35begin
36 Result := BinToHexString(SHA1(FloatToStr(Now)));
37 while FileExists(Directory + '/' + Result) do
38 Result := BinToHexString(SHA1(FloatToStr(Now)));
39end;
40
41procedure TFileHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData);
42begin
43 with HandlerData do begin
44 if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
45 SessionId := Request.Cookies.Values[SessionIdCookieName];
46 end else begin
47 SessionId := GetNewSessionId;
48 Response.Cookies.Values[SessionIdCookieName] := SessionId;
49 end;
50 end;
51end;
52
53procedure TFileHTTPSessionStorage.Load(HandlerData: THTTPHandlerData);
54var
55 SessionFile: string;
56begin
57 GetSessionId(HandlerData);
58 try
59 Lock.Acquire;
60 SessionFile := Directory + '/' + HandlerData.SessionId;
61 if FileExists(SessionFile) then
62 HandlerData.Session.LoadFromFile(SessionFile)
63 else HandlerData.SessionId := GetNewSessionId;
64 finally
65 Lock.Release;
66 end;
67 inherited;
68end;
69
70procedure TFileHTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
71var
72 SessionFile: string;
73begin
74 try
75 Lock.Acquire;
76 SessionFile := Directory + '/' + HandlerData.SessionId;
77 ForceDirectories(Directory);
78 if DirectoryExists(Directory) then begin
79 DeleteFile(SessionFile);
80 HandlerData.Session.SaveToFile(SessionFile)
81 end else raise Exception.Create('Can''t create session storage directory.');
82
83 HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
84 finally
85 Lock.Release;
86 end;
87 inherited;
88end;
89
90constructor TFileHTTPSessionStorage.Create;
91begin
92 inherited Create;
93 Lock := TCriticalSection.Create;
94 Sessions := TStringList.Create;
95 SessionIdCookieName := 'SessionId';
96 Directory := 'Session';
97 Timeout := 3600;
98end;
99
100destructor TFileHTTPSessionStorage.Destroy;
101begin
102 Sessions.Destroy;
103 Lock.Destroy;
104 inherited Destroy;
105end;
106
107end.
Note: See TracBrowser for help on using the repository browser.