| 1 | unit UHTTPSessionFile; | 
|---|
| 2 |  | 
|---|
| 3 | {$mode Delphi}{$H+} | 
|---|
| 4 |  | 
|---|
| 5 | interface | 
|---|
| 6 |  | 
|---|
| 7 | uses | 
|---|
| 8 | Classes, SysUtils, UHTTPServer, syncobjs, synacode, UCommon; | 
|---|
| 9 |  | 
|---|
| 10 | type | 
|---|
| 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 |  | 
|---|
| 30 | implementation | 
|---|
| 31 |  | 
|---|
| 32 | { THTTPSession } | 
|---|
| 33 |  | 
|---|
| 34 | function TFileHTTPSessionStorage.GetNewSessionId: string; | 
|---|
| 35 | begin | 
|---|
| 36 | Result := BinToHexString(SHA1(FloatToStr(Now))); | 
|---|
| 37 | while FileExists(Directory + '/' + Result) do | 
|---|
| 38 | Result := BinToHexString(SHA1(FloatToStr(Now))); | 
|---|
| 39 | end; | 
|---|
| 40 |  | 
|---|
| 41 | procedure TFileHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData); | 
|---|
| 42 | begin | 
|---|
| 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; | 
|---|
| 51 | end; | 
|---|
| 52 |  | 
|---|
| 53 | procedure TFileHTTPSessionStorage.Load(HandlerData: THTTPHandlerData); | 
|---|
| 54 | var | 
|---|
| 55 | SessionFile: string; | 
|---|
| 56 | begin | 
|---|
| 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; | 
|---|
| 68 | end; | 
|---|
| 69 |  | 
|---|
| 70 | procedure TFileHTTPSessionStorage.Save(HandlerData: THTTPHandlerData); | 
|---|
| 71 | var | 
|---|
| 72 | SessionFile: string; | 
|---|
| 73 | begin | 
|---|
| 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; | 
|---|
| 88 | end; | 
|---|
| 89 |  | 
|---|
| 90 | constructor TFileHTTPSessionStorage.Create; | 
|---|
| 91 | begin | 
|---|
| 92 | inherited Create; | 
|---|
| 93 | Lock := TCriticalSection.Create; | 
|---|
| 94 | Sessions := TStringList.Create; | 
|---|
| 95 | SessionIdCookieName := 'SessionId'; | 
|---|
| 96 | Directory := 'Session'; | 
|---|
| 97 | Timeout := 3600; | 
|---|
| 98 | end; | 
|---|
| 99 |  | 
|---|
| 100 | destructor TFileHTTPSessionStorage.Destroy; | 
|---|
| 101 | begin | 
|---|
| 102 | Sessions.Destroy; | 
|---|
| 103 | Lock.Destroy; | 
|---|
| 104 | inherited Destroy; | 
|---|
| 105 | end; | 
|---|
| 106 |  | 
|---|
| 107 | end. | 
|---|