From 3dc9b0a32ea8c9b2bd8d5f9764e4228e623889a2 Mon Sep 17 00:00:00 2001 From: prozacx Date: Wed, 8 Feb 2006 05:47:31 +0000 Subject: [PATCH] Added support for auto detecting content-types in PrintFile() git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@613 726aef4b-f618-498e-8847-2d620e286838 --- HTTPSock.cpp | 38 ++++++++++++++++++++++++++++++++------ HTTPSock.h | 2 +- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/HTTPSock.cpp b/HTTPSock.cpp index 621c78a9..4588abd9 100644 --- a/HTTPSock.cpp +++ b/HTTPSock.cpp @@ -98,28 +98,54 @@ void CHTTPSock::GetPage() { } } -bool CHTTPSock::PrintFile(const CString& sFileName, const CString& sContentType) { +bool CHTTPSock::PrintFile(const CString& sFileName, CString sContentType) { CString sFilePath = sFileName; if (!m_sDocRoot.empty()) { - sFilePath = CUtils::ChangeDir(m_sDocRoot, sFileName, m_sDocRoot); + while (sFilePath.Left(1) == "/") { + sFilePath.LeftChomp(1); + } + + sFilePath = CUtils::ChangeDir(m_sDocRoot, sFilePath, m_sDocRoot); if (sFilePath.Left(m_sDocRoot.size()) != m_sDocRoot) { - PrintErrorPage(403, "Forbidden", "You don't have permission to access to this document on this server."); + PrintErrorPage(403, "Forbidden", "You don't have permission to access that file on this server."); DEBUG_ONLY(cout << "THIS FILE: [" << sFilePath << "] does not live in ..." << endl); DEBUG_ONLY(cout << "DOCUMENT ROOT: [" << m_sDocRoot << "]" << endl); return false; } } - CFile File(sFileName); + CFile File(sFilePath); if (!File.Open(CFile::F_Read)) { PrintNotFound(); return false; } - PrintHeader(File.GetSize(), "text/css"); + if (sContentType.empty()) { + if (sFileName.Right(5).CaseCmp(".html") == 0 || sFileName.Right(4).CaseCmp(".htm") == 0) { + sContentType = "text/html"; + } else if (sFileName.Right(4).CaseCmp(".css") == 0) { + sContentType = "text/css"; + } else if (sFileName.Right(3).CaseCmp(".js") == 0) { + sContentType = "application/x-javascript"; + } else if (sFileName.Right(4).CaseCmp(".jpg") == 0) { + sContentType = "image/jpeg"; + } else if (sFileName.Right(4).CaseCmp(".gif") == 0) { + sContentType = "image/gif"; + } else if (sFileName.Right(4).CaseCmp(".ico") == 0) { + sContentType = "image/x-icon"; + } else if (sFileName.Right(4).CaseCmp(".png") == 0) { + sContentType = "image/png"; + } else if (sFileName.Right(4).CaseCmp(".bmp") == 0) { + sContentType = "image/bmp"; + } else { + sContentType = "text/plain"; + } + } + + PrintHeader(File.GetSize(), sContentType); char szBuf[4096]; int iLen = 0; @@ -154,7 +180,7 @@ void CHTTPSock::ParseParams(const CString& sParams) { } void CHTTPSock::SetDocRoot(const CString& s) { - m_sDocRoot = s; + m_sDocRoot = s + "/"; m_sDocRoot.Replace("//", "/"); } diff --git a/HTTPSock.h b/HTTPSock.h index f7f61fda..4284b6f0 100644 --- a/HTTPSock.h +++ b/HTTPSock.h @@ -41,7 +41,7 @@ public: void ParseParams(const CString& sParams); void ParseURI(); void GetPage(); - bool PrintFile(const CString& sFileName, const CString& sContentType); + bool PrintFile(const CString& sFileName, CString sContentType); // Setters void SetDocRoot(const CString& s);