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"
|
2016-01-03 09:59:55 -05:00
|
|
|
#include "HTTP/HTTPServer.h"
|
2016-02-20 05:50:52 -05:00
|
|
|
#include "HTTP/HTTPMessage.h"
|
2013-10-05 17:08:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2015-04-21 04:54:43 -04:00
|
|
|
/** The entire URL presented to the HTTP server. */
|
|
|
|
AString URL;
|
|
|
|
|
|
|
|
/** HTTP method used for the request ("GET", "POST" etc.) */
|
2013-08-08 10:30:02 -04:00
|
|
|
AString Method;
|
2015-04-21 04:54:43 -04:00
|
|
|
|
|
|
|
/** The Path part of the request's URL (excluding GET params). */
|
2013-08-08 10:30:02 -04:00
|
|
|
AString Path;
|
2015-04-21 04:54:43 -04:00
|
|
|
|
|
|
|
/** Name of the logged-in user. Empty if not logged in. */
|
2013-08-08 10:30:02 -04:00
|
|
|
AString Username;
|
2015-04-21 04:54:43 -04:00
|
|
|
|
2013-08-08 10:30:02 -04:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
struct sWebAdminPage
|
|
|
|
{
|
|
|
|
AString Content;
|
|
|
|
AString PluginName;
|
2016-03-02 04:05:10 -05:00
|
|
|
AString TabTitle;
|
|
|
|
AString TabUrlPath;
|
|
|
|
AString ContentType;
|
2013-07-28 20:37:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** 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;
|
2017-07-20 07:19:18 -04:00
|
|
|
std::shared_ptr<cWebTabCallback> m_Callback;
|
2016-03-02 04:05:10 -05:00
|
|
|
|
2017-07-20 07:19:18 -04:00
|
|
|
cWebTab(const AString & a_Title, const AString & a_UrlPath, const AString & a_PluginName, std::shared_ptr<cWebTabCallback> a_Callback):
|
2016-03-02 04:05:10 -05:00
|
|
|
m_Title(a_Title),
|
|
|
|
m_UrlPath(a_UrlPath),
|
|
|
|
m_PluginName(a_PluginName),
|
2020-05-14 18:15:35 -04:00
|
|
|
m_Callback(std::move(a_Callback))
|
2016-03-02 04:05:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2017-07-20 07:19:18 -04:00
|
|
|
typedef std::shared_ptr<cWebTab> cWebTabPtr;
|
2016-03-02 04:05:10 -05:00
|
|
|
typedef std::vector<cWebTabPtr> cWebTabPtrs;
|
2013-08-08 10:30:02 -04:00
|
|
|
|
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
cWebAdmin(void);
|
2017-05-20 02:16:28 -04:00
|
|
|
virtual ~cWebAdmin() override;
|
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
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** Starts the HTTP server taking care of the webadmin. Returns true if successful */
|
2013-10-05 17:08:16 -04:00
|
|
|
bool Start(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
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
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** Loads the login template into m_LoginPage.
|
|
|
|
Returns true if the loading succeeds, false if not. */
|
|
|
|
bool LoadLoginPage(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** Returns a copy of all the registered web tabs.
|
|
|
|
Exported to Lua in ManualBindings.cpp. */
|
|
|
|
cWebTabPtrs GetAllWebTabs(void) { return m_WebTabs; }
|
2013-07-28 20:37:59 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** Removes all WebTabs registered by the specified plugin. */
|
|
|
|
void RemoveAllPluginWebTabs(const AString & a_PluginName);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** Returns the (inner) page contents for the specified request.
|
|
|
|
Calls the appropriate WebTab handler to get the contents.
|
|
|
|
Exported to Lua in ManualBindings.cpp. */
|
2013-10-21 07:22:47 -04:00
|
|
|
sWebAdminPage GetPage(const HTTPRequest & a_Request);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
// tolua_begin
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** 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);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** Returns the list of ports on which the webadmin is configured to listen. */
|
2015-01-24 04:11:34 -05:00
|
|
|
AString GetPorts(void) const { return StringsConcat(m_Ports, ','); }
|
2017-05-01 04:31:18 -04:00
|
|
|
|
|
|
|
/** Returns the prefix needed for making a link point to the webadmin root from the given URL ("../../../webadmin"-style). */
|
|
|
|
static AString GetBaseURL(const AString & a_URL);
|
|
|
|
|
|
|
|
/** Returns the content type from the file extension.
|
|
|
|
If the extension isn't in the list, the function returns an empty string. */
|
|
|
|
static AString GetContentTypeFromFileExt(const AString & a_FileExtension);
|
|
|
|
|
|
|
|
/** Escapes text passed into it, so it can be embedded into html. */
|
|
|
|
static AString GetHTMLEscapedString(const AString & a_Input);
|
2014-08-09 16:54:43 -04:00
|
|
|
// tolua_end
|
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** 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,
|
2017-07-20 07:19:18 -04:00
|
|
|
std::shared_ptr<cWebTabCallback> a_Callback
|
2016-03-02 04:05:10 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
/** 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 the string for use in an URL
|
|
|
|
Exported to Lua in ManualBindings.cpp. */
|
2014-08-09 16:34:59 -04:00
|
|
|
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
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
protected:
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
/** 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;
|
|
|
|
|
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;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2016-03-21 04:58:15 -04:00
|
|
|
/** The HTTP server which provides the underlying HTTP parsing, serialization and events */
|
|
|
|
cHTTPServer m_HTTPServer;
|
2016-03-02 04:05:10 -05:00
|
|
|
|
2016-03-02 04:05:10 -05:00
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Handles requests coming to the "/webadmin" or "/~webadmin" URLs */
|
2016-02-20 05:50:52 -05:00
|
|
|
void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-02-07 06:26:41 -05:00
|
|
|
/** Handles requests for the root page */
|
2016-02-20 05:50:52 -05:00
|
|
|
void HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
|
2013-10-19 12:17:33 -04:00
|
|
|
|
2014-09-02 14:02:52 -04:00
|
|
|
/** Handles requests for a file */
|
2016-02-20 05:50:52 -05:00
|
|
|
void HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
|
2014-09-02 14:02:52 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
// cHTTPServer::cCallbacks overrides:
|
2016-02-20 05:50:52 -05:00
|
|
|
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 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|