src/HTTPDHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Apr 2022 02:49:50 +0200
branchv_0
changeset 8 90990c8b6aef
parent 7 315d985f8424
permissions -rw-r--r--
headers, cookies, GET parameters
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"
7
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    42
#include "Hex.h"
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace relpipe {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace tr {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
namespace httpd {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
class HttpdHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
private:
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    50
	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
    51
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    52
	std::mutex relationalWriterMutex;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	Configuration configuration;
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
	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
    55
	relpipe::common::type::StringX currentRelationName;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
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);
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   116
5
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",{
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   128
				// TODO: timestamp
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   129
				// TODO: ordinal number?
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   130
				{L"id", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   131
				{L"url", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   132
				{L"method", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   133
				{L"request_text", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   134
				{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
   135
				{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
   136
				{L"response_text", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   137
				{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
   138
				{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
   139
				{L"response_code", relpipe::writer::TypeId::INTEGER},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   140
			}, true);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   141
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   142
			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
   143
			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
   144
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   145
			relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   146
			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   147
			relationalWriter->writeAttribute(convertor.from_bytes(request.method));
7
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   148
			relationalWriter->writeAttribute(Hex::toTxt(request.body));
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   149
			relationalWriter->writeAttribute(Hex::toHex(request.body));
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   150
			relationalWriter->writeAttribute(&requestSize, typeid (requestSize));
7
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   151
			relationalWriter->writeAttribute(Hex::toTxt(response.body));
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   152
			relationalWriter->writeAttribute(Hex::toHex(response.body));
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   153
			relationalWriter->writeAttribute(&responseSize, typeid (responseSize));
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   154
			relationalWriter->writeAttribute(std::to_wstring(response.code));
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
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   157
			relationalWriter->startRelation(L"header",{
6
b04bde9083d4 add request and response sizes
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   158
				// TODO: ordinal number?
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   159
				{L"exchange", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   160
				{L"url", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   161
				{L"direction", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   162
				{L"name", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   163
				{L"value", relpipe::writer::TypeId::STRING},
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   164
			}, true);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   165
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   166
			for (const HTTPServer::AVP& h : request.header) {
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   167
				relationalWriter->writeAttribute(exchangeId);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   168
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   169
				relationalWriter->writeAttribute(L"request");
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   170
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   171
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   172
			}
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   173
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   174
			for (const HTTPServer::AVP& h : response.header) {
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   175
				relationalWriter->writeAttribute(exchangeId);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   176
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   177
				relationalWriter->writeAttribute(L"response");
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   178
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   179
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   180
			}
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   181
			
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   182
			relationalWriter->startRelation(L"cookie",{
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   183
				// TODO: ordinal number?
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   184
				{L"exchange", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   185
				{L"url", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   186
				{L"direction", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   187
				{L"name", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   188
				{L"value", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   189
			}, true);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   190
			
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   191
			for (const HTTPServer::AVP& h : request.cookie) {
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   192
				relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   193
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   194
				relationalWriter->writeAttribute(L"request");
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   195
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   196
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
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
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   199
			for (const HTTPServer::AVP& h : response.cookie) {
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   200
				relationalWriter->writeAttribute(exchangeId);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   201
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   202
				relationalWriter->writeAttribute(L"response");
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   203
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   204
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   205
			}
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   206
			
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   207
			relationalWriter->startRelation(L"get_parameter",{
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   208
				// TODO: ordinal number?
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   209
				{L"exchange", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   210
				{L"url", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   211
				{L"name", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   212
				{L"value", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   213
			}, true);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   214
			
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   215
			for (const HTTPServer::AVP& h : request.getParameter) {
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   216
				relationalWriter->writeAttribute(exchangeId);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   217
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   218
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   219
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   220
			}
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   221
			
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   222
			relationalWriter->startRelation(L"post_parameter",{
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   223
				// TODO: ordinal number?
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   224
				{L"exchange", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   225
				{L"url", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   226
				{L"name", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   227
				{L"value", relpipe::writer::TypeId::STRING},
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   228
			}, true);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   229
			
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   230
			for (const HTTPServer::AVP& h : request.postParameter) {
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   231
				relationalWriter->writeAttribute(exchangeId);
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   232
				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   233
				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   234
				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   235
			}
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   236
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   237
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   238
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   239
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   240
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   241
		std::string generateExchangeId() {
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   242
			char buffer[37];
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   243
			uuid_t uuid;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   244
			uuid_generate_random(uuid);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   245
			// uuid_generate_time(uuid);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   246
			uuid_unparse_lower(uuid, buffer);
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   247
			return buffer;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   248
		}
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   249
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   250
	public:
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   251
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   252
		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
   253
		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
   254
		}
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
		virtual ~RequestHandler() = default;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   257
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   258
		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
   259
			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
   260
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   261
			response.code = 404;
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   262
			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
   263
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   264
			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
   265
			for (ResponseTemplate t : *responseTemplates) {
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   266
				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
   267
					response.code = t.code;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   268
					response.body = t.body;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   269
					// TODO: replace global header values with request-specific ones instead of appending?
8
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   270
					for (const GlobalHeaderTemplate& h : *headerTemplates) if (h.matches(request.method, request.url)) response.header.push_back({h.name, h.value});
90990c8b6aef headers, cookies, GET parameters
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   271
					for (const HeaderTemplate& h : t.headers) response.header.push_back({h.name, h.value});
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   272
					break;
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   273
				}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   274
			}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   275
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   276
			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
   277
			return response;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   278
		}
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   279
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   280
	};
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   281
5
121981e6bd54 generate some output relations
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   282
	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
   283
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   284
	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
   285
		// might be configurable - parametrized
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   286
		return L"header.";
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   287
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   288
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   289
	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
   290
		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
   291
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   292
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   293
	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
   294
		// 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
   295
		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
   296
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   297
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   298
	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
   299
		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
   300
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   301
		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
   302
		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
   303
		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
   304
		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
   305
		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
   306
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   307
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   308
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   309
		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
   310
			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
   311
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   312
			headerTemplates.push_back(headerTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   313
			headerTemplate = GlobalHeaderTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   314
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   315
	}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   316
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   317
	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
   318
		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
   319
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   320
		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
   321
		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
   322
		else if (attributeName == L"code") responseTemplate.code = std::stoi(value);
7
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   323
		else if (attributeName == L"text" && value.size()) responseTemplate.body = convertor.to_bytes(value);
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   324
		else if (attributeName == L"data" && value.size()) responseTemplate.body = Hex::fromHex(value).str();
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   325
		else if (attributeName == L"text"); // keep empty or value from 'data'
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   326
		else if (attributeName == L"data"); // keep empty or value from 'text'
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   327
		else if (isHeaderAttribute(attributeName)) responseTemplate.headers.push_back(HeaderTemplate(convertor.to_bytes(fetchHeaderName(attributeName)), convertor.to_bytes(value))); // TODO: header encoding?
7
315d985f8424 use common hex function, serve also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   328
		else throw std::invalid_argument("Unsupported attribute in the response_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   329
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   330
		currentAttributeIndex++;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   331
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   332
		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
   333
			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
   334
			currentAttributeIndex = 0;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   335
			responseTemplates.push_back(responseTemplate);
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   336
			responseTemplate = ResponseTemplate();
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   337
		}
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   338
	}
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   339
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   340
public:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   341
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   342
	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
   343
		httpServer->setRequestHandler(requestHandler);
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   344
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   345
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   346
	virtual ~HttpdHandler() {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   347
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   348
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   349
	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
   350
		currentRelationName = name;
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   351
		currentReaderMetadata = attributes;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   352
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   353
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   354
	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
   355
		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
   356
		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
   357
		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
   358
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   359
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   360
	void endOfPipe() {
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   361
		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
   362
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   363
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   364
};
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   365
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   366
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   367
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   368
}