| 1 | unit UHTTPSessionFile;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, UHTTPServer, syncobjs, synacode, UCommon, FileUtil;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { THTTPSessionStorageFile }
|
|---|
| 13 |
|
|---|
| 14 | THTTPSessionStorageFile = class(THTTPSessionStorage)
|
|---|
| 15 | private
|
|---|
| 16 | FDirectory: string;
|
|---|
| 17 | FSessionIdCookieName: string;
|
|---|
| 18 | FTimeout: Integer;
|
|---|
| 19 | Lock: TCriticalSection;
|
|---|
| 20 | function GetNewSessionId: string;
|
|---|
| 21 | procedure GetSessionId(HandlerData: THTTPHandlerData);
|
|---|
| 22 | public
|
|---|
| 23 | Sessions: TStringList;
|
|---|
| 24 | procedure Load(HandlerData: THTTPHandlerData); override;
|
|---|
| 25 | procedure Save(HandlerData: THTTPHandlerData); override;
|
|---|
| 26 | constructor Create(AOwner: TComponent); override;
|
|---|
| 27 | destructor Destroy; override;
|
|---|
| 28 | published
|
|---|
| 29 | property Timeout: Integer read FTimeout write FTimeout; // in seconds
|
|---|
| 30 | property Directory: string read FDirectory write FDirectory;
|
|---|
| 31 | property SessionIdCookieName: string read FSessionIdCookieName
|
|---|
| 32 | write FSessionIdCookieName;
|
|---|
| 33 | end;
|
|---|
| 34 |
|
|---|
| 35 | procedure Register;
|
|---|
| 36 |
|
|---|
| 37 | implementation
|
|---|
| 38 |
|
|---|
| 39 | resourcestring
|
|---|
| 40 | SCantCreateSessionStorageDirectory = 'Can''t create session storage directory.';
|
|---|
| 41 |
|
|---|
| 42 | procedure Register;
|
|---|
| 43 | begin
|
|---|
| 44 | RegisterComponents('CoolWeb', [THTTPSessionStorageFile]);
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | { THTTPSession }
|
|---|
| 49 |
|
|---|
| 50 | function THTTPSessionStorageFile.GetNewSessionId: string;
|
|---|
| 51 | begin
|
|---|
| 52 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
|---|
| 53 | while FileExistsUTF8(Directory + DirectorySeparator + Result) do
|
|---|
| 54 | Result := BinToHexString(SHA1(FloatToStr(Now)));
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | procedure THTTPSessionStorageFile.GetSessionId(HandlerData: THTTPHandlerData);
|
|---|
| 58 | begin
|
|---|
| 59 | with HandlerData do begin
|
|---|
| 60 | if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
|
|---|
| 61 | SessionId := Request.Cookies.Values[SessionIdCookieName];
|
|---|
| 62 | end else begin
|
|---|
| 63 | SessionId := GetNewSessionId;
|
|---|
| 64 | Response.Cookies.Values[SessionIdCookieName] := SessionId;
|
|---|
| 65 | end;
|
|---|
| 66 | end;
|
|---|
| 67 | end;
|
|---|
| 68 |
|
|---|
| 69 | procedure THTTPSessionStorageFile.Load(HandlerData: THTTPHandlerData);
|
|---|
| 70 | var
|
|---|
| 71 | SessionFile: string;
|
|---|
| 72 | begin
|
|---|
| 73 | GetSessionId(HandlerData);
|
|---|
| 74 | try
|
|---|
| 75 | Lock.Acquire;
|
|---|
| 76 | SessionFile := Directory + DirectorySeparator + HandlerData.SessionId;
|
|---|
| 77 | if FileExistsUTF8(SessionFile) then
|
|---|
| 78 | HandlerData.Session.LoadFromFile(SessionFile)
|
|---|
| 79 | else HandlerData.SessionId := GetNewSessionId;
|
|---|
| 80 | finally
|
|---|
| 81 | Lock.Release;
|
|---|
| 82 | end;
|
|---|
| 83 | inherited;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | procedure THTTPSessionStorageFile.Save(HandlerData: THTTPHandlerData);
|
|---|
| 87 | var
|
|---|
| 88 | SessionFile: string;
|
|---|
| 89 | begin
|
|---|
| 90 | try
|
|---|
| 91 | Lock.Acquire;
|
|---|
| 92 | SessionFile := Directory + DirectorySeparator + HandlerData.SessionId;
|
|---|
| 93 | ForceDirectories(Directory);
|
|---|
| 94 | if DirectoryExistsUTF8(Directory) then begin
|
|---|
| 95 | DeleteFile(SessionFile);
|
|---|
| 96 | HandlerData.Session.SaveToFile(SessionFile)
|
|---|
| 97 | end else raise Exception.Create(SCantCreateSessionStorageDirectory);
|
|---|
| 98 |
|
|---|
| 99 | HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
|
|---|
| 100 | finally
|
|---|
| 101 | Lock.Release;
|
|---|
| 102 | end;
|
|---|
| 103 | inherited;
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | constructor THTTPSessionStorageFile.Create(AOwner: TComponent);
|
|---|
| 107 | begin
|
|---|
| 108 | inherited;
|
|---|
| 109 | Lock := TCriticalSection.Create;
|
|---|
| 110 | Sessions := TStringList.Create;
|
|---|
| 111 | SessionIdCookieName := 'SessionId';
|
|---|
| 112 | Directory := 'Session';
|
|---|
| 113 | Timeout := 3600;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | destructor THTTPSessionStorageFile.Destroy;
|
|---|
| 117 | begin
|
|---|
| 118 | Sessions.Destroy;
|
|---|
| 119 | Lock.Destroy;
|
|---|
| 120 | inherited Destroy;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | end.
|
|---|