How to serve file referenced from HTML file using WebBroker (TIdHTTPWebBrokerBridge) and TWebModule

  delphi, http, windows

I built tiny http server (Delphi 10.2) to return some calculations to client on-line.
I used page producer with TPageProducer.HTMLFile set to local file.
The html file has reference to css file on local disk like: <link rel="stylesheet" href="data/style.css"> in the head section. It works from Firefox styling my html.

To be able to serve this css file from local folder I handled WebModule.BeforeDispatch:

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; 
  Response: TWebResponse; var Handled: Boolean);
var
  LocalPath: String;
begin
  if Request.PathInfo.StartsWith('/data/') then
  begin
    LocalPath := DataPath + StringReplace(Request.PathInfo, '/', '', [rfReplaceAll]);
    if FileExists(LocalPath) then
    begin
      Response.ContentStream := TFileStream.Create(LocalPath, fmShareDenyWrite);
      Assert(Response.ContentStream.Size > 0);
    end;
    Handled := True;
  end;
end;

When I run my server and I go to address: http://localhost/data/style.css
I obtain proper contents of my css file in a browser window as a result.

Why is it not used as style for my HTML file even if it is correctly read in OnBeforeDispatch when using address like http://localhost/test.html?

It seems that one file (CSS) is referenced from other (HTML) and this make things cluttered.

Source: Windows Questions

LEAVE A COMMENT