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(std::move(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 
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 
232 
236 
240 
244 
247 
250 
253 
256 
257 
261  bool LoadIniFile(void);
262 
264  bool HasUsers();
265 
267  void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
268 
270  void HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
271 
273  void HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request);
274 
275  // cHTTPServer::cCallbacks overrides:
276  virtual void OnRequestBegun (cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request) override;
277  virtual void OnRequestBody (cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request, const char * a_Data, size_t a_Size) override;
278  virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPIncomingRequest & a_Request) override;
279 } ; // tolua_export
280 
281 
282 
283 
284 
285 // Revert MSVC warnings back to orignal state:
286 #if defined(_MSC_VER)
287  #pragma warning(pop)
288 #endif
289 
290 
291 
292 
AString StringsConcat(const AStringVector &a_Strings, char a_Separator)
Concatenates the specified strings into a single string, separated by the specified separator charact...
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
Definition: FastNBT.h:132
Encapsulates a Lua state and provides some syntactic sugar for common operations.
Definition: LuaState.h:56
Provides storage for an incoming HTTP request.
Definition: HTTPMessage.h:90
std::string Value
Definition: WebAdmin.h:31
std::string Name
Definition: WebAdmin.h:30
std::string Type
Definition: WebAdmin.h:32
FormDataMap FormData
Same as PostParams.
Definition: WebAdmin.h:66
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
AString URL
The entire URL presented to the HTTP server.
Definition: WebAdmin.h:46
AString Method
HTTP method used for the request ("GET", "POST" etc.)
Definition: WebAdmin.h:49
std::map< std::string, std::string > StringStringMap
Definition: WebAdmin.h:42
StringStringMap Params
Parameters given in the URL, after the questionmark.
Definition: WebAdmin.h:60
AString Path
The Path part of the request's URL (excluding GET params).
Definition: WebAdmin.h:52
AString Username
Name of the logged-in user.
Definition: WebAdmin.h:55
std::map< std::string, HTTPFormData > FormDataMap
Definition: WebAdmin.h:43
HTTPRequest Request
Definition: WebAdmin.h:76
AString PluginName
Definition: WebAdmin.h:87
AString TabUrlPath
Definition: WebAdmin.h:89
AString Content
Definition: WebAdmin.h:86
AString ContentType
Definition: WebAdmin.h:90
AString TabTitle
Definition: WebAdmin.h:88
virtual void OnRequestBody(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request, const char *a_Data, size_t a_Size) override
Called when another part of request body has arrived.
Definition: WebAdmin.cpp:655
cLuaState m_TemplateScript
The Lua template script to provide templates.
Definition: WebAdmin.h:235
void Stop(void)
Stops the HTTP server, if it was started.
Definition: WebAdmin.cpp:128
static AString GetContentTypeFromFileExt(const AString &a_FileExtension)
Returns the content type from the file extension.
Definition: WebAdmin.cpp:439
AStringVector m_Ports
The ports on which the webadmin is running.
Definition: WebAdmin.h:252
std::shared_ptr< cWebTab > cWebTabPtr
Definition: WebAdmin.h:147
cIniFile m_IniFile
The webadmin.ini file, used for the settings and allowed logins.
Definition: WebAdmin.h:243
void HandleFileRequest(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)
Handles requests for a file.
Definition: WebAdmin.cpp:387
virtual void OnRequestBegun(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request) override
Called when a new request arrives over a connection and all its headers have been parsed.
Definition: WebAdmin.cpp:631
cCriticalSection m_CS
Protects m_WebTabs, m_TemplateScript, m_LoginTemplate and m_IniFile against multithreaded access.
Definition: WebAdmin.h:227
void HandleWebadminRequest(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)
Handles requests coming to the "/webadmin" or "/~webadmin" URLs.
Definition: WebAdmin.cpp:275
virtual void OnRequestFinished(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request) override
Called when the request body has been fully received in previous calls to OnRequestBody()
Definition: WebAdmin.cpp:670
bool DelWebTab(const AString &a_UrlPath)
Removes the WebTab with the specified URL path.
Definition: WebAdmin.cpp:553
std::vector< cWebTabPtr > cWebTabPtrs
Definition: WebAdmin.h:148
bool LoadIniFile(void)
Loads webadmin.ini into m_IniFile.
Definition: WebAdmin.cpp:234
void HandleRootRequest(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)
Handles requests for the root page.
Definition: WebAdmin.cpp:372
bool LoadLoginPage(void)
Loads the login template into m_LoginPage.
Definition: WebAdmin.cpp:144
bool Start(void)
Starts the HTTP server taking care of the webadmin.
Definition: WebAdmin.cpp:110
bool Init(void)
Initializes the object.
Definition: WebAdmin.cpp:81
cWebAdmin(void)
Definition: WebAdmin.cpp:61
bool m_IsRunning
Set to true if Start() succeeds in starting the server, reset back to false in Stop().
Definition: WebAdmin.h:249
virtual ~cWebAdmin() override
Definition: WebAdmin.cpp:72
bool HasUsers()
Checks inside the webadmin.ini file if there are users configured.
Definition: WebAdmin.cpp:258
void RemoveAllPluginWebTabs(const AString &a_PluginName)
Removes all WebTabs registered by the specified plugin.
Definition: WebAdmin.cpp:167
static AString GetURLEncodedString(const AString &a_Input)
Escapes the string for use in an URL Exported to Lua in ManualBindings.cpp.
Definition: WebAdmin.cpp:604
AString GetPorts(void) const
Returns the list of ports on which the webadmin is configured to listen.
Definition: WebAdmin.h:186
void Reload(void)
Reloads m_IniFile, m_LoginPage and m_TemplateScript.
Definition: WebAdmin.cpp:182
void AddWebTab(const AString &a_Title, const AString &a_UrlPath, const AString &a_PluginName, std::shared_ptr< cWebTabCallback > a_Callback)
Adds a new WebTab handler.
Definition: WebAdmin.cpp:538
cWebTabPtrs m_WebTabs
All registered WebTab handlers.
Definition: WebAdmin.h:231
cHTTPServer m_HTTPServer
The HTTP server which provides the underlying HTTP parsing, serialization and events.
Definition: WebAdmin.h:255
static AString GetBaseURL(const AString &a_URL)
Returns the prefix needed for making a link point to the webadmin root from the given URL ("....
Definition: WebAdmin.cpp:529
cWebTabPtrs GetAllWebTabs(void)
Returns a copy of all the registered web tabs.
Definition: WebAdmin.h:169
static AString GetHTMLEscapedString(const AString &a_Input)
Escapes text passed into it, so it can be embedded into html.
Definition: WebAdmin.cpp:573
bool m_IsInitialized
Set to true if Init() succeeds and the webadmin isn't to be disabled.
Definition: WebAdmin.h:246
sWebAdminPage GetPage(const HTTPRequest &a_Request)
Returns the (inner) page contents for the specified request.
Definition: WebAdmin.cpp:478
AString m_LoginPage
The HTML page that provides the login.
Definition: WebAdmin.h:239
Container for a single web tab.
Definition: WebAdmin.h:132
AString m_UrlPath
Definition: WebAdmin.h:135
AString m_PluginName
Definition: WebAdmin.h:136
cWebTab(const AString &a_Title, const AString &a_UrlPath, const AString &a_PluginName, std::shared_ptr< cWebTabCallback > a_Callback)
Definition: WebAdmin.h:139
std::shared_ptr< cWebTabCallback > m_Callback
Definition: WebAdmin.h:137