author | František Kučera <franta-hg@frantovo.cz> |
Thu, 07 Apr 2022 23:04:12 +0200 | |
branch | v_0 |
changeset 2 | 4b05b16b97e6 |
parent 1 | 23c516259cc5 |
child 3 | 1184f3de5533 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
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/>. |
|
16 |
*/ |
|
17 |
||
18 |
#include <microhttpd.h> |
|
1 | 19 |
#include <stdexcept> |
0 | 20 |
|
21 |
#include "HTTPServer.h" |
|
22 |
||
23 |
namespace relpipe { |
|
24 |
namespace tr { |
|
25 |
namespace httpd { |
|
26 |
||
27 |
class HTTPServer::HTTPServerImpl { |
|
1 | 28 |
public: |
29 |
MHD_Daemon* mhd = nullptr; |
|
2
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
30 |
std::shared_ptr<RequestHandler> requestHandler; |
0 | 31 |
}; |
32 |
||
2
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
33 |
void HTTPServer::setRequestHandler(std::shared_ptr<RequestHandler> handler) { |
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
34 |
impl->requestHandler = handler; |
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
35 |
} |
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
36 |
|
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
37 |
HTTPServer* HTTPServer::create(HTTPServer::Options options) { |
0 | 38 |
HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl(); |
39 |
||
1 | 40 |
void* acceptCallbackData = nullptr; |
41 |
MHD_AcceptPolicyCallback acceptCallback = [](void* cls, const struct sockaddr* addr, socklen_t addrlen) { |
|
42 |
return MHD_YES; |
|
43 |
}; |
|
0 | 44 |
|
1 | 45 |
void* accessCallbackData = nullptr; |
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) { |
|
47 |
static const char html[] = "<p>Hello, <b>relational</b> world!</p>"; |
|
48 |
struct MHD_Response* response = MHD_create_response_from_buffer(sizeof (html), (void*) html, MHD_RESPMEM_PERSISTENT); |
|
49 |
MHD_queue_response(connection, 200, response); |
|
50 |
MHD_destroy_response(response); |
|
51 |
return MHD_YES; |
|
52 |
}; |
|
53 |
||
54 |
||
55 |
impl->mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, |
|
2
4b05b16b97e6
configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
56 |
options.tcpPort, |
1 | 57 |
acceptCallback, acceptCallbackData, |
58 |
accessCallback, accessCallbackData, |
|
59 |
MHD_OPTION_THREAD_POOL_SIZE, 10, |
|
60 |
MHD_OPTION_CONNECTION_TIMEOUT, 60, |
|
61 |
MHD_OPTION_END); |
|
62 |
||
63 |
||
64 |
if (impl->mhd) return new HTTPServer(impl); |
|
65 |
else throw std::logic_error("Unable to start MHD."); |
|
0 | 66 |
} |
67 |
||
68 |
HTTPServer::~HTTPServer() { |
|
1 | 69 |
MHD_stop_daemon(impl->mhd); |
70 |
delete impl; |
|
0 | 71 |
} |
72 |
||
73 |
||
74 |
} |
|
75 |
} |
|
76 |
} |