1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
procedure TFormMain.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
tempresp : string;
FileStream : TFileStream;
AccessSessionID: string;
tempind : Integer;
begin
if UpperCase(ARequestInfo.Document) <> '/FAVICON.ICO' then
begin
AuthValide := false;
AccessSessionID := '';
if ARequestInfo.Cookies.GetCookieIndex('AccessSessionID') > -1 then
begin
AccessSessionID := ARequestInfo.Cookies.Cookies[ARequestInfo.Cookies.GetCookieIndex('AccessSessionID')].Value;
end;
if (ARequestInfo.Params.values['Logout'] = 'true') then
begin
if (HTTPSessionCokie.IndexOf(AccessSessionID) > -1) then HTTPSessionCokie.Delete(HTTPSessionCokie.IndexOf(AccessSessionID));
AccessSessionID := '';
end;
if (ARequestInfo.Params.values['Login'] = 'true') then
begin
if (ARequestInfo.Params.values['UserName'] = DefaultWebUserName) and (ARequestInfo.Params.values['Password'] = DefaultWebPassword) then
begin
AccessSessionID := '';
Randomize;
// genere une chaine aleatoire de 64 lettres comme no de session
for tempind := 1 to 64 do
begin
AccessSessionID := AccessSessionID + Chr(Random(26) + 65);
end;
HTTPSessionCokie.Add(AccessSessionID);
AResponseInfo.CustomHeaders.AddValue('Set-Cookie', 'AccessSessionID=' + AccessSessionID + ';');
end;
end;
if (HTTPSessionCokie.IndexOf(AccessSessionID) > -1) then
begin
tempresp := '<b>Hello World !</b><br>';
tempresp := tempresp + '<form id="LogoutForm" method="post">';
tempresp := tempresp + '<input type="hidden" name="Logout" value="true">';
tempresp := tempresp + ' <button type="submit">Logout</button>';
tempresp := tempresp + '</form>';
AResponseInfo.ContentText := tempresp;
AResponseInfo.ResponseNo := 200;
end
else
begin
if (HTTPSessionCokie.IndexOf(AccessSessionID) > -1) then HTTPSessionCokie.Delete(HTTPSessionCokie.IndexOf(AccessSessionID));
AccessSessionID := '';
tempresp := '<b>Private Website, proced with login :</b><br><br>';
tempresp := tempresp + '<form id="LoginForm" method="post">';
tempresp := tempresp + '<input type="hidden" name="Login" value="true">';
tempresp := tempresp + '<table><tr>';
tempresp := tempresp + '<td>User</td><td><input type="text" name="UserName" value=""></td>';
tempresp := tempresp + '</tr><tr>';
tempresp := tempresp + '<td>Pass</td><td><input type="text" name="Password" value=""></td>';
tempresp := tempresp + '</tr></table><br>';
tempresp := tempresp + '<button type="submit">Login</button>';
tempresp := tempresp + '</form>';
AResponseInfo.ContentText := tempresp;
AResponseInfo.ResponseNo := 200;
end;
end
else
begin
FileStream := TFileStream.create(ExtractFileDir(ExeName) + '\icon.ico', fmOpenRead or fmShareDenyWrite);
try
AResponseInfo.ContentType := 'image/x-icon';
AResponseInfo.ContentStream := FileStream;
AResponseInfo.ContentLength := FileStream.Size;
except
FileStream.Free;
raise;
end;
end;
end; |
Partager