procedure TMyService.HTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
const
// http://de.selfhtml.org/diverses/mimetypen.htm
FileExt:
array[0..7]
of string = ('
', '
.txt', '
.html', '
.css', '
.js', '
.gif', '
.jpeg', '
.png');
ContentType:
array[0..7]
of string = ('
application/octet-stream', '
text/plain', '
text/html', '
text/css', '
text/js', '
image/gif', '
image/jpeg', '
image/png');
var
HTMLPath:
string;
begin
try
HTMLPath := ARequestInfo.Document;
while (HTMLPath <> '
')
and CharInSet(HTMLPath[1], ['
/', '
\', '
.', '
:'])
do
Delete(HTMLPath, 1, 1);
if HTMLPath = '
info'
then begin
LogEvent('
WebServer-Get 200: ' + ARequestInfo.URI + '
=> ' + ARequestInfo.From);
AResponseInfo.ContentType := '
text/plain';
AResponseInfo.ResponseNo := 200;
AResponseInfo.ResponseText := '
OK';
AResponseInfo.ContentText := '
ServiceName: ' + DataServer1.ServiceName + #10
+ '
ServicePath: ' + ParamStr(0) + #10
+ '
ServerName: ' + TDSServerMethods(
nil).ServerComputerName + #10
// geht, da ServerComputerName auf nichts einer TDSServerMethods-Instanz zugreift
+ #10
+ '
HelpName: ' + HTTPServer1.ServerSoftware + #10
+ '
HelpPort: ' + IntToStr(HTTPServer1.DefaultPort) + #10
+ '
HelpPath: ' + Path_Help + #10;
end else if (Trim(HTMLPath) = '
')
or (TPath.GetExtendedPrefix(HTMLPath) <> TPathPrefixType.pptNoPrefix)
or not TPath.HasValidFileNameChars(HTMLPath, False)
then begin
LogEvent('
WebServer-Get 403: ' + ARequestInfo.URI + '
=> ' + ARequestInfo.From);
AResponseInfo.ContentType := '
text/plain';
AResponseInfo.ResponseNo := 403;
AResponseInfo.ResponseText := '
Forbidden';
AResponseInfo.ContentText := '
403 Forbidden'#10#10 + HTMLPath;
end else if not FileExists(Path_Help + HTMLPath)
then begin
LogEvent('
WebServer-Get 404: ' + ARequestInfo.URI + '
=> ' + ARequestInfo.From);
AResponseInfo.ContentType := '
text/plain';
AResponseInfo.ResponseNo := 404;
AResponseInfo.ResponseText := '
Not Found';
AResponseInfo.ContentText := '
404 Not Found'#10#10 + HTMLPath;
end else begin
LogEvent('
WebServer-Get: ' + ARequestInfo.URI + '
=> ' + ARequestInfo.From);
AResponseInfo.ContentType := ContentType[Max(IndexText(ExtractFileExt(HTMLPath), FileExt), 0)];
AResponseInfo.ResponseNo := 200;
AResponseInfo.ResponseText := '
OK';
AResponseInfo.ContentStream := TFileStream.Create(Path_Help + HTMLPath, fmOpenRead
or fmShareDenyWrite);
// Daten
AResponseInfo.ContentLength := AResponseInfo.ContentStream.Size;
// Datenmenge
AResponseInfo.CacheControl := '
public';
// Browsercache verwenden
AResponseInfo.Date := TFile.GetLastWriteTime(Path_Help + HTMLPath);
// Änderungsdatum
AResponseInfo.ETag := Format('
%d_%.5f', [AResponseInfo.ContentLength, AResponseInfo.Date]);
// (billiger) Hash
end;
except
on E:
Exception do begin
LogEvent('
WebServer-Get-Error: ' + ARequestInfo.URI + '
=> ' + ARequestInfo.From + sLineBreak + E.ClassName + '
: ' + E.
Message);
AResponseInfo.ContentType := '
text/plain';
AResponseInfo.ResponseNo := 500;
AResponseInfo.ResponseText := '
Internal Error';
AResponseInfo.ContentStream.Free;
AResponseInfo.ContentStream :=
nil;
AResponseInfo.ContentLength := 0;
AResponseInfo.ContentText := '
500 Internal Error'#10#10 + E.ClassName + '
: ' + E.
Message + #10#10 + HTMLPath;
AResponseInfo.CacheControl := '
';
AResponseInfo.Date := 0;
AResponseInfo.ETag := '
';
end;
end;
end;