source: branches/DirectWeb/UHTTPSessionFile.pas

Last change on this file was 88, checked in by george, 14 years ago
  • Upraveno: Nedokončené přepracování správy vláken. Použit systém přidělování ze společné sady o dané velikosti namísto přidělování vláken ke každému požadavku samostatně. Vlákna jsou inicializována při spuštění aplikace, jsou opakovaně používána a uvolněna až při ukončení běhu aplikace.
  • Přidáno: Třída pro práci s opakovaně spustitelnými vlákny.
  • Přidáno: Třída přidělování objektů ze zásoby o dané velikosti.
  • Přidáno: Třída pro zobrazování stránkování seznamů položek.
File size: 2.3 KB
Line 
1unit UHTTPSessionFile;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UHTTPServer, syncobjs;
9
10type
11
12 { TFileHTTPSessionStorage }
13
14 TFileHTTPSessionStorage = class(THTTPSessionStorage)
15 private
16 Lock: TCriticalSection;
17 procedure GetSessionId(HandlerData: THTTPHandlerData);
18 public
19 Directory: string;
20 SessionIdCookieName: string;
21 Sessions: TStringList;
22 procedure Load(HandlerData: THTTPHandlerData); override;
23 procedure Save(HandlerData: THTTPHandlerData); override;
24 constructor Create; override;
25 destructor Destroy; override;
26 end;
27
28implementation
29
30{ THTTPSession }
31
32procedure TFileHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData);
33begin
34 with HandlerData do
35 if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
36 SessionId := Request.Cookies.Values[SessionIdCookieName];
37 end else begin
38 SessionId := IntToStr(Random(2000000000));
39 // TODO: Check if new id already used
40 end;
41end;
42
43procedure TFileHTTPSessionStorage.Load(HandlerData: THTTPHandlerData);
44var
45 SessionFile: string;
46begin
47 GetSessionId(HandlerData);
48 try
49 Lock.Acquire;
50 SessionFile := Directory + '/' + HandlerData.SessionId;
51 if FileExists(SessionFile) then
52 HandlerData.Session.LoadFromFile(SessionFile);
53 finally
54 Lock.Release;
55 end;
56 inherited;
57end;
58
59procedure TFileHTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
60var
61 SessionFile: string;
62begin
63 try
64 Lock.Acquire;
65 SessionFile := Directory + '/' + HandlerData.SessionId;
66 ForceDirectories(Directory);
67 if DirectoryExists(Directory) then begin
68 DeleteFile(SessionFile);
69 HandlerData.Session.SaveToFile(SessionFile)
70 end else raise Exception.Create('Can''t create session storage directory.');
71
72 HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
73 finally
74 Lock.Release;
75 end;
76 inherited;
77end;
78
79constructor TFileHTTPSessionStorage.Create;
80begin
81 inherited Create;
82 Lock := TCriticalSection.Create;
83 Sessions := TStringList.Create;
84 SessionIdCookieName := 'SessionId';
85 Directory := 'Session';
86end;
87
88destructor TFileHTTPSessionStorage.Destroy;
89begin
90 Sessions.Destroy;
91 Lock.Destroy;
92 inherited Destroy;
93end;
94
95end.
Note: See TracBrowser for help on using the repository browser.