return also the HTTP response code and URL v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 06 Mar 2022 22:01:32 +0100
branchv_0
changeset 3 9c710397ced6
parent 2 dccedac46e7e
child 4 602462d04c57
return also the HTTP response code and URL
src/HTTPHandler.h
src/relpipe-tr-http.cpp
--- a/src/HTTPHandler.h	Sun Mar 06 19:51:37 2022 +0100
+++ b/src/HTTPHandler.h	Sun Mar 06 22:01:32 2022 +0100
@@ -52,12 +52,14 @@
 	size_t currentRecordNumber = 1;
 
 	void writeCallback(std::string value) {
-		relationalWriter->writeAttribute(L"got response: " + convertor.from_bytes(value));
+		// TODO: write this attribute even if this method was not called
+		// TODO: support also binary data or other encodings, not only UTF-8 text
+		relationalWriter->writeAttribute(convertor.from_bytes(value));
 	}
 
-	static uint writeCurlCallback(char* in, uint size, uint nmemb, HTTPHandler* instance) {
-		uint r = size * nmemb;
-		instance->writeCallback(std::string(in, r));
+	static uint writeCurlCallback(char* buffer, size_t size, size_t nmemb, HTTPHandler* instance) {
+		size_t r = size * nmemb;
+		instance->writeCallback(std::string(buffer, r));
 		return r;
 	}
 
@@ -72,24 +74,38 @@
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
 
 
-		relationalWriter->startRelation(name + L"_curl",{
+		relationalWriter->startRelation(name + L"_curl_info",{
+			{L"name", relpipe::writer::TypeId::STRING},
 			{L"value", relpipe::writer::TypeId::STRING}
 		}, true);
 
-		relationalWriter->writeAttribute(L"curl version = " + convertor.from_bytes(curl_version()));
+		relationalWriter->writeAttribute(L"curl version");
+		relationalWriter->writeAttribute(convertor.from_bytes(curl_version()));
+
+		relationalWriter->startRelation(name + L"_curl_response",{
+			{L"url", relpipe::writer::TypeId::STRING},
+			{L"body", relpipe::writer::TypeId::STRING},
+			{L"response_code", relpipe::writer::TypeId::INTEGER}
+		}, true);
 
 	}
 
 	void attribute(const relpipe::common::type::StringX& value) override {
 
+		relationalWriter->writeAttribute(value);
+
 		std::string url = convertor.to_bytes(value);
+		relpipe::common::type::Integer responseCode = 0;
 
 		CURL* curl = curl_easy_init();
 		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCurlCallback);
 		curl_easy_perform(curl);
+		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
 		curl_easy_cleanup(curl);
+
+		relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
 	}
 
 	void endOfPipe() {
--- a/src/relpipe-tr-http.cpp	Sun Mar 06 19:51:37 2022 +0100
+++ b/src/relpipe-tr-http.cpp	Sun Mar 06 22:01:32 2022 +0100
@@ -65,6 +65,10 @@
 		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str());
 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
+	} catch (RelpipeWriterException& e) {
+		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessage().c_str());
+		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+		resultCode = CLI::EXIT_CODE_DATA_ERROR;
 	}
 
 	return resultCode;