src/HTTPDHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 Apr 2022 02:43:50 +0200
branchv_0
changeset 5 121981e6bd54
parent 4 37a86904145c
child 6 b04bde9083d4
permissions -rw-r--r--
generate some output relations
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>
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    27
#include <iomanip>
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    28
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    29
#include <uuid/uuid.h>
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/common/type/typedefs.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/TypeId.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/reader/handlers/AttributeMetadata.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include <relpipe/writer/Factory.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include <relpipe/cli/RelpipeCLIException.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
#include "Configuration.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
#include "HTTPServer.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace relpipe {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace tr {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace httpd {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
class HttpdHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
private:
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    49
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encoding than UTF-8
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    51
	std::mutex relationalWriterMutex;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	Configuration configuration;
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
	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
    54
	relpipe::common::type::StringX currentRelationName;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	size_t currentAttributeIndex = 0;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	size_t currentRecordNumber = 1;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
	class RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
		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
    63
		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
    64
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
		virtual ~RequestMatcher() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
		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
    68
			bool result = true;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    69
			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
    70
			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
    71
			return result;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
	};
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    74
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
	class HeaderTemplate {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
		std::string name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    78
		std::string value;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    79
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
		HeaderTemplate() {
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
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
		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
    84
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
		virtual ~HeaderTemplate() = default;
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
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    89
	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
    90
	} headerTemplate;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    91
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    92
	std::vector<GlobalHeaderTemplate> headerTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    93
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    94
	class ResponseTemplate : public RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    95
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    96
		std::string body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    97
		uint16_t code = 200;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    98
		std::vector<HeaderTemplate> headers;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    99
	} responseTemplate;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   100
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   101
	std::vector<ResponseTemplate> responseTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   102
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   103
	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
   104
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   105
	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
   106
	private:
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   107
		shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   108
		std::mutex* relationalWriterMutex;
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   109
		std::vector<ResponseTemplate>* responseTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   110
		std::vector<GlobalHeaderTemplate>* headerTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   111
		std::mutex* templatesMutex;
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   112
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   113
		void writeExchange(const HTTPServer::Request& request, const HTTPServer::Response& response) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   114
			std::wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encoding than UTF-8
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   115
			std::lock_guard<std::mutex> lock(*relationalWriterMutex);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   116
			
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   117
			auto exchangeId = convertor.from_bytes(generateExchangeId());
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   118
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   119
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   120
			// TODO: multiple modes:
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   121
			// a) interleaved (current)
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   122
			// b) write exchanges immediatelly + cache headers and flush them at the end
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   123
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   124
			// TODO: data types
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   125
			// TODO: support multiple encodings
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   126
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   127
			relationalWriter->startRelation(L"exchange",{
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   128
				{L"id", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   129
				{L"url", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   130
				{L"method", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   131
				{L"request_text", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   132
				{L"request_data", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   133
				{L"response_text", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   134
				{L"response_data", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   135
				{L"response_code", relpipe::writer::TypeId::INTEGER},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   136
			}, true);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   137
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   138
			relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   139
			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   140
			relationalWriter->writeAttribute(convertor.from_bytes(request.method));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   141
			relationalWriter->writeAttribute(bodyToText(convertor, request.body));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   142
			relationalWriter->writeAttribute(bodyToHex(convertor, request.body));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   143
			relationalWriter->writeAttribute(bodyToText(convertor, response.body));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   144
			relationalWriter->writeAttribute(bodyToHex(convertor, response.body));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   145
			relationalWriter->writeAttribute(std::to_wstring(response.code));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   146
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   147
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   148
			relationalWriter->startRelation(L"header",{
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   149
				{L"exchange", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   150
				{L"url", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   151
				{L"direction", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   152
				{L"name", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   153
				{L"value", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   154
			}, true);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   155
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   156
			for (const HTTPServer::Header& h : request.header) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   157
				relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   158
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   159
				relationalWriter->writeAttribute(L"request");
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   160
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   161
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   162
			}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   163
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   164
			for (const HTTPServer::Header& h : response.header) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   165
				relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   166
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   167
				relationalWriter->writeAttribute(L"response");
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   168
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   169
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   170
			}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   171
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   172
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   173
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   174
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   175
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   176
		relpipe::common::type::StringX bodyToText(std::wstring_convert < codecvt_utf8<wchar_t>>&convertor, const std::string& body, bool* validEncoding = nullptr) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   177
			try {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   178
				if (validEncoding) *validEncoding = true;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   179
				// TODO: use encoding from the HTTP response headers instead of the constant one?
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   180
				return convertor.from_bytes(body);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   181
			} catch (...) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   182
				if (validEncoding) *validEncoding = false;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   183
				std::stringstream filtered;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   184
				for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   185
				return convertor.from_bytes(filtered.str());
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   186
			}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   187
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   188
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   189
		relpipe::common::type::StringX bodyToHex(std::wstring_convert < codecvt_utf8<wchar_t>>&convertor, const std::string& body) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   190
			std::stringstream hex;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   191
			hex << std::hex << std::setfill('0') << std::hex;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   192
			for (size_t i = 0, size = body.size(); i < size; i++) hex << std::setw(2) << (0xff & body[i]);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   193
			return convertor.from_bytes(hex.str());
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   194
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   195
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   196
		std::string generateExchangeId() {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   197
			char buffer[37];
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   198
			uuid_t uuid;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   199
			uuid_generate_random(uuid);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   200
			// uuid_generate_time(uuid);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   201
			uuid_unparse_lower(uuid, buffer);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   202
			return buffer;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   203
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   204
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   205
	public:
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   206
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   207
		RequestHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, std::mutex* relationalWriterMutex, std::vector<ResponseTemplate>* responseTemplates, std::vector<GlobalHeaderTemplate>* headerTemplates, std::mutex* templatesMutex) :
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   208
		relationalWriter(relationalWriter), relationalWriterMutex(relationalWriterMutex), responseTemplates(responseTemplates), headerTemplates(headerTemplates), templatesMutex(templatesMutex) {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   209
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   210
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   211
		virtual ~RequestHandler() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   212
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   213
		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
   214
			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
   215
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   216
			response.code = 404;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   217
			response.body = "<h1>HTTP 404: Not Found</h1><p>(no response template matched)</p>";
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   218
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   219
			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
   220
			for (ResponseTemplate t : *responseTemplates) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   221
				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
   222
					response.code = t.code;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   223
					response.body = t.body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   224
					// 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
   225
					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
   226
					for (const HeaderTemplate& h : t.headers) response.header.push_back(HTTPServer::Header(h.name, h.value));
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   227
					break;
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   228
				}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   229
			}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   230
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   231
			writeExchange(request, response);
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   232
			return response;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   233
		}
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   234
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   235
	};
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   236
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   237
	std::shared_ptr<RequestHandler> requestHandler = std::make_shared<RequestHandler>(relationalWriter, &relationalWriterMutex, &responseTemplates, &headerTemplates, &templatesMutex);
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   238
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   239
	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
   240
		// might be configurable - parametrized
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   241
		return L"header.";
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   242
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   243
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   244
	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
   245
		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
   246
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   247
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   248
	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
   249
		// 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
   250
		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
   251
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   252
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   253
	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
   254
		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
   255
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   256
		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
   257
		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
   258
		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
   259
		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
   260
		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
   261
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   262
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   263
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   264
		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
   265
			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
   266
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   267
			headerTemplates.push_back(headerTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   268
			headerTemplate = GlobalHeaderTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   269
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   270
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   271
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   272
	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
   273
		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
   274
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   275
		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
   276
		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
   277
		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
   278
		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
   279
		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
   280
		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
   281
		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
   282
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   283
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   284
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   285
		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
   286
			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
   287
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   288
			responseTemplates.push_back(responseTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   289
			responseTemplate = ResponseTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   290
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   291
	}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   292
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   293
public:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   294
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   295
	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
   296
		httpServer->setRequestHandler(requestHandler);
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   297
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   298
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   299
	virtual ~HttpdHandler() {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   300
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   301
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
	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
   303
		currentRelationName = name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   304
		currentReaderMetadata = attributes;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   306
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   307
	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
   308
		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
   309
		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
   310
		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
   311
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   312
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   313
	void endOfPipe() {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   314
		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
   315
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   316
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
};
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   318
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   319
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   320
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   321
}