summaryrefslogtreecommitdiffstats
path: root/src/WebAdmin.h
blob: 1a27eabfe9ae000dc1b8f60a3662a02c939cbad4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

// WebAdmin.h

// Declares the cWebAdmin class representing the admin interface over http protocol, and related services (API)

#pragma once

#include "OSSupport/Socket.h"
#include "Bindings/LuaState.h"
#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





// fwd:
class cEvent;
class cWebPlugin;





// tolua_begin
struct HTTPFormData
{
	std::string Name;
	std::string Value;
	std::string Type;
} ;
// tolua_end




// tolua_begin
struct HTTPRequest
{
	typedef std::map< std::string, std::string > StringStringMap;
	typedef std::map< std::string, HTTPFormData > FormDataMap;

	AString Method;
	AString Path;
	AString Username;
	// tolua_end

	/// Parameters given in the URL, after the questionmark
	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)
	StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<

	/// Same as PostParams
	FormDataMap FormData;       // >> EXPORTED IN MANUALBINDINGS <<
} ; // tolua_export





// tolua_begin
struct HTTPTemplateRequest
{
	HTTPRequest Request;
} ;
// tolua_end





// tolua_begin
struct sWebAdminPage
{
	AString Content;
	AString PluginName;
	AString TabName;
};
// tolua_end





// tolua_begin
class cWebAdmin :
	public cHTTPServer::cCallbacks
{
public:
	// tolua_end

	typedef std::list< cWebPlugin* > PluginList;


	cWebAdmin(void);
	virtual ~cWebAdmin();

	/// 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);

	void AddPlugin( cWebPlugin* a_Plugin );
	void RemovePlugin( cWebPlugin* a_Plugin );

	// 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 <<

	// tolua_begin

	sWebAdminPage GetPage(const HTTPRequest & a_Request);

	/// 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);

	AString GetIPv4Ports(void) const { return m_PortsIPv4; }
	AString GetIPv6Ports(void) const { return m_PortsIPv6; }

	// 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);

protected:
	/// Common base class for request body data handlers
	class cRequestData
	{
	public:
		virtual ~cRequestData() {}  // Force a virtual destructor in all descendants

		/// 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 &, const AString & /*a_FileName*/) override {}
		virtual void OnFileData(cHTTPFormParser &, const char * /*a_Data*/, int /*a_Size*/) override {}
		virtual void OnFileEnd(cHTTPFormParser &) override {}
	} ;


	/// Set to true if Init() succeeds and the webadmin isn't to be disabled
	bool m_IsInitialized;

	/// The webadmin.ini file, used for the settings and allowed logins
	cIniFile   m_IniFile;

	PluginList m_Plugins;

	AString m_PortsIPv4;
	AString m_PortsIPv6;

	/// The Lua template script to provide templates:
	cLuaState m_TemplateScript;

	/// The HTTP server which provides the underlying HTTP parsing, serialization and events
	cHTTPServer m_HTTPServer;


	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;
} ; // tolua_export





// Revert MSVC warnings back to orignal state:
#if defined(_MSC_VER)
	#pragma warning(pop)
#endif