Open Lighting Architecture  0.9.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OlaHTTPServer.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * OlaHTTPServer.h
17  * A HTTP Server with export map integration.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 
22 #ifndef INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_
23 #define INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_
24 
25 #include <ola/Clock.h>
26 #include <ola/ExportMap.h>
27 #include <ola/base/Macro.h>
28 #include <ola/http/HTTPServer.h>
29 #include <string>
30 
31 namespace ola {
32 namespace http {
33 
34 /*
35  * A HTTP Server with ExportMap support. You can inherit from this class to
36  * implement specific handlers.
37  */
39  public:
41  ola::ExportMap *export_map);
42  virtual ~OlaHTTPServer() {}
43 
44  virtual bool Init();
45  bool Start() { return m_server.Start(); }
46  void Stop() { return m_server.Stop(); }
47 
48  protected:
49  Clock m_clock;
50  ola::ExportMap *m_export_map;
51  HTTPServer m_server;
52  TimeStamp m_start_time;
53 
57  void RegisterFile(const std::string &file,
58  const std::string &content_type) {
59  m_server.RegisterFile("/" + file, file, content_type);
60  }
61 
62  private:
63  static const char K_DATA_DIR_VAR[];
64  static const char K_UPTIME_VAR[];
65 
66  inline void RegisterHandler(
67  const std::string &path,
68  int (OlaHTTPServer::*method)(const HTTPRequest*, HTTPResponse*)) {
69  m_server.RegisterHandler(
70  path,
72  int,
73  const HTTPRequest*,
74  HTTPResponse*>(
75  this,
76  method));
77  }
78 
79  int DisplayDebug(const HTTPRequest *request, HTTPResponse *response);
80  int DisplayHandlers(const HTTPRequest *request, HTTPResponse *response);
81 
82  DISALLOW_COPY_AND_ASSIGN(OlaHTTPServer);
83 };
84 } // namespace http
85 } // namespace ola
86 #endif // INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_