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
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;
112 explicit HTTPResponse(
struct MHD_Connection *connection):
113 m_connection(connection),
114 m_status_code(MHD_HTTP_OK) {}
116 void Append(
const std::string &data) { m_data.append(data); }
118 void SetHeader(
const std::string &key,
const std::string &value);
119 void SetStatus(
unsigned int status) { m_status_code = status; }
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;
167 std::string data_dir;
191 bool RegisterHandler(
const std::string &path, BaseHTTPCallback *handler);
195 const std::string &content_type);
197 const std::string &file,
198 const std::string &content_type);
202 void Handlers(std::vector<std::string> *handlers)
const;
203 const std::string DataDir()
const {
return m_data_dir; }
212 const std::string &content_type,
215 static const char CONTENT_TYPE_PLAIN[];
216 static const char CONTENT_TYPE_HTML[];
217 static const char CONTENT_TYPE_GIF[];
218 static const char CONTENT_TYPE_PNG[];
219 static const char CONTENT_TYPE_CSS[];
220 static const char CONTENT_TYPE_JS[];
221 static const char CONTENT_TYPE_OCT[];
226 static struct MHD_Response *BuildResponse(
void *data,
size_t size);
230 std::string file_path;
231 std::string content_type;
234 struct DescriptorState {
237 : descriptor(_descriptor), read(0), write(0) {}
245 struct Descriptor_lt {
246 bool operator()(
const DescriptorState *d1,
247 const DescriptorState *d2)
const {
248 return d1->descriptor->ReadDescriptor() <
249 d2->descriptor->ReadDescriptor();
253 typedef std::set<DescriptorState*, Descriptor_lt> SocketSet;
255 struct MHD_Daemon *m_httpd;
256 std::auto_ptr<ola::io::SelectServer> m_select_server;
259 std::map<std::string, BaseHTTPCallback*> m_handlers;
260 std::map<std::string, static_file_info> m_static_content;
261 BaseHTTPCallback *m_default_handler;
263 std::string m_data_dir;
266 HTTPResponse *response);
268 void InsertSocket(
bool is_readable,
bool is_writeable,
int fd);
269 void FreeSocket(DescriptorState *state);
Basic data types used to represent elements in a JSON document.
A 2 argument callback which can be called multiple times.
Definition Callback.h:1895
Definition HTTPServer.h:65
void AddPostParameter(const std::string &key, const std::string &value)
Add a post parameter.
Definition HTTPServer.cpp:269
void AddHeader(const std::string &key, const std::string &value)
Add a header to the request object.
Definition HTTPServer.cpp:256
void ProcessPostData(const char *data, size_t *data_size)
Process post data.
Definition HTTPServer.cpp:284
const std::string GetParameter(const std::string &key) const
Return the value of a url parameter.
Definition HTTPServer.cpp:310
bool CheckParameterExists(const std::string &key) const
Return whether an url parameter exists.
Definition HTTPServer.cpp:326
const std::string GetPostParameter(const std::string &key) const
Lookup a post parameter in this request.
Definition HTTPServer.cpp:349
const std::string GetHeader(const std::string &key) const
Return the value of the header sent with this request.
Definition HTTPServer.cpp:294
Definition HTTPServer.h:110
void SetHeader(const std::string &key, const std::string &value)
Set a header in the response.
Definition HTTPServer.cpp:384
int SendJson(const ola::web::JsonValue &json)
Send a JsonObject as the response.
Definition HTTPServer.cpp:394
int Send()
Send the HTTP response.
Definition HTTPServer.cpp:415
void SetNoCache()
Set the appropriate headers so this response isn't cached.
Definition HTTPServer.cpp:373
void SetContentType(const std::string &type)
Set the content-type header.
Definition HTTPServer.cpp:365
The base HTTP Server.
Definition HTTPServer.h:157
static int ServeRedirect(HTTPResponse *response, const std::string &location)
Serve a redirect.
Definition HTTPServer.cpp:776
void UpdateSockets()
This is run every loop iteration to update the list of sockets in the SelectServer from MHD.
Definition HTTPServer.cpp:550
int ServeError(HTTPResponse *response, const std::string &details="")
Serve an error.
Definition HTTPServer.cpp:744
void * Run()
The entry point into the new thread.
Definition HTTPServer.cpp:504
void RegisterDefaultHandler(BaseHTTPCallback *handler)
Set the default handler.
Definition HTTPServer.cpp:718
bool RegisterFile(const std::string &path, const std::string &content_type)
Register a static file. The root of the URL corresponds to the data dir.
Definition HTTPServer.cpp:676
void HandleHTTPIO()
Definition HTTPServer.h:186
virtual ~HTTPServer()
Destroy this object.
Definition HTTPServer.cpp:452
int DispatchRequest(const HTTPRequest *request, HTTPResponse *response)
Call the appropriate handler.
Definition HTTPServer.cpp:629
int ServeStaticContent(const std::string &path, const std::string &content_type, HTTPResponse *response)
Return the contents of a file.
Definition HTTPServer.cpp:789
void Handlers(std::vector< std::string > *handlers) const
Return a list of all handlers registered.
Definition HTTPServer.cpp:726
bool Init()
Setup the HTTP server.
Definition HTTPServer.cpp:476
bool RegisterHandler(const std::string &path, BaseHTTPCallback *handler)
Register a handler.
Definition HTTPServer.cpp:659
void Stop()
Stop the HTTP server.
Definition HTTPServer.cpp:535
int ServeNotFound(HTTPResponse *response)
Serve a 404.
Definition HTTPServer.cpp:762
HTTPServer(const HTTPServerOptions &options)
Setup the HTTP server.
Definition HTTPServer.cpp:435
A single threaded I/O event management system.
Definition SelectServer.h:63
Allows a FD created by a library to be used with the SelectServer.
Definition Descriptor.h:247
The base class for JSON values.
Definition Json.h:119
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
Definition HTTPServer.h:162