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[];
213 static struct MHD_Response *BuildResponse(
void *data,
size_t size);
217 std::string file_path;
218 std::string content_type;
221 struct DescriptorState {
224 : descriptor(_descriptor), read(0), write(0) {}
232 struct Descriptor_lt {
233 bool operator()(
const DescriptorState *d1,
234 const DescriptorState *d2)
const {
235 return d1->descriptor->ReadDescriptor() <
236 d2->descriptor->ReadDescriptor();
240 typedef std::set<DescriptorState*, Descriptor_lt> SocketSet;
242 struct MHD_Daemon *m_httpd;
243 std::auto_ptr<ola::io::SelectServer> m_select_server;
246 std::map<std::string, BaseHTTPCallback*> m_handlers;
247 std::map<std::string, static_file_info> m_static_content;
248 BaseHTTPCallback *m_default_handler;
250 std::string m_data_dir;
253 HTTPResponse *response);
255 void InsertSocket(
bool is_readable,
bool is_writeable,
int fd);
256 void FreeSocket(DescriptorState *state);
262 #endif // INCLUDE_OLA_HTTP_HTTPSERVER_H_
The base HTTP Server.
Definition: HTTPServer.h:144
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:248
HTTPServer(const HTTPServerOptions &options)
Setup the HTTP server.
Definition: HTTPServer.cpp:422
int ServeStaticContent(const std::string &path, const std::string &content_type, HTTPResponse *response)
Return the contents of a file.
Definition: HTTPServer.cpp:768
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
int ServeNotFound(HTTPResponse *response)
Serve a 404.
Definition: HTTPServer.cpp:741
int ServeError(HTTPResponse *response, const std::string &details="")
Serve an error.
Definition: HTTPServer.cpp:723
Definition: HTTPServer.h:54
const std::string GetHeader(const std::string &key) const
Return the value of the header sent with this request.
Definition: HTTPServer.cpp:286
Definition: HTTPServer.h:149
void SetContentType(const std::string &type)
Set the content-type header.
Definition: HTTPServer.cpp:354
bool Init()
Setup the HTTP server.
Definition: HTTPServer.cpp:462
void * Run()
The entry point into the new thread.
Definition: HTTPServer.cpp:490
void SetNoCache()
Set the appropriate headers so this response isn't cached.
Definition: HTTPServer.cpp:362
void ProcessPostData(const char *data, size_t *data_size)
Process post data.
Definition: HTTPServer.cpp:276
Definition: HTTPServer.h:98
int SendJson(const ola::web::JsonValue &json)
Send a JsonObject as the response.
Definition: HTTPServer.cpp:383
void Stop()
Stop the HTTP server.
Definition: HTTPServer.cpp:521
bool CheckParameterExists(const std::string &key) const
Return whether an url parameter exists.
Definition: HTTPServer.cpp:316
A single threaded I/O event management system.
Definition: SelectServer.h:63
virtual ~HTTPServer()
Destroy this object.
Definition: HTTPServer.cpp:439
bool RegisterHandler(const std::string &path, BaseHTTPCallback *handler)
Register a handler.
Definition: HTTPServer.cpp:642
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:658
int DispatchRequest(const HTTPRequest *request, HTTPResponse *response)
Call the appropriate handler.
Definition: HTTPServer.cpp:615
static int ServeRedirect(HTTPResponse *response, const std::string &location)
Serve a redirect.
Definition: HTTPServer.cpp:755
A 2 argument callback which can be called multiple times.
Definition: Callback.h:1895
const std::string GetParameter(const std::string &key) const
Return the value of a url parameter.
Definition: HTTPServer.cpp:301
void Handlers(std::vector< std::string > *handlers) const
Return a list of all handlers registered.
Definition: HTTPServer.cpp:707
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:261
void RegisterDefaultHandler(BaseHTTPCallback *handler)
Set the default handler.
Definition: HTTPServer.cpp:699
int Send()
Send the HTTP response.
Definition: HTTPServer.cpp:403
Basic data types used to represent elements in a JSON document.
void HandleHTTPIO()
Definition: HTTPServer.h:173
void UpdateSockets()
This is run every loop iteration to update the list of sockets in the SelectServer from MHD...
Definition: HTTPServer.cpp:536
const std::string GetPostParameter(const std::string &key) const
Lookup a post parameter in this request.
Definition: HTTPServer.cpp:339
void SetHeader(const std::string &key, const std::string &value)
Set a header in the response.
Definition: HTTPServer.cpp:373