src/HTTPDHandler.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 08 Apr 2022 22:38:45 +0200
branchv_0
changeset 3 1184f3de5533
parent 2 4b05b16b97e6
child 4 37a86904145c
permissions -rw-r--r--
return responses from HTTPDHandler::requestHandler instead of constant ones
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>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/common/type/typedefs.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/TypeId.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/AttributeMetadata.h>
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/writer/Factory.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/cli/RelpipeCLIException.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "Configuration.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "HTTPServer.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
namespace relpipe {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace tr {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace httpd {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
class HttpdHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
private:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML is in UTF-8
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	Configuration configuration;
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
	std::shared_ptr<HTTPServer> httpServer;
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	size_t currentAttributeIndex = 0;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	size_t currentRecordNumber = 1;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
3
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
	class RequestHandler : public HTTPServer::RequestHandler {
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
	public:
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
		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
    57
			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
    58
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
			// TODO: return real responses
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
			response.code = 200;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
			response.body = "<h1>greetings and salutations</h1>";
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
			return response;
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
		}
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
	};
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    68
	std::shared_ptr<RequestHandler> requestHandler = std::make_shared<RequestHandler>();
1184f3de5533 return responses from HTTPDHandler::requestHandler instead of constant ones
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    69
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
public:
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
	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
    73
		httpServer->setRequestHandler(requestHandler);
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	virtual ~HttpdHandler() {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	void attribute(const relpipe::common::type::StringX& value) override {
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	void endOfPipe() {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
};
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
}