Files
cuberite-2a/source/WebAdmin.h
T

217 lines
4.9 KiB
C++
Raw Normal View History

2013-10-05 23:08:16 +02:00
// WebAdmin.h
// Declares the cWebAdmin class representing the admin interface over http protocol, and related services (API)
#pragma once
#include "OSSupport/Socket.h"
2013-08-08 16:30:02 +02:00
#include "LuaState.h"
2013-10-05 23:08:16 +02: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
2013-08-08 16:30:02 +02:00
// fwd:
class cStringMap;
2013-08-08 16:30:02 +02:00
class cEvent;
class cWebPlugin;
// tolua_begin
struct HTTPFormData
{
std::string Name;
std::string Value;
std::string Type;
} ;
// tolua_end
2013-08-08 16:30:02 +02:00
// tolua_begin
struct HTTPRequest
{
typedef std::map< std::string, std::string > StringStringMap;
typedef std::map< std::string, HTTPFormData > FormDataMap;
2013-08-08 16:30:02 +02:00
AString Method;
AString Path;
AString Username;
// tolua_end
2013-10-13 20:29:57 +02:00
/// Parameters given in the URL, after the questionmark
2013-07-29 02:37:59 +02:00
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
2013-10-13 20:29:57 +02:00
/// Parameters posted as a part of a form - either in the URL (GET method) or in the body (POST method)
2013-07-29 02:37:59 +02:00
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
2013-10-13 20:29:57 +02:00
/// Same as PostParams
2013-07-29 02:37:59 +02:00
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
2013-08-08 16:30:02 +02:00
} ; // tolua_export
// tolua_begin
struct HTTPTemplateRequest
{
HTTPRequest Request;
} ;
// tolua_end
2013-07-29 02:37:59 +02:00
// tolua_begin
struct sWebAdminPage
{
AString Content;
AString PluginName;
AString TabName;
};
// tolua_end
2013-08-08 16:30:02 +02:00
// tolua_begin
2013-10-05 23:08:16 +02:00
class cWebAdmin :
public cHTTPServer::cCallbacks
2013-08-08 16:30:02 +02:00
{
public:
// tolua_end
2013-08-08 16:30:02 +02:00
typedef std::list< cWebPlugin* > PluginList;
2013-10-05 23:08:16 +02:00
cWebAdmin(void);
2013-10-18 12:50:35 +02:00
~cWebAdmin();
2013-10-05 23:08:16 +02:00
/// Initializes the object. Returns true if successfully initialized and ready to start
bool Init(void);
2013-10-05 23:08:16 +02:00
/// Starts the HTTP server taking care of the admin. Returns true if successful
bool Start(void);
2013-10-05 23:08:16 +02:00
void AddPlugin( cWebPlugin* a_Plugin );
void RemovePlugin( cWebPlugin* a_Plugin );
2013-07-29 02:37:59 +02: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-07-29 02:37:59 +02:00
2013-08-08 16:30:02 +02:00
// tolua_begin
sWebAdminPage GetPage(const HTTPRequest & a_Request);
2013-10-05 23:08:16 +02:00
/// Returns the contents of the default page - the list of plugins and players
AString GetDefaultPage(void);
/// Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style)
AString GetBaseURL(const AString & a_URL);
/// Escapes text passed into it, so it can be embedded into html.
static AString GetHTMLEscapedString(const AString & a_Input);
2013-08-08 16:30:02 +02:00
// tolua_end
/// Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style)
AString GetBaseURL(const AStringVector& a_URLSplit);
2013-10-05 23:08:16 +02:00
protected:
/// Common base class for request body data handlers
class cRequestData
{
public:
2013-10-06 16:40:28 +02:00
virtual ~cRequestData() {} // Force a virtual destructor in all descendants
2013-10-05 23:08:16 +02:00
/// Called when a new chunk of body data is received
virtual void OnBody(const char * a_Data, int a_Size) = 0;
} ;
2013-10-05 23:08:16 +02:00
/// The body handler for requests in the "/webadmin" and "/~webadmin" paths
class cWebadminRequestData :
public cRequestData,
public cHTTPFormParser::cCallbacks
{
public:
cHTTPFormParser m_Form;
2013-10-05 23:08:16 +02:00
cWebadminRequestData(cHTTPRequest & a_Request) :
m_Form(a_Request, *this)
{
}
2013-10-05 23:08:16 +02:00
// 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 {}
} ;
2013-10-05 23:08:16 +02:00
/// Set to true if Init() succeeds and the webadmin isn't to be disabled
bool m_IsInitialized;
2013-08-08 16:30:02 +02:00
2013-10-05 23:08:16 +02:00
/// The webadmin.ini file, used for the settings and allowed logins
cIniFile m_IniFile;
2013-08-08 16:30:02 +02:00
PluginList m_Plugins;
/// The Lua template script to provide templates:
cLuaState m_TemplateScript;
2013-10-05 23:08:16 +02:00
/// The HTTP server which provides the underlying HTTP parsing, serialization and events
cHTTPServer m_HTTPServer;
2013-07-29 02:37:59 +02:00
2013-10-05 23:08:16 +02:00
AString GetTemplate(void);
2013-10-05 23:08:16 +02:00
/// Handles requests coming to the "/webadmin" or "/~webadmin" URLs
void HandleWebadminRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
2013-10-05 23:08:16 +02:00
/// Handles requests for the root page
void HandleRootRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
2013-10-05 23:08:16 +02:00
// 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 16:30:02 +02:00
} ; // tolua_export
2013-10-05 23:08:16 +02:00
// Revert MSVC warnings back to orignal state:
#if defined(_MSC_VER)
#pragma warning(pop)
#endif