src/HTTPServer.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 01 May 2022 17:58:11 +0200
branchv_0
changeset 9 36479a47faa8
parent 8 90990c8b6aef
permissions -rw-r--r--
headers, cookies, GET parameters: single function
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;
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    59
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    60
			// TODO: return also client IP address etc.
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    61
			// const MHD_ConnectionInfo* connetionInfo = MHD_get_connection_info(connection, MHD_ConnectionInfoType::MHD_CONNECTION_INFO_CLIENT_ADDRESS);
9
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    62
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
			// FIXME: request.body = ...
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
			// FIXME: multiple calls for one request
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    65
			// FIXME: POST parameters
9
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    66
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    67
			// MHD_create_post_processor()
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    68
			// https://git.gnunet.org/libmicrohttpd.git/tree/src/examples/post_example.c
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    69
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    70
			MHD_KeyValueIterator avpVectorAppender = [](void* cls, MHD_ValueKind kind, const char* key, const char* value) {
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    71
				(static_cast<std::vector<AVP>*> (cls))->push_back({key, value});
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    72
				return MHD_YES;
9
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    73
			};
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    74
9
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    75
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_HEADER_KIND, avpVectorAppender, &request.header);
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    76
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_COOKIE_KIND, avpVectorAppender, &request.cookie);
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    77
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_GET_ARGUMENT_KIND, avpVectorAppender, &request.getParameter);
36479a47faa8 headers, cookies, GET parameters: single function
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    78
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_POSTDATA_KIND, avpVectorAppender, &request.postParameter);
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    79
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    80
			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
    81
			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY);
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    82
			for (AVP h : response.header) MHD_add_response_header(mhdResponse, h.name.c_str(), h.value.c_str());
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    83
			// FIXME: cookies: for (AVP c : response.cookie) ...
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    84
			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
    85
			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
    86
			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
    87
		} else {
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    88
			// 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
    89
			// 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
    90
			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
    91
			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
    92
			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
    93
			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
    94
			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
    95
		}
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
	};
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    98
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    99
	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
   100
			options.tcpPort,
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
			acceptCallback, acceptCallbackData,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
			accessCallback, accessCallbackData,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
			MHD_OPTION_THREAD_POOL_SIZE, 10,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
			MHD_OPTION_CONNECTION_TIMEOUT, 60,
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   105
			MHD_OPTION_END);
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   106
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
	if (impl->mhd) return new HTTPServer(impl);
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
	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
   110
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
HTTPServer::~HTTPServer() {
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
	MHD_stop_daemon(impl->mhd);
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
	delete impl;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
}