diff options
author | Mattes D <github@xoft.cz> | 2016-03-02 10:05:10 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2016-03-17 19:29:01 +0100 |
commit | 62d81eb763e6fb72af5d828c1892156e1f5a3127 (patch) | |
tree | 7c04181f9f1f21dbe6a5ef00d5fed53c1baa8a54 /src/WebAdmin.h | |
parent | Added cLuaState::cCallback for representing (resettable) Lua callbacks. (diff) | |
download | cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.tar cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.tar.gz cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.tar.bz2 cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.tar.lz cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.tar.xz cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.tar.zst cuberite-62d81eb763e6fb72af5d828c1892156e1f5a3127.zip |
Diffstat (limited to '')
-rw-r--r-- | src/WebAdmin.h | 158 |
1 files changed, 117 insertions, 41 deletions
diff --git a/src/WebAdmin.h b/src/WebAdmin.h index 5e48f597c..b76ca6df8 100644 --- a/src/WebAdmin.h +++ b/src/WebAdmin.h @@ -89,14 +89,14 @@ struct HTTPTemplateRequest -// tolua_begin struct sWebAdminPage { AString Content; AString PluginName; - AString TabName; + AString TabTitle; + AString TabUrlPath; + AString ContentType; }; -// tolua_end @@ -111,7 +111,49 @@ class cWebAdmin : public: // tolua_end - typedef std::list< cWebPlugin* > PluginList; + /** Interface for getting the content of a single WebTab. */ + class cWebTabCallback abstract + { + public: + // Force a virtual destructor in descendants + virtual ~cWebTabCallback() {} + + /** Returns the contents for the specified request. + Returns true if the call was successful, false on an error. + a_Request is the full HTTP request object, as received from the client. + a_UrlPath is the UrlPath of the WebTab registered for this request, as parsed from a_Request. + Descendants should fill a_Content with the page contents + and optionally set a_ContentType [defaults to "text/html"] */ + virtual bool Call( + const HTTPRequest & a_Request, + const AString & a_UrlPath, + AString & a_Content, + AString & a_ContentType + ) = 0; + }; + + + /** Container for a single web tab. + Each web tab has a title, URL path and an associated plugin's name. + Each web tab is registered with a callback to provide the content. */ + class cWebTab + { + public: + AString m_Title; + AString m_UrlPath; + AString m_PluginName; + SharedPtr<cWebTabCallback> m_Callback; + + cWebTab(const AString & a_Title, const AString & a_UrlPath, const AString & a_PluginName, SharedPtr<cWebTabCallback> a_Callback): + m_Title(a_Title), + m_UrlPath(a_UrlPath), + m_PluginName(a_PluginName), + m_Callback(a_Callback) + { + } + }; + typedef SharedPtr<cWebTab> cWebTabPtr; + typedef std::vector<cWebTabPtr> cWebTabPtrs; cWebAdmin(void); @@ -120,81 +162,115 @@ public: /** 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 */ + /** Starts the HTTP server taking care of the webadmin. Returns true if successful */ bool Start(void); /** Stops the HTTP server, if it was started. */ void Stop(void); - /** 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); + /** Loads the login template into m_LoginPage. + Returns true if the loading succeeds, false if not. */ + bool LoadLoginPage(void); - // 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 << + /** Returns a copy of all the registered web tabs. + Exported to Lua in ManualBindings.cpp. */ + cWebTabPtrs GetAllWebTabs(void) { return m_WebTabs; } - // tolua_begin + /** Removes all WebTabs registered by the specified plugin. */ + void RemoveAllPluginWebTabs(const AString & a_PluginName); + /** Returns the (inner) page contents for the specified request. + Calls the appropriate WebTab handler to get the contents. + Exported to Lua in ManualBindings.cpp. */ sWebAdminPage GetPage(const HTTPRequest & a_Request); - /** Returns the contents of the default page - the list of plugins and players */ - AString GetDefaultPage(void); + // tolua_begin - /** 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); + /** Reloads m_IniFile, m_LoginPage and m_TemplateScript. + Note that reloading will not change the "enabled" state of the server, and it will not update listening ports. */ + void Reload(void); - /** Returns the list of ports used for the webadmin. */ + /** Returns the list of ports on which the webadmin is configured to listen. */ 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(); } - // tolua_end - /** Escapes text passed into it, so it can be embedded into html. */ + /** Adds a new WebTab handler. + a_Title is the display title of the tab + a_UrlPath is the part of the URL that uniquely identifies this tab. + a_PluginName is the display name of the plugin creating this tab. + a_Callback is used to provide the actual WebTab contents, when requested. + Exported in ManualBindings.cpp. */ + void AddWebTab( + const AString & a_Title, + const AString & a_UrlPath, + const AString & a_PluginName, + SharedPtr<cWebTabCallback> a_Callback + ); + + /** Removes the WebTab with the specified URL path. + Returns true if WebTab was found and removed, false if not found. + Exported in ManualBindings.cpp */ + bool DelWebTab(const AString & a_UrlPath); + + /** Escapes text passed into it, so it can be embedded into html. + Exported to Lua in ManualBindings.cpp. */ static AString GetHTMLEscapedString(const AString & a_Input); - /** Escapes the string for use in an URL */ + /** Escapes the string for use in an URL + Exported to Lua in ManualBindings.cpp. */ 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). + Exported to Lua in ManualBindings.cpp. */ + static AString GetBaseURL(const AString & a_URL); + /** Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style) */ static AString GetBaseURL(const AStringVector & a_URLSplit); - /** Returns the content type from the file extension. If the extension isn't in the list, the function returns "text/html" */ + /** Returns the content type from the file extension. + If the extension isn't in the list, the function returns an empty string. + Exported to Lua in ManualBindings.cpp. */ static AString GetContentTypeFromFileExt(const AString & a_FileExtension); protected: + /** Protects m_WebTabs, m_TemplateScript, m_LoginTemplate and m_IniFile against multithreaded access. */ + cCriticalSection m_CS; + + /** All registered WebTab handlers. + Protected against multithreaded access by m_CS. */ + cWebTabPtrs m_WebTabs; + + /** The Lua template script to provide templates. + Protected against multithreaded access by m_CS. */ + cLuaState m_TemplateScript; + + /** The HTML page that provides the login. + Protected against multithreaded access by m_CS. */ + AString m_LoginPage; + + /** The webadmin.ini file, used for the settings and allowed logins. + Protected against multithreaded access by m_CS. */ + cIniFile m_IniFile; + /** Set to true if Init() succeeds and the webadmin isn't to be disabled */ bool m_IsInitialized; /** Set to true if Start() succeeds in starting the server, reset back to false in Stop(). */ bool m_IsRunning; - /** The webadmin.ini file, used for the settings and allowed logins */ - cIniFile m_IniFile; - - PluginList m_Plugins; - /** The ports on which the webadmin is running. */ AStringVector m_Ports; - /** The Lua template script to provide templates: */ - cLuaState m_TemplateScript; - - /** The template that provides the login site: */ - AString m_LoginTemplate; - /** The HTTP server which provides the underlying HTTP parsing, serialization and events */ cHTTPServer m_HTTPServer; + + /** Loads webadmin.ini into m_IniFile. + Creates a default file if it doesn't exist. + Returns true if webadmin is enabled, false if disabled. */ + bool LoadIniFile(void); + /** Handles requests coming to the "/webadmin" or "/~webadmin" URLs */ void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request); |