From 9701a7fb84e73d5147c077a4aa08304b6c4eee49 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sat, 19 Oct 2013 17:17:33 +0100 Subject: Added a HTML escaping function to cWebAdmin. Apparently my editor fixed some failed tabs too. --- source/WebAdmin.cpp | 69 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 15 deletions(-) (limited to 'source/WebAdmin.cpp') diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp index 393e5ce52..1d2fe93b5 100644 --- a/source/WebAdmin.cpp +++ b/source/WebAdmin.cpp @@ -32,7 +32,7 @@ class cPlayerAccum : m_Contents.append(""); return false; } - + public: AString m_Contents; @@ -90,18 +90,18 @@ bool cWebAdmin::Init(void) { return false; } - + LOG("Initialising WebAdmin..."); - + if (!m_IniFile.GetValueSetB("WebAdmin", "Enabled", true)) { // WebAdmin is disabled, bail out faking a success return true; } - + AString PortsIPv4 = m_IniFile.GetValueSet("WebAdmin", "Port", "8080"); AString PortsIPv6 = m_IniFile.GetValueSet("WebAdmin", "PortsIPv6", ""); - + if (!m_HTTPServer.Initialize(PortsIPv4, PortsIPv6)) { return false; @@ -121,9 +121,9 @@ bool cWebAdmin::Start(void) // Not initialized return false; } - + LOG("Starting WebAdmin..."); - + // Initialize the WebAdmin template script and load the file m_TemplateScript.Create(); if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua")) @@ -176,12 +176,12 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque a_Connection.SendNeedAuth("MCServer WebAdmin - bad username or password"); return; } - + // Check if the contents should be wrapped in the template: AString URL = a_Request.GetBareURL(); ASSERT(URL.length() > 0); bool ShouldWrapInTemplate = ((URL.length() > 1) && (URL[1] != '~')); - + // Retrieve the request data: cWebadminRequestData * Data = (cWebadminRequestData *)(a_Request.GetUserData()); if (Data == NULL) @@ -189,14 +189,14 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque a_Connection.SendStatusAndReason(500, "Bad UserData"); return; } - + // Wrap it all up for the Lua call: AString Template; HTTPTemplateRequest TemplateRequest; TemplateRequest.Request.Username = a_Request.GetAuthUsername(); TemplateRequest.Request.Method = a_Request.GetMethod(); TemplateRequest.Request.Path = URL.substr(1); - + if (Data->m_Form.Finish()) { for (cHTTPFormParser::const_iterator itr = Data->m_Form.begin(), end = Data->m_Form.end(); itr != end; ++itr) @@ -208,7 +208,7 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque TemplateRequest.Request.FormData[itr->first] = HTTPfd; TemplateRequest.Request.PostParams[itr->first] = itr->second; } // for itr - Data->m_Form[] - + // Parse the URL into individual params: size_t idxQM = a_Request.GetURL().find('?'); if (idxQM != AString::npos) @@ -221,7 +221,7 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque } // for itr - URLParams[] } } - + // Try to get the template from the Lua template script if (ShouldWrapInTemplate) { @@ -236,7 +236,7 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque a_Connection.SendStatusAndReason(500, "m_TemplateScript failed"); return; } - + AString BaseURL = GetBaseURL(URL); AString Menu; Template = "{CONTENT}"; @@ -397,6 +397,45 @@ AString cWebAdmin::GetBaseURL( const AString& a_URL ) +AString cWebAdmin::GetHTMLEscapedString( const AString& a_Input ) +{ + + // Define a stringstream to write the output to. + std::stringstream dst; + + // Loop over input and substitute HTML characters for their alternatives. + for (char workingCharacter : a_Input) { + switch (workingCharacter) + { + case '&': + dst << "&"; + break; + case '\'': + dst << "'"; + break; + case '"': + dst << """; + break; + case '<': + dst << "<"; + break; + case '>': + dst << ">"; + break; + default: + dst << workingCharacter; + break; + } + } + + return dst.str(); + +} + + + + + AString cWebAdmin::GetBaseURL( const AStringVector& a_URLSplit ) { AString BaseURL = "./"; @@ -481,7 +520,7 @@ void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & { // TODO: Handle other requests } - + // Delete any request data assigned to the request: cRequestData * Data = (cRequestData *)(a_Request.GetUserData()); delete Data; -- cgit v1.2.3 From f7c9230106503149061f31efea0e387122fc1224 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sat, 19 Oct 2013 17:37:47 +0100 Subject: Fixed general failings with everything. Fixes #211. My editor fixed some extra tabs in globals. --- source/WebAdmin.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source/WebAdmin.cpp') diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp index 1d2fe93b5..f72f9f63b 100644 --- a/source/WebAdmin.cpp +++ b/source/WebAdmin.cpp @@ -404,8 +404,8 @@ AString cWebAdmin::GetHTMLEscapedString( const AString& a_Input ) std::stringstream dst; // Loop over input and substitute HTML characters for their alternatives. - for (char workingCharacter : a_Input) { - switch (workingCharacter) + for (int i = 0; i < a_Input.length(); i++) { + switch ( a_Input[i] ) { case '&': dst << "&"; @@ -423,7 +423,7 @@ AString cWebAdmin::GetHTMLEscapedString( const AString& a_Input ) dst << ">"; break; default: - dst << workingCharacter; + dst << a_Input[i]; break; } } -- cgit v1.2.3 From 0677872d880cd2fd9d67933059a9df1fd08aa0d4 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sat, 19 Oct 2013 21:13:47 +0100 Subject: Changed the code according to xoft's suggestions. --- source/WebAdmin.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'source/WebAdmin.cpp') diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp index f72f9f63b..7d17ddc03 100644 --- a/source/WebAdmin.cpp +++ b/source/WebAdmin.cpp @@ -400,35 +400,35 @@ AString cWebAdmin::GetBaseURL( const AString& a_URL ) AString cWebAdmin::GetHTMLEscapedString( const AString& a_Input ) { - // Define a stringstream to write the output to. - std::stringstream dst; + // Define a string to write the output to. + AString dst = ""; // Loop over input and substitute HTML characters for their alternatives. - for (int i = 0; i < a_Input.length(); i++) { + for (size_t i = 0; i < a_Input.length(); i++) { switch ( a_Input[i] ) { case '&': - dst << "&"; + dst =+ "&"; break; case '\'': - dst << "'"; + dst =+ "'"; break; case '"': - dst << """; + dst =+ """; break; case '<': - dst << "<"; + dst =+ "<"; break; case '>': - dst << ">"; + dst =+ ">"; break; default: - dst << a_Input[i]; + dst =+ a_Input[i]; break; } } - return dst.str(); + return dst(); } -- cgit v1.2.3 From 5ba998174ae7690c7ded25bbac99429b88fdcee9 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sat, 19 Oct 2013 21:21:44 +0100 Subject: Fixed compile error. silly parentheses creeping in. --- source/WebAdmin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/WebAdmin.cpp') diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp index 7d17ddc03..a81d85f5f 100644 --- a/source/WebAdmin.cpp +++ b/source/WebAdmin.cpp @@ -428,7 +428,7 @@ AString cWebAdmin::GetHTMLEscapedString( const AString& a_Input ) } } - return dst(); + return dst; } -- cgit v1.2.3 From 3272b45bc53dd50e830f507e98643281d85ecab1 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Sat, 19 Oct 2013 21:30:54 +0100 Subject: Added a string reserve. --- source/WebAdmin.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'source/WebAdmin.cpp') diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp index a81d85f5f..d0b9648d9 100644 --- a/source/WebAdmin.cpp +++ b/source/WebAdmin.cpp @@ -402,6 +402,7 @@ AString cWebAdmin::GetHTMLEscapedString( const AString& a_Input ) // Define a string to write the output to. AString dst = ""; + dst.reserve(a_Input.length()); // Loop over input and substitute HTML characters for their alternatives. for (size_t i = 0; i < a_Input.length(); i++) { -- cgit v1.2.3