src/HTTPHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 15 Mar 2022 01:47:05 +0100
branchv_0
changeset 8 3f4e60990393
parent 7 0b0374746e48
child 9 9fdbfbe24161
permissions -rw-r--r--
request headers support in HTTPHandler

/**
 * Relational pipes
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <memory>
#include <string>
#include <vector>
#include <codecvt>
#include <regex>
#include <stdexcept>

#include <curl/curl.h>

#include <relpipe/common/type/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/RelpipeCLIException.h>

#include "Configuration.h"
#include "HTTPClient.h"

namespace relpipe {
namespace tr {
namespace http {

class HTTPHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
private:

	class HeaderDefinition {
	public:
		// TODO: filters/patterns/condition
		relpipe::common::type::StringX name;
		relpipe::common::type::StringX value;
	};

	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
	Configuration configuration;
	relpipe::common::type::StringX currentRelationName;
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
	HeaderDefinition requestHeader;
	std::vector<HeaderDefinition> requestHeaders;
	std::vector<relpipe::common::type::StringX> responseHeaders;
	size_t currentAttributeIndex = 0;
	size_t currentRecordNumber = 1;

	void writeHeaders() {
		if (responseHeaders.size()) {
			relationalWriter->startRelation(L"header",{
				// TODO: request ID instead of URL (or both)
				{L"url", relpipe::writer::TypeId::STRING},
				{L"name", relpipe::writer::TypeId::STRING},
				{L"value", relpipe::writer::TypeId::STRING},
			}, true);

			for (auto s : responseHeaders) relationalWriter->writeAttribute(s);

			responseHeaders.clear();
		}
	}

public:

	HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
	}

	virtual ~HTTPHandler() {
	}

	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
		writeHeaders(); // from previous relation

		currentRelationName = name;
		currentReaderMetadata = attributes;

		if (currentRelationName == L"header") {
			// TODO: analyze header attributes
		} else if (currentRelationName == L"request") {
			relationalWriter->startRelation(L"response",{
				// TODO: request ID
				// TODO: body in hexadecimal/binary format
				{L"url", relpipe::writer::TypeId::STRING},
				{L"body", relpipe::writer::TypeId::STRING},
				{L"code", relpipe::writer::TypeId::INTEGER},
			}, true);
		}
	}

	void attribute(const relpipe::common::type::StringX& value) override {
		if (currentRelationName == L"header") headerAttribute(value);
		else if (currentRelationName == L"request") requestAttribute(value);
		else throw std::invalid_argument("Unsupported relation: " + convertor.to_bytes(currentRelationName));
	}

private:

	void headerAttribute(const relpipe::common::type::StringX& value) {
		if (currentReaderMetadata[currentAttributeIndex].getAttributeName() == L"name") requestHeader.name = value;
		else if (currentReaderMetadata[currentAttributeIndex].getAttributeName() == L"value") requestHeader.value = value;
		else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(currentReaderMetadata[currentAttributeIndex].getAttributeName() + L" = " + value));

		currentAttributeIndex++;

		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
			currentAttributeIndex = 0;
			requestHeaders.push_back(requestHeader);
			requestHeader = HeaderDefinition();
		}

	}

	void requestAttribute(const relpipe::common::type::StringX& value) {
		std::shared_ptr<HTTPClient> http(HTTPClient::open());

		HTTPClient::Request request;
		request.method = HTTPClient::Method::GET;
		request.url = convertor.to_bytes(value);

		// TODO: set request headers from the "header" relation
		// request.headers.push_back("Authorization");
		// request.headers.push_back("Basic YWhvajpoZXNsbw==");
		// request.headers.push_back("Accept");
		// request.headers.push_back("application/xml");
		// request.headers.push_back("User-Agent");
		// request.headers.push_back("curl/7.58.0");
		for (const HeaderDefinition& h : requestHeaders) {
			request.headers.push_back(convertor.to_bytes(h.name));
			request.headers.push_back(convertor.to_bytes(h.value));
		}

		HTTPClient::Response response = http->exchange(request);
		relpipe::common::type::Integer responseCode = response.responseCode;

		relationalWriter->writeAttribute(value);
		relationalWriter->writeAttribute(convertor.from_bytes(response.body));
		relationalWriter->writeAttribute(&responseCode, typeid (responseCode));

		for (size_t i = 0; i < response.headers.size(); i += 2) {
			responseHeaders.push_back(convertor.from_bytes(request.url));
			responseHeaders.push_back(convertor.from_bytes(response.headers[i]));
			responseHeaders.push_back(convertor.from_bytes(response.headers[i + 1]));
		}
	}

public:

	void endOfPipe() {
		writeHeaders(); // from last relation
	}

};

}
}
}