--- a/src/HTTPDHandler.h Thu Apr 21 00:23:08 2022 +0200
+++ b/src/HTTPDHandler.h Thu Apr 21 00:43:54 2022 +0200
@@ -39,6 +39,7 @@
#include "Configuration.h"
#include "HTTPServer.h"
+#include "Hex.h"
namespace relpipe {
namespace tr {
@@ -144,11 +145,11 @@
relationalWriter->writeAttribute(exchangeId);
relationalWriter->writeAttribute(convertor.from_bytes(request.url));
relationalWriter->writeAttribute(convertor.from_bytes(request.method));
- relationalWriter->writeAttribute(bodyToText(convertor, request.body));
- relationalWriter->writeAttribute(bodyToHex(convertor, request.body));
+ relationalWriter->writeAttribute(Hex::toTxt(request.body));
+ relationalWriter->writeAttribute(Hex::toHex(request.body));
relationalWriter->writeAttribute(&requestSize, typeid (requestSize));
- relationalWriter->writeAttribute(bodyToText(convertor, response.body));
- relationalWriter->writeAttribute(bodyToHex(convertor, response.body));
+ relationalWriter->writeAttribute(Hex::toTxt(response.body));
+ relationalWriter->writeAttribute(Hex::toHex(response.body));
relationalWriter->writeAttribute(&responseSize, typeid (responseSize));
relationalWriter->writeAttribute(std::to_wstring(response.code));
@@ -182,26 +183,6 @@
}
- relpipe::common::type::StringX bodyToText(std::wstring_convert < codecvt_utf8<wchar_t>>&convertor, 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;
- std::stringstream filtered;
- for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
- return convertor.from_bytes(filtered.str());
- }
- }
-
- relpipe::common::type::StringX bodyToHex(std::wstring_convert < codecvt_utf8<wchar_t>>&convertor, 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());
- }
-
std::string generateExchangeId() {
char buffer[37];
uuid_t uuid;
@@ -284,10 +265,12 @@
if (attributeName == L"url") responseTemplate.url = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
else if (attributeName == L"method") responseTemplate.method = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
else if (attributeName == L"code") responseTemplate.code = std::stoi(value);
- else if (attributeName == L"text") responseTemplate.body = convertor.to_bytes(value);
- else if (attributeName == L"data") responseTemplate.body = "TODO: read binary data: " + convertor.to_bytes(value); // TODO: read hex/binary request body
+ else if (attributeName == L"text" && value.size()) responseTemplate.body = convertor.to_bytes(value);
+ else if (attributeName == L"data" && value.size()) responseTemplate.body = Hex::fromHex(value).str();
+ else if (attributeName == L"text"); // keep empty or value from 'data'
+ else if (attributeName == L"data"); // keep empty or value from 'text'
else if (isHeaderAttribute(attributeName)) responseTemplate.headers.push_back(HeaderTemplate(convertor.to_bytes(fetchHeaderName(attributeName)), convertor.to_bytes(value))); // TODO: header encoding?
- else throw std::invalid_argument("Unsupported attribute in the request_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
+ else throw std::invalid_argument("Unsupported attribute in the response_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
currentAttributeIndex++;