2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "WebAdmin.h"
|
2013-12-08 06:17:54 -05:00
|
|
|
#include "Bindings/WebPlugin.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-12-08 06:17:54 -05:00
|
|
|
#include "Bindings/PluginManager.h"
|
|
|
|
#include "Bindings/Plugin.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "World.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "Entities/Player.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Server.h"
|
|
|
|
#include "Root.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
#include "HTTPServer/HTTPMessage.h"
|
|
|
|
#include "HTTPServer/HTTPConnection.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Helper class - appends all player names together in a HTML list
|
|
|
|
class cPlayerAccum :
|
|
|
|
public cPlayerListCallback
|
|
|
|
{
|
|
|
|
virtual bool Item(cPlayer * a_Player) override
|
|
|
|
{
|
|
|
|
m_Contents.append("<li>");
|
|
|
|
m_Contents.append(a_Player->GetName());
|
|
|
|
m_Contents.append("</li>");
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
public:
|
|
|
|
|
|
|
|
AString m_Contents;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebAdmin::cWebAdmin(void) :
|
|
|
|
m_IsInitialized(false),
|
2014-02-07 06:26:41 -05:00
|
|
|
m_IsRunning(false),
|
2013-10-25 05:15:44 -04:00
|
|
|
m_TemplateScript("<webadmin_template>")
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-05 14:57:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-18 06:50:35 -04:00
|
|
|
cWebAdmin::~cWebAdmin()
|
|
|
|
{
|
2014-02-07 06:26:41 -05:00
|
|
|
ASSERT(!m_IsRunning); // Was the HTTP server stopped properly?
|
2013-10-18 06:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
void cWebAdmin::AddPlugin(cWebPlugin * a_Plugin)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-02-07 06:26:41 -05:00
|
|
|
m_Plugins.remove(a_Plugin);
|
|
|
|
m_Plugins.push_back(a_Plugin);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-02-05 14:57:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
void cWebAdmin::RemovePlugin(cWebPlugin * a_Plugin)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-02-07 06:26:41 -05:00
|
|
|
m_Plugins.remove(a_Plugin);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
bool cWebAdmin::Init(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:15:44 -04:00
|
|
|
if (!m_IniFile.ReadFile("webadmin.ini"))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-11-04 16:51:24 -05:00
|
|
|
LOGWARN("Regenerating webadmin.ini, all settings will be reset");
|
|
|
|
m_IniFile.AddHeaderComment(" This file controls the webadmin feature of MCServer");
|
2014-02-07 06:26:41 -05:00
|
|
|
m_IniFile.AddHeaderComment(" Username format: [User:*username*]");
|
|
|
|
m_IniFile.AddHeaderComment(" Password format: Password=*password*; for example:");
|
2013-11-04 16:51:24 -05:00
|
|
|
m_IniFile.AddHeaderComment(" [User:admin]");
|
|
|
|
m_IniFile.AddHeaderComment(" Password=admin");
|
2014-04-30 11:37:25 -04:00
|
|
|
m_IniFile.WriteFile("webadmin.ini");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-13 16:19:13 -04:00
|
|
|
if (!m_IniFile.GetValueSetB("WebAdmin", "Enabled", true))
|
|
|
|
{
|
|
|
|
// WebAdmin is disabled, bail out faking a success
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-11-04 16:51:24 -05:00
|
|
|
LOGD("Initialising WebAdmin...");
|
2013-10-25 05:15:44 -04:00
|
|
|
|
2013-12-24 08:44:24 -05:00
|
|
|
m_PortsIPv4 = m_IniFile.GetValueSet("WebAdmin", "Port", "8080");
|
|
|
|
m_PortsIPv6 = m_IniFile.GetValueSet("WebAdmin", "PortsIPv6", "");
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-12-24 08:44:24 -05:00
|
|
|
if (!m_HTTPServer.Initialize(m_PortsIPv4, m_PortsIPv6))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-10-05 17:08:16 -04:00
|
|
|
m_IsInitialized = true;
|
2013-11-04 16:51:24 -05:00
|
|
|
m_IniFile.WriteFile("webadmin.ini");
|
2013-10-05 17:08:16 -04:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
bool cWebAdmin::Start(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
if (!m_IsInitialized)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
// Not initialized
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-11-04 16:51:24 -05:00
|
|
|
LOGD("Starting WebAdmin...");
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-07-28 20:37:59 -04:00
|
|
|
// Initialize the WebAdmin template script and load the file
|
2013-08-08 10:30:02 -04:00
|
|
|
m_TemplateScript.Create();
|
2014-03-12 08:05:28 -04:00
|
|
|
m_TemplateScript.RegisterAPILibs();
|
2013-08-08 10:30:02 -04:00
|
|
|
if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
2013-08-08 10:30:02 -04:00
|
|
|
LOGWARN("Could not load WebAdmin template \"%s\", using default template.", FILE_IO_PREFIX "webadmin/template.lua");
|
|
|
|
m_TemplateScript.Close();
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
m_IsRunning = m_HTTPServer.Start(*this);
|
|
|
|
return m_IsRunning;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWebAdmin::Stop(void)
|
|
|
|
{
|
|
|
|
if (!m_IsRunning)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Stopping WebAdmin...");
|
|
|
|
m_HTTPServer.Stop();
|
|
|
|
m_IsRunning = false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
AString cWebAdmin::GetTemplate()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
AString retVal = "";
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
char SourceFile[] = "webadmin/template.html";
|
|
|
|
|
|
|
|
cFile f;
|
|
|
|
if (!f.Open(SourceFile, cFile::fmRead))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// copy the file into the buffer:
|
|
|
|
f.ReadRestOfFile(retVal);
|
|
|
|
|
|
|
|
return retVal;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
void cWebAdmin::HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
if (!a_Request.HasAuth())
|
|
|
|
{
|
|
|
|
a_Connection.SendNeedAuth("MCServer WebAdmin");
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// Check auth:
|
|
|
|
AString UserPassword = m_IniFile.GetValue("User:" + a_Request.GetAuthUsername(), "Password", "");
|
|
|
|
if ((UserPassword == "") || (a_Request.GetAuthPassword() != UserPassword))
|
|
|
|
{
|
|
|
|
a_Connection.SendNeedAuth("MCServer WebAdmin - bad username or password");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// 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] != '~'));
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// Retrieve the request data:
|
|
|
|
cWebadminRequestData * Data = (cWebadminRequestData *)(a_Request.GetUserData());
|
|
|
|
if (Data == NULL)
|
|
|
|
{
|
|
|
|
a_Connection.SendStatusAndReason(500, "Bad UserData");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// 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);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
if (Data->m_Form.Finish())
|
|
|
|
{
|
|
|
|
for (cHTTPFormParser::const_iterator itr = Data->m_Form.begin(), end = Data->m_Form.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
HTTPFormData HTTPfd;
|
|
|
|
HTTPfd.Value = itr->second;
|
|
|
|
HTTPfd.Type = "";
|
|
|
|
HTTPfd.Name = itr->first;
|
|
|
|
TemplateRequest.Request.FormData[itr->first] = HTTPfd;
|
|
|
|
TemplateRequest.Request.PostParams[itr->first] = itr->second;
|
|
|
|
} // for itr - Data->m_Form[]
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-13 14:29:57 -04:00
|
|
|
// Parse the URL into individual params:
|
2014-04-18 15:09:44 -04:00
|
|
|
const AString & URL = a_Request.GetURL();
|
|
|
|
size_t idxQM = URL.find('?');
|
2013-10-13 14:29:57 -04:00
|
|
|
if (idxQM != AString::npos)
|
|
|
|
{
|
2014-04-18 15:09:44 -04:00
|
|
|
cHTTPFormParser URLParams(cHTTPFormParser::fpkURL, URL.c_str() + idxQM + 1, URL.length() - idxQM - 1, *Data);
|
2013-10-13 14:29:57 -04:00
|
|
|
URLParams.Finish();
|
|
|
|
for (cHTTPFormParser::const_iterator itr = URLParams.begin(), end = URLParams.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
TemplateRequest.Request.Params[itr->first] = itr->second;
|
|
|
|
} // for itr - URLParams[]
|
|
|
|
}
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// Try to get the template from the Lua template script
|
|
|
|
if (ShouldWrapInTemplate)
|
|
|
|
{
|
2013-10-06 08:38:10 -04:00
|
|
|
if (m_TemplateScript.Call("ShowPage", this, &TemplateRequest, cLuaState::Return, Template))
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
cHTTPResponse Resp;
|
|
|
|
Resp.SetContentType("text/html");
|
|
|
|
a_Connection.Send(Resp);
|
|
|
|
a_Connection.Send(Template.c_str(), Template.length());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
a_Connection.SendStatusAndReason(500, "m_TemplateScript failed");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
AString BaseURL = GetBaseURL(URL);
|
|
|
|
AString Menu;
|
|
|
|
Template = "{CONTENT}";
|
|
|
|
AString FoundPlugin;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
for (PluginList::iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebPlugin * WebPlugin = *itr;
|
|
|
|
std::list< std::pair<AString, AString> > NameList = WebPlugin->GetTabNames();
|
|
|
|
for (std::list< std::pair<AString, AString> >::iterator Names = NameList.begin(); Names != NameList.end(); ++Names)
|
|
|
|
{
|
|
|
|
Menu += "<li><a href='" + BaseURL + WebPlugin->GetWebTitle().c_str() + "/" + (*Names).second + "'>" + (*Names).first + "</a></li>";
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
sWebAdminPage Page = GetPage(TemplateRequest.Request);
|
|
|
|
AString Content = Page.Content;
|
|
|
|
FoundPlugin = Page.PluginName;
|
|
|
|
if (!Page.TabName.empty())
|
|
|
|
{
|
|
|
|
FoundPlugin += " - " + Page.TabName;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
if (FoundPlugin.empty()) // Default page
|
|
|
|
{
|
|
|
|
Content = GetDefaultPage();
|
|
|
|
}
|
|
|
|
|
2013-10-21 07:22:47 -04:00
|
|
|
int MemUsageKiB = cRoot::GetPhysicalRAMUsage();
|
2013-10-05 17:08:16 -04:00
|
|
|
if (MemUsageKiB > 0)
|
|
|
|
{
|
|
|
|
ReplaceString(Template, "{MEM}", Printf("%.02f", (double)MemUsageKiB / 1024));
|
|
|
|
ReplaceString(Template, "{MEMKIB}", Printf("%d", MemUsageKiB));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReplaceString(Template, "{MEM}", "unknown");
|
|
|
|
ReplaceString(Template, "{MEMKIB}", "unknown");
|
|
|
|
}
|
|
|
|
ReplaceString(Template, "{USERNAME}", a_Request.GetAuthUsername());
|
|
|
|
ReplaceString(Template, "{MENU}", Menu);
|
|
|
|
ReplaceString(Template, "{PLUGIN_NAME}", FoundPlugin);
|
|
|
|
ReplaceString(Template, "{CONTENT}", Content);
|
|
|
|
ReplaceString(Template, "{TITLE}", "MCServer");
|
|
|
|
|
|
|
|
AString NumChunks;
|
|
|
|
Printf(NumChunks, "%d", cRoot::Get()->GetTotalChunkCount());
|
|
|
|
ReplaceString(Template, "{NUMCHUNKS}", NumChunks);
|
|
|
|
|
|
|
|
cHTTPResponse Resp;
|
|
|
|
Resp.SetContentType("text/html");
|
|
|
|
a_Connection.Send(Resp);
|
|
|
|
a_Connection.Send(Template.c_str(), Template.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWebAdmin::HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request)
|
|
|
|
{
|
2013-12-22 09:00:32 -05:00
|
|
|
UNUSED(a_Request);
|
2013-10-05 17:08:16 -04:00
|
|
|
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.FinishResponse();
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
sWebAdminPage cWebAdmin::GetPage(const HTTPRequest & a_Request)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
|
|
|
sWebAdminPage Page;
|
|
|
|
AStringVector Split = StringSplit(a_Request.Path, "/");
|
|
|
|
|
|
|
|
// Find the plugin that corresponds to the requested path
|
|
|
|
AString FoundPlugin;
|
|
|
|
if (Split.size() > 1)
|
|
|
|
{
|
2013-10-06 08:38:10 -04:00
|
|
|
for (PluginList::iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); ++itr)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
|
|
|
if ((*itr)->GetWebTitle() == Split[1])
|
|
|
|
{
|
|
|
|
Page.Content = (*itr)->HandleWebRequest(&a_Request);
|
|
|
|
cWebPlugin * WebPlugin = *itr;
|
|
|
|
FoundPlugin = WebPlugin->GetWebTitle();
|
|
|
|
AString TabName = WebPlugin->GetTabNameForRequest(&a_Request).first;
|
|
|
|
Page.PluginName = FoundPlugin;
|
|
|
|
Page.TabName = TabName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the page contents
|
|
|
|
return Page;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
AString cWebAdmin::GetDefaultPage(void)
|
|
|
|
{
|
|
|
|
AString Content;
|
|
|
|
Content += "<h4>Server Name:</h4>";
|
|
|
|
Content += "<p>" + AString( cRoot::Get()->GetServer()->GetServerID() ) + "</p>";
|
|
|
|
|
|
|
|
Content += "<h4>Plugins:</h4><ul>";
|
|
|
|
cPluginManager * PM = cPluginManager::Get();
|
|
|
|
const cPluginManager::PluginMap & List = PM->GetAllPlugins();
|
|
|
|
for (cPluginManager::PluginMap::const_iterator itr = List.begin(); itr != List.end(); ++itr)
|
|
|
|
{
|
|
|
|
if (itr->second == NULL)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AppendPrintf(Content, "<li>%s V.%i</li>", itr->second->GetName().c_str(), itr->second->GetVersion());
|
|
|
|
}
|
|
|
|
Content += "</ul>";
|
|
|
|
Content += "<h4>Players:</h4><ul>";
|
|
|
|
|
|
|
|
cPlayerAccum PlayerAccum;
|
|
|
|
cWorld * World = cRoot::Get()->GetDefaultWorld(); // TODO - Create a list of worlds and players
|
|
|
|
if( World != NULL )
|
|
|
|
{
|
|
|
|
World->ForEachPlayer(PlayerAccum);
|
|
|
|
Content.append(PlayerAccum.m_Contents);
|
|
|
|
}
|
|
|
|
Content += "</ul><br>";
|
|
|
|
return Content;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-28 20:37:59 -04:00
|
|
|
AString cWebAdmin::GetBaseURL( const AString& a_URL )
|
|
|
|
{
|
|
|
|
return GetBaseURL(StringSplit(a_URL, "/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-20 03:14:30 -04:00
|
|
|
AString cWebAdmin::GetHTMLEscapedString(const AString & a_Input)
|
2013-10-19 12:17:33 -04:00
|
|
|
{
|
2013-10-20 03:14:30 -04:00
|
|
|
AString dst;
|
2013-10-19 16:30:54 -04:00
|
|
|
dst.reserve(a_Input.length());
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-20 03:14:30 -04:00
|
|
|
// Loop over input and substitute HTML characters for their alternatives:
|
|
|
|
size_t len = a_Input.length();
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
switch (a_Input[i])
|
2013-10-19 12:17:33 -04:00
|
|
|
{
|
2013-10-20 03:14:30 -04:00
|
|
|
case '&': dst.append("&"); break;
|
|
|
|
case '\'': dst.append("'"); break;
|
|
|
|
case '"': dst.append("""); break;
|
|
|
|
case '<': dst.append("<"); break;
|
|
|
|
case '>': dst.append(">"); break;
|
2013-10-19 12:17:33 -04:00
|
|
|
default:
|
2013-10-20 03:14:30 -04:00
|
|
|
{
|
|
|
|
dst.push_back(a_Input[i]);
|
2013-10-19 12:17:33 -04:00
|
|
|
break;
|
2013-10-20 03:14:30 -04:00
|
|
|
}
|
|
|
|
} // switch (a_Input[i])
|
|
|
|
} // for i - a_Input[]
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-19 16:21:44 -04:00
|
|
|
return dst;
|
2013-10-19 12:17:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-20 03:14:30 -04:00
|
|
|
AString cWebAdmin::GetBaseURL(const AStringVector & a_URLSplit)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
|
|
|
AString BaseURL = "./";
|
|
|
|
if (a_URLSplit.size() > 1)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < a_URLSplit.size(); i++)
|
|
|
|
{
|
|
|
|
BaseURL += "../";
|
|
|
|
}
|
|
|
|
BaseURL += "webadmin/";
|
|
|
|
}
|
|
|
|
return BaseURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
void cWebAdmin::OnRequestBegun(cHTTPConnection & a_Connection, cHTTPRequest & a_Request)
|
|
|
|
{
|
2013-12-22 09:00:32 -05:00
|
|
|
UNUSED(a_Connection);
|
2013-10-05 17:08:16 -04:00
|
|
|
const AString & URL = a_Request.GetURL();
|
|
|
|
if (
|
|
|
|
(strncmp(URL.c_str(), "/webadmin", 9) == 0) ||
|
|
|
|
(strncmp(URL.c_str(), "/~webadmin", 10) == 0)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
a_Request.SetUserData(new cWebadminRequestData(a_Request));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (URL == "/")
|
|
|
|
{
|
|
|
|
// The root needs no body handler and is fully handled in the OnRequestFinished() call
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: Handle other requests
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
void cWebAdmin::OnRequestBody(cHTTPConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
2013-12-22 09:00:32 -05:00
|
|
|
UNUSED(a_Connection);
|
2013-10-05 17:08:16 -04:00
|
|
|
cRequestData * Data = (cRequestData *)(a_Request.GetUserData());
|
|
|
|
if (Data == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Data->OnBody(a_Data, a_Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & a_Request)
|
|
|
|
{
|
|
|
|
const AString & URL = a_Request.GetURL();
|
|
|
|
if (
|
|
|
|
(strncmp(URL.c_str(), "/webadmin", 9) == 0) ||
|
|
|
|
(strncmp(URL.c_str(), "/~webadmin", 10) == 0)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
HandleWebadminRequest(a_Connection, a_Request);
|
|
|
|
}
|
2013-10-06 10:40:28 -04:00
|
|
|
else if (URL == "/")
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
// The root needs no body handler and is fully handled in the OnRequestFinished() call
|
|
|
|
HandleRootRequest(a_Connection, a_Request);
|
|
|
|
}
|
2013-10-06 10:40:28 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: Handle other requests
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-06 10:40:28 -04:00
|
|
|
// Delete any request data assigned to the request:
|
|
|
|
cRequestData * Data = (cRequestData *)(a_Request.GetUserData());
|
|
|
|
delete Data;
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cWebAdmin::cWebadminRequestData
|
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
void cWebAdmin::cWebadminRequestData::OnBody(const char * a_Data, size_t a_Size)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
m_Form.Parse(a_Data, a_Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|