src/HTTPDHandler.h
branchv_0
changeset 7 315d985f8424
parent 6 b04bde9083d4
child 8 90990c8b6aef
equal deleted inserted replaced
6:b04bde9083d4 7:315d985f8424
    37 
    37 
    38 #include <relpipe/cli/RelpipeCLIException.h>
    38 #include <relpipe/cli/RelpipeCLIException.h>
    39 
    39 
    40 #include "Configuration.h"
    40 #include "Configuration.h"
    41 #include "HTTPServer.h"
    41 #include "HTTPServer.h"
       
    42 #include "Hex.h"
    42 
    43 
    43 namespace relpipe {
    44 namespace relpipe {
    44 namespace tr {
    45 namespace tr {
    45 namespace httpd {
    46 namespace httpd {
    46 
    47 
   142 			relpipe::common::type::Integer responseSize = response.body.size();
   143 			relpipe::common::type::Integer responseSize = response.body.size();
   143 
   144 
   144 			relationalWriter->writeAttribute(exchangeId);
   145 			relationalWriter->writeAttribute(exchangeId);
   145 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
   146 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
   146 			relationalWriter->writeAttribute(convertor.from_bytes(request.method));
   147 			relationalWriter->writeAttribute(convertor.from_bytes(request.method));
   147 			relationalWriter->writeAttribute(bodyToText(convertor, request.body));
   148 			relationalWriter->writeAttribute(Hex::toTxt(request.body));
   148 			relationalWriter->writeAttribute(bodyToHex(convertor, request.body));
   149 			relationalWriter->writeAttribute(Hex::toHex(request.body));
   149 			relationalWriter->writeAttribute(&requestSize, typeid (requestSize));
   150 			relationalWriter->writeAttribute(&requestSize, typeid (requestSize));
   150 			relationalWriter->writeAttribute(bodyToText(convertor, response.body));
   151 			relationalWriter->writeAttribute(Hex::toTxt(response.body));
   151 			relationalWriter->writeAttribute(bodyToHex(convertor, response.body));
   152 			relationalWriter->writeAttribute(Hex::toHex(response.body));
   152 			relationalWriter->writeAttribute(&responseSize, typeid (responseSize));
   153 			relationalWriter->writeAttribute(&responseSize, typeid (responseSize));
   153 			relationalWriter->writeAttribute(std::to_wstring(response.code));
   154 			relationalWriter->writeAttribute(std::to_wstring(response.code));
   154 
   155 
   155 
   156 
   156 			relationalWriter->startRelation(L"header",{
   157 			relationalWriter->startRelation(L"header",{
   178 				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
   179 				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
   179 			}
   180 			}
   180 
   181 
   181 
   182 
   182 
   183 
   183 		}
       
   184 
       
   185 		relpipe::common::type::StringX bodyToText(std::wstring_convert < codecvt_utf8<wchar_t>>&convertor, const std::string& body, bool* validEncoding = nullptr) {
       
   186 			try {
       
   187 				if (validEncoding) *validEncoding = true;
       
   188 				// TODO: use encoding from the HTTP response headers instead of the constant one?
       
   189 				return convertor.from_bytes(body);
       
   190 			} catch (...) {
       
   191 				if (validEncoding) *validEncoding = false;
       
   192 				std::stringstream filtered;
       
   193 				for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
       
   194 				return convertor.from_bytes(filtered.str());
       
   195 			}
       
   196 		}
       
   197 
       
   198 		relpipe::common::type::StringX bodyToHex(std::wstring_convert < codecvt_utf8<wchar_t>>&convertor, const std::string& body) {
       
   199 			std::stringstream hex;
       
   200 			hex << std::hex << std::setfill('0') << std::hex;
       
   201 			for (size_t i = 0, size = body.size(); i < size; i++) hex << std::setw(2) << (0xff & body[i]);
       
   202 			return convertor.from_bytes(hex.str());
       
   203 		}
   184 		}
   204 
   185 
   205 		std::string generateExchangeId() {
   186 		std::string generateExchangeId() {
   206 			char buffer[37];
   187 			char buffer[37];
   207 			uuid_t uuid;
   188 			uuid_t uuid;
   282 		auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName();
   263 		auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName();
   283 
   264 
   284 		if (attributeName == L"url") responseTemplate.url = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
   265 		if (attributeName == L"url") responseTemplate.url = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
   285 		else if (attributeName == L"method") responseTemplate.method = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
   266 		else if (attributeName == L"method") responseTemplate.method = std::regex(value.size() ? convertor.to_bytes(value) : ".*");
   286 		else if (attributeName == L"code") responseTemplate.code = std::stoi(value);
   267 		else if (attributeName == L"code") responseTemplate.code = std::stoi(value);
   287 		else if (attributeName == L"text") responseTemplate.body = convertor.to_bytes(value);
   268 		else if (attributeName == L"text" && value.size()) responseTemplate.body = convertor.to_bytes(value);
   288 		else if (attributeName == L"data") responseTemplate.body = "TODO: read binary data: " + convertor.to_bytes(value); // TODO: read hex/binary request body
   269 		else if (attributeName == L"data" && value.size()) responseTemplate.body = Hex::fromHex(value).str();
       
   270 		else if (attributeName == L"text"); // keep empty or value from 'data'
       
   271 		else if (attributeName == L"data"); // keep empty or value from 'text'
   289 		else if (isHeaderAttribute(attributeName)) responseTemplate.headers.push_back(HeaderTemplate(convertor.to_bytes(fetchHeaderName(attributeName)), convertor.to_bytes(value))); // TODO: header encoding?
   272 		else if (isHeaderAttribute(attributeName)) responseTemplate.headers.push_back(HeaderTemplate(convertor.to_bytes(fetchHeaderName(attributeName)), convertor.to_bytes(value))); // TODO: header encoding?
   290 		else throw std::invalid_argument("Unsupported attribute in the request_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
   273 		else throw std::invalid_argument("Unsupported attribute in the response_template relation: " + convertor.to_bytes(attributeName + L" = " + value));
   291 
   274 
   292 		currentAttributeIndex++;
   275 		currentAttributeIndex++;
   293 
   276 
   294 		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
   277 		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
   295 			std::lock_guard<std::mutex> lock(templatesMutex);
   278 			std::lock_guard<std::mutex> lock(templatesMutex);