Cuberite
A lightweight, fast and extensible game server for Minecraft
WebAdmin.h
Go to the documentation of this file.
1 
2 // WebAdmin.h
3 
4 // Declares the cWebAdmin class representing the admin interface over http protocol, and related services (API)
5 
6 #pragma once
7 
8 #include "Bindings/LuaState.h"
9 #include "IniFile.h"
10 #include "HTTP/HTTPServer.h"
11 #include "HTTP/HTTPMessage.h"
12 
13 
14 
15 
16 
17 // Disable MSVC warnings:
18 #if defined(_MSC_VER)
19  #pragma warning(push)
20  #pragma warning(disable:4355) // 'this' : used in base member initializer list
21 #endif
22 
23 
24 
25 
26 
27 // tolua_begin
29 {
30  std::string Name;
31  std::string Value;
32  std::string Type;
33 } ;
34 // tolua_end
35 
36 
37 
38 
39 // tolua_begin
41 {
42  typedef std::map< std::string, std::string > StringStringMap;
43  typedef std::map< std::string, HTTPFormData > FormDataMap;
44 
47 
50 
53 
56 
57  // tolua_end
58 
60  StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
61 
63  StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
64 
66  FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
67 } ; // tolua_export
68 
69 
70 
71 
72 
73 // tolua_begin
75 {
77 } ;
78 // tolua_end
79 
80 
81 
82 
83 
85 {
91 };
92 
93 
94 
95 
96 
97 // tolua_begin
98 class cWebAdmin :
99  // tolua_end
101  // tolua_begin
102 {
103 public:
104  // tolua_end
105 
107  class cWebTabCallback abstract
108  {
109  public:
110  // Force a virtual destructor in descendants
111  virtual ~cWebTabCallback() {}
112 
119  virtual bool Call(
120  const HTTPRequest & a_Request,
121  const AString & a_UrlPath,
122  AString & a_Content,
123  AString & a_ContentType
124  ) = 0;
125  };
126 
127 
131  class cWebTab
132  {
133  public:
137  std::shared_ptr<cWebTabCallback> m_Callback;
138 
139  cWebTab(const AString & a_Title, const AString & a_UrlPath, const AString & a_PluginName, std::shared_ptr<cWebTabCallback> a_Callback):
140  m_Title(a_Title),
141  m_UrlPath(a_UrlPath),
142  m_PluginName(a_PluginName),
143  m_Callback(a_Callback)
144  {
145  }
146  };
147  typedef std::shared_ptr<cWebTab> cWebTabPtr;
148  typedef std::vector<cWebTabPtr> cWebTabPtrs;
149 
150 
151  cWebAdmin(void);
152  virtual ~cWebAdmin() override;
153 
155  bool Init(void);
156 
158  bool Start(void);
159 
161  void Stop(void);
162 
165  bool LoadLoginPage(void);
166 
169  cWebTabPtrs GetAllWebTabs(void) { return m_WebTabs; }
170 
172  void RemoveAllPluginWebTabs(const AString & a_PluginName);
173 
177  sWebAdminPage GetPage(const HTTPRequest & a_Request);
178 
179  // tolua_begin
180 
183  void Reload(void);
184 
186  AString GetPorts(void) const { return StringsConcat(m_Ports, ','); }
187 
189  static AString GetBaseURL(const AString & a_URL);
190 
193  static AString GetContentTypeFromFileExt(const AString & a_FileExtension);
194 
196  static AString GetHTMLEscapedString(const AString & a_Input);
197  // tolua_end
198 
205  void AddWebTab(
206  const AString & a_Title,
207  const AString & a_UrlPath,
208  const AString & a_PluginName,
209  std::shared_ptr<cWebTabCallback> a_Callback
210  );
211 
215  bool DelWebTab(const AString & a_UrlPath);
216 
219  static AString GetURLEncodedString(const AString & a_Input);
220 
222  static AString GetBaseURL(const AStringVector & a_URLSplit);
223 
224 protected:
225 
228 
231  cWebTabPtrs m_WebTabs;
232 
236 
240 
244 
247 
250 
253 
256 
257 
261  bool LoadIniFile(void);
262 
264  void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
265 
267  void HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
268 
270  void HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
271 
272  // cHTTPServer::cCallbacks overrides:
273  virtual void OnRequestBegun (cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request) override;
274  virtual void OnRequestBody (cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request, const char * a_Data, size_t a_Size) override;
275  virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request) override;
276 } ; // tolua_export
277 
278 
279 
280 
281 
282 // Revert MSVC warnings back to orignal state:
283 #if defined(_MSC_VER)
284  #pragma warning(pop)
285 #endif
286 
287 
288 
289 
HTTPRequest Request
Definition: WebAdmin.h:76
AString m_PluginName
Definition: WebAdmin.h:136
AString URL
The entire URL presented to the HTTP server.
Definition: WebAdmin.h:46
AString PluginName
Definition: WebAdmin.h:87
AString StringsConcat(const AStringVector &a_Strings, char a_Separator)
Concatenates the specified strings into a single string, separated by the specified separator charact...
StringStringMap PostParams
Parameters posted as a part of a form - either in the URL (GET method) or in the body (POST method) ...
Definition: WebAdmin.h:63
Provides storage for an incoming HTTP request.
Definition: HTTPMessage.h:85
std::map< std::string, HTTPFormData > FormDataMap
Definition: WebAdmin.h:43
cWebTab(const AString &a_Title, const AString &a_UrlPath, const AString &a_PluginName, std::shared_ptr< cWebTabCallback > a_Callback)
Definition: WebAdmin.h:139
std::map< std::string, std::string > StringStringMap
Definition: WebAdmin.h:42
AString m_UrlPath
Definition: WebAdmin.h:135
Encapsulates a Lua state and provides some syntactic sugar for common operations. ...
Definition: LuaState.h:57
AString TabUrlPath
Definition: WebAdmin.h:89
AString Username
Name of the logged-in user.
Definition: WebAdmin.h:55
cIniFile m_IniFile
The webadmin.ini file, used for the settings and allowed logins.
Definition: WebAdmin.h:243
Container for a single web tab.
Definition: WebAdmin.h:131
AString Method
HTTP method used for the request ("GET", "POST" etc.)
Definition: WebAdmin.h:49
AString GetPorts(void) const
Returns the list of ports on which the webadmin is configured to listen.
Definition: WebAdmin.h:186
std::vector< AString > AStringVector
Definition: StringUtils.h:14
cWebTabPtrs GetAllWebTabs(void)
Returns a copy of all the registered web tabs.
Definition: WebAdmin.h:169
cWebTabPtrs m_WebTabs
All registered WebTab handlers.
Definition: WebAdmin.h:231
cLuaState m_TemplateScript
The Lua template script to provide templates.
Definition: WebAdmin.h:235
std::string Type
Definition: WebAdmin.h:32
AString TabTitle
Definition: WebAdmin.h:88
AString ContentType
Definition: WebAdmin.h:90
std::string Name
Definition: WebAdmin.h:30
cHTTPServer m_HTTPServer
The HTTP server which provides the underlying HTTP parsing, serialization and events.
Definition: WebAdmin.h:255
std::shared_ptr< cWebTab > cWebTabPtr
Definition: WebAdmin.h:147
std::string AString
Definition: StringUtils.h:13
AString Content
Definition: WebAdmin.h:86
std::shared_ptr< cWebTabCallback > m_Callback
Definition: WebAdmin.h:137
FormDataMap FormData
Same as PostParams.
Definition: WebAdmin.h:66
AString Path
The Path part of the request&#39;s URL (excluding GET params).
Definition: WebAdmin.h:52
std::vector< cWebTabPtr > cWebTabPtrs
Definition: WebAdmin.h:148
bool m_IsRunning
Set to true if Start() succeeds in starting the server, reset back to false in Stop().
Definition: WebAdmin.h:249
AString m_LoginPage
The HTML page that provides the login.
Definition: WebAdmin.h:239
bool m_IsInitialized
Set to true if Init() succeeds and the webadmin isn&#39;t to be disabled.
Definition: WebAdmin.h:246
StringStringMap Params
Parameters given in the URL, after the questionmark.
Definition: WebAdmin.h:60
cCriticalSection m_CS
Protects m_WebTabs, m_TemplateScript, m_LoginTemplate and m_IniFile against multithreaded access...
Definition: WebAdmin.h:227
std::string Value
Definition: WebAdmin.h:31
AStringVector m_Ports
The ports on which the webadmin is running.
Definition: WebAdmin.h:252