src/HTTPClient.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Mar 2022 01:17:59 +0100
branchv_0
changeset 6 59c9ca066322
parent 5 165f6162524d
child 7 0b0374746e48
permissions -rw-r--r--
write response headers as a relation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
165f6162524d introduce HTTPClient wrapper around CURL
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
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <sstream>
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    19
#include <iostream>
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <curl/curl.h>
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "HTTPClient.h"
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace relpipe {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace tr {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace http {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
class HTTPClient::HTTPClientImpl {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
public:
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	CURL* curl;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	std::stringstream responseBody;
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    34
	std::stringstream responseHeaders;
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	HTTPClientImpl(CURL* curl) : curl(curl) {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    39
	std::vector<std::string> getResponseHeaders() {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    40
		std::vector<std::string> heathers;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    41
		std::stringstream name;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    42
		std::stringstream value;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    43
		std::stringstream* current = &name;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    44
		for (char ch = responseHeaders.get(); responseHeaders.good(); ch = responseHeaders.get()) {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    45
			if (ch == ':') {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    46
				current = &value;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    47
				for (char space = responseHeaders.get(); responseHeaders.good() && responseHeaders.peek() == ' '; space = responseHeaders.get()); // skip spaces
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    48
			} else if (ch == '\n') {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    49
				if (name.tellp() > 0 && current == &value) {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    50
					heathers.push_back(name.str());
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    51
					heathers.push_back(value.str());
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    52
				} else if (name.tellp() > 0 && current == &value) {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    53
					// TODO: usually "HTTP/1.1 200 OK" → extract HTTP version and message?
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    54
				}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    55
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    56
				name = std::stringstream();
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    57
				value = std::stringstream();
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    58
				current = &name;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    59
			} else if (ch == '\r') {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    60
				// ignore
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    61
			} else {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    62
				current->put(ch);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    63
			}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    64
		}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    65
		return heathers;
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
};
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
HTTPClient* HTTPClient::open() {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	return new HTTPClient(new HTTPClient::HTTPClientImpl(curl_easy_init()));
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
HTTPClient::~HTTPClient() {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	curl_easy_cleanup(impl->curl);
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	delete impl;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
const HTTPClient::Response HTTPClient::exchange(const Request& request) {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	HTTPClient::Response response;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	// TODO: set request headers
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	// TODO: set request method
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	// TODO: get response headers
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	curl_easy_setopt(impl->curl, CURLOPT_URL, request.url.c_str());
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    88
	typedef size_t(*CurlWriteCallback)(char*, size_t, size_t, HTTPClient::HTTPClientImpl*);
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    90
	curl_easy_setopt(impl->curl, CURLOPT_WRITEDATA, impl);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    91
	curl_easy_setopt(impl->curl, CURLOPT_WRITEFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    92
		size_t r = size * nmemb;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    93
		impl->responseBody.write(buffer, r);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    94
		return r;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    95
	});
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    96
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    97
	curl_easy_setopt(impl->curl, CURLOPT_HEADERDATA, impl);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    98
	curl_easy_setopt(impl->curl, CURLOPT_HEADERFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    99
		size_t r = size * nmemb;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   100
		impl->responseHeaders.write(buffer, r);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   101
		return r;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   102
	});
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	curl_easy_perform(impl->curl);
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	curl_easy_getinfo(impl->curl, CURLINFO_RESPONSE_CODE, &response.responseCode);
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   107
	response.headers = impl->getResponseHeaders();
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
	response.body = impl->responseBody.str();
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	impl->responseBody = std::stringstream();
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	return response;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
}