From dcef688ccc0ae60b001ce40fd591a2dfafbac294 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 9 Aug 2014 22:34:59 +0200 Subject: WebAdmin: Added GetURLEncodedString(). --- src/WebAdmin.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index f5dc6fde7..ab6925e55 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -444,6 +444,38 @@ AString cWebAdmin::GetHTMLEscapedString(const AString & a_Input) +AString cWebAdmin::GetURLEncodedString(const AString & a_Input) +{ + // Translation table from nibble to hex: + static const char Hex[] = "0123456789abcdef"; + + // Preallocate the output to match input: + AString dst; + size_t len = a_Input.length(); + dst.reserve(len); + + // Loop over input and substitute whatever is needed: + for (size_t i = 0; i < len; i++) + { + char ch = a_Input[i]; + if (isalnum(ch) || (ch == '-') || (ch == '_') || (ch == '.') || (ch == '~')) + { + dst.push_back(ch); + } + else + { + dst.push_back('%'); + dst.push_back(Hex[(ch >> 4) & 0x0f]); + dst.push_back(Hex[ch & 0x0f]); + } + } // for i - a_Input[] + return dst; +} + + + + + AString cWebAdmin::GetBaseURL(const AStringVector & a_URLSplit) { AString BaseURL = "./"; -- cgit v1.2.3 From 60ea4bb937ba56c6dcaf57dda377beed972cf0a5 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 13 Aug 2014 11:56:47 +0100 Subject: Removed unused method --- src/WebAdmin.cpp | 22 ---------------------- 1 file changed, 22 deletions(-) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index ab6925e55..6882d7bc4 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -159,28 +159,6 @@ void cWebAdmin::Stop(void) -AString cWebAdmin::GetTemplate() -{ - AString retVal = ""; - - char SourceFile[] = "webadmin/template.html"; - - cFile f; - if (!f.Open(SourceFile, cFile::fmRead)) - { - return ""; - } - - // copy the file into the buffer: - f.ReadRestOfFile(retVal); - - return retVal; -} - - - - - void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) { if (!a_Request.HasAuth()) -- cgit v1.2.3 From 2321d2af41e935d8353b18592c29d58c766a5658 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 13 Aug 2014 12:01:13 +0100 Subject: Fixed shadowing variable --- src/WebAdmin.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 6882d7bc4..23eedbd14 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -176,9 +176,9 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque } // 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] != '~')); + AString BareURL = a_Request.GetBareURL(); + ASSERT(BareURL.length() > 0); + bool ShouldWrapInTemplate = ((BareURL.length() > 1) && (BareURL[1] != '~')); // Retrieve the request data: cWebadminRequestData * Data = (cWebadminRequestData *)(a_Request.GetUserData()); @@ -193,7 +193,7 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque HTTPTemplateRequest TemplateRequest; TemplateRequest.Request.Username = a_Request.GetAuthUsername(); TemplateRequest.Request.Method = a_Request.GetMethod(); - TemplateRequest.Request.Path = URL.substr(1); + TemplateRequest.Request.Path = BareURL.substr(1); if (Data->m_Form.Finish()) { @@ -236,7 +236,7 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque return; } - AString BaseURL = GetBaseURL(URL); + AString BaseURL = GetBaseURL(BareURL); AString Menu; Template = "{CONTENT}"; AString FoundPlugin; -- cgit v1.2.3 From 7e5f22141f72fd1ad0ec7982df03f126e9c11244 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 31 Aug 2014 12:59:04 +0200 Subject: WebAdmin: Added "files" folder and load the login template from login_template.html --- src/WebAdmin.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 96 insertions(+), 10 deletions(-) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 23eedbd14..0646c9d1c 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -135,6 +135,20 @@ bool cWebAdmin::Start(void) m_TemplateScript.Close(); } + if (!LoadLoginTemplate()) + { + LOGWARN("Could not load WebAdmin login template \"%s\", using fallback template.", FILE_IO_PREFIX "webadmin/login_template.html"); + + // Sets the fallback template: + m_LoginTemplate = \ + "

MCServer WebAdmin

" \ + "
" \ + "
" \ + "" \ + "
" \ + "
"; + } + m_IsRunning = m_HTTPServer.Start(*this); return m_IsRunning; } @@ -159,6 +173,28 @@ void cWebAdmin::Stop(void) +bool cWebAdmin::LoadLoginTemplate(void) +{ + cFile File(FILE_IO_PREFIX "webadmin/login_template.html", cFile::fmRead); + if (!File.IsOpen()) + { + return false; + } + + AString TemplateContent; + if (File.ReadRestOfFile(TemplateContent) == -1) + { + return false; + } + + m_LoginTemplate = TemplateContent; + return true; +} + + + + + void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) { if (!a_Request.HasAuth()) @@ -298,17 +334,11 @@ void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPReque void cWebAdmin::HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) { UNUSED(a_Request); - static const char LoginForm[] = \ - "

MCServer WebAdmin

" \ - "
" \ - "
" \ - "" \ - "
" \ - "
"; + cHTTPResponse Resp; Resp.SetContentType("text/html"); a_Connection.Send(Resp); - a_Connection.Send(LoginForm, sizeof(LoginForm) - 1); + a_Connection.Send(m_LoginTemplate); a_Connection.FinishResponse(); } @@ -528,7 +558,64 @@ void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & } else { - // TODO: Handle other requests + AString FileURL = URL; + std::replace(FileURL.begin(), FileURL.end(), '\\', '/'); + + // Remove all backsplashes on the first place: + if (FileURL[0] == '/') + { + size_t FirstCharToRead = FileURL.find_first_not_of('/'); + if (FirstCharToRead != AString::npos) + { + FileURL = FileURL.substr(FirstCharToRead); + } + } + + // Remove all "../" strings: + ReplaceString(FileURL, "../", ""); + + bool LoadedSuccessfull = false; + AString Content = "

404 Not Found

"; + AString Path = Printf(FILE_IO_PREFIX "webadmin/files/%s", FileURL.c_str()); + if (cFile::IsFile(Path)) + { + cFile File(Path, cFile::fmRead); + AString FileContent; + if (File.IsOpen() && (File.ReadRestOfFile(FileContent) != -1)) + { + LoadedSuccessfull = true; + Content = FileContent; + } + } + + // Find content type (The currently method is very bad. We should change it later) + AString ContentType = "text/html"; + size_t LastPointPosition = Path.find_last_of('.'); + if (LoadedSuccessfull && (LastPointPosition != AString::npos) && (LastPointPosition < Path.length())) + { + const AString & FileExtension = StrToLower(Path.substr(LastPointPosition + 1)); + if (FileExtension == "png") ContentType = "image/png"; + if (FileExtension == "fif") ContentType = "image/fif"; + if (FileExtension == "gif") ContentType = "image/gif"; + if (FileExtension == "jpeg") ContentType = "image/jpeg"; + if (FileExtension == "jpg") ContentType = "image/jpeg"; + if (FileExtension == "jpe") ContentType = "image/jpeg"; + if (FileExtension == "tiff") ContentType = "image/tiff"; + if (FileExtension == "ico") ContentType = "image/ico"; + if (FileExtension == "csv") ContentType = "text/comma-separated-values"; + if (FileExtension == "css") ContentType = "text/css"; + if (FileExtension == "js") ContentType = "text/javascript"; + if (FileExtension == "txt") ContentType = "text/plain"; + if (FileExtension == "rtx") ContentType = "text/richtext"; + if (FileExtension == "xml") ContentType = "text/xml"; + } + + // Send the response: + cHTTPResponse Resp; + Resp.SetContentType(ContentType); + a_Connection.Send(Resp); + a_Connection.Send(Content); + a_Connection.FinishResponse(); } // Delete any request data assigned to the request: @@ -551,4 +638,3 @@ void cWebAdmin::cWebadminRequestData::OnBody(const char * a_Data, size_t a_Size) - -- cgit v1.2.3 From e67bb7f431e275b5313ff412266905dc1499294a Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 31 Aug 2014 15:03:23 +0200 Subject: WebAdmin: Stop webadmin if template can't load. --- src/WebAdmin.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 0646c9d1c..35a6d401c 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -131,8 +131,10 @@ bool cWebAdmin::Start(void) m_TemplateScript.RegisterAPILibs(); if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua")) { - LOGWARN("Could not load WebAdmin template \"%s\", using default template.", FILE_IO_PREFIX "webadmin/template.lua"); + LOGERROR("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua"); m_TemplateScript.Close(); + m_HTTPServer.Stop(); + return false; } if (!LoadLoginTemplate()) -- cgit v1.2.3 From 017235e15ccdfca901d63793c5544626e730ec48 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 2 Sep 2014 20:02:52 +0200 Subject: [WebAdmin] Code improvements. --- src/WebAdmin.cpp | 150 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 59 deletions(-) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 35a6d401c..341c64236 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -131,7 +131,7 @@ bool cWebAdmin::Start(void) m_TemplateScript.RegisterAPILibs(); if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua")) { - LOGERROR("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua"); + LOGWARN("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua"); m_TemplateScript.Close(); m_HTTPServer.Stop(); return false; @@ -348,6 +348,94 @@ void cWebAdmin::HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & +void cWebAdmin::HandleFileRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) +{ + AString FileURL = a_Request.GetURL(); + std::replace(FileURL.begin(), FileURL.end(), '\\', '/'); + + // Remove all leading backslashes: + if (FileURL[0] == '/') + { + size_t FirstCharToRead = FileURL.find_first_not_of('/'); + if (FirstCharToRead != AString::npos) + { + FileURL = FileURL.substr(FirstCharToRead); + } + } + + // Remove all "../" strings: + ReplaceString(FileURL, "../", ""); + + bool LoadedSuccessfull = false; + AString Content = "

404 Not Found

"; + AString Path = Printf(FILE_IO_PREFIX "webadmin/files/%s", FileURL.c_str()); + if (cFile::IsFile(Path)) + { + cFile File(Path, cFile::fmRead); + AString FileContent; + if (File.IsOpen() && (File.ReadRestOfFile(FileContent) != -1)) + { + LoadedSuccessfull = true; + Content = FileContent; + } + } + + // Find content type (The currently method is very bad. We should change it later) + AString ContentType = "text/html"; + size_t LastPointPosition = Path.find_last_of('.'); + if (LoadedSuccessfull && (LastPointPosition != AString::npos) && (LastPointPosition < Path.length())) + { + AString FileExtension = Path.substr(LastPointPosition + 1); + ContentType = GetContentTypeFromFileExt(FileExtension); + } + + // Send the response: + cHTTPResponse Resp; + Resp.SetContentType(ContentType); + a_Connection.Send(Resp); + a_Connection.Send(Content); + a_Connection.FinishResponse(); +} + + + + + +AString cWebAdmin::GetContentTypeFromFileExt(const AString & a_FileExtension) +{ + static bool IsInitialized = false; + static std::map ContentTypeMap; + if (!IsInitialized) + { + // Initialize the ContentTypeMap: + ContentTypeMap["png"] = "image/png"; + ContentTypeMap["fif"] = "image/fif"; + ContentTypeMap["gif"] = "image/gif"; + ContentTypeMap["jpeg"] = "image/jpeg"; + ContentTypeMap["jpg"] = "image/jpeg"; + ContentTypeMap["jpe"] = "image/jpeg"; + ContentTypeMap["tiff"] = "image/tiff"; + ContentTypeMap["ico"] = "image/ico"; + ContentTypeMap["csv"] = "image/comma-separated-values"; + ContentTypeMap["css"] = "text/css"; + ContentTypeMap["js"] = "text/javascript"; + ContentTypeMap["txt"] = "text/plain"; + ContentTypeMap["rtx"] = "text/richtext"; + ContentTypeMap["xml"] = "text/xml"; + } + + AString FileExtension = StrToLower(a_FileExtension); + if (ContentTypeMap.find(a_FileExtension) == ContentTypeMap.end()) + { + return "text/html"; + } + return ContentTypeMap[FileExtension]; +} + + + + + sWebAdminPage cWebAdmin::GetPage(const HTTPRequest & a_Request) { sWebAdminPage Page; @@ -414,6 +502,7 @@ AString cWebAdmin::GetDefaultPage(void) + AString cWebAdmin::GetBaseURL( const AString& a_URL) { return GetBaseURL(StringSplit(a_URL, "/")); @@ -560,64 +649,7 @@ void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & } else { - AString FileURL = URL; - std::replace(FileURL.begin(), FileURL.end(), '\\', '/'); - - // Remove all backsplashes on the first place: - if (FileURL[0] == '/') - { - size_t FirstCharToRead = FileURL.find_first_not_of('/'); - if (FirstCharToRead != AString::npos) - { - FileURL = FileURL.substr(FirstCharToRead); - } - } - - // Remove all "../" strings: - ReplaceString(FileURL, "../", ""); - - bool LoadedSuccessfull = false; - AString Content = "

404 Not Found

"; - AString Path = Printf(FILE_IO_PREFIX "webadmin/files/%s", FileURL.c_str()); - if (cFile::IsFile(Path)) - { - cFile File(Path, cFile::fmRead); - AString FileContent; - if (File.IsOpen() && (File.ReadRestOfFile(FileContent) != -1)) - { - LoadedSuccessfull = true; - Content = FileContent; - } - } - - // Find content type (The currently method is very bad. We should change it later) - AString ContentType = "text/html"; - size_t LastPointPosition = Path.find_last_of('.'); - if (LoadedSuccessfull && (LastPointPosition != AString::npos) && (LastPointPosition < Path.length())) - { - const AString & FileExtension = StrToLower(Path.substr(LastPointPosition + 1)); - if (FileExtension == "png") ContentType = "image/png"; - if (FileExtension == "fif") ContentType = "image/fif"; - if (FileExtension == "gif") ContentType = "image/gif"; - if (FileExtension == "jpeg") ContentType = "image/jpeg"; - if (FileExtension == "jpg") ContentType = "image/jpeg"; - if (FileExtension == "jpe") ContentType = "image/jpeg"; - if (FileExtension == "tiff") ContentType = "image/tiff"; - if (FileExtension == "ico") ContentType = "image/ico"; - if (FileExtension == "csv") ContentType = "text/comma-separated-values"; - if (FileExtension == "css") ContentType = "text/css"; - if (FileExtension == "js") ContentType = "text/javascript"; - if (FileExtension == "txt") ContentType = "text/plain"; - if (FileExtension == "rtx") ContentType = "text/richtext"; - if (FileExtension == "xml") ContentType = "text/xml"; - } - - // Send the response: - cHTTPResponse Resp; - Resp.SetContentType(ContentType); - a_Connection.Send(Resp); - a_Connection.Send(Content); - a_Connection.FinishResponse(); + HandleFileRequest(a_Connection, a_Request); } // Delete any request data assigned to the request: -- cgit v1.2.3 From d2c1518145932bf6ef3cf30feac8d5698a77bf7e Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 7 Sep 2014 19:45:15 +0200 Subject: Webadmin: Added default ports to auto-generated ini file. Fixes #1382. --- src/WebAdmin.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/WebAdmin.cpp') diff --git a/src/WebAdmin.cpp b/src/WebAdmin.cpp index 341c64236..db2ace386 100644 --- a/src/WebAdmin.cpp +++ b/src/WebAdmin.cpp @@ -43,6 +43,8 @@ public: cWebAdmin::cWebAdmin(void) : m_IsInitialized(false), m_IsRunning(false), + m_PortsIPv4("8080"), + m_PortsIPv6(""), m_TemplateScript("") { } @@ -89,6 +91,8 @@ bool cWebAdmin::Init(void) m_IniFile.AddHeaderComment(" Password format: Password=*password*; for example:"); m_IniFile.AddHeaderComment(" [User:admin]"); m_IniFile.AddHeaderComment(" Password=admin"); + m_IniFile.SetValue("WebAdmin", "Port", m_PortsIPv4); + m_IniFile.SetValue("WebAdmin", "PortsIPv6", m_PortsIPv6); m_IniFile.WriteFile("webadmin.ini"); } @@ -100,8 +104,8 @@ bool cWebAdmin::Init(void) LOGD("Initialising WebAdmin..."); - m_PortsIPv4 = m_IniFile.GetValueSet("WebAdmin", "Port", "8080"); - m_PortsIPv6 = m_IniFile.GetValueSet("WebAdmin", "PortsIPv6", ""); + m_PortsIPv4 = m_IniFile.GetValueSet("WebAdmin", "Port", m_PortsIPv4); + m_PortsIPv6 = m_IniFile.GetValueSet("WebAdmin", "PortsIPv6", m_PortsIPv6); if (!m_HTTPServer.Initialize(m_PortsIPv4, m_PortsIPv6)) { -- cgit v1.2.3