20 #ifndef HTTPSERVER_H_AIH108EG
21 #define HTTPSERVER_H_AIH108EG
31 #include "event2/util.h"
32 #include <event2/http.h>
38 #include "uscxml/config.h"
43 class WebSocketServlet;
51 struct evhttp_request* evhttpReq;
54 return evhttpReq != NULL;
61 std::string privateKey;
62 std::string publicKey;
70 struct evws_connection* evwsConn;
75 Reply() : status(200), type(
"get"), evhttpReq(NULL) {}
76 Reply(
Request req) : status(200), type(req.data.compound[
"type"].atom), evhttpReq(req.evhttpReq) {}
79 type = req.data.compound[
"type"].atom;
80 evhttpReq = req.evhttpReq;
85 std::map<std::string, std::string> headers;
87 struct evhttp_request* evhttpReq;
92 evhttp_request* httpReq;
101 static HTTPServer* getInstance(
unsigned short port,
unsigned short wsPort, SSLConfig* sslConf = NULL);
103 return getInstance(0, 0, NULL);
106 static std::string getBaseURL(ServerType type = HTTP);
108 static void reply(
const Reply& reply);
109 static void wsSend(
struct evws_connection *conn,
enum evws_opcode opcode,
const char *data, uint64_t length);
110 static void wsBroadcast(
const char *uri,
enum evws_opcode opcode,
const char *data, uint64_t length);
112 static bool registerServlet(
const std::string& path, HTTPServlet* servlet);
113 static void unregisterServlet(HTTPServlet* servlet);
115 static bool registerServlet(
const std::string& path, WebSocketServlet* servlet);
116 static void unregisterServlet(WebSocketServlet* servlet);
122 WSData(
struct evws_connection *conn_,
const char *uri_,
enum evws_opcode opcode_,
const char *data_, uint64_t length_) {
127 data = std::string(data_, length_);
129 struct evws_connection *conn;
135 struct comp_strsize_less {
136 bool operator()(std::string
const& l, std::string
const& r)
const {
137 if (l.size() < r.size())
144 HTTPServer(
unsigned short port,
unsigned short wsPort, SSLConfig* sslConf);
145 virtual ~HTTPServer();
149 static void run(
void* instance);
151 void determineAddress();
153 static void replyCallback(evutil_socket_t fd,
short what,
void *arg);
154 static void wsSendCallback(evutil_socket_t fd,
short what,
void *arg);
156 static void httpRecvReqCallback(
struct evhttp_request *req,
void *callbackData);
157 static void wsRecvReqCallback(
struct evws_connection *conn,
struct evws_frame *,
void *callbackData);
159 void processByMatchingServlet(
const Request& request);
160 void processByMatchingServlet(evws_connection* conn,
const WSFrame& frame);
162 static std::map<std::string, std::string> mimeTypes;
163 std::map<std::string, HTTPServlet*> _httpServlets;
164 typedef std::map<std::string, HTTPServlet*>::iterator http_servlet_iter_t;
166 std::map<std::string, WebSocketServlet*> _wsServlets;
167 typedef std::map<std::string, WebSocketServlet*>::iterator ws_servlet_iter_t;
169 struct event_base* _base;
170 struct evhttp* _http;
173 struct evhttp_bound_socket* _httpHandle;
174 evutil_socket_t _wsHandle;
176 unsigned short _port;
177 unsigned short _wsPort;
178 std::string _address;
180 static HTTPServer* _instance;
182 static std::recursive_mutex _instanceMutex;
183 std::thread* _thread;
184 std::recursive_mutex _mutex;
187 friend class HTTPServlet;
188 friend class WebSocketServlet;
190 #if (defined EVENT_SSL_FOUND && defined OPENSSL_FOUND && defined OPENSSL_HAS_ELIPTIC_CURVES)
191 struct evhttp* _https;
192 struct evhttp_bound_socket* _sslHandle;
193 unsigned short _sslPort;
195 static struct bufferevent* sslBufferEventCallback(
struct event_base *base,
void *arg);
196 static void sslGeneralBufferEventCallback (
struct evhttp_request *req,
void *arg);
204 virtual void setURL(
const std::string& url) = 0;
213 virtual bool requestFromWS(
struct evws_connection *conn,
const HTTPServer::WSFrame& frame) = 0;
214 virtual void setURL(
const std::string& url) = 0;
Definition: HTTPServer.h:210
virtual bool canAdaptPath()
Called by the server with the actual URL.
Definition: HTTPServer.h:205
Definition: HTTPServer.h:66
Definition: HTTPServer.h:45
Definition: Breakpoint.cpp:26
Definition: HTTPServer.h:73
Definition: HTTPServer.h:47
Definition: HTTPServer.h:90
Definition: HTTPServer.h:200
Definition: HTTPServer.h:58
virtual bool canAdaptPath()
Called by the server with the actual URL.
Definition: HTTPServer.h:215