src/HTTPClient.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 05 Apr 2022 00:18:49 +0200
branchv_0
changeset 26 53c6f76603f6
parent 25 dbeae485a3fd
permissions -rw-r--r--
request body support: version with std::string buffer
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
14
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    18
#include <string>
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <sstream>
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    20
#include <iostream>
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <curl/curl.h>
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include "HTTPClient.h"
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
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace http {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class HTTPClient::HTTPClientImpl {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
public:
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	CURL* curl;
11
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    34
	char curlErrorBuffer[CURL_ERROR_SIZE];
26
53c6f76603f6 request body support: version with std::string buffer
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    35
	std::string requestBody;
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	std::stringstream responseBody;
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    37
	std::stringstream responseHeaders;
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	HTTPClientImpl(CURL* curl) : curl(curl) {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    42
	std::vector<std::string> getResponseHeaders() {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    43
		std::vector<std::string> heathers;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    44
		std::stringstream name;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    45
		std::stringstream value;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    46
		std::stringstream* current = &name;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    47
		for (char ch = responseHeaders.get(); responseHeaders.good(); ch = responseHeaders.get()) {
16
60688cf1f165 fix header parsing in getResponseHeaders()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    48
			if (current == &name && ch == ':') {
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    49
				current = &value;
16
60688cf1f165 fix header parsing in getResponseHeaders()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    50
				while (responseHeaders.good() && responseHeaders.peek() == ' ') responseHeaders.get(); // skip spaces
6
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    51
			} else if (ch == '\n') {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    52
				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
					heathers.push_back(name.str());
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    54
					heathers.push_back(value.str());
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    55
				} 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
    56
					// 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
    57
				}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    58
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    59
				name = std::stringstream();
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    60
				value = std::stringstream();
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    61
				current = &name;
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    62
			} else if (ch == '\r') {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    63
				// ignore
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    64
			} else {
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    65
				current->put(ch);
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    66
			}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    67
		}
59c9ca066322 write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    68
		return heathers;
5
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
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
};
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
7
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    73
class CurlList {
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    74
private:
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    75
	curl_slist* list = nullptr;
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    76
public:
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    77
	CurlList() = default;
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    78
	CurlList(const HTTPClient&) = delete;
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    79
	CurlList& operator=(const CurlList&) = delete;
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    80
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    81
	virtual ~CurlList() {
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    82
		curl_slist_free_all(list);
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    83
	}
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    84
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    85
	void append(std::string item) {
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    86
		list = curl_slist_append(list, item.c_str());
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    87
	}
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    88
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    89
	curl_slist* getList() {
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    90
		return list;
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    91
	}
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    92
};
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    93
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
HTTPClient* HTTPClient::open() {
10
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    95
	HTTPClient::HTTPClientImpl* impl = new HTTPClient::HTTPClientImpl(curl_easy_init());
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    96
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    97
	typedef size_t(*CurlWriteCallback)(char*, size_t, size_t, HTTPClient::HTTPClientImpl*);
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    98
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    99
	// set response body callback
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   100
	curl_easy_setopt(impl->curl, CURLOPT_WRITEDATA, impl);
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   101
	curl_easy_setopt(impl->curl, CURLOPT_WRITEFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t {
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   102
		size_t r = size * nmemb;
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   103
		impl->responseBody.write(buffer, r);
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   104
		return r;
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   105
	});
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   106
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   107
	// set response headers callback
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   108
	curl_easy_setopt(impl->curl, CURLOPT_HEADERDATA, impl);
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   109
	curl_easy_setopt(impl->curl, CURLOPT_HEADERFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t {
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   110
		size_t r = size * nmemb;
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   111
		impl->responseHeaders.write(buffer, r);
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   112
		return r;
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   113
	});
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   114
11
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   115
	// set the error buffer
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   116
	curl_easy_setopt(impl->curl, CURLOPT_ERRORBUFFER, impl->curlErrorBuffer);
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   117
12
2f2d64333867 enable HTTP and HTTPS protocols only
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   118
	// enable HTTP and HTTPS only
2f2d64333867 enable HTTP and HTTPS protocols only
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   119
	curl_easy_setopt(impl->curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
2f2d64333867 enable HTTP and HTTPS protocols only
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   120
13
18f4f7e93c48 disable protocol guesswork (HTTP or HTTPS must be explicitly specified in the URL)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   121
	// disable protocol guesswork
18f4f7e93c48 disable protocol guesswork (HTTP or HTTPS must be explicitly specified in the URL)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   122
	curl_easy_setopt(impl->curl, CURLOPT_DEFAULT_PROTOCOL, "no-default-protocol-specify-http-or-https-explicitly-in-url");
18f4f7e93c48 disable protocol guesswork (HTTP or HTTPS must be explicitly specified in the URL)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   123
24
4f96098f7c57 TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   124
	// TODO: automatic decompression of response bodies
4f96098f7c57 TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   125
	// curl_easy_setopt(impl->curl, CURLOPT_ACCEPT_ENCODING, "");
4f96098f7c57 TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   126
	// curl_easy_setopt(impl->curl, CURLOPT_HTTP_CONTENT_DECODING, 1L);
4f96098f7c57 TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   127
	// curl_easy_setopt(impl->curl, CURLOPT_HTTP_TRANSFER_DECODING, 1L);
4f96098f7c57 TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   128
10
479557122717 initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   129
	return new HTTPClient(impl);
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
HTTPClient::~HTTPClient() {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
	curl_easy_cleanup(impl->curl);
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
	delete impl;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
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
   138
	HTTPClient::Response response;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
14
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   140
	// set request method
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   141
	std::string method;
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   142
	if (request.method == Method::DELETE) method = "DELETE";
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   143
	else if (request.method == Method::GET) method = "GET";
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   144
	else if (request.method == Method::HEAD) method = "HEAD";
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   145
	else if (request.method == Method::PATCH) method = "PATCH";
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   146
	else if (request.method == Method::POST) method = "POST";
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   147
	else if (request.method == Method::PUT) method = "PUT";
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   148
	else throw std::invalid_argument("Unsupported HTTP method: " + std::to_string((int) request.method));
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   149
	curl_easy_setopt(impl->curl, CURLOPT_CUSTOMREQUEST, method.c_str());
e41abdd36ff1 request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   150
	if (request.method == Method::HEAD) curl_easy_setopt(impl->curl, CURLOPT_NOBODY, 1L);
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
7
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   152
	// set URL
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
	curl_easy_setopt(impl->curl, CURLOPT_URL, request.url.c_str());
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   154
7
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   155
	// set request headers
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   156
	CurlList requestHeders;
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   157
	for (size_t i = 0; i < request.headers.size(); i += 2) requestHeders.append(request.headers[i] + ": " + request.headers[i + 1]); // TODO: validate, no CR/LF...
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   158
	curl_easy_setopt(impl->curl, CURLOPT_HTTPHEADER, requestHeders.getList());
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
26
53c6f76603f6 request body support: version with std::string buffer
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   160
	// set request body
25
dbeae485a3fd request body support: unfinished version with read callback
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   161
	if (request.body.size()) {
26
53c6f76603f6 request body support: version with std::string buffer
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   162
		impl->requestBody = request.body;
53c6f76603f6 request body support: version with std::string buffer
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   163
		curl_off_t requestBodySize = impl->requestBody.size();
53c6f76603f6 request body support: version with std::string buffer
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   164
		curl_easy_setopt(impl->curl, CURLOPT_POSTFIELDS, impl->requestBody.c_str());
53c6f76603f6 request body support: version with std::string buffer
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   165
		curl_easy_setopt(impl->curl, CURLOPT_POSTFIELDSIZE_LARGE, requestBodySize);
25
dbeae485a3fd request body support: unfinished version with read callback
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   166
	}
dbeae485a3fd request body support: unfinished version with read callback
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   167
dbeae485a3fd request body support: unfinished version with read callback
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   168
11
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   169
	// clear the error buffer
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   170
	impl->curlErrorBuffer[0] = 0;
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   171
7
0b0374746e48 request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   172
	// do HTTP call
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   173
	CURLcode result = curl_easy_perform(impl->curl);
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   174
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   175
	if (result == CURLE_OK) {
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   177
		// response code and fill the result object
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   178
		curl_easy_getinfo(impl->curl, CURLINFO_RESPONSE_CODE, &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: 7
diff changeset
   179
		response.headers = impl->getResponseHeaders();
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   180
		response.body = impl->responseBody.str();
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   181
		impl->responseBody = std::stringstream();
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   183
		return response;
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   184
	} else {
11
6b913e82f52a provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   185
		throw Exception(curl_easy_strerror(result), impl->curlErrorBuffer);
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   186
	}
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   190
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   191
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
}