--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HTTPClient.cpp Sat Mar 12 20:48:25 2022 +0100
@@ -0,0 +1,84 @@
+/**
+ * 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/>.
+ */
+
+#include <sstream>
+
+#include <curl/curl.h>
+
+#include "HTTPClient.h"
+
+
+namespace relpipe {
+namespace tr {
+namespace http {
+
+class HTTPClient::HTTPClientImpl {
+public:
+ CURL* curl;
+ std::stringstream responseBody;
+ std::vector<std::string> responseHeaders;
+
+ HTTPClientImpl(CURL* curl) : curl(curl) {
+ }
+
+ static size_t curlWriteCallback(char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl) {
+ size_t r = size * nmemb;
+ impl->responseBody.write(buffer, r);
+ return r;
+ }
+
+};
+
+HTTPClient* HTTPClient::open() {
+ return new HTTPClient(new HTTPClient::HTTPClientImpl(curl_easy_init()));
+}
+
+HTTPClient::~HTTPClient() {
+ curl_easy_cleanup(impl->curl);
+ delete impl;
+}
+
+const HTTPClient::Response HTTPClient::exchange(const Request& request) {
+ HTTPClient::Response response;
+
+ // TODO: set request headers
+ // TODO: set request method
+ // TODO: get response headers
+
+ curl_easy_setopt(impl->curl, CURLOPT_URL, request.url.c_str());
+
+ curl_easy_setopt(impl->curl, CURLOPT_WRITEDATA, impl);
+ curl_easy_setopt(impl->curl, CURLOPT_WRITEFUNCTION, HTTPClientImpl::curlWriteCallback);
+
+ // curl_easy_setopt(curl, CURLOPT_HEADERDATA, this);
+ // curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headersCurlCallback);
+
+ curl_easy_perform(impl->curl);
+
+ curl_easy_getinfo(impl->curl, CURLINFO_RESPONSE_CODE, &response.responseCode);
+ response.success = response.responseCode >= 200 && response.responseCode <= 299;
+
+ response.body = impl->responseBody.str();
+ impl->responseBody = std::stringstream();
+
+ return response;
+}
+
+
+}
+}
+}