Open Lighting Architecture
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/http/HTTPServer.h>
28 #include <string>
29 
30 namespace ola {
31 namespace http {
32 
33 using ola::ExportMap;
34 using std::string;
35 using ola::NewCallback;
36 
37 /*
38  * A HTTP Server with ExportMap support. You can inherit from this class to
39  * implement specific handlers.
40  */
42  public:
44  ExportMap *export_map);
45  virtual ~OlaHTTPServer() {}
46 
47  virtual bool Init();
48  bool Start() { return m_server.Start(); }
49  void Stop() { return m_server.Stop(); }
50 
51  protected:
52  Clock m_clock;
53  ExportMap *m_export_map;
54  HTTPServer m_server;
55  TimeStamp m_start_time;
56 
60  void RegisterFile(const string &file, const string &content_type) {
61  m_server.RegisterFile("/" + file, file, content_type);
62  }
63 
64  private:
65  static const char K_DATA_DIR_VAR[];
66  static const char K_UPTIME_VAR[];
67 
68  inline void RegisterHandler(
69  const string &path,
70  int (OlaHTTPServer::*method)(const HTTPRequest*, HTTPResponse*)) {
71  m_server.RegisterHandler(
72  path,
73  NewCallback<OlaHTTPServer, int, const HTTPRequest*, HTTPResponse*>(
74  this,
75  method));
76  }
77 
78  int DisplayDebug(const HTTPRequest *request, HTTPResponse *response);
79  int DisplayHandlers(const HTTPRequest *request, HTTPResponse *response);
80 };
81 } // namespace http
82 } // namespace ola
83 #endif // INCLUDE_OLA_HTTP_OLAHTTPSERVER_H_