src/HTTPDHandler.h
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
#pragma once
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <codecvt>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <regex>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <stdexcept>
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    25
#include <mutex>
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    26
#include <shared_mutex>
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/common/type/typedefs.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/writer/Factory.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <relpipe/cli/RelpipeCLIException.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include "Configuration.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include "HTTPServer.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace relpipe {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
namespace tr {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace httpd {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
class HttpdHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
private:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML is in UTF-8
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	Configuration configuration;
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	std::shared_ptr<HTTPServer> httpServer;
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
	relpipe::common::type::StringX currentRelationName;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	size_t currentAttributeIndex = 0;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	size_t currentRecordNumber = 1;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
	class RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
		std::regex method = std::regex(".*");
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
		std::regex url = std::regex(".*");
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
		virtual ~RequestMatcher() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
		bool matches(const std::string& method, const std::string& url) const {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
			bool result = true;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
			result &= std::regex_match(method, this->method);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
			result &= std::regex_match(url, this->url);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
			return result;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    68
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    69
	};
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    70
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    71
	class HeaderTemplate {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
		std::string name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    74
		std::string value;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
		HeaderTemplate() {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    78
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    79
		HeaderTemplate(std::string name, std::string value) : name(name), value(value) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
		virtual ~HeaderTemplate() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
	};
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    84
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
	class GlobalHeaderTemplate : public HeaderTemplate, public RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
	} headerTemplate;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    87
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    88
	std::vector<GlobalHeaderTemplate> headerTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    89
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    90
	class ResponseTemplate : public RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    91
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    92
		std::string body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    93
		uint16_t code = 200;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    94
		std::vector<HeaderTemplate> headers;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    95
	} responseTemplate;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    96
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    97
	std::vector<ResponseTemplate> responseTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    98
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    99
	std::mutex templatesMutex; // TODO: read-write lock (for responseTemplates and headerTemplates)
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   100
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   101
	class RequestHandler : public HTTPServer::RequestHandler {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   102
	private:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   103
		std::vector<ResponseTemplate>* responseTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   104
		std::vector<GlobalHeaderTemplate>* headerTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   105
		std::mutex* templatesMutex;
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   106
	public:
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   107
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   108
		RequestHandler(std::mutex* templatesMutex, std::vector<ResponseTemplate>* responseTemplates, std::vector<GlobalHeaderTemplate>* headerTemplates) : templatesMutex(templatesMutex), responseTemplates(responseTemplates), headerTemplates(headerTemplates) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   109
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   110
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   111
		virtual ~RequestHandler() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   112
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   113
		const HTTPServer::Response handle(const HTTPServer::Request& request) override {
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
			HTTPServer::Response response;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   116
			std::lock_guard<std::mutex> lock(*templatesMutex);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   117
			for (ResponseTemplate t : *responseTemplates) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   118
				if (t.matches(request.method, request.url)) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   119
					response.code = t.code;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   120
					response.body = t.body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   121
					// TODO: replace global header values with request-specific ones instead of appending?
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   122
					for (const GlobalHeaderTemplate& h : *headerTemplates) if (h.matches(request.method, request.url)) response.header.push_back(HTTPServer::Header(h.name, h.value));
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   123
					for (const HeaderTemplate& h : t.headers) response.header.push_back(HTTPServer::Header(h.name, h.value));
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   124
					return response;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   125
				}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   126
			}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   127
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   128
			response.code = 404;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   129
			response.body = "<h1>HTTP 404: Not Found</h1><p>(no response template matched)</p>";
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   130
			return response;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   131
		}
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   132
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   133
	};
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   134
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   135
	std::shared_ptr<RequestHandler> requestHandler = std::make_shared<RequestHandler>(&templatesMutex, &responseTemplates, &headerTemplates);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   136
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   137
	relpipe::common::type::StringX getHeaderAttributePrefix() {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   138
		// might be configurable - parametrized
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   139
		return L"header.";
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   140
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   141
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   142
	bool isHeaderAttribute(const relpipe::common::type::StringX& attributeName) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   143
		return attributeName.rfind(getHeaderAttributePrefix(), 0) == 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   144
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   145
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   146
	relpipe::common::type::StringX fetchHeaderName(const relpipe::common::type::StringX& attributeName) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   147
		// TODO: recognize several modes: header.set.*, header.add.*, header.remove.*
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   148
		return attributeName.substr(getHeaderAttributePrefix().size());
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   149
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   150
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   151
	void headerTemplateAttribute(const relpipe::common::type::StringX& value) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   152
		auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   153
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   154
		if (attributeName == L"url") headerTemplate.url = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   155
		else if (attributeName == L"method") headerTemplate.method = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   156
		else if (attributeName == L"name") headerTemplate.name = convertor.to_bytes(value);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   157
		else if (attributeName == L"value") headerTemplate.value = convertor.to_bytes(value); // TODO: header encoding?
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   158
		else throw std::invalid_argument("Unsupported attribute in the request_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   159
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   160
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   161
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   162
		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   163
			std::lock_guard<std::mutex> lock(templatesMutex);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   164
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   165
			headerTemplates.push_back(headerTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   166
			headerTemplate = GlobalHeaderTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   167
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   168
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   169
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   170
	void responseTemplateAttribute(const relpipe::common::type::StringX& value) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   171
		auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   172
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   173
		if (attributeName == L"url") responseTemplate.url = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   174
		else if (attributeName == L"method") responseTemplate.method = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   175
		else if (attributeName == L"code") responseTemplate.code = std::stoi(value);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   176
		else if (attributeName == L"text") responseTemplate.body = convertor.to_bytes(value);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   177
		else if (attributeName == L"data") responseTemplate.body = "TODO: read binary data: " + convertor.to_bytes(value); // TODO: read hex/binary request body
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   178
		else if (isHeaderAttribute(attributeName)) responseTemplate.headers.push_back(HeaderTemplate(convertor.to_bytes(fetchHeaderName(attributeName)), convertor.to_bytes(value))); // TODO: header encoding?
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   179
		else throw std::invalid_argument("Unsupported attribute in the request_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   180
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   181
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   182
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   183
		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   184
			std::lock_guard<std::mutex> lock(templatesMutex);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   185
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   186
			responseTemplates.push_back(responseTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   187
			responseTemplate = ResponseTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   188
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   189
	}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   190
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   191
public:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   193
	HttpdHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration, std::shared_ptr<HTTPServer> httpServer) : relationalWriter(relationalWriter), configuration(configuration), httpServer(httpServer) {
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   194
		httpServer->setRequestHandler(requestHandler);
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   196
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
	virtual ~HttpdHandler() {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   201
		currentRelationName = name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   202
		currentReaderMetadata = attributes;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
	void attribute(const relpipe::common::type::StringX& value) override {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   206
		if (currentRelationName == L"header_template") headerTemplateAttribute(value);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   207
		else if (currentRelationName == L"response_template") responseTemplateAttribute(value);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   208
		else throw std::invalid_argument("Unsupported relation: " + convertor.to_bytes(currentRelationName));
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   209
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   210
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   211
	void endOfPipe() {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   212
		sleep(60); // FIXME: run for configured interval or number of requests or forever (until some stop signal)
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   213
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   214
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   215
};
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   216
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   219
}