Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
httpserver.h
Go to the documentation of this file.
1 
18 #ifndef ION_REMOTE_HTTPSERVER_H_
19 #define ION_REMOTE_HTTPSERVER_H_
20 
21 #include <map>
22 #include <string>
23 
24 #include "ion/base/referent.h"
25 #include "ion/port/mutex.h"
26 
27 struct mg_connection;
28 struct mg_context;
29 
30 namespace ion {
31 namespace remote {
32 
33 class ION_API HttpServer {
34  public:
35  typedef std::map<std::string, std::string> QueryMap;
36 
38  class WebsocketHelper;
39  typedef std::map<void*, WebsocketHelper*> WebsocketMap;
40 
45  class Websocket : public base::Referent {
46  public:
47  Websocket() : helper_(NULL) {}
48  ~Websocket() override {}
49 
51  virtual void ConnectionReady() {}
52 
54  virtual int ReceiveData(char* data, size_t data_len, bool is_binary) = 0;
55 
56  protected:
58  void SendData(const char* data, size_t data_len, bool is_binary);
59 
60  private:
61  void SetHelper(WebsocketHelper* helper) { helper_ = helper; }
62  friend class WebsocketHelper;
63  WebsocketHelper* helper_;
64  };
66 
68  class RequestHandler : public base::Referent {
69  public:
81  virtual const std::string HandleRequest(const std::string& path,
82  const QueryMap& args,
83  std::string* content_type) = 0;
84 
86  virtual const WebsocketPtr ConnectWebsocket(const std::string& path,
87  const QueryMap& args) {
88  return WebsocketPtr();
89  }
90 
92  const std::string& GetBasePath() const { return base_path_; }
93 
94  protected:
107  explicit RequestHandler(const std::string& base_path);
108 
110  ~RequestHandler() override;
111 
112  private:
113  const std::string base_path_;
114  };
116  typedef std::map<std::string, RequestHandlerPtr> HandlerMap;
117 
122  HttpServer(int port, int num_threads);
123  virtual ~HttpServer();
124 
127  const std::string GetUriData(const std::string& uri) const;
128 
130  bool IsRunning() const;
131 
134  void RegisterHandler(const RequestHandlerPtr& handler);
136  void UnregisterHandler(const std::string& path);
137 
139  HandlerMap GetHandlers() const;
140 
144  bool EmbedLocalSourcedFiles() const { return embed_local_sourced_files_; }
145  void SetEmbedLocalSourcedFiles(bool embed) {
146  embed_local_sourced_files_ = embed;
147  }
148 
150  size_t WebsocketCount();
151 
156  const std::string& GetFooterHtml() const { return footer_; }
157  const std::string& GetHeaderHtml() const { return header_; }
158  void SetFooterHtml(const std::string& str) { footer_ = str; }
159  void SetHeaderHtml(const std::string& str) { header_ = str; }
160 
161  private:
166  friend class WebsocketHelper;
167  void RegisterWebsocket(void* key, WebsocketHelper* helper);
168  void UnregisterWebsocket(void* key);
169  WebsocketHelper* FindWebsocket(void* key);
170  WebsocketMap websockets_;
171  port::Mutex websocket_mutex_;
172 
173  mg_context* context_;
177  HandlerMap handlers_;
178  mutable ion::port::Mutex handlers_mutex_;
179 
181  std::string header_;
182  std::string footer_;
183 
186  bool embed_local_sourced_files_;
187 };
188 
189 } // namespace remote
190 } // namespace ion
191 
192 #endif // ION_REMOTE_HTTPSERVER_H_
const std::string & str
const std::string & GetHeaderHtml() const
Definition: httpserver.h:157
std::map< std::string, RequestHandlerPtr > HandlerMap
Definition: httpserver.h:116
Represents the server side of a connected Websocket.
Definition: httpserver.h:45
base::ReferentPtr< Websocket >::Type WebsocketPtr
Definition: httpserver.h:65
Thread-safe abstract base class.
Definition: referent.h:49
virtual void ConnectionReady()
Override to take some action when the connection is first established.
Definition: httpserver.h:51
const std::string & GetBasePath() const
Returns the path this handler is registered at.
Definition: httpserver.h:92
const std::string & GetFooterHtml() const
Sets/gets the header and footer HTML, which are both empty by default.
Definition: httpserver.h:156
void SetFooterHtml(const std::string &str)
Definition: httpserver.h:158
base::ReferentPtr< RequestHandler >::Type RequestHandlerPtr
Definition: httpserver.h:115
void SetEmbedLocalSourcedFiles(bool embed)
Definition: httpserver.h:145
bool EmbedLocalSourcedFiles() const
Gets, sets whether local sourced files (tags with src=...
Definition: httpserver.h:144
virtual const WebsocketPtr ConnectWebsocket(const std::string &path, const QueryMap &args)
By default, RequestHandlers don't support websocket connections.
Definition: httpserver.h:86
std::map< std::string, std::string > QueryMap
Definition: httpserver.h:35
RequestHandlers handle requests for a file or path.
Definition: httpserver.h:68
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
void SetHeaderHtml(const std::string &str)
Definition: httpserver.h:159
std::map< void *, WebsocketHelper * > WebsocketMap
Definition: httpserver.h:38