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
|
|
|
|
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "OSSupport/Socket.h"
|
2013-08-08 10:30:02 -04:00
|
|
|
#include "LuaState.h"
|
2013-10-05 17:08:16 -04:00
|
|
|
#include "../iniFile/iniFile.h"
|
|
|
|
#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:
|
2012-06-14 09:06:06 -04:00
|
|
|
class cStringMap;
|
2013-08-08 10:30:02 -04:00
|
|
|
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-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
AString Method;
|
|
|
|
AString Path;
|
|
|
|
AString Username;
|
|
|
|
// tolua_end
|
2013-10-13 14:29:57 -04:00
|
|
|
|
|
|
|
/// Parameters given in the URL, after the questionmark
|
2013-07-28 20:37:59 -04:00
|
|
|
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
|
2013-10-13 14:29:57 -04:00
|
|
|
|
|
|
|
/// Parameters posted as a part of a form - either in the URL (GET method) or in the body (POST method)
|
2013-07-28 20:37:59 -04:00
|
|
|
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
|
2013-10-13 14:29:57 -04:00
|
|
|
|
|
|
|
/// Same as PostParams
|
2013-07-28 20:37:59 -04:00
|
|
|
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
|
2013-08-08 10:30:02 -04:00
|
|
|
} ; // tolua_export
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 :
|
|
|
|
public cHTTPServer::cCallbacks
|
2013-08-08 10:30:02 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// tolua_end
|
|
|
|
|
|
|
|
typedef std::list< cWebPlugin* > PluginList;
|
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebAdmin(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
/// Initializes the object. Returns true if successfully initialized and ready to start
|
|
|
|
bool Init(void);
|
|
|
|
|
|
|
|
/// Starts the HTTP server taking care of the admin. Returns true if successful
|
|
|
|
bool Start(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-05 17:08:16 -04: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
|
|
|
|
PluginList GetPlugins() const { return m_Plugins; } // >> EXPORTED IN MANUALBINDINGS <<
|
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
// tolua_begin
|
2013-09-21 13:00:01 -04:00
|
|
|
|
|
|
|
/// Returns the amount of currently used memory, in KiB, or -1 if it cannot be queried
|
|
|
|
static int GetMemoryUsage(void);
|
2013-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
sWebAdminPage GetPage(const HTTPRequest& a_Request);
|
2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
/// Returns the contents of the default page - the list of plugins and players
|
|
|
|
AString GetDefaultPage(void);
|
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
AString GetBaseURL(const AString& a_URL);
|
|
|
|
|
|
|
|
// tolua_end
|
2012-09-30 12:37:44 -04:00
|
|
|
|
2013-07-28 20:37:59 -04:00
|
|
|
AString GetBaseURL(const AStringVector& a_URLSplit);
|
2013-08-08 10:30:02 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
protected:
|
|
|
|
/// Common base class for request body data handlers
|
|
|
|
class cRequestData
|
|
|
|
{
|
|
|
|
public:
|
2013-10-06 10:40:28 -04:00
|
|
|
virtual ~cRequestData() {} // Force a virtual destructor in all descendants
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
/// Called when a new chunk of body data is received
|
|
|
|
virtual void OnBody(const char * a_Data, int a_Size) = 0;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
/// The body handler for requests in the "/webadmin" and "/~webadmin" paths
|
|
|
|
class cWebadminRequestData :
|
|
|
|
public cRequestData,
|
|
|
|
public cHTTPFormParser::cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cHTTPFormParser m_Form;
|
|
|
|
|
|
|
|
|
|
|
|
cWebadminRequestData(cHTTPRequest & a_Request) :
|
|
|
|
m_Form(a_Request, *this)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// cRequestData overrides:
|
|
|
|
virtual void OnBody(const char * a_Data, int a_Size) override;
|
|
|
|
|
|
|
|
// cHTTPFormParser::cCallbacks overrides. Files are ignored:
|
|
|
|
virtual void OnFileStart(cHTTPFormParser & a_Parser, const AString & a_FileName) override {}
|
|
|
|
virtual void OnFileData(cHTTPFormParser & a_Parser, const char * a_Data, int a_Size) override {}
|
|
|
|
virtual void OnFileEnd(cHTTPFormParser & a_Parser) override {}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
/// Set to true if Init() succeeds and the webadmin isn't to be disabled
|
|
|
|
bool m_IsInitialized;
|
2013-08-08 10:30:02 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
/// The webadmin.ini file, used for the settings and allowed logins
|
|
|
|
cIniFile m_IniFile;
|
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
PluginList m_Plugins;
|
|
|
|
|
|
|
|
/// The Lua template script to provide templates:
|
|
|
|
cLuaState m_TemplateScript;
|
2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
/// The HTTP server which provides the underlying HTTP parsing, serialization and events
|
|
|
|
cHTTPServer m_HTTPServer;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-28 20:37:59 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
AString GetTemplate(void);
|
|
|
|
|
|
|
|
/// Handles requests coming to the "/webadmin" or "/~webadmin" URLs
|
|
|
|
void HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
|
|
|
|
|
|
|
/// Handles requests for the root page
|
|
|
|
void HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
|
|
|
|
|
|
|
// cHTTPServer::cCallbacks overrides:
|
|
|
|
virtual void OnRequestBegun (cHTTPConnection & a_Connection, cHTTPRequest & a_Request) override;
|
|
|
|
virtual void OnRequestBody (cHTTPConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, int a_Size) override;
|
|
|
|
virtual void OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) override;
|
2013-08-08 10:30:02 -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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|