src/HTTPHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 15 Mar 2022 23:43:02 +0100
branchv_0
changeset 11 6b913e82f52a
parent 9 9fdbfbe24161
child 15 25be376736cc
permissions -rw-r--r--
provide also error details in exception and output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
ce520a238309 new project relpipe-tr-http
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
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <codecvt>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <regex>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <stdexcept>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
1
ddc12e789d00 link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <curl/curl.h>
ddc12e789d00 link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/common/type/typedefs.h>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/writer/Factory.h>
1
ddc12e789d00 link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
#include <relpipe/writer/TypeId.h>
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include <relpipe/cli/RelpipeCLIException.h>
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include "Configuration.h"
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    39
#include "HTTPClient.h"
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
namespace relpipe {
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace tr {
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace http {
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
class HTTPHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
private:
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    47
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    48
	class HeaderDefinition {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    49
	public:
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    50
		// TODO: filters/patterns/condition
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    51
		relpipe::common::type::StringX name;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    52
		relpipe::common::type::StringX value;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    53
	};
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    54
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	Configuration configuration;
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    58
	relpipe::common::type::StringX currentRelationName;
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    61
	HeaderDefinition requestHeader;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    62
	std::vector<HeaderDefinition> requestHeaders;
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    63
	std::vector<relpipe::common::type::StringX> responseHeaders;
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	size_t currentAttributeIndex = 0;
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	size_t currentRecordNumber = 1;
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    67
	void writeHeaders() {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    68
		if (responseHeaders.size()) {
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    69
			relationalWriter->startRelation(L"header",{
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    70
				// TODO: request ID instead of URL (or both)
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    71
				{L"url", relpipe::writer::TypeId::STRING},
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    72
				{L"name", relpipe::writer::TypeId::STRING},
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    73
				{L"value", relpipe::writer::TypeId::STRING},
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    74
			}, true);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    75
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    76
			for (auto s : responseHeaders) relationalWriter->writeAttribute(s);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    77
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    78
			responseHeaders.clear();
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    79
		}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    80
	}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    81
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
public:
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	virtual ~HTTPHandler() {
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    91
		writeHeaders(); // from previous relation
1
ddc12e789d00 link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    93
		currentRelationName = name;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    94
		currentReaderMetadata = attributes;
1
ddc12e789d00 link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    96
		if (currentRelationName == L"header") {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    97
			// TODO: analyze header attributes
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    98
		} else if (currentRelationName == L"request") {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    99
			relationalWriter->startRelation(L"response",{
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   100
				// TODO: request ID
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   101
				// TODO: body in hexadecimal/binary format
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   102
				{L"url", relpipe::writer::TypeId::STRING},
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   103
				{L"body", relpipe::writer::TypeId::STRING},
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   104
				{L"code", relpipe::writer::TypeId::INTEGER},
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   105
			}, true);
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   106
		}
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   107
	}
3
9c710397ced6 return also the HTTP response code and URL
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   108
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   109
	void attribute(const relpipe::common::type::StringX& value) override {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   110
		if (currentRelationName == L"header") headerAttribute(value);
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   111
		else if (currentRelationName == L"request") requestAttribute(value);
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   112
		else throw std::invalid_argument("Unsupported relation: " + convertor.to_bytes(currentRelationName));
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   113
	}
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   114
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   115
private:
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   116
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   117
	void headerAttribute(const relpipe::common::type::StringX& value) {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   118
		if (currentReaderMetadata[currentAttributeIndex].getAttributeName() == L"name") requestHeader.name = value;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   119
		else if (currentReaderMetadata[currentAttributeIndex].getAttributeName() == L"value") requestHeader.value = value;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   120
		else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(currentReaderMetadata[currentAttributeIndex].getAttributeName() + L" = " + value));
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   121
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   122
		currentAttributeIndex++;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   123
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   124
		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   125
			currentAttributeIndex = 0;
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   126
			requestHeaders.push_back(requestHeader);
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   127
			requestHeader = HeaderDefinition();
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   128
		}
1
ddc12e789d00 link to the CURL library
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   129
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
	}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   132
	void requestAttribute(const relpipe::common::type::StringX& value) {
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   133
		std::shared_ptr<HTTPClient> http(HTTPClient::open());
2
dccedac46e7e do HTTP request for each attribute value
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   134
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   135
		HTTPClient::Request request;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   136
		request.method = HTTPClient::Method::GET;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   137
		request.url = convertor.to_bytes(value);
4
602462d04c57 print somehow HTTP headers on STDERR
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   138
7
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   139
		// TODO: set request headers from the "header" relation
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   140
		// request.headers.push_back("Authorization");
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   141
		// request.headers.push_back("Basic YWhvajpoZXNsbw==");
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   142
		// request.headers.push_back("Accept");
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   143
		// request.headers.push_back("application/xml");
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   144
		// request.headers.push_back("User-Agent");
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   145
		// request.headers.push_back("curl/7.58.0");
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   146
		for (const HeaderDefinition& h : requestHeaders) {
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   147
			request.headers.push_back(convertor.to_bytes(h.name));
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   148
			request.headers.push_back(convertor.to_bytes(h.value));
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   149
		}
7
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   150
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   151
		std::string body;
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   152
		relpipe::common::type::Integer responseCode = -1;
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   153
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   154
		try {
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   155
			HTTPClient::Response response = http->exchange(request);
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   156
			responseCode = response.responseCode;
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   157
			body = response.body;
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   158
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   159
			for (size_t i = 0; i < response.headers.size(); i += 2) {
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   160
				responseHeaders.push_back(convertor.from_bytes(request.url));
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   161
				responseHeaders.push_back(convertor.from_bytes(response.headers[i]));
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   162
				responseHeaders.push_back(convertor.from_bytes(response.headers[i + 1]));
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   163
			}
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   164
		} catch (const HTTPClient::Exception& e) {
11
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   165
			body = e.getFullMessage();
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   166
			// TODO: move error message into separate attribute?
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   167
		}
4
602462d04c57 print somehow HTTP headers on STDERR
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   168
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   169
		relationalWriter->writeAttribute(value);
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   170
		relationalWriter->writeAttribute(convertor.from_bytes(body));
3
9c710397ced6 return also the HTTP response code and URL
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   171
		relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
	}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
8
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   174
public:
3f4e60990393 request headers support in HTTPHandler
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   175
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
	void endOfPipe() {
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   177
		writeHeaders(); // from last relation
0
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
	}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
};
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
}
ce520a238309 new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
}