# HG changeset patch # User František Kučera # Date 1648165151 -3600 # Node ID 1bbfcb95db8200f285de64c2784d6c79a2f94036 # Parent 1a4174f4693a2b9b3cbc96cf8312ff5be91fe7cf format response body as unicode text and hexadecimal diff -r 1a4174f4693a -r 1bbfcb95db82 src/HTTPHandler.h --- a/src/HTTPHandler.h Thu Mar 24 02:19:39 2022 +0100 +++ b/src/HTTPHandler.h Fri Mar 25 00:39:11 2022 +0100 @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -129,6 +130,23 @@ return convertor.from_bytes(buffer); } + relpipe::common::type::StringX bodyToText(const std::string& body, bool* validEncoding = nullptr) { + try { + if (validEncoding) *validEncoding = true; + return convertor.from_bytes(body); + } catch (...) { + if (validEncoding) *validEncoding = false; + return L""; + } + } + + relpipe::common::type::StringX bodyToHex(const std::string& body) { + std::stringstream hex; + hex << std::hex << std::setfill('0') << std::hex; + for (size_t i = 0, size = body.size(); i < size; i++) hex << std::setw(2) << (0xff & body[i]); + return convertor.from_bytes(hex.str()); + } + public: HTTPHandler(shared_ptr relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { @@ -148,10 +166,10 @@ // TODO: analyze header attributes } else if (currentRelationName == L"request") { relationalWriter->startRelation(L"response",{ - // TODO: body in hexadecimal/binary format {L"request", relpipe::writer::TypeId::STRING}, {L"url", relpipe::writer::TypeId::STRING}, - {L"body", relpipe::writer::TypeId::STRING}, + {L"text", relpipe::writer::TypeId::STRING}, + {L"data", relpipe::writer::TypeId::STRING}, {L"code", relpipe::writer::TypeId::INTEGER}, }, true); } @@ -224,7 +242,8 @@ relationalWriter->writeAttribute(requestId); relationalWriter->writeAttribute(convertor.from_bytes(request.url)); - relationalWriter->writeAttribute(convertor.from_bytes(body)); + relationalWriter->writeAttribute(bodyToText(body)); + relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal relationalWriter->writeAttribute(&responseCode, typeid (responseCode)); request = HTTPClient::Request();