src/HTTPServer.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 09 Apr 2022 17:50:46 +0200
branchv_0
changeset 4 37a86904145c
parent 3 1184f3de5533
child 5 121981e6bd54
permissions -rw-r--r--
partial implementation of response and header templates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <microhttpd.h>
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    19
#include <stdexcept>
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include "HTTPServer.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
namespace relpipe {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
namespace tr {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace httpd {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
class HTTPServer::HTTPServerImpl {
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    28
public:
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    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
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
};
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl();
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	};
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
	};
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    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
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
			acceptCallback, acceptCallbackData,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
			accessCallback, accessCallbackData,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
			MHD_OPTION_THREAD_POOL_SIZE, 10,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
			MHD_OPTION_CONNECTION_TIMEOUT, 60,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
			MHD_OPTION_END);
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
	if (impl->mhd) return new HTTPServer(impl);
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
	else throw std::logic_error("Unable to start MHD.");
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
HTTPServer::~HTTPServer() {
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
	MHD_stop_daemon(impl->mhd);
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
	delete impl;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
}