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> 53 #if MHD_VERSION >= 0x00097002 54 #define MHD_RESULT enum MHD_Result 56 #define MHD_RESULT int 68 const std::string &method,
69 const std::string &version,
70 struct MHD_Connection *connection);
75 const std::string Url()
const {
return m_url; }
76 const std::string Method()
const {
return m_method; }
77 const std::string Version()
const {
return m_version; }
79 void AddHeader(
const std::string &key,
const std::string &value);
82 const std::string
GetHeader(
const std::string &key)
const;
84 const std::string
GetParameter(
const std::string &key)
const;
87 bool InFlight()
const {
return m_in_flight; }
88 void SetInFlight() { m_in_flight =
true; }
93 std::string m_version;
94 struct MHD_Connection *m_connection;
95 std::map<std::string, std::string> m_headers;
96 std::map<std::string, std::string> m_post_params;
97 struct MHD_PostProcessor *m_processor;
100 static const unsigned int K_POST_BUFFER_SIZE = 1024;
111 explicit HTTPResponse(
struct MHD_Connection *connection):
112 m_connection(connection),
113 m_status_code(MHD_HTTP_OK) {}
115 void Append(
const std::string &data) { m_data.append(data); }
116 void SetContentType(
const std::string &type);
117 void SetHeader(
const std::string &key,
const std::string &value);
118 void SetStatus(
unsigned int status) { m_status_code = status; }
120 void SetAccessControlAllowOriginAll();
123 struct MHD_Connection *Connection()
const {
return m_connection; }
126 struct MHD_Connection *m_connection;
127 typedef std::multimap<std::string, std::string> HeadersMultiMap;
128 HeadersMultiMap m_headers;
129 unsigned int m_status_code;
166 std::string data_dir;
179 void UpdateSockets();
190 bool RegisterHandler(
const std::string &path, BaseHTTPCallback *handler);
193 bool RegisterFile(
const std::string &path,
194 const std::string &content_type);
195 bool RegisterFile(
const std::string &path,
196 const std::string &file,
197 const std::string &content_type);
199 void RegisterDefaultHandler(BaseHTTPCallback *handler);
201 void Handlers(std::vector<std::string> *handlers)
const;
202 const std::string DataDir()
const {
return m_data_dir; }
205 int ServeError(
HTTPResponse *response,
const std::string &details =
"");
207 static int ServeRedirect(
HTTPResponse *response,
const std::string &location);
210 int ServeStaticContent(
const std::string &path,
211 const std::string &content_type,
214 static const char CONTENT_TYPE_PLAIN[];
215 static const char CONTENT_TYPE_HTML[];
216 static const char CONTENT_TYPE_GIF[];
217 static const char CONTENT_TYPE_PNG[];
218 static const char CONTENT_TYPE_ICO[];
219 static const char CONTENT_TYPE_CSS[];
220 static const char CONTENT_TYPE_JS[];
221 static const char CONTENT_TYPE_OCT[];
222 static const char CONTENT_TYPE_XML[];
223 static const char CONTENT_TYPE_JSON[];
228 static struct MHD_Response *BuildResponse(
void *data,
size_t size);
232 std::string file_path;
233 std::string content_type;
236 struct DescriptorState {
239 : descriptor(_descriptor), read(0), write(0) {}
247 struct Descriptor_lt {
248 bool operator()(
const DescriptorState *d1,
249 const DescriptorState *d2)
const {
250 return d1->descriptor->ReadDescriptor() <
251 d2->descriptor->ReadDescriptor();
255 typedef std::set<DescriptorState*, Descriptor_lt> SocketSet;
257 struct MHD_Daemon *m_httpd;
258 std::auto_ptr<ola::io::SelectServer> m_select_server;
261 std::map<std::string, BaseHTTPCallback*> m_handlers;
262 std::map<std::string, static_file_info> m_static_content;
263 BaseHTTPCallback *m_default_handler;
265 std::string m_data_dir;
267 int ServeStaticContent(static_file_info *file_info,
270 void InsertSocket(
bool is_readable,
bool is_writeable,
int fd);
271 void FreeSocket(DescriptorState *state);
277 #endif // INCLUDE_OLA_HTTP_HTTPSERVER_H_ The base HTTP Server.
Definition: HTTPServer.h:156
The base class for JSON values.
Definition: Json.h:119
void AddHeader(const std::string &key, const std::string &value)
Add a header to the request object.
Definition: HTTPServer.cpp:258
const std::string GetPostParameter(const std::string &key) const
Lookup a post parameter in this request.
Definition: HTTPServer.cpp:351
const std::string GetHeader(const std::string &key) const
Return the value of the header sent with this request.
Definition: HTTPServer.cpp:296
const std::string GetParameter(const std::string &key) const
Return the value of a url parameter.
Definition: HTTPServer.cpp:312
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: HTTPServer.h:65
Definition: HTTPServer.h:161
void ProcessPostData(const char *data, size_t *data_size)
Process post data.
Definition: HTTPServer.cpp:286
bool CheckParameterExists(const std::string &key) const
Return whether an url parameter exists.
Definition: HTTPServer.cpp:328
Definition: HTTPServer.h:109
A single threaded I/O event management system.
Definition: SelectServer.h:63
A 2 argument callback which can be called multiple times.
Definition: Callback.h:1895
Allows a FD created by a library to be used with the SelectServer.
Definition: Descriptor.h:247
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
void AddPostParameter(const std::string &key, const std::string &value)
Add a post parameter.
Definition: HTTPServer.cpp:271
Basic data types used to represent elements in a JSON document.
void HandleHTTPIO()
Definition: HTTPServer.h:185