Maybe found a bug in TMVCStaticContents.SendFile
MPannier opened this issue · comments
Mathias Pannier commented
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.