From 1d7fdc2954ee5f97f98c4bc35204bf93b8fa583a Mon Sep 17 00:00:00 2001 From: psychon Date: Mon, 15 Mar 2010 15:58:15 +0000 Subject: [PATCH] Change the return type of OnPageRequestInternal() Instead of return a bool, this now returns an enum. This explains the meaning of the returned value better and will be used in the next commit to fix a bug. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1833 726aef4b-f618-498e-8847-2d620e286838 --- WebModules.cpp | 58 ++++++++++++++++++++++++++++++++------------------ WebModules.h | 11 +++++++--- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/WebModules.cpp b/WebModules.cpp index b63abd56..b7dc2d8a 100644 --- a/WebModules.cpp +++ b/WebModules.cpp @@ -412,13 +412,17 @@ bool CWebSock::AddModLoop(const CString& sLoopName, CModule& Module) { return false; } -bool CWebSock::PrintStaticFile(const CString& sPath, CString& sPageRet, CModule* pModule) { +CWebSock::EPageReqResult CWebSock::PrintStaticFile(const CString& sPath, CString& sPageRet, CModule* pModule) { SetPaths(pModule); DEBUG("About to print [" + m_Template.ExpandFile(sPath) + "]"); - return PrintFile(m_Template.ExpandFile(sPath.TrimLeft_n("/"))); + if (PrintFile(m_Template.ExpandFile(sPath.TrimLeft_n("/")))) { + return PAGE_PRINT; + } else { + return PAGE_NOTFOUND; + } } -bool CWebSock::PrintTemplate(const CString& sPageName, CString& sPageRet, CModule* pModule) { +CWebSock::EPageReqResult CWebSock::PrintTemplate(const CString& sPageName, CString& sPageRet, CModule* pModule) { SetVars(); m_Template["PageName"] = sPageName; @@ -437,10 +441,14 @@ bool CWebSock::PrintTemplate(const CString& sPageName, CString& sPageRet, CModul } if (m_Template.GetFileName().empty() && !m_Template.SetFile(sPageName + ".tmpl")) { - return false; + return PAGE_NOTFOUND; } - return m_Template.PrintString(sPageRet); + if (m_Template.PrintString(sPageRet)) { + return PAGE_PRINT; + } else { + return PAGE_NOTFOUND; + } } CString CWebSock::GetModWebPath(const CString& sModName) const { @@ -495,14 +503,18 @@ bool CWebSock::SetCookie(const CString& sKey, const CString& sValue) { void CWebSock::OnPageRequest(const CString& sURI) { CString sPageRet; - if (OnPageRequestInternal(sURI, sPageRet)) { + EPageReqResult eRet = OnPageRequestInternal(sURI, sPageRet); + switch (eRet) { + case PAGE_PRINT: PrintPage(sPageRet); - } else { + break; + default: PrintNotFound(); + break; } } -bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { +CWebSock::EPageReqResult CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { DEBUG("CWebSock::OnPageRequest(" + sURI + ")"); m_spSession = GetSession(); SetCookie("SessionId", m_spSession->GetId()); @@ -522,7 +534,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { SetLoggedIn(false); Redirect("/"); - return true; + return PAGE_PRINT; } else if (sURI == "/login" || sURI.Left(7) == "/login/") { if (GetParam("submitted").ToBool()) { m_sUser = GetParam("user"); @@ -530,7 +542,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { m_bLoggedIn = OnLogin(m_sUser, m_sPass); Redirect("/"); - return true; + return PAGE_PRINT; } return PrintTemplate("login", sPageRet); @@ -541,7 +553,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { // Make sure modules are treated as directories if (sURI.Right(1) != "/" && sURI.find(".") == CString::npos && sURI.TrimLeft_n("/mods/").TrimLeft_n("/").find("/") == CString::npos) { Redirect(sURI + "/"); - return true; + return PAGE_PRINT; } if (m_sModName.empty()) { @@ -553,22 +565,22 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { if (!pModule && m_sForceUser.empty()) { if (!ForceLogin()) { - return true; + return PAGE_PRINT; } pModule = CZNC::Get().FindModule(m_sModName, m_spSession->GetUser()); } if (!pModule) { - return false; + return PAGE_NOTFOUND; } else if (pModule->WebRequiresLogin() && !ForceLogin()) { - return true; + return PAGE_PRINT; } else if (pModule->WebRequiresAdmin() && !m_spSession->IsAdmin()) { sPageRet = GetErrorPage(403, "Forbidden", "You need to be an admin to access this module"); - return true; + return PAGE_PRINT; } else if (pModule && !pModule->IsGlobal() && pModule->GetUser() != m_spSession->GetUser()) { sPageRet = GetErrorPage(403, "Forbidden", "You must login as " + pModule->GetUser()->GetUserName() + " in order to view this page"); - return true; + return PAGE_PRINT; } VWebSubPages& vSubPages = pModule->GetSubPages(); @@ -580,7 +592,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { if (bActive && SubPage->RequiresAdmin() && !m_spSession->IsAdmin()) { sPageRet = GetErrorPage(403, "Forbidden", "You need to be an admin to access this page"); - return true; + return PAGE_PRINT; } } @@ -593,7 +605,11 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { m_Template.AppendPath(GetModWebPath(m_sModName) + "/files/"); std::cerr << "===================== fffffffffffffffffffff [" << m_sModName << "] [" << m_sPage << "]" << std::endl; - return PrintFile(m_Template.ExpandFile(m_sPage.TrimLeft_n("/"))); + if (PrintFile(m_Template.ExpandFile(m_sPage.TrimLeft_n("/")))) { + return PAGE_PRINT; + } else { + return PAGE_NOTFOUND; + } } else { SetPaths(pModule, true); @@ -602,7 +618,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { } sPageRet = GetErrorPage(404, "Not Implemented", "The requested module does not acknowledge web requests"); - return false; + return PAGE_PRINT; } } else { CString sPage(sURI.Trim_n("/")); @@ -611,7 +627,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { unsigned char c = sPage[a]; if ((c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && c != '_') { - return false; + return PAGE_NOTFOUND; } } @@ -619,7 +635,7 @@ bool CWebSock::OnPageRequestInternal(const CString& sURI, CString& sPageRet) { } } - return false; + return PAGE_NOTFOUND; } void CWebSock::PrintErrorPage(const CString& sMessage) { diff --git a/WebModules.h b/WebModules.h index 2f4465db..06ba7d9f 100644 --- a/WebModules.h +++ b/WebModules.h @@ -112,6 +112,11 @@ class CWebSessionMap : public TCacheMap > { class CWebSock : public CHTTPSock { public: + enum EPageReqResult { + PAGE_NOTFOUND, + PAGE_PRINT + }; + CWebSock(CModule* pModule); CWebSock(CModule* pModule, const CString& sHostname, unsigned short uPort, int iTimeout = 60); virtual ~CWebSock(); @@ -124,8 +129,8 @@ public: CModule* ResolveModule(); //virtual bool PrintFile(const CString& sFileName, CString sContentType = ""); - bool PrintTemplate(const CString& sPageName, CString& sPageRet, CModule* pModule = NULL); - bool PrintStaticFile(const CString& sPath, CString& sPageRet, CModule* pModule = NULL); + EPageReqResult PrintTemplate(const CString& sPageName, CString& sPageRet, CModule* pModule = NULL); + EPageReqResult PrintStaticFile(const CString& sPath, CString& sPageRet, CModule* pModule = NULL); bool AddModLoop(const CString& sLoopName, CModule& Module); void SetPaths(CModule* pModule, bool bIsTemplate = false); @@ -158,7 +163,7 @@ public: } private: - bool OnPageRequestInternal(const CString& sURI, CString& sPageRet); + EPageReqResult OnPageRequestInternal(const CString& sURI, CString& sPageRet); bool m_bPathsSet; CTemplate m_Template;