src/HTTPHandler.h
branchv_0
changeset 23 33f8162a4971
parent 22 1bbfcb95db82
child 25 dbeae485a3fd
equal deleted inserted replaced
22:1bbfcb95db82 23:33f8162a4971
   131 	}
   131 	}
   132 
   132 
   133 	relpipe::common::type::StringX bodyToText(const std::string& body, bool* validEncoding = nullptr) {
   133 	relpipe::common::type::StringX bodyToText(const std::string& body, bool* validEncoding = nullptr) {
   134 		try {
   134 		try {
   135 			if (validEncoding) *validEncoding = true;
   135 			if (validEncoding) *validEncoding = true;
       
   136 			// TODO: use encoding from the HTTP response headers instead of the constant one?
   136 			return convertor.from_bytes(body);
   137 			return convertor.from_bytes(body);
   137 		} catch (...) {
   138 		} catch (...) {
   138 			if (validEncoding) *validEncoding = false;
   139 			if (validEncoding) *validEncoding = false;
   139 			return L"";
   140 			std::stringstream filtered;
       
   141 			for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
       
   142 			return convertor.from_bytes(filtered.str());
   140 		}
   143 		}
   141 	}
   144 	}
   142 
   145 
   143 	relpipe::common::type::StringX bodyToHex(const std::string& body) {
   146 	relpipe::common::type::StringX bodyToHex(const std::string& body) {
   144 		std::stringstream hex;
   147 		std::stringstream hex;
   168 			relationalWriter->startRelation(L"response",{
   171 			relationalWriter->startRelation(L"response",{
   169 				{L"request", relpipe::writer::TypeId::STRING},
   172 				{L"request", relpipe::writer::TypeId::STRING},
   170 				{L"url", relpipe::writer::TypeId::STRING},
   173 				{L"url", relpipe::writer::TypeId::STRING},
   171 				{L"text", relpipe::writer::TypeId::STRING},
   174 				{L"text", relpipe::writer::TypeId::STRING},
   172 				{L"data", relpipe::writer::TypeId::STRING},
   175 				{L"data", relpipe::writer::TypeId::STRING},
       
   176 				{L"text_valid", relpipe::writer::TypeId::BOOLEAN},
   173 				{L"code", relpipe::writer::TypeId::INTEGER},
   177 				{L"code", relpipe::writer::TypeId::INTEGER},
   174 			}, true);
   178 			}, true);
   175 		}
   179 		}
   176 	}
   180 	}
   177 
   181 
   238 			} catch (const HTTPClient::Exception& e) {
   242 			} catch (const HTTPClient::Exception& e) {
   239 				body = e.getFullMessage();
   243 				body = e.getFullMessage();
   240 				// TODO: move error message into separate attribute?
   244 				// TODO: move error message into separate attribute?
   241 			}
   245 			}
   242 
   246 
       
   247 			bool validText = false;
   243 			relationalWriter->writeAttribute(requestId);
   248 			relationalWriter->writeAttribute(requestId);
   244 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
   249 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
   245 			relationalWriter->writeAttribute(bodyToText(body));
   250 			relationalWriter->writeAttribute(bodyToText(body, &validText));
   246 			relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal
   251 			relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal
       
   252 			relationalWriter->writeAttribute(&validText, typeid (validText));
   247 			relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
   253 			relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
   248 
   254 
   249 			request = HTTPClient::Request();
   255 			request = HTTPClient::Request();
   250 			requestId.clear();
   256 			requestId.clear();
   251 		}
   257 		}