34 #ifndef INCLUDE_OLA_HTTP_HTTPSERVER_H_
35 #define INCLUDE_OLA_HTTP_HTTPSERVER_H_
38 #include <ola/io/Descriptor.h>
39 #include <ola/io/SelectServer.h>
40 #include <ola/thread/Thread.h>
41 #include <ola/web/Json.h>
46 #include <sys/select.h>
47 #include <sys/socket.h>
48 #include <microhttpd.h>
69 const string &version,
70 struct MHD_Connection *connection);
75 const string Url()
const {
return m_url; }
76 const string Method()
const {
return m_method; }
77 const string Version()
const {
return m_version; }
79 void AddHeader(
const string &key,
const string &value);
80 void AddPostParameter(
const string &key,
const string &value);
81 void ProcessPostData(
const char *data,
size_t *data_size);
82 const string GetHeader(
const string &key)
const;
84 const string GetParameter(
const string &key)
const;
85 const string GetPostParameter(
const string &key)
const;
87 bool InFlight()
const {
return m_in_flight; }
88 void SetInFlight() { m_in_flight =
true; }
94 struct MHD_Connection *m_connection;
95 map<string, string> m_headers;
96 map<string, string> m_post_params;
97 struct MHD_PostProcessor *m_processor;
100 static const unsigned int K_POST_BUFFER_SIZE = 1024;
109 explicit HTTPResponse(
struct MHD_Connection *connection):
110 m_connection(connection),
111 m_status_code(MHD_HTTP_OK) {}
113 void Append(
const string &data) { m_data.append(data); }
114 void SetContentType(
const string &type);
115 void SetHeader(
const string &key,
const string &value);
116 void SetStatus(
unsigned int status) { m_status_code = status; }
120 struct MHD_Connection *Connection()
const {
return m_connection; }
123 struct MHD_Connection *m_connection;
124 multimap<string, string> m_headers;
125 unsigned int m_status_code;
166 bool RegisterHandler(
const string &path, BaseHTTPCallback *handler);
169 bool RegisterFile(
const string &path,
170 const string &content_type);
171 bool RegisterFile(
const string &path,
173 const string &content_type);
175 void RegisterDefaultHandler(BaseHTTPCallback *handler);
177 void Handlers(vector<string> *handlers)
const;
178 const string DataDir()
const {
return m_data_dir; }
181 int ServeError(
HTTPResponse *response,
const string &details=
"");
183 static int ServeRedirect(
HTTPResponse *response,
const string &location);
187 const string &content_type,
190 static const char CONTENT_TYPE_PLAIN[];
191 static const char CONTENT_TYPE_HTML[];
192 static const char CONTENT_TYPE_GIF[];
193 static const char CONTENT_TYPE_PNG[];
194 static const char CONTENT_TYPE_CSS[];
195 static const char CONTENT_TYPE_JS[];
209 struct MHD_Daemon *m_httpd;
213 map<string, BaseHTTPCallback*> m_handlers;
214 map<string, static_file_info> m_static_content;
215 BaseHTTPCallback *m_default_handler;
220 HTTPServer(
const HTTPServer&);
221 HTTPServer& operator=(
const HTTPServer&);
224 HTTPResponse *response);
232 #endif // INCLUDE_OLA_HTTP_HTTPSERVER_H_