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
|
|
|
|
2016-01-03 09:59:55 -05:00
|
|
|
#include "HTTP/HTTPRequestParser.h"
|
|
|
|
#include "HTTP/HTTPServerConnection.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
static const char DEFAULT_WEBADMIN_PORTS[] = "8080";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cPlayerAccum:
|
|
|
|
|
|
|
|
/** Helper class - appends all player names together in an HTML list */
|
2012-06-14 09:06:06 -04:00
|
|
|
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;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cWebAdmin:
|
|
|
|
|
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");
|
2015-09-25 04:14:17 -04:00
|
|
|
m_IniFile.AddHeaderComment(" This file controls the webadmin feature of Cuberite");
|
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");
|
2015-01-24 04:11:34 -05:00
|
|
|
m_IniFile.SetValue("WebAdmin", "Ports", DEFAULT_WEBADMIN_PORTS);
|
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-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
|
|
|
{
|
2014-09-02 14:02:52 -04:00
|
|
|
LOGWARN("Could not load WebAdmin template \"%s\". WebAdmin disabled!", FILE_IO_PREFIX "webadmin/template.lua");
|
2013-08-08 10:30:02 -04:00
|
|
|
m_TemplateScript.Close();
|
2014-08-31 09:03:23 -04:00
|
|
|
m_HTTPServer.Stop();
|
|
|
|
return false;
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
// Load the login template, provide a fallback default if not found:
|
2014-08-31 06:59:04 -04:00
|
|
|
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 = \
|
2015-09-25 04:14:17 -04:00
|
|
|
"<h1>Cuberite WebAdmin</h1>" \
|
2014-08-31 06:59:04 -04:00
|
|
|
"<center>" \
|
|
|
|
"<form method='get' action='webadmin/'>" \
|
|
|
|
"<input type='submit' value='Log in'>" \
|
|
|
|
"</form>" \
|
|
|
|
"</center>";
|
|
|
|
}
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
// Read the ports to be used:
|
|
|
|
// Note that historically the ports were stored in the "Port" and "PortsIPv6" values
|
|
|
|
m_Ports = ReadUpgradeIniPorts(m_IniFile, "WebAdmin", "Ports", "Port", "PortsIPv6", DEFAULT_WEBADMIN_PORTS);
|
|
|
|
|
|
|
|
if (!m_HTTPServer.Initialize())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_IsInitialized = true;
|
|
|
|
m_IniFile.WriteFile("webadmin.ini");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cWebAdmin::Start(void)
|
|
|
|
{
|
|
|
|
if (!m_IsInitialized)
|
|
|
|
{
|
|
|
|
// Not initialized
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Starting WebAdmin...");
|
|
|
|
|
|
|
|
m_IsRunning = m_HTTPServer.Start(*this, m_Ports);
|
2014-02-07 06:26:41 -05:00
|
|
|
return m_IsRunning;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWebAdmin::Stop(void)
|
|
|
|
{
|
|
|
|
if (!m_IsRunning)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
LOGD("Stopping WebAdmin...");
|
|
|
|
m_HTTPServer.Stop();
|
|
|
|
m_IsRunning = false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-31 06:59:04 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:54:04 -05:00
|
|
|
void cWebAdmin::HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-05 17:08:16 -04:00
|
|
|
if (!a_Request.HasAuth())
|
|
|
|
{
|
2015-09-25 04:14:17 -04:00
|
|
|
a_Connection.SendNeedAuth("Cuberite WebAdmin");
|
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
|
|
|
// Check auth:
|
|
|
|
AString UserPassword = m_IniFile.GetValue("User:" + a_Request.GetAuthUsername(), "Password", "");
|
|
|
|
if ((UserPassword == "") || (a_Request.GetAuthPassword() != UserPassword))
|
|
|
|
{
|
2015-09-25 04:14:17 -04:00
|
|
|
a_Connection.SendNeedAuth("Cuberite WebAdmin - bad username or password");
|
2013-10-05 17:08:16 -04:00
|
|
|
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:
|
2014-08-13 07:01:13 -04:00
|
|
|
AString BareURL = a_Request.GetBareURL();
|
|
|
|
ASSERT(BareURL.length() > 0);
|
|
|
|
bool ShouldWrapInTemplate = ((BareURL.length() > 1) && (BareURL[1] != '~'));
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// Retrieve the request data:
|
2015-04-21 04:54:43 -04:00
|
|
|
cWebadminRequestData * Data = reinterpret_cast<cWebadminRequestData *>(a_Request.GetUserData());
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Data == nullptr)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
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;
|
2015-04-21 04:54:43 -04:00
|
|
|
TemplateRequest.Request.URL = a_Request.GetURL();
|
2013-10-05 17:08:16 -04:00
|
|
|
TemplateRequest.Request.Username = a_Request.GetAuthUsername();
|
|
|
|
TemplateRequest.Request.Method = a_Request.GetMethod();
|
2014-08-13 07:01:13 -04:00
|
|
|
TemplateRequest.Request.Path = BareURL.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
|
|
|
|
2014-08-13 07:01:13 -04:00
|
|
|
AString BaseURL = GetBaseURL(BareURL);
|
2013-10-05 17:08:16 -04:00
|
|
|
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)
|
|
|
|
{
|
2015-05-24 07:56:56 -04:00
|
|
|
ReplaceString(Template, "{MEM}", Printf("%.02f", static_cast<double>(MemUsageKiB) / 1024));
|
2013-10-05 17:08:16 -04:00
|
|
|
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);
|
2015-09-25 04:14:17 -04:00
|
|
|
ReplaceString(Template, "{TITLE}", "Cuberite");
|
2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:54:04 -05:00
|
|
|
void cWebAdmin::HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
2013-12-22 09:00:32 -05:00
|
|
|
UNUSED(a_Request);
|
2014-08-31 06:59:04 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cHTTPResponse Resp;
|
|
|
|
Resp.SetContentType("text/html");
|
|
|
|
a_Connection.Send(Resp);
|
2014-08-31 06:59:04 -04:00
|
|
|
a_Connection.Send(m_LoginTemplate);
|
2013-10-05 17:08:16 -04:00
|
|
|
a_Connection.FinishResponse();
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:54:04 -05:00
|
|
|
void cWebAdmin::HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
2014-09-02 14:02:52 -04:00
|
|
|
{
|
|
|
|
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 = "<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()))
|
|
|
|
{
|
|
|
|
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<AString, AString> 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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])
|
|
|
|
{
|
2015-04-19 08:35:04 -04:00
|
|
|
Page.Content = (*itr)->HandleWebRequest(a_Request);
|
2013-07-28 20:37:59 -04:00
|
|
|
cWebPlugin * WebPlugin = *itr;
|
|
|
|
FoundPlugin = WebPlugin->GetWebTitle();
|
2015-04-19 08:35:04 -04:00
|
|
|
AString TabName = WebPlugin->GetTabNameForRequest(a_Request).first;
|
2013-07-28 20:37:59 -04:00
|
|
|
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>";
|
2014-07-21 09:19:48 -04:00
|
|
|
Content += "<p>" + AString( cRoot::Get()->GetServer()->GetServerID()) + "</p>";
|
2013-10-05 17:08:16 -04:00
|
|
|
|
2015-04-19 04:57:41 -04:00
|
|
|
// Display a list of all plugins:
|
2013-10-05 17:08:16 -04:00
|
|
|
Content += "<h4>Plugins:</h4><ul>";
|
2015-04-19 04:57:41 -04:00
|
|
|
struct cPluginCallback:
|
|
|
|
public cPluginManager::cPluginCallback
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
2015-04-19 04:57:41 -04:00
|
|
|
AString & m_Content;
|
|
|
|
|
|
|
|
cPluginCallback(AString & a_Content):
|
|
|
|
m_Content(a_Content)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
}
|
2015-04-19 04:57:41 -04:00
|
|
|
|
|
|
|
virtual bool Item(cPlugin * a_Plugin) override
|
|
|
|
{
|
|
|
|
if (a_Plugin->IsLoaded())
|
|
|
|
{
|
|
|
|
AppendPrintf(m_Content, "<li>%s V.%i</li>", a_Plugin->GetName().c_str(), a_Plugin->GetVersion());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} Callback(Content);
|
|
|
|
cPluginManager::Get()->ForEachPlugin(Callback);
|
2013-10-05 17:08:16 -04:00
|
|
|
Content += "</ul>";
|
|
|
|
|
2015-04-19 04:57:41 -04:00
|
|
|
// Display a list of all players:
|
|
|
|
Content += "<h4>Players:</h4><ul>";
|
2013-10-05 17:08:16 -04:00
|
|
|
cPlayerAccum PlayerAccum;
|
2014-07-17 13:13:23 -04:00
|
|
|
cWorld * World = cRoot::Get()->GetDefaultWorld(); // TODO - Create a list of worlds and players
|
2014-10-20 16:55:07 -04:00
|
|
|
if (World != nullptr)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
World->ForEachPlayer(PlayerAccum);
|
|
|
|
Content.append(PlayerAccum.m_Contents);
|
|
|
|
}
|
|
|
|
Content += "</ul><br>";
|
|
|
|
return Content;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
|
2015-05-09 05:16:56 -04:00
|
|
|
AString cWebAdmin::GetBaseURL(const AString & a_URL)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-09 16:34:59 -04:00
|
|
|
AString cWebAdmin::GetURLEncodedString(const AString & a_Input)
|
|
|
|
{
|
|
|
|
// Translation table from nibble to hex:
|
|
|
|
static const char Hex[] = "0123456789abcdef";
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-08-09 16:34:59 -04:00
|
|
|
// Preallocate the output to match input:
|
|
|
|
AString dst;
|
|
|
|
size_t len = a_Input.length();
|
|
|
|
dst.reserve(len);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-08-09 16:34:59 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:54:04 -05:00
|
|
|
void cWebAdmin::OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
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
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:54:04 -05:00
|
|
|
void cWebAdmin::OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequestParser & 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);
|
2015-05-28 07:29:26 -04:00
|
|
|
cRequestData * Data = reinterpret_cast<cRequestData *>(a_Request.GetUserData());
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Data == nullptr)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Data->OnBody(a_Data, a_Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:54:04 -05:00
|
|
|
void cWebAdmin::OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
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)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2014-09-02 14:02:52 -04:00
|
|
|
HandleFileRequest(a_Connection, a_Request);
|
2013-10-06 10:40:28 -04:00
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-06 10:40:28 -04:00
|
|
|
// Delete any request data assigned to the request:
|
2015-05-28 07:29:26 -04:00
|
|
|
cRequestData * Data = reinterpret_cast<cRequestData *>(a_Request.GetUserData());
|
2013-10-06 10:40:28 -04:00
|
|
|
delete Data;
|
2014-10-20 16:55:07 -04:00
|
|
|
Data = nullptr;
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|