src/HTTPHandler.h
branchv_0
changeset 22 1bbfcb95db82
parent 21 1a4174f4693a
child 23 33f8162a4971
equal deleted inserted replaced
21:1a4174f4693a 22:1bbfcb95db82
    21 #include <vector>
    21 #include <vector>
    22 #include <codecvt>
    22 #include <codecvt>
    23 #include <regex>
    23 #include <regex>
    24 #include <locale>
    24 #include <locale>
    25 #include <stdexcept>
    25 #include <stdexcept>
       
    26 #include <iomanip>
    26 
    27 
    27 #include <curl/curl.h>
    28 #include <curl/curl.h>
    28 #include <uuid/uuid.h>
    29 #include <uuid/uuid.h>
    29 
    30 
    30 #include <relpipe/common/type/typedefs.h>
    31 #include <relpipe/common/type/typedefs.h>
   127 		// uuid_generate_time(uuid);
   128 		// uuid_generate_time(uuid);
   128 		uuid_unparse_lower(uuid, buffer);
   129 		uuid_unparse_lower(uuid, buffer);
   129 		return convertor.from_bytes(buffer);
   130 		return convertor.from_bytes(buffer);
   130 	}
   131 	}
   131 
   132 
       
   133 	relpipe::common::type::StringX bodyToText(const std::string& body, bool* validEncoding = nullptr) {
       
   134 		try {
       
   135 			if (validEncoding) *validEncoding = true;
       
   136 			return convertor.from_bytes(body);
       
   137 		} catch (...) {
       
   138 			if (validEncoding) *validEncoding = false;
       
   139 			return L"";
       
   140 		}
       
   141 	}
       
   142 
       
   143 	relpipe::common::type::StringX bodyToHex(const std::string& body) {
       
   144 		std::stringstream hex;
       
   145 		hex << std::hex << std::setfill('0') << std::hex;
       
   146 		for (size_t i = 0, size = body.size(); i < size; i++) hex << std::setw(2) << (0xff & body[i]);
       
   147 		return convertor.from_bytes(hex.str());
       
   148 	}
       
   149 
   132 public:
   150 public:
   133 
   151 
   134 	HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
   152 	HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
   135 	}
   153 	}
   136 
   154 
   146 
   164 
   147 		if (currentRelationName == L"header") {
   165 		if (currentRelationName == L"header") {
   148 			// TODO: analyze header attributes
   166 			// TODO: analyze header attributes
   149 		} else if (currentRelationName == L"request") {
   167 		} else if (currentRelationName == L"request") {
   150 			relationalWriter->startRelation(L"response",{
   168 			relationalWriter->startRelation(L"response",{
   151 				// TODO: body in hexadecimal/binary format
       
   152 				{L"request", relpipe::writer::TypeId::STRING},
   169 				{L"request", relpipe::writer::TypeId::STRING},
   153 				{L"url", relpipe::writer::TypeId::STRING},
   170 				{L"url", relpipe::writer::TypeId::STRING},
   154 				{L"body", relpipe::writer::TypeId::STRING},
   171 				{L"text", relpipe::writer::TypeId::STRING},
       
   172 				{L"data", relpipe::writer::TypeId::STRING},
   155 				{L"code", relpipe::writer::TypeId::INTEGER},
   173 				{L"code", relpipe::writer::TypeId::INTEGER},
   156 			}, true);
   174 			}, true);
   157 		}
   175 		}
   158 	}
   176 	}
   159 
   177 
   222 				// TODO: move error message into separate attribute?
   240 				// TODO: move error message into separate attribute?
   223 			}
   241 			}
   224 
   242 
   225 			relationalWriter->writeAttribute(requestId);
   243 			relationalWriter->writeAttribute(requestId);
   226 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
   244 			relationalWriter->writeAttribute(convertor.from_bytes(request.url));
   227 			relationalWriter->writeAttribute(convertor.from_bytes(body));
   245 			relationalWriter->writeAttribute(bodyToText(body));
       
   246 			relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal
   228 			relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
   247 			relationalWriter->writeAttribute(&responseCode, typeid (responseCode));
   229 
   248 
   230 			request = HTTPClient::Request();
   249 			request = HTTPClient::Request();
   231 			requestId.clear();
   250 			requestId.clear();
   232 		}
   251 		}