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[];
208 static const char CONTENT_TYPE_OCT[];
215 std::string file_path;
216 std::string content_type;
219 struct DescriptorState {
222 : descriptor(descriptor), read(0), write(0) {}
230 struct Descriptor_lt {
231 bool operator()(
const DescriptorState *d1,
232 const DescriptorState *d2)
const {
233 return d1->descriptor->ReadDescriptor() <
234 d2->descriptor->ReadDescriptor();
238 typedef std::set<DescriptorState*, Descriptor_lt> SocketSet;
240 struct MHD_Daemon *m_httpd;
241 std::auto_ptr<ola::io::SelectServer> m_select_server;
244 std::map<std::string, BaseHTTPCallback*> m_handlers;
245 std::map<std::string, static_file_info> m_static_content;
246 BaseHTTPCallback *m_default_handler;
248 std::string m_data_dir;
251 HTTPResponse *response);
253 void InsertSocket(
bool is_readable,
bool is_writeable,
int fd);
254 void FreeSocket(DescriptorState *state);
260 #endif // INCLUDE_OLA_HTTP_HTTPSERVER_H_