# HG changeset patch # User František Kučera # Date 1650495450 -7200 # Node ID fe61bf7d171687a3bde01223da149d69512f9c09 # Parent b679797949e99a7c88b61a17c9dd6cc97ecd87b6 use common hex function, send also binary content, not only text diff -r b679797949e9 -r fe61bf7d1716 src/HTTPHandler.h --- a/src/HTTPHandler.h Thu Apr 21 00:22:02 2022 +0200 +++ b/src/HTTPHandler.h Thu Apr 21 00:57:30 2022 +0200 @@ -40,6 +40,7 @@ #include "Configuration.h" #include "HTTPClient.h" +#include "Hex.h" namespace relpipe { namespace tr { @@ -129,26 +130,6 @@ return convertor.from_bytes(buffer); } - 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; - std::stringstream filtered; - for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.'); - return convertor.from_bytes(filtered.str()); - } - } - - 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) { @@ -211,8 +192,10 @@ if (attributeName == L"id") requestId = value; else if (attributeName == L"url") request.url = convertor.to_bytes(value); else if (attributeName == L"method") request.method = parseMethod(value); - else if (attributeName == L"text") request.body = convertor.to_bytes(value); - else if (attributeName == L"data") request.body = "TODO: read binary data: " + convertor.to_bytes(value); // TODO: read hex/binary request body + else if (attributeName == L"text" && value.size()) request.body = convertor.to_bytes(value); + else if (attributeName == L"data" && value.size()) request.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)) appendRequestHeader(fetchHeaderName(attributeName), value); else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(attributeName + L" = " + value)); @@ -248,8 +231,8 @@ bool validText = false; relationalWriter->writeAttribute(requestId); relationalWriter->writeAttribute(convertor.from_bytes(request.url)); - relationalWriter->writeAttribute(bodyToText(body, &validText)); - relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal + relationalWriter->writeAttribute(Hex::toTxt(body, &validText)); + relationalWriter->writeAttribute(Hex::toHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal relationalWriter->writeAttribute(&validText, typeid (validText)); relationalWriter->writeAttribute(&responseCode, typeid (responseCode)); diff -r b679797949e9 -r fe61bf7d1716 src/Hex.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Hex.h Thu Apr 21 00:57:30 2022 +0200 @@ -0,0 +1,85 @@ +/** + * 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 . + */ +#pragma once + +#include +#include +#include +#include +#include + +namespace relpipe { +namespace tr { +namespace http { + +class Hex { +private: + + Hex() { + } + + static char fromHex(wchar_t ch) { + if (L'0' <= ch && ch <= L'9') return ch - L'0'; + else if (L'a' <= ch && ch <= L'f') return ch - L'a' + 10; + else throw std::invalid_argument("Unable to decode hexadeximal string."); + } + +public: + + static std::stringstream fromHex(const std::wstring& hex) { + std::stringstream octets; + + char octet = 0; + + for (size_t i = 0, limit = hex.size(); i < limit; i++) { + if (i % 2 == 0) { + octet = fromHex(hex[i]) << 4; + } else { + octet += fromHex(hex[i]); + octets.put(octet); + } + } + + return octets; + } + + static std::wstring toHex(const std::string& octets) { + std::wstring_convert < codecvt_utf8> convertor; // TODO: do not create converter each time + std::stringstream hex; + hex << std::hex << std::setfill('0') << std::hex; + for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]); + return convertor.from_bytes(hex.str()); + } + + static std::wstring toTxt(const std::string& octets, bool* validEncoding = nullptr) { + std::wstring_convert < codecvt_utf8> convertor; // TODO: do not create converter each time + try { + if (validEncoding) *validEncoding = true; + // TODO: use encoding from the HTTP response headers instead of the constant one? + return convertor.from_bytes(octets); + } catch (...) { + if (validEncoding) *validEncoding = false; + std::stringstream filtered; + for (char ch : octets) filtered << (ch >= ' ' && ch < 127 ? ch : '.'); + return convertor.from_bytes(filtered.str()); + } + } +}; + +} +} +}