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

/**
 * Relational pipes
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <microhttpd.h>
#include <stdexcept>

#include "HTTPServer.h"

namespace relpipe {
namespace tr {
namespace httpd {

class HTTPServer::HTTPServerImpl {
public:
	MHD_Daemon* mhd = nullptr;
	std::shared_ptr<RequestHandler> requestHandler;
};

void HTTPServer::setRequestHandler(std::shared_ptr<RequestHandler> handler) {
	impl->requestHandler = handler;
}

HTTPServer* HTTPServer::create(HTTPServer::Options options) {
	HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl();

	void* acceptCallbackData = impl;
	MHD_AcceptPolicyCallback acceptCallback = [](void* rawImpl, const struct sockaddr* addr, socklen_t addrlen) {
		if (rawImpl) {
			HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl);
			// TODO: call impl if present and has such method
			return MHD_YES;
		} else {
			return MHD_YES;
		}
	};

	void* accessCallbackData = impl;
	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) {
		HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl);
		// TODO: return also HTTP headers
		if (impl->requestHandler) {
			HTTPServer::Request request;
			request.method = method;
			request.url = url;

			// TODO: return also client IP address etc.
			// const MHD_ConnectionInfo* connetionInfo = MHD_get_connection_info(connection, MHD_ConnectionInfoType::MHD_CONNECTION_INFO_CLIENT_ADDRESS);

			// FIXME: request.body = ...
			// FIXME: multiple calls for one request
			// FIXME: POST parameters

			// MHD_create_post_processor()
			// https://git.gnunet.org/libmicrohttpd.git/tree/src/examples/post_example.c

			MHD_KeyValueIterator avpVectorAppender = [](void* cls, MHD_ValueKind kind, const char* key, const char* value) {
				(static_cast<std::vector<AVP>*> (cls))->push_back({key, value});
				return MHD_YES;
			};

			MHD_get_connection_values(connection, MHD_ValueKind::MHD_HEADER_KIND, avpVectorAppender, &request.header);
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_COOKIE_KIND, avpVectorAppender, &request.cookie);
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_GET_ARGUMENT_KIND, avpVectorAppender, &request.getParameter);
			MHD_get_connection_values(connection, MHD_ValueKind::MHD_POSTDATA_KIND, avpVectorAppender, &request.postParameter);

			const HTTPServer::Response response = impl->requestHandler->handle(request);
			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY);
			for (AVP h : response.header) MHD_add_response_header(mhdResponse, h.name.c_str(), h.value.c_str());
			// FIXME: cookies: for (AVP c : response.cookie) ...
			MHD_queue_response(connection, response.code, mhdResponse);
			MHD_destroy_response(mhdResponse);
			return MHD_YES;
		} else {
			// there might be a point in time when the HTTP server is started and HTTP handler is not set
			// TODO: just return MHD_NO?
			static const char body[] = "<h1>HTTP 503: Service Unavailable</h1><p>not fully started yet</p>";
			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT);
			MHD_queue_response(connection, 503, mhdResponse);
			MHD_destroy_response(mhdResponse);
			return MHD_YES;
		}
	};


	impl->mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD,
			options.tcpPort,
			acceptCallback, acceptCallbackData,
			accessCallback, accessCallbackData,
			MHD_OPTION_THREAD_POOL_SIZE, 10,
			MHD_OPTION_CONNECTION_TIMEOUT, 60,
			MHD_OPTION_END);


	if (impl->mhd) return new HTTPServer(impl);
	else throw std::logic_error("Unable to start MHD.");
}

HTTPServer::~HTTPServer() {
	MHD_stop_daemon(impl->mhd);
	delete impl;
}


}
}
}