2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
// WebAdmin.h
|
|
|
|
|
|
|
|
// Declares the cWebAdmin class representing the admin interface over http protocol, and related services (API)
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#pragma once
|
|
|
|
|
2013-12-08 06:17:54 -05:00
|
|
|
#include "Bindings/LuaState.h"
|
2014-10-23 09:15:10 -04:00
|
|
|
#include "IniFile.h"
|
2013-10-05 17:08:16 -04:00
|
|
|
#include "HTTPServer/HTTPServer.h"
|
|
|
|
#include "HTTPServer/HTTPFormParser.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disable MSVC warnings:
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4355) // 'this' : used in base member initializer list
|
|
|
|
#endif
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
|
|
|
class cEvent;
|
|
|
|
class cWebPlugin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tolua_begin
|
|
|
|
struct HTTPFormData
|
|
|
|
{
|
|
|
|
std::string Name;
|
|
|
|
std::string Value;
|
|
|
|
std::string Type;
|
|
|
|
} ;
|
|
|
|
// tolua_end
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
// tolua_begin
|
|
|
|
struct HTTPRequest
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef std::map< std::string, std::string > StringStringMap;
|
|
|
|
typedef std::map< std::string, HTTPFormData > FormDataMap;
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
AString Method;
|
|
|
|
AString Path;
|
|
|
|
AString Username;
|
|
|
|
// tolua_end
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Parameters given in the URL, after the questionmark */
|
2013-07-28 20:37:59 -04:00
|
|
|
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Parameters posted as a part of a form - either in the URL (GET method) or in the body (POST method) */
|
2014-07-17 13:13:23 -04:00
|
|
|
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Same as PostParams */
|
2013-07-28 20:37:59 -04:00
|
|
|
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
|
2014-07-17 13:13:23 -04:00
|
|
|
} ; // tolua_export
|
2013-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tolua_begin
|
|
|
|
struct HTTPTemplateRequest
|
|
|
|
{
|
|
|
|
HTTPRequest Request;
|
|
|
|
} ;
|
|
|
|
// tolua_end
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-28 20:37:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
// tolua_begin
|
|
|
|
struct sWebAdminPage
|
|
|
|
{
|
|
|
|
AString Content;
|
|
|
|
AString PluginName;
|
|
|
|
AString TabName;
|
|
|
|
};
|
|
|
|
// tolua_end
|
|
|
|
|
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tolua_begin
|
2013-10-05 17:08:16 -04:00
|
|
|
class cWebAdmin :
|
2015-03-19 16:26:38 -04:00
|
|
|
// tolua_end
|
2013-10-05 17:08:16 -04:00
|
|
|
public cHTTPServer::cCallbacks
|
2015-03-19 16:26:38 -04:00
|
|
|
// tolua_begin
|
2013-08-08 10:30:02 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// tolua_end
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
typedef std::list< cWebPlugin* > PluginList;
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebAdmin(void);
|
2013-12-21 10:38:37 -05:00
|
|
|
virtual ~cWebAdmin();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Initializes the object. Returns true if successfully initialized and ready to start */
|
2013-10-05 17:08:16 -04:00
|
|
|
bool Init(void);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Starts the HTTP server taking care of the admin. Returns true if successful */
|
2013-10-05 17:08:16 -04:00
|
|
|
bool Start(void);
|
2014-02-07 06:26:41 -05:00
|
|
|
|
|
|
|
/** Stops the HTTP server, if it was started. */
|
|
|
|
void Stop(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
/** Loads the login template. Returns true if the loading succeeds, false if not. */
|
2014-08-31 06:59:04 -04:00
|
|
|
bool LoadLoginTemplate(void);
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
void AddPlugin(cWebPlugin * a_Plugin);
|
|
|
|
void RemovePlugin(cWebPlugin * a_Plugin);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-28 20:37:59 -04:00
|
|
|
// TODO: Convert this to the auto-locking callback mechanism used for looping players in worlds and such
|
2014-07-17 13:13:23 -04:00
|
|
|
PluginList GetPlugins() const { return m_Plugins; } // >> EXPORTED IN MANUALBINDINGS <<
|
2013-07-28 20:37:59 -04:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
// tolua_begin
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-21 07:22:47 -04:00
|
|
|
sWebAdminPage GetPage(const HTTPRequest & a_Request);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Returns the contents of the default page - the list of plugins and players */
|
2013-10-05 17:08:16 -04:00
|
|
|
AString GetDefaultPage(void);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style) */
|
2013-10-21 07:22:47 -04:00
|
|
|
AString GetBaseURL(const AString & a_URL);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
/** Returns the list of ports used for the webadmin. */
|
|
|
|
AString GetPorts(void) const { return StringsConcat(m_Ports, ','); }
|
|
|
|
|
|
|
|
/** OBSOLETE: Returns the list of IPv4 ports used for the webadmin.
|
|
|
|
Currently there is no distinction between IPv4 and IPv6; use GetPorts() instead. */
|
|
|
|
AString GetIPv4Ports(void) const { return GetPorts(); }
|
|
|
|
|
|
|
|
/** OBSOLETE: Returns the list of IPv6 ports used for the webadmin.
|
|
|
|
Currently there is no distinction between IPv4 and IPv6; use GetPorts() instead. */
|
|
|
|
AString GetIPv6Ports(void) const { return GetPorts(); }
|
2014-08-09 16:54:43 -04:00
|
|
|
|
|
|
|
// tolua_end
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Escapes text passed into it, so it can be embedded into html. */
|
2013-10-21 07:22:47 -04:00
|
|
|
static AString GetHTMLEscapedString(const AString & a_Input);
|
2014-08-09 16:34:59 -04:00
|
|
|
|
|
|
|
/** Escapes the string for use in an URL */
|
|
|
|
static AString GetURLEncodedString(const AString & a_Input);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style) */
|
2014-08-09 16:34:59 -04:00
|
|
|
static AString GetBaseURL(const AStringVector & a_URLSplit);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
/** Returns the content type from the file extension. If the extension isn't in the list, the function returns "text/html" */
|
2014-09-06 04:56:22 -04:00
|
|
|
static AString GetContentTypeFromFileExt(const AString & a_FileExtension);
|
2014-09-02 14:02:52 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
protected:
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Common base class for request body data handlers */
|
2013-10-05 17:08:16 -04:00
|
|
|
class cRequestData
|
|
|
|
{
|
|
|
|
public:
|
2013-10-06 10:40:28 -04:00
|
|
|
virtual ~cRequestData() {} // Force a virtual destructor in all descendants
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Called when a new chunk of body data is received */
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnBody(const char * a_Data, size_t a_Size) = 0;
|
2013-10-05 17:08:16 -04:00
|
|
|
} ;
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** The body handler for requests in the "/webadmin" and "/~webadmin" paths */
|
2013-10-05 17:08:16 -04:00
|
|
|
class cWebadminRequestData :
|
|
|
|
public cRequestData,
|
|
|
|
public cHTTPFormParser::cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cHTTPFormParser m_Form;
|
2013-10-19 12:17:33 -04:00
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebadminRequestData(cHTTPRequest & a_Request) :
|
|
|
|
m_Form(a_Request, *this)
|
|
|
|
{
|
|
|
|
}
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// cRequestData overrides:
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnBody(const char * a_Data, size_t a_Size) override;
|
2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
// cHTTPFormParser::cCallbacks overrides. Files are ignored:
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnFileStart(cHTTPFormParser &, const AString & a_FileName) override
|
2013-12-22 08:46:55 -05:00
|
|
|
{
|
|
|
|
UNUSED(a_FileName);
|
|
|
|
}
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnFileData(cHTTPFormParser &, const char * a_Data, size_t a_Size) override
|
2013-12-22 08:46:55 -05:00
|
|
|
{
|
|
|
|
UNUSED(a_Data);
|
|
|
|
UNUSED(a_Size);
|
|
|
|
}
|
2013-12-21 10:38:37 -05:00
|
|
|
virtual void OnFileEnd(cHTTPFormParser &) override {}
|
2013-10-05 17:08:16 -04:00
|
|
|
} ;
|
2013-10-19 12:17:33 -04:00
|
|
|
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Set to true if Init() succeeds and the webadmin isn't to be disabled */
|
2013-10-05 17:08:16 -04:00
|
|
|
bool m_IsInitialized;
|
2014-02-07 06:26:41 -05:00
|
|
|
|
|
|
|
/** Set to true if Start() succeeds in starting the server, reset back to false in Stop(). */
|
|
|
|
bool m_IsRunning;
|
2013-08-08 10:30:02 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** The webadmin.ini file, used for the settings and allowed logins */
|
2013-10-05 17:08:16 -04:00
|
|
|
cIniFile m_IniFile;
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
PluginList m_Plugins;
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
/** The ports on which the webadmin is running. */
|
|
|
|
AStringVector m_Ports;
|
2013-12-24 08:44:24 -05:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** The Lua template script to provide templates: */
|
2013-08-08 10:30:02 -04:00
|
|
|
cLuaState m_TemplateScript;
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
/** The template that provides the login site: */
|
2014-08-31 06:59:04 -04:00
|
|
|
AString m_LoginTemplate;
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** The HTTP server which provides the underlying HTTP parsing, serialization and events */
|
2013-10-05 17:08:16 -04:00
|
|
|
cHTTPServer m_HTTPServer;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Handles requests coming to the "/webadmin" or "/~webadmin" URLs */
|
2013-10-05 17:08:16 -04:00
|
|
|
void HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Handles requests for the root page */
|
2013-10-05 17:08:16 -04:00
|
|
|
void HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
/** Handles requests for a file */
|
|
|
|
void HandleFileRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// cHTTPServer::cCallbacks overrides:
|
|
|
|
virtual void OnRequestBegun (cHTTPConnection & a_Connection, cHTTPRequest & a_Request) override;
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnRequestBody (cHTTPConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size) override;
|
2013-10-05 17:08:16 -04:00
|
|
|
virtual void OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) override;
|
2014-07-17 13:13:23 -04:00
|
|
|
} ; // tolua_export
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
// Revert MSVC warnings back to orignal state:
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|