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

225 lines
5.7 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 "Bindings/LuaState.h"
2014-10-23 15:15:10 +02:00
#include "IniFile.h"
2016-01-03 15:59:55 +01:00
#include "HTTP/HTTPServer.h"
#include "HTTP/HTTPMessage.h"
2013-10-05 23:08:16 +02:00
// 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 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;
/** The entire URL presented to the HTTP server. */
AString URL;
/** HTTP method used for the request ("GET", "POST" etc.) */
2013-08-08 16:30:02 +02:00
AString Method;
/** The Path part of the request's URL (excluding GET params). */
2013-08-08 16:30:02 +02:00
AString Path;
/** Name of the logged-in user. Empty if not logged in. */
2013-08-08 16:30:02 +02:00
AString Username;
2013-08-08 16:30:02 +02:00
// tolua_end
/** Parameters given in the URL, after the questionmark */
2013-07-29 02:37:59 +02:00
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
/** Parameters posted as a part of a form - either in the URL (GET method) or in the body (POST method) */
2014-07-17 19:13:23 +02:00
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
/** Same as PostParams */
2013-07-29 02:37:59 +02:00
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
2014-07-17 19:13:23 +02:00
} ; // tolua_export
2013-08-08 16:30:02 +02:00
// tolua_begin
struct HTTPTemplateRequest
{
HTTPRequest Request;
} ;
// tolua_end
2013-07-29 02:37:59 +02:00
2016-03-21 09:58:15 +01:00
// tolua_begin
2013-07-29 02:37:59 +02:00
struct sWebAdminPage
{
AString Content;
AString PluginName;
2016-03-21 09:58:15 +01:00
AString TabName;
2013-07-29 02:37:59 +02:00
};
2016-03-21 09:58:15 +01:00
// tolua_end
2013-07-29 02:37:59 +02:00
2013-08-08 16:30:02 +02:00
// tolua_begin
2013-10-05 23:08:16 +02:00
class cWebAdmin :
2015-03-19 21:26:38 +01:00
// tolua_end
2013-10-05 23:08:16 +02:00
public cHTTPServer::cCallbacks
2015-03-19 21:26:38 +01:00
// tolua_begin
2013-08-08 16:30:02 +02:00
{
public:
// tolua_end
2016-03-21 09:58:15 +01:00
typedef std::list< cWebPlugin* > PluginList;
2013-08-08 16:30:02 +02:00
2013-10-05 23:08:16 +02:00
cWebAdmin(void);
2013-12-21 15:38:37 +00:00
virtual ~cWebAdmin();
/** Initializes the object. Returns true if successfully initialized and ready to start */
2013-10-05 23:08:16 +02:00
bool Init(void);
2016-03-21 09:58:15 +01:00
/** Starts the HTTP server taking care of the admin. Returns true if successful */
2013-10-05 23:08:16 +02:00
bool Start(void);
2016-02-05 23:45:45 +02:00
/** Stops the HTTP server, if it was started. */
void Stop(void);
2016-03-21 09:58:15 +01:00
/** Loads the login template. Returns true if the loading succeeds, false if not. */
bool LoadLoginTemplate(void);
void AddPlugin(cWebPlugin * a_Plugin);
void RemovePlugin(cWebPlugin * a_Plugin);
2016-03-21 09:58:15 +01: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
2016-03-21 09:58:15 +01:00
// tolua_begin
sWebAdminPage GetPage(const HTTPRequest & a_Request);
2016-03-21 09:58:15 +01:00
/** Returns the contents of the default page - the list of plugins and players */
AString GetDefaultPage(void);
2016-03-21 09:58:15 +01:00
/** 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);
2016-03-21 09:58:15 +01:00
/** Returns the list of ports used for the webadmin. */
2015-01-24 10:11:34 +01:00
AString GetPorts(void) const { return StringsConcat(m_Ports, ','); }
2016-03-21 09:58:15 +01:00
/** 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(); }
// tolua_end
2016-03-21 09:58:15 +01:00
/** Escapes text passed into it, so it can be embedded into html. */
static AString GetHTMLEscapedString(const AString & a_Input);
2016-02-05 23:45:45 +02:00
2016-03-21 09:58:15 +01:00
/** Escapes the string for use in an URL */
2014-08-09 22:34:59 +02:00
static AString GetURLEncodedString(const AString & a_Input);
/** Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style) */
2014-08-09 22:34:59 +02:00
static AString GetBaseURL(const AStringVector & a_URLSplit);
2016-03-21 09:58:15 +01: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 09:56:22 +01:00
static AString GetContentTypeFromFileExt(const AString & a_FileExtension);
2014-09-02 20:02:52 +02:00
2013-10-05 23:08:16 +02:00
protected:
/** Set to true if Init() succeeds and the webadmin isn't to be disabled */
2013-10-05 23:08:16 +02:00
bool m_IsInitialized;
2016-02-05 23:45:45 +02:00
/** Set to true if Start() succeeds in starting the server, reset back to false in Stop(). */
bool m_IsRunning;
2013-08-08 16:30:02 +02:00
2016-03-21 09:58:15 +01:00
/** The webadmin.ini file, used for the settings and allowed logins */
cIniFile m_IniFile;
PluginList m_Plugins;
2015-01-24 10:11:34 +01:00
/** The ports on which the webadmin is running. */
AStringVector m_Ports;
2016-03-21 09:58:15 +01:00
/** The Lua template script to provide templates: */
cLuaState m_TemplateScript;
2016-03-21 09:58:15 +01:00
/** The template that provides the login site: */
AString m_LoginTemplate;
2016-03-21 09:58:15 +01:00
/** The HTTP server which provides the underlying HTTP parsing, serialization and events */
cHTTPServer m_HTTPServer;
/** Handles requests coming to the "/webadmin" or "/~webadmin" URLs */
void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
/** Handles requests for the root page */
void HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
2014-09-02 20:02:52 +02:00
/** Handles requests for a file */
void HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
2014-09-02 20:02:52 +02:00
2013-10-05 23:08:16 +02:00
// cHTTPServer::cCallbacks overrides:
virtual void OnRequestBegun (cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request) override;
virtual void OnRequestBody (cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request, const char * a_Data, size_t a_Size) override;
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request) override;
2014-07-17 19:13:23 +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