35 } |
35 } |
36 |
36 |
37 HTTPServer* HTTPServer::create(HTTPServer::Options options) { |
37 HTTPServer* HTTPServer::create(HTTPServer::Options options) { |
38 HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl(); |
38 HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl(); |
39 |
39 |
40 void* acceptCallbackData = nullptr; |
40 void* acceptCallbackData = impl; |
41 MHD_AcceptPolicyCallback acceptCallback = [](void* cls, const struct sockaddr* addr, socklen_t addrlen) { |
41 MHD_AcceptPolicyCallback acceptCallback = [](void* rawImpl, const struct sockaddr* addr, socklen_t addrlen) { |
42 return MHD_YES; |
42 if (rawImpl) { |
|
43 HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl); |
|
44 // TODO: call impl if present and has such method |
|
45 return MHD_YES; |
|
46 } else { |
|
47 return MHD_YES; |
|
48 } |
43 }; |
49 }; |
44 |
50 |
45 void* accessCallbackData = nullptr; |
51 void* accessCallbackData = impl; |
46 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) { |
52 MHD_AccessHandlerCallback accessCallback = [](void* rawImpl, 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) { |
47 static const char html[] = "<p>Hello, <b>relational</b> world!</p>"; |
53 HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl); |
48 struct MHD_Response* response = MHD_create_response_from_buffer(sizeof (html), (void*) html, MHD_RESPMEM_PERSISTENT); |
54 // TODO: return also HTTP headers |
49 MHD_queue_response(connection, 200, response); |
55 if (impl->requestHandler) { |
50 MHD_destroy_response(response); |
56 HTTPServer::Request request; |
51 return MHD_YES; |
57 const HTTPServer::Response response = impl->requestHandler->handle(request); |
|
58 struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY); |
|
59 MHD_queue_response(connection, response.code, mhdResponse); |
|
60 MHD_destroy_response(mhdResponse); |
|
61 return MHD_YES; |
|
62 } else { |
|
63 // there might be a point in time when the HTTP server is started and HTTP handler is not set |
|
64 // TODO: just return MHD_NO? |
|
65 static const char body[] = "<h1>HTTP 404: Not found</h1>"; |
|
66 struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT); |
|
67 MHD_queue_response(connection, 404, mhdResponse); |
|
68 MHD_destroy_response(mhdResponse); |
|
69 return MHD_YES; |
|
70 } |
52 }; |
71 }; |
53 |
72 |
54 |
73 |
55 impl->mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, |
74 impl->mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, |
56 options.tcpPort, |
75 options.tcpPort, |