src/HTTPDHandler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Apr 2022 00:23:08 +0200
branchv_0
changeset 6 b04bde9083d4
parent 5 121981e6bd54
child 7 315d985f8424
permissions -rw-r--r--
add request and response sizes
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
	size_t currentAttributeIndex = 0;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	size_t currentRecordNumber = 1;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
	class RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
		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
    62
		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
    63
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
		virtual ~RequestMatcher() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
		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
    67
			bool result = true;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    68
			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
    69
			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
    70
			return result;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    71
		}
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
	class HeaderTemplate {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
		std::string name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
		std::string value;
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() {
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
		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
    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
		virtual ~HeaderTemplate() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
	};
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
	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
    89
	} headerTemplate;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    90
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    91
	std::vector<GlobalHeaderTemplate> headerTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    92
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    93
	class ResponseTemplate : public RequestMatcher {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    94
	public:
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    95
		std::string body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    96
		uint16_t code = 200;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    97
		std::vector<HeaderTemplate> headers;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    98
	} responseTemplate;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    99
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   100
	std::vector<ResponseTemplate> responseTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   101
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   102
	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
   103
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   104
	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
   105
	private:
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   106
		shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   107
		std::mutex* relationalWriterMutex;
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   108
		std::vector<ResponseTemplate>* responseTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   109
		std::vector<GlobalHeaderTemplate>* headerTemplates;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   110
		std::mutex* templatesMutex;
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   111
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   112
		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
   113
			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
   114
			std::lock_guard<std::mutex> lock(*relationalWriterMutex);
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   115
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   116
			auto exchangeId = convertor.from_bytes(generateExchangeId());
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   117
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
			// TODO: multiple modes:
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   120
			// a) interleaved (current)
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   121
			// 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
   122
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   123
			// TODO: data types
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   124
			// TODO: support multiple encodings
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   125
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   126
			relationalWriter->startRelation(L"exchange",{
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   127
				// TODO: timestamp
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   128
				// TODO: ordinal number?
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   129
				{L"id", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   130
				{L"url", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   131
				{L"method", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   132
				{L"request_text", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   133
				{L"request_data", relpipe::writer::TypeId::STRING},
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   134
				{L"request_size", relpipe::writer::TypeId::INTEGER},
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   135
				{L"response_text", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   136
				{L"response_data", relpipe::writer::TypeId::STRING},
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   137
				{L"response_size", relpipe::writer::TypeId::INTEGER},
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   138
				{L"response_code", relpipe::writer::TypeId::INTEGER},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   139
			}, true);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   140
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   141
			relpipe::common::type::Integer requestSize = request.body.size();
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   142
			relpipe::common::type::Integer responseSize = response.body.size();
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   143
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   144
			relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   145
			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   146
			relationalWriter->writeAttribute(convertor.from_bytes(request.method));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   147
			relationalWriter->writeAttribute(bodyToText(convertor, request.body));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   148
			relationalWriter->writeAttribute(bodyToHex(convertor, request.body));
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   149
			relationalWriter->writeAttribute(&requestSize, typeid (requestSize));
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   150
			relationalWriter->writeAttribute(bodyToText(convertor, response.body));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   151
			relationalWriter->writeAttribute(bodyToHex(convertor, response.body));
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   152
			relationalWriter->writeAttribute(&responseSize, typeid (responseSize));
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   153
			relationalWriter->writeAttribute(std::to_wstring(response.code));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   154
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
			relationalWriter->startRelation(L"header",{
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   157
				// TODO: ordinal number?
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   158
				{L"exchange", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   159
				{L"url", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   160
				{L"direction", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   161
				{L"name", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   162
				{L"value", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   163
			}, true);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   164
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   165
			for (const HTTPServer::Header& h : request.header) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   166
				relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   167
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   168
				relationalWriter->writeAttribute(L"request");
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   169
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   170
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
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
			for (const HTTPServer::Header& h : response.header) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   174
				relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   175
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   176
				relationalWriter->writeAttribute(L"response");
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   177
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   178
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   179
			}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   180
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   181
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   182
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   183
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   184
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   185
		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
   186
			try {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   187
				if (validEncoding) *validEncoding = true;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   188
				// 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
   189
				return convertor.from_bytes(body);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   190
			} catch (...) {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   191
				if (validEncoding) *validEncoding = false;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   192
				std::stringstream filtered;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   193
				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
   194
				return convertor.from_bytes(filtered.str());
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
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   197
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   198
		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
   199
			std::stringstream hex;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   200
			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
   201
			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
   202
			return convertor.from_bytes(hex.str());
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
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   205
		std::string generateExchangeId() {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   206
			char buffer[37];
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   207
			uuid_t uuid;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   208
			uuid_generate_random(uuid);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   209
			// uuid_generate_time(uuid);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   210
			uuid_unparse_lower(uuid, buffer);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   211
			return buffer;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   212
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   213
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   214
	public:
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
		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
   217
		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
   218
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   219
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   220
		virtual ~RequestHandler() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   221
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   222
		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
   223
			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
   224
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   225
			response.code = 404;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   226
			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
   227
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   228
			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
   229
			for (ResponseTemplate t : *responseTemplates) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   230
				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
   231
					response.code = t.code;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   232
					response.body = t.body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   233
					// 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
   234
					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
   235
					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
   236
					break;
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   237
				}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   238
			}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   239
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   240
			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
   241
			return response;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   242
		}
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   243
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   244
	};
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   245
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   246
	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
   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 getHeaderAttributePrefix() {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   249
		// might be configurable - parametrized
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   250
		return L"header.";
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
	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
   254
		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
   255
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   256
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   257
	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
   258
		// 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
   259
		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
   260
	}
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
	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
   263
		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
   264
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   265
		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
   266
		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
   267
		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
   268
		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
   269
		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
   270
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   271
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   272
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   273
		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
   274
			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
   275
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   276
			headerTemplates.push_back(headerTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   277
			headerTemplate = GlobalHeaderTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   278
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   279
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   280
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   281
	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
   282
		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
   283
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   284
		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
   285
		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
   286
		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
   287
		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
   288
		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
   289
		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
   290
		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
   291
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   292
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   293
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   294
		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
   295
			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
   296
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   297
			responseTemplates.push_back(responseTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   298
			responseTemplate = ResponseTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   299
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   300
	}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   301
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
public:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   303
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   304
	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
   305
		httpServer->setRequestHandler(requestHandler);
0
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
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   308
	virtual ~HttpdHandler() {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   309
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   310
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   311
	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
   312
		currentRelationName = name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   313
		currentReaderMetadata = attributes;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   314
	}
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
	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
   317
		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
   318
		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
   319
		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
   320
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   321
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   322
	void endOfPipe() {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   323
		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
   324
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   325
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   326
};
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   327
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   328
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   329
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   330
}