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"
|
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/HTTPServerConnection.h"
|
2016-02-20 05:50:52 -05:00
|
|
|
#include "HTTP/HTTPFormParser.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;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cWebadminRequestData
|
|
|
|
|
|
|
|
/** The form parser callbacks for requests in the "/webadmin" and "/~webadmin" paths */
|
|
|
|
class cWebadminRequestData :
|
|
|
|
public cHTTPFormParser::cCallbacks,
|
|
|
|
public cHTTPIncomingRequest::cUserData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cHTTPFormParser m_Form;
|
|
|
|
|
|
|
|
|
|
|
|
cWebadminRequestData(const cHTTPIncomingRequest & a_Request):
|
|
|
|
m_Form(a_Request, *this)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// cHTTPFormParser::cCallbacks overrides. Files are ignored:
|
|
|
|
virtual void OnFileStart(cHTTPFormParser &, const AString & a_FileName) override
|
|
|
|
{
|
|
|
|
UNUSED(a_FileName);
|
|
|
|
}
|
|
|
|
virtual void OnFileData(cHTTPFormParser &, const char * a_Data, size_t a_Size) override
|
|
|
|
{
|
|
|
|
UNUSED(a_Data);
|
|
|
|
UNUSED(a_Size);
|
|
|
|
}
|
|
|
|
virtual void OnFileEnd(cHTTPFormParser &) override {}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cWebAdmin:
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebAdmin::cWebAdmin(void) :
|
2016-03-02 04:05:10 -05:00
|
|
|
m_TemplateScript("<webadmin_template>"),
|
2013-10-05 17:08:16 -04:00
|
|
|
m_IsInitialized(false),
|
2016-03-02 04:05:10 -05:00
|
|
|
m_IsRunning(false)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
bool cWebAdmin::Init(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
if (!LoadIniFile())
|
2013-10-13 16:19:13 -04:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
Reload();
|
2014-08-31 06:59:04 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
bool cWebAdmin::LoadLoginPage(void)
|
2014-08-31 06:59:04 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
m_LoginPage = TemplateContent;
|
2014-08-31 06:59:04 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
void cWebAdmin::RemoveAllPluginWebTabs(const AString & a_PluginName)
|
|
|
|
{
|
|
|
|
cCSLock lock(m_CS);
|
|
|
|
m_WebTabs.erase(std::remove_if(m_WebTabs.begin(), m_WebTabs.end(), [=](cWebTabPtr a_CBWebTab)
|
|
|
|
{
|
|
|
|
return (a_CBWebTab->m_PluginName == a_PluginName);
|
|
|
|
}),
|
|
|
|
m_WebTabs.end()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWebAdmin::Reload(void)
|
|
|
|
{
|
|
|
|
cCSLock lock(m_CS);
|
|
|
|
if (!LoadIniFile())
|
|
|
|
{
|
|
|
|
// We are asked to disable the webadmin, cannot do that, so warn the admin:
|
|
|
|
LOGWARNING(
|
|
|
|
"WebAdmin was previously enabled and now the settings say to disable it."
|
|
|
|
" This will not take effect until you restart the server."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the WebAdmin template script and reload the file:
|
|
|
|
if (m_TemplateScript.IsValid())
|
|
|
|
{
|
|
|
|
m_TemplateScript.Close();
|
|
|
|
}
|
|
|
|
m_TemplateScript.Create();
|
|
|
|
m_TemplateScript.RegisterAPILibs();
|
|
|
|
if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
|
|
|
|
{
|
|
|
|
LOGWARN("Could not load WebAdmin template \"%s\". WebAdmin will not work properly!", FILE_IO_PREFIX "webadmin/template.lua");
|
|
|
|
m_TemplateScript.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the login template, provide a fallback default if not found:
|
|
|
|
if (!LoadLoginPage())
|
|
|
|
{
|
|
|
|
LOGWARN("Could not load WebAdmin login page \"%s\", using fallback template.", FILE_IO_PREFIX "webadmin/login_template.html");
|
|
|
|
|
|
|
|
// Set the fallback:
|
|
|
|
m_LoginPage = \
|
|
|
|
"<h1>Cuberite WebAdmin</h1>" \
|
|
|
|
"<center>" \
|
|
|
|
"<form method='get' action='webadmin/'>" \
|
|
|
|
"<input type='submit' value='Log in'>" \
|
|
|
|
"</form>" \
|
|
|
|
"</center>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cWebAdmin::LoadIniFile(void)
|
|
|
|
{
|
|
|
|
m_IniFile.Clear();
|
|
|
|
if (!m_IniFile.ReadFile("webadmin.ini"))
|
|
|
|
{
|
|
|
|
LOGWARN("Regenerating webadmin.ini, all settings will be reset");
|
|
|
|
m_IniFile.AddHeaderComment(" This file controls the webadmin feature of Cuberite");
|
|
|
|
m_IniFile.AddHeaderComment(" It specifies whether webadmin is enabled, and what logins are allowed. ");
|
|
|
|
m_IniFile.AddHeaderComment(" Username format: [User:*username*]");
|
|
|
|
m_IniFile.AddHeaderComment(" Password format: Password=*password*; for example:");
|
|
|
|
m_IniFile.AddHeaderComment(" [User:admin]");
|
|
|
|
m_IniFile.AddHeaderComment(" Password=admin");
|
|
|
|
m_IniFile.SetValue("WebAdmin", "Ports", DEFAULT_WEBADMIN_PORTS);
|
|
|
|
m_IniFile.WriteFile("webadmin.ini");
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_IniFile.GetValueSetB("WebAdmin", "Enabled", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
void cWebAdmin::HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & 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:
|
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
AString UserPassword = m_IniFile.GetValue("User:" + a_Request.GetAuthUsername(), "Password", "");
|
|
|
|
if ((UserPassword == "") || (a_Request.GetAuthPassword() != UserPassword))
|
|
|
|
{
|
|
|
|
a_Connection.SendNeedAuth("Cuberite WebAdmin - bad username or password");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
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:
|
2016-02-20 05:50:52 -05:00
|
|
|
auto BareURL = a_Request.GetURLPath();
|
2014-08-13 07:01:13 -04:00
|
|
|
ASSERT(BareURL.length() > 0);
|
2016-03-02 04:05:10 -05:00
|
|
|
bool ShouldWrapInTemplate = (!BareURL.empty() && (BareURL[1] != '~'));
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// Retrieve the request data:
|
2016-02-20 05:50:52 -05:00
|
|
|
auto Data = std::static_pointer_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)
|
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
cCSLock Lock(m_CS);
|
2017-01-18 10:30:28 -05:00
|
|
|
cLuaState::cLock lock(m_TemplateScript);
|
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
|
|
|
{
|
2016-02-20 06:33:27 -05:00
|
|
|
cHTTPOutgoingResponse Resp;
|
2013-10-05 17:08:16 -04:00
|
|
|
Resp.SetContentType("text/html");
|
|
|
|
a_Connection.Send(Resp);
|
|
|
|
a_Connection.Send(Template.c_str(), Template.length());
|
2016-03-03 07:46:45 -05:00
|
|
|
a_Connection.FinishResponse();
|
2013-10-05 17:08:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
a_Connection.SendStatusAndReason(500, "m_TemplateScript failed");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
// Send the un-decorated page content:
|
|
|
|
auto page = GetPage(TemplateRequest.Request);
|
|
|
|
cHTTPOutgoingResponse resp;
|
|
|
|
resp.SetContentType(page.ContentType);
|
|
|
|
a_Connection.Send(resp);
|
|
|
|
a_Connection.Send(page.Content.c_str(), page.Content.length());
|
2016-02-20 05:50:52 -05:00
|
|
|
a_Connection.FinishResponse();
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
void cWebAdmin::HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & 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
|
|
|
|
2016-02-20 06:33:27 -05:00
|
|
|
cHTTPOutgoingResponse Resp;
|
2013-10-05 17:08:16 -04:00
|
|
|
Resp.SetContentType("text/html");
|
|
|
|
a_Connection.Send(Resp);
|
2016-03-02 04:05:10 -05:00
|
|
|
a_Connection.Send(m_LoginPage);
|
2013-10-05 17:08:16 -04:00
|
|
|
a_Connection.FinishResponse();
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
void cWebAdmin::HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & 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:
|
2016-03-02 04:05:10 -05:00
|
|
|
if (!FileURL.empty() && (FileURL[0] == '/'))
|
2014-09-02 14:02:52 -04:00
|
|
|
{
|
|
|
|
size_t FirstCharToRead = FileURL.find_first_not_of('/');
|
|
|
|
if (FirstCharToRead != AString::npos)
|
|
|
|
{
|
|
|
|
FileURL = FileURL.substr(FirstCharToRead);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all "../" strings:
|
|
|
|
ReplaceString(FileURL, "../", "");
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
// Read the file contents and guess its mime-type, based on the extension:
|
2014-09-02 14:02:52 -04:00
|
|
|
AString Content = "<h2>404 Not Found</h2>";
|
2016-03-02 04:05:10 -05:00
|
|
|
AString ContentType;
|
2014-09-02 14:02:52 -04:00
|
|
|
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))
|
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
std::swap(Content, FileContent);
|
|
|
|
size_t LastPointPosition = Path.find_last_of('.');
|
|
|
|
if (LastPointPosition != AString::npos)
|
|
|
|
{
|
|
|
|
ContentType = GetContentTypeFromFileExt(Path.substr(LastPointPosition + 1));
|
|
|
|
}
|
2014-09-02 14:02:52 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 04:05:10 -05:00
|
|
|
if (ContentType.empty())
|
2014-09-02 14:02:52 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
ContentType = "application/unknown";
|
2014-09-02 14:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the response:
|
2016-02-20 06:33:27 -05:00
|
|
|
cHTTPOutgoingResponse Resp;
|
2014-09-02 14:02:52 -04:00
|
|
|
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;
|
2016-03-02 04:05:10 -05:00
|
|
|
static AStringMap ContentTypeMap;
|
2014-09-02 14:02:52 -04:00
|
|
|
if (!IsInitialized)
|
|
|
|
{
|
|
|
|
// Initialize the ContentTypeMap:
|
2016-03-02 04:05:10 -05:00
|
|
|
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"] = "text/csv";
|
|
|
|
ContentTypeMap["css"] = "text/css";
|
|
|
|
ContentTypeMap["js"] = "text/javascript";
|
|
|
|
ContentTypeMap["txt"] = "text/plain";
|
|
|
|
ContentTypeMap["rtx"] = "text/richtext";
|
|
|
|
ContentTypeMap["rtf"] = "text/richtext";
|
|
|
|
ContentTypeMap["xml"] = "text/xml";
|
|
|
|
ContentTypeMap["html"] = "text/html";
|
|
|
|
ContentTypeMap["htm"] = "text/html";
|
|
|
|
ContentTypeMap["xhtml"] = "application/xhtml+xml"; // Not recomended for IE6, but no-one uses that anymore
|
2014-09-02 14:02:52 -04:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
auto itr = ContentTypeMap.find(StrToLower(a_FileExtension));
|
|
|
|
if (itr == ContentTypeMap.end())
|
2014-09-02 14:02:52 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
return AString();
|
2014-09-02 14:02:52 -04:00
|
|
|
}
|
2016-03-02 04:05:10 -05:00
|
|
|
return itr->second;
|
2014-09-02 14:02:52 -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
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
sWebAdminPage page;
|
|
|
|
auto split = StringSplit(a_Request.Path, "/");
|
2013-07-28 20:37:59 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
// If no specific page was requested, return an empty object:
|
|
|
|
if (split.size() <= 2)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the WebTab handler responsible for the request:
|
|
|
|
cWebTabPtr tab;
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
for (auto & wt: m_WebTabs)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
if (
|
|
|
|
(wt->m_PluginName == split[1]) &&
|
|
|
|
(wt->m_UrlPath == split[2])
|
|
|
|
)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
tab = wt;
|
2013-07-28 20:37:59 -04:00
|
|
|
break;
|
|
|
|
}
|
2016-03-02 04:05:10 -05:00
|
|
|
} // for wt - m_WebTabs[]
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a WebTab handler was found, call it:
|
|
|
|
if (tab != nullptr)
|
|
|
|
{
|
|
|
|
page.ContentType = "text/html"; // Default to HTML content type, unless overridden by a plugin
|
|
|
|
if (!tab->m_Callback->Call(a_Request, split[1], page.Content, page.ContentType))
|
|
|
|
{
|
|
|
|
page.Content = GetHTMLEscapedString(Printf(
|
|
|
|
"WebTab callback for plugin %s, page %s has failed.",
|
|
|
|
tab->m_PluginName.c_str(), tab->m_Title.c_str()
|
|
|
|
));
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
2016-03-02 04:05:10 -05:00
|
|
|
page.PluginName = tab->m_PluginName;
|
|
|
|
page.TabTitle = tab->m_Title;
|
|
|
|
page.TabUrlPath = split[1];
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
return page;
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
AString cWebAdmin::GetBaseURL(const AString & a_URL)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
return GetBaseURL(StringSplit(a_URL, "/"));
|
|
|
|
}
|
2016-03-02 04:05:10 -05:00
|
|
|
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWebAdmin::AddWebTab(
|
|
|
|
const AString & a_Title,
|
|
|
|
const AString & a_UrlPath,
|
|
|
|
const AString & a_PluginName,
|
|
|
|
SharedPtr<cWebAdmin::cWebTabCallback> a_Callback
|
|
|
|
)
|
|
|
|
{
|
|
|
|
cCSLock lock(m_CS);
|
|
|
|
m_WebTabs.emplace_back(std::make_shared<cWebTab>(a_Title, a_UrlPath, a_PluginName, a_Callback));
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
bool cWebAdmin::DelWebTab(const AString & a_UrlPath)
|
2013-07-28 20:37:59 -04:00
|
|
|
{
|
2016-03-02 04:05:10 -05:00
|
|
|
cCSLock lock(m_CS);
|
|
|
|
for (auto itr = m_WebTabs.begin(), end = m_WebTabs.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
if ((*itr)->m_UrlPath == a_UrlPath)
|
|
|
|
{
|
|
|
|
m_WebTabs.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // for itr - m_WebTabs[]
|
|
|
|
|
|
|
|
// Not found:
|
|
|
|
return false;
|
2013-07-28 20:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-12-25 12:29:21 -05:00
|
|
|
return URLEncode(a_Input);
|
2014-08-09 16:34:59 -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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
void cWebAdmin::OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & 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)
|
|
|
|
)
|
|
|
|
{
|
2016-02-20 05:50:52 -05:00
|
|
|
a_Request.SetUserData(std::make_shared<cWebadminRequestData>(a_Request));
|
2013-10-05 17:08:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (URL == "/")
|
|
|
|
{
|
|
|
|
// The root needs no body handler and is fully handled in the OnRequestFinished() call
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: Handle other requests
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
void cWebAdmin::OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & 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);
|
2016-02-20 05:50:52 -05:00
|
|
|
auto Data = std::static_pointer_cast<cWebadminRequestData>(a_Request.GetUserData());
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Data == nullptr)
|
2013-10-05 17:08:16 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-02-20 05:50:52 -05:00
|
|
|
Data->m_Form.Parse(a_Data, a_Size);
|
2013-10-05 17:08:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
void cWebAdmin::OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & 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-05 17:08:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|