author | František Kučera <franta-hg@frantovo.cz> |
Sat, 09 Apr 2022 17:50:46 +0200 | |
branch | v_0 |
changeset 4 | 37a86904145c |
parent 3 | 1184f3de5533 |
child 5 | 121981e6bd54 |
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; |
4
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
57 |
request.method = method; |
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
58 |
request.url = url; |
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
59 |
// FIXME: request.body = ... |
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
60 |
// FIXME: multiple calls for one request |
3
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
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
|
62 |
struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY); |
4
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
63 |
for (Header h : response.header) MHD_add_response_header(mhdResponse, h.name.c_str(), h.value.c_str()); |
3
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
} else { |
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
// 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
|
69 |
// TODO: just return MHD_NO? |
4
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
70 |
static const char body[] = "<h1>HTTP 503: Service Unavailable</h1><p>not fully started yet</p>"; |
3
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT); |
4
37a86904145c
partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
72 |
MHD_queue_response(connection, 503, mhdResponse); |
3
1184f3de5533
return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
} |
1 | 76 |
}; |
77 |
||
78 |
||
79 |
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
|
80 |
options.tcpPort, |
1 | 81 |
acceptCallback, acceptCallbackData, |
82 |
accessCallback, accessCallbackData, |
|
83 |
MHD_OPTION_THREAD_POOL_SIZE, 10, |
|
84 |
MHD_OPTION_CONNECTION_TIMEOUT, 60, |
|
85 |
MHD_OPTION_END); |
|
86 |
||
87 |
||
88 |
if (impl->mhd) return new HTTPServer(impl); |
|
89 |
else throw std::logic_error("Unable to start MHD."); |
|
0 | 90 |
} |
91 |
||
92 |
HTTPServer::~HTTPServer() { |
|
1 | 93 |
MHD_stop_daemon(impl->mhd); |
94 |
delete impl; |
|
0 | 95 |
} |
96 |
||
97 |
||
98 |
} |
|
99 |
} |
|
100 |
} |