Http Server

Giganews Newsgroups
Subject: Http Server
Posted by:  Richard (rwskinn…@ATccwipDOTnet)
Date: Wed, 26 Aug 2009

Indy 10.5.6 and D2009

I converter my web server from version 9 to version 10 and I have almost
everything working.
I do have a problem now that each time I serve a static web html page from
disk, the client (IE6) always ask to Open or Save the File instead of simply
displaying the page.

Mime type returned for the Html page is text/html.
Also, the IE Dialog that comes up shows a weird filename which is part of
the IP Address like....

File: 192.168.0.html
Size: 362 Bytes
From: 192.168.0.2
Content: HTML Data
Open, Save, Cancel

The localdoc var has the correct filename and path.  c:\web\index.html

procedure TWebServerForm.HttpServerCommandGet(AContext: TidContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);

  procedure INVALIDNUM;
  begin
    WriteLog(2,'Entered an Invalid Value');
    AResponseInfo.ContentText :=
'<html><head><title>Error</title></head><body><h1>The Value you entered is
Invalid</h1></body></html>';
  end;

  procedure AuthFailed;
  begin
    WriteLog(2,'User failed Login - Remote IP Address: ' +
aRequestInfo.RemoteIP);
    AResponseInfo.ContentText :=
'<html><head><title>Error</title></head><body><h1>Authentication
failed</h1></body></html>';
    AResponseInfo.AuthRealm := sauthenticationrealm;
  end;

  procedure AccessDenied;
  begin
    WriteLog(2,'User attempting to access directories not allowed - Remote
IP Address: ' + aRequestInfo.RemoteIP);
    AResponseInfo.ContentText :=
'<html><head><title>Error</title></head><body><h1>Access denied</h1>'#13 +
      'You do not have sufficient priviligies to access this
document.</body></html>';
    AResponseInfo.ResponseNo := 403;
  end;

var
  LocalDoc      : string;
  ResultFile        : TFileStream;
  pw                  : String;
  tmpDateStr    : String;

begin

  // Log the request
  pw := 'xxxx';
  if ((aRequestInfo.AuthUsername <> 'xxxx') or (aRequestInfo.AuthPassword <>
pw)) then
  begin
    AuthFailed;
    exit;
  end;

  If aRequestinfo.Document = '/datalogs.html' then
  begin
    ServeDataLogPage (AContext, aRequestInfo, aResponseInfo);  //OK 2009
  end else

  If aRequestinfo.Document = '/mbregs.html' then
  begin
    ServeMBPage (AContext, aRequestInfo, aResponseInfo);      //OK 2009
  end else

  Begin
      edRoot := MyProgramPath + 'Web\';
    LocalDoc := ExpandFilename(edRoot + aRequestInfo.Document);

{    If aRequestinfo.Document = '/' then
      LocalDoc := ExpandFileName(LocalDoc + '/index.html');
}

  LocalDoc := ExpandFileName(LocalDoc + '/index.html');

  if FileExists(LocalDoc) then
    begin
      if
AnsiSameText(Copy(LocalDoc,1,Length(edRoot)),ExtractFilePath(edRoot)) then
      begin
        if AnsiSameText(aRequestInfo.Command, 'HEAD') then
        begin
          ResultFile := TFileStream.Create(localDoc,fmOpenRead or
fmShareDenyWrite);
          Try
            AResponseInfo.ResponseNo := 200;
            AResponseInfo.ContentType := GetMIMEType(LocalDoc);
            AResponseInfo.ContentLength := ResultFile.Size;
          Finally
            ResultFile.Free;
          End;
        end  {End of Head}
        else
        begin

          If (Uppercase(ExtractFileExt(LocalDoc)) = '.CSV') then
          Begin
            AResponseInfo.CustomHeaders.Values['Content-Disposition'] :=
'filename='
            + AnsiQuotedStr(ExtractFileName(LocalDoc), '"');
            AResponseInfo.ContentType := 'application/excel';
          End Else
          Begin
            {Normal HTML}
            AResponseInfo.ResponseNo := 200;
            AResponseInfo.ContentType := GetMIMEType(LocalDoc);
            //AResponseInfo.ContentStream := TFileStream.Create(LocalDoc,
fmOpenRead + fmShareDenyWrite);
          End;
          AResponseInfo.ServeFile(AContext,LocalDoc);
        End;

    end  else
    Begin
        AccessDenied;
    End;

    end else
    begin //File is missing
        AResponseInfo.ResponseNo := 404; // Not found
        AResponseInfo.ContentText :=
'<html><head><title>Error</title></head><body><h1>' +
AResponseInfo.ResponseText + '</h1></body></html>';
    end;
  end;
end;

Replies