Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Maybe found a bug in TMVCStaticContents.SendFile #664

Closed
MPannier opened this issue May 26, 2023 · 0 comments
Closed

Maybe found a bug in TMVCStaticContents.SendFile #664

MPannier opened this issue May 26, 2023 · 0 comments
Assignees
Labels
accepted Issue has been accepted and inserted in a future milestone
Milestone

Comments

@MPannier
Copy link
Contributor

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.

@danieleteti danieleteti self-assigned this May 26, 2023
@danieleteti danieleteti added the accepted Issue has been accepted and inserted in a future milestone label May 26, 2023
@danieleteti danieleteti added this to the 3.4.0-neon milestone May 26, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
accepted Issue has been accepted and inserted in a future milestone
Projects
None yet
Development

No branches or pull requests

2 participants