return at least ASCII text when unable to decode v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 01 Apr 2022 22:40:02 +0200
branchv_0
changeset 23 33f8162a4971
parent 22 1bbfcb95db82
child 24 4f96098f7c57
return at least ASCII text when unable to decode
src/HTTPHandler.h
--- a/src/HTTPHandler.h	Fri Mar 25 00:39:11 2022 +0100
+++ b/src/HTTPHandler.h	Fri Apr 01 22:40:02 2022 +0200
@@ -133,10 +133,13 @@
 	relpipe::common::type::StringX bodyToText(const std::string& body, bool* validEncoding = nullptr) {
 		try {
 			if (validEncoding) *validEncoding = true;
+			// TODO: use encoding from the HTTP response headers instead of the constant one?
 			return convertor.from_bytes(body);
 		} catch (...) {
 			if (validEncoding) *validEncoding = false;
-			return L"";
+			std::stringstream filtered;
+			for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
+			return convertor.from_bytes(filtered.str());
 		}
 	}
 
@@ -170,6 +173,7 @@
 				{L"url", relpipe::writer::TypeId::STRING},
 				{L"text", relpipe::writer::TypeId::STRING},
 				{L"data", relpipe::writer::TypeId::STRING},
+				{L"text_valid", relpipe::writer::TypeId::BOOLEAN},
 				{L"code", relpipe::writer::TypeId::INTEGER},
 			}, true);
 		}
@@ -240,10 +244,12 @@
 				// TODO: move error message into separate attribute?
 			}
 
+			bool validText = false;
 			relationalWriter->writeAttribute(requestId);
 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
-			relationalWriter->writeAttribute(bodyToText(body));
+			relationalWriter->writeAttribute(bodyToText(body, &validText));
 			relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal
+			relationalWriter->writeAttribute(&validText, typeid (validText));
 			relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
 
 			request = HTTPClient::Request();