diff --git a/HTTPSock.cpp b/HTTPSock.cpp index bdfac1ff..621c78a9 100644 --- a/HTTPSock.cpp +++ b/HTTPSock.cpp @@ -93,12 +93,46 @@ void CHTTPSock::GetPage() { } if (PrintHeader(sPage.length())) { - DEBUG_ONLY(cout << "- 200 (OK)" << endl); Write(sPage); Close(Csock::CLT_AFTERWRITE); } } +bool CHTTPSock::PrintFile(const CString& sFileName, const CString& sContentType) { + CString sFilePath = sFileName; + + if (!m_sDocRoot.empty()) { + sFilePath = CUtils::ChangeDir(m_sDocRoot, sFileName, 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."); + DEBUG_ONLY(cout << "THIS FILE: [" << sFilePath << "] does not live in ..." << endl); + DEBUG_ONLY(cout << "DOCUMENT ROOT: [" << m_sDocRoot << "]" << endl); + return false; + } + } + + CFile File(sFileName); + + if (!File.Open(CFile::F_Read)) { + PrintNotFound(); + return false; + } + + PrintHeader(File.GetSize(), "text/css"); + + char szBuf[4096]; + int iLen = 0; + + while((iLen = File.Read(szBuf, 4096)) > 0) { + Write(szBuf, iLen); + } + + Close(Csock::CLT_AFTERWRITE); + + return true; +} + void CHTTPSock::ParseURI() { ParseParams(m_sURI.Token(1, true, "?")); m_sURI = m_sURI.Token(0, false, "?"); @@ -119,6 +153,15 @@ void CHTTPSock::ParseParams(const CString& sParams) { } } +void CHTTPSock::SetDocRoot(const CString& s) { + m_sDocRoot = s; + m_sDocRoot.Replace("//", "/"); +} + +const CString& CHTTPSock::GetDocRoot() const { + return m_sDocRoot; +} + const CString& CHTTPSock::GetUser() const { return m_sUser; } @@ -184,6 +227,23 @@ bool CHTTPSock::OnPageRequest(const CString& sURI, CString& sPageRet) { return false; } +bool CHTTPSock::PrintNotFound() { + return PrintErrorPage(404, "Not Found", "The requested URL was not found on this server."); +} + +bool CHTTPSock::PrintErrorPage(unsigned int uStatusId, const CString& sStatusMsg, const CString& sMessage) { + if (SentHeader()) { + return false; + } + + CString sPage = GetErrorPage(uStatusId, sStatusMsg, sMessage); + PrintHeader(sPage.length(), "text/html", uStatusId, sStatusMsg); + Write(sPage); + Close(Csock::CLT_AFTERWRITE); + + return true; +} + CString CHTTPSock::GetErrorPage(unsigned int uStatusId, const CString& sStatusMsg, const CString& sMessage) { return "\r\n" "\r\n" + CString::ToString(uStatusId) + " " + sStatusMsg.Escape_n(CString::EHTML) + "\r\n" @@ -215,29 +275,17 @@ bool CHTTPSock::OnLogin(const CString& sUser, const CString& sPass) { return false; } -bool CHTTPSock::PrintNotFound() { - if (SentHeader()) { - return false; - } - - DEBUG_ONLY(cout << "- 404 (Not Found)" << endl); - CString sPage = GetErrorPage(404, "Not Found", "The requested URL was not found on this server."); - PrintHeader(sPage.length(), "text/html", 404, "Not Found"); - Write(sPage); - Close(Csock::CLT_AFTERWRITE); - - return true; -} - bool CHTTPSock::SentHeader() const { return m_bSentHeader; } bool CHTTPSock::PrintHeader(unsigned long uContentLength, const CString& sContentType, unsigned int uStatusId, const CString& sStatusMsg) { if (SentHeader()) { + DEBUG_ONLY(cout << "- Header already sent!" << endl); return false; } + DEBUG_ONLY(cout << "- " << uStatusId << " (" << sStatusMsg << ")" << endl); if (!sContentType.empty()) { m_sContentType = sContentType; } diff --git a/HTTPSock.h b/HTTPSock.h index a28307ac..f7f61fda 100644 --- a/HTTPSock.h +++ b/HTTPSock.h @@ -37,16 +37,20 @@ public: bool Redirect(const CString& sURL); bool ForceLogin(); CString GetErrorPage(unsigned int uStatusId, const CString& sStatusMsg, const CString& sMessage); + bool PrintErrorPage(unsigned int uStatusId, const CString& sStatusMsg, const CString& sMessage); void ParseParams(const CString& sParams); void ParseURI(); void GetPage(); + bool PrintFile(const CString& sFileName, const CString& sContentType); // Setters + void SetDocRoot(const CString& s); // !Setters // Getters bool HasParam(const CString& sName) const; CString GetParam(const CString& sName) const; + const CString& GetDocRoot() const; const CString& GetUser() const; const CString& GetPass() const; const CString& GetParamString() const; @@ -68,6 +72,7 @@ protected: CString m_sUser; CString m_sPass; CString m_sContentType; + CString m_sDocRoot; map m_msvsParams; MCString m_msHeaders; };