14 * You should have received a copy of the GNU General Public License |
14 * You should have received a copy of the GNU General Public License |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 */ |
16 */ |
17 |
17 |
18 #include <microhttpd.h> |
18 #include <microhttpd.h> |
|
19 #include <stdexcept> |
19 |
20 |
20 #include "HTTPServer.h" |
21 #include "HTTPServer.h" |
21 |
22 |
22 namespace relpipe { |
23 namespace relpipe { |
23 namespace tr { |
24 namespace tr { |
24 namespace httpd { |
25 namespace httpd { |
25 |
26 |
26 class HTTPServer::HTTPServerImpl { |
27 class HTTPServer::HTTPServerImpl { |
|
28 public: |
|
29 MHD_Daemon* mhd = nullptr; |
27 }; |
30 }; |
28 |
31 |
29 HTTPServer* HTTPServer::create() { |
32 HTTPServer* HTTPServer::create() { |
30 HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl(); |
33 HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl(); |
31 |
34 |
|
35 void* acceptCallbackData = nullptr; |
|
36 MHD_AcceptPolicyCallback acceptCallback = [](void* cls, const struct sockaddr* addr, socklen_t addrlen) { |
|
37 return MHD_YES; |
|
38 }; |
32 |
39 |
33 return new HTTPServer(impl); |
40 void* accessCallbackData = nullptr; |
|
41 MHD_AccessHandlerCallback accessCallback = [](void* cls, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { |
|
42 static const char html[] = "<p>Hello, <b>relational</b> world!</p>"; |
|
43 struct MHD_Response* response = MHD_create_response_from_buffer(sizeof (html), (void*) html, MHD_RESPMEM_PERSISTENT); |
|
44 MHD_queue_response(connection, 200, response); |
|
45 MHD_destroy_response(response); |
|
46 return MHD_YES; |
|
47 }; |
|
48 |
|
49 |
|
50 impl->mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, |
|
51 8080, |
|
52 acceptCallback, acceptCallbackData, |
|
53 accessCallback, accessCallbackData, |
|
54 MHD_OPTION_THREAD_POOL_SIZE, 10, |
|
55 MHD_OPTION_CONNECTION_TIMEOUT, 60, |
|
56 MHD_OPTION_END); |
|
57 |
|
58 |
|
59 if (impl->mhd) return new HTTPServer(impl); |
|
60 else throw std::logic_error("Unable to start MHD."); |
34 } |
61 } |
35 |
62 |
36 HTTPServer::~HTTPServer() { |
63 HTTPServer::~HTTPServer() { |
37 |
64 MHD_stop_daemon(impl->mhd); |
|
65 delete impl; |
38 } |
66 } |
39 |
67 |
40 |
68 |
41 } |
69 } |
42 } |
70 } |