22 #ifndef INCLUDE_OLA_HTTP_HTTPSERVER_H_
23 #define INCLUDE_OLA_HTTP_HTTPSERVER_H_
28 #include <ola/io/SelectServer.h>
29 #include <ola/thread/Thread.h>
36 #define WIN32_LEAN_AND_MEAN
37 #include <ola/win/CleanWinSock2.h>
39 #include <sys/select.h>
40 #include <sys/socket.h>
42 #include <microhttpd.h>
57 const std::string &method,
58 const std::string &version,
59 struct MHD_Connection *connection);
64 const std::string Url()
const {
return m_url; }
65 const std::string Method()
const {
return m_method; }
66 const std::string Version()
const {
return m_version; }
68 void AddHeader(
const std::string &key,
const std::string &value);
71 const std::string
GetHeader(
const std::string &key)
const;
73 const std::string
GetParameter(
const std::string &key)
const;
76 bool InFlight()
const {
return m_in_flight; }
77 void SetInFlight() { m_in_flight =
true; }
82 std::string m_version;
83 struct MHD_Connection *m_connection;
84 std::map<std::string, std::string> m_headers;
85 std::map<std::string, std::string> m_post_params;
86 struct MHD_PostProcessor *m_processor;
89 static const unsigned int K_POST_BUFFER_SIZE = 1024;
100 explicit HTTPResponse(
struct MHD_Connection *connection):
101 m_connection(connection),
102 m_status_code(MHD_HTTP_OK) {}
104 void Append(
const std::string &data) { m_data.append(data); }
106 void SetHeader(
const std::string &key,
const std::string &value);
107 void SetStatus(
unsigned int status) { m_status_code = status; }
111 struct MHD_Connection *Connection()
const {
return m_connection; }
114 struct MHD_Connection *m_connection;
115 typedef std::multimap<std::string, std::string> HeadersMultiMap;
116 HeadersMultiMap m_headers;
117 unsigned int m_status_code;
154 std::string data_dir;
178 bool RegisterHandler(
const std::string &path, BaseHTTPCallback *handler);
182 const std::string &content_type);
184 const std::string &file,
185 const std::string &content_type);
189 void Handlers(std::vector<std::string> *handlers)
const;
190 const std::string DataDir()
const {
return m_data_dir; }
199 const std::string &content_type,
202 static const char CONTENT_TYPE_PLAIN[];
203 static const char CONTENT_TYPE_HTML[];
204 static const char CONTENT_TYPE_GIF[];
205 static const char CONTENT_TYPE_PNG[];
206 static const char CONTENT_TYPE_CSS[];
207 static const char CONTENT_TYPE_JS[];
214 std::string file_path;
215 std::string content_type;
218 struct DescriptorState {
221 : descriptor(descriptor), read(0), write(0) {}
229 struct Descriptor_lt {
230 bool operator()(
const DescriptorState *d1,
231 const DescriptorState *d2)
const {
232 return d1->descriptor->ReadDescriptor() <
233 d2->descriptor->ReadDescriptor();
237 typedef std::set<DescriptorState*, Descriptor_lt> SocketSet;
239 struct MHD_Daemon *m_httpd;
243 std::map<std::string, BaseHTTPCallback*> m_handlers;
244 std::map<std::string, static_file_info> m_static_content;
245 BaseHTTPCallback *m_default_handler;
247 std::string m_data_dir;
250 HTTPResponse *response);
252 void InsertSocket(
bool is_readable,
bool is_writeable,
int fd);
253 void FreeSocket(DescriptorState *state);
259 #endif // INCLUDE_OLA_HTTP_HTTPSERVER_H_