danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maybe found a bug in TMVCStaticContents.SendFile

MPannier opened this issue · comments

I have updated to the latest version and get an "Integer Overflow" exception while using "SendFile" in one of my controller methods.
In the function TMVCStaticContents.SendFile there is the line:
AContext.Response.SetCustomHeader('Last-Modified', LocalDateTimeToHttpStr(FileDate));
FileDate in my case has some wired value. The reason is FileDate is not initialized in my case. I have changed the function to:

class procedure TMVCStaticContents.SendFile(
  const AFileName, AMediaType: string; AContext: TWebContext);
var
  FileDate: TDateTime;
  IfModifiedSinceDate: TDateTime;
  lIfModifiedSince: string;
begin
  if not FileExists(AFileName) then
  begin
    AContext.Response.StatusCode := 404;
  end
  else
  begin
    //https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
    if AContext.Request.HTTPMethod in [httpGET, httpHEAD] then
    begin
      lIfModifiedSince := AContext.Request.Headers['If-Modified-Since'];
      FileDate := IndyFileAge(AFileName); //HERE IS MY CHANGE
      if lIfModifiedSince <> '' then
      begin
        //FileDate := IndyFileAge(AFileName); --> Moved outside the if statement
        IfModifiedSinceDate := GMTToLocalDateTime(lIfModifiedSince);
        if (IfModifiedSinceDate <> 0) and (abs(IfModifiedSinceDate - FileDate) < 2 * (1 / (24 * 60 * 60))) then
        begin
          AContext.Response.ContentType := AMediaType;
          AContext.Response.StatusCode := 304;
          Exit;
        end
      end;
      AContext.Response.SetCustomHeader('Last-Modified', LocalDateTimeToHttpStr(FileDate));
      AContext.Response.SetContentStream(
        TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone), AMediaType);
    end
    else
    begin
      AContext.Response.SetContentStream(
        TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone), AMediaType);
    end;
  end;
end;

which will work for me.