WebAdmin: Added "files" folder and load the login template from login_template.html
This commit is contained in:
parent
9f321e29cf
commit
7e5f22141f
106
src/WebAdmin.cpp
106
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 = \
|
||||
"<h1>MCServer WebAdmin</h1>" \
|
||||
"<center>" \
|
||||
"<form method='get' action='webadmin/'>" \
|
||||
"<input type='submit' value='Log in'>" \
|
||||
"</form>" \
|
||||
"</center>";
|
||||
}
|
||||
|
||||
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[] = \
|
||||
"<h1>MCServer WebAdmin</h1>" \
|
||||
"<center>" \
|
||||
"<form method='get' action='webadmin/'>" \
|
||||
"<input type='submit' value='Log in'>" \
|
||||
"</form>" \
|
||||
"</center>";
|
||||
|
||||
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 = "<h2>404 Not Found</h2>";
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -116,6 +116,9 @@ public:
|
||||
/** Stops the HTTP server, if it was started. */
|
||||
void Stop(void);
|
||||
|
||||
/** Loads the login template. Returns true if the loading success, false if not. */
|
||||
bool LoadLoginTemplate(void);
|
||||
|
||||
void AddPlugin(cWebPlugin * a_Plugin);
|
||||
void RemovePlugin(cWebPlugin * a_Plugin);
|
||||
|
||||
@ -205,6 +208,9 @@ protected:
|
||||
/** The Lua template script to provide templates: */
|
||||
cLuaState m_TemplateScript;
|
||||
|
||||
/** The template who provide the login side: */
|
||||
AString m_LoginTemplate;
|
||||
|
||||
/** The HTTP server which provides the underlying HTTP parsing, serialization and events */
|
||||
cHTTPServer m_HTTPServer;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user