author | František Kučera <franta-hg@frantovo.cz> |
Fri, 08 Apr 2022 22:38:45 +0200 | |
branch | v_0 |
changeset 3 | 1184f3de5533 |
parent 2 | 4b05b16b97e6 |
child 4 | 37a86904145c |
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 |
||
3
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
40 |
void* acceptCallbackData = impl; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
41 |
MHD_AcceptPolicyCallback acceptCallback = [](void* rawImpl, const struct sockaddr* addr, socklen_t addrlen) { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
42 |
if (rawImpl) { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
43 |
HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
44 |
// TODO: call impl if present and has such method |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
45 |
return MHD_YES; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
46 |
} else { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
47 |
return MHD_YES; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
48 |
} |
1 | 49 |
}; |
0 | 50 |
|
3
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
51 |
void* accessCallbackData = impl; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
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) { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
54 |
// TODO: return also HTTP headers |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
if (impl->requestHandler) { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
HTTPServer::Request request; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
const HTTPServer::Response response = impl->requestHandler->handle(request); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
MHD_queue_response(connection, response.code, mhdResponse); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
MHD_destroy_response(mhdResponse); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
return MHD_YES; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
} else { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
// there might be a point in time when the HTTP server is started and HTTP handler is not set |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
// TODO: just return MHD_NO? |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
static const char body[] = "<h1>HTTP 404: Not found</h1>"; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
MHD_queue_response(connection, 404, mhdResponse); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
MHD_destroy_response(mhdResponse); |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
return MHD_YES; |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
} |
1 | 71 |
}; |
72 |
||
73 |
||
74 |
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
|
75 |
options.tcpPort, |
1 | 76 |
acceptCallback, acceptCallbackData, |
77 |
accessCallback, accessCallbackData, |
|
78 |
MHD_OPTION_THREAD_POOL_SIZE, 10, |
|
79 |
MHD_OPTION_CONNECTION_TIMEOUT, 60, |
|
80 |
MHD_OPTION_END); |
|
81 |
||
82 |
||
83 |
if (impl->mhd) return new HTTPServer(impl); |
|
84 |
else throw std::logic_error("Unable to start MHD."); |
|
0 | 85 |
} |
86 |
||
87 |
HTTPServer::~HTTPServer() { |
|
1 | 88 |
MHD_stop_daemon(impl->mhd); |
89 |
delete impl; |
|
0 | 90 |
} |
91 |
||
92 |
||
93 |
} |
|
94 |
} |
|
95 |
} |