HTTPSock: Don't transfer endless static files in PrintFile()

This limits the max file size to 16 MiB and makes the read loop stop after
it has read as many bytes as GetSize() said the file is long. This fixes
an endless loop when trying to transfer endless files like /dev/zero.


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1374 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2009-02-05 17:11:45 +00:00
parent 8177a609d2
commit 8b15351aa2
+21 -4
View File
@@ -176,13 +176,30 @@ bool CHTTPSock::PrintFile(const CString& sFileName, CString sContentType) {
if (bNotModified) {
PrintHeader(0, sContentType, 304, "Not Modified");
} else {
unsigned long long iSize = File.GetSize();
// Don't try to send files over 16 MiB, because it might block
// the whole process and use huge amounts of memory.
if (iSize > 16 * 1024 * 1024) {
DEBUG("- Abort: File is over 16 MiB big: " << iSize);
PrintErrorPage(500, "Internal Server Error", "File too big");
return true;
}
char szBuf[4096];
int iLen = 0;
unsigned long long iLen = 0;
int i;
PrintHeader(File.GetSize(), sContentType);
PrintHeader(iSize, sContentType);
while ((iLen = File.Read(szBuf, 4096)) > 0) {
Write(szBuf, iLen);
// while we haven't reached iSize and read() succeeds...
while (iLen < iSize && (i = File.Read(szBuf, sizeof(szBuf))) > 0) {
Write(szBuf, i);
iLen += i;
}
if (i < 0) {
DEBUG("- Error while reading file: " << strerror(errno));
}
}