equal
deleted
inserted
replaced
38 |
38 |
39 #include <relpipe/cli/RelpipeCLIException.h> |
39 #include <relpipe/cli/RelpipeCLIException.h> |
40 |
40 |
41 #include "Configuration.h" |
41 #include "Configuration.h" |
42 #include "HTTPClient.h" |
42 #include "HTTPClient.h" |
|
43 #include "Hex.h" |
43 |
44 |
44 namespace relpipe { |
45 namespace relpipe { |
45 namespace tr { |
46 namespace tr { |
46 namespace http { |
47 namespace http { |
47 |
48 |
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 |
132 relpipe::common::type::StringX bodyToText(const std::string& body, bool* validEncoding = nullptr) { |
|
133 try { |
|
134 if (validEncoding) *validEncoding = true; |
|
135 // TODO: use encoding from the HTTP response headers instead of the constant one? |
|
136 return convertor.from_bytes(body); |
|
137 } catch (...) { |
|
138 if (validEncoding) *validEncoding = false; |
|
139 std::stringstream filtered; |
|
140 for (char ch : body) filtered << (ch >= ' ' && ch < 127 ? ch : '.'); |
|
141 return convertor.from_bytes(filtered.str()); |
|
142 } |
|
143 } |
|
144 |
|
145 relpipe::common::type::StringX bodyToHex(const std::string& body) { |
|
146 std::stringstream hex; |
|
147 hex << std::hex << std::setfill('0') << std::hex; |
|
148 for (size_t i = 0, size = body.size(); i < size; i++) hex << std::setw(2) << (0xff & body[i]); |
|
149 return convertor.from_bytes(hex.str()); |
|
150 } |
|
151 |
|
152 public: |
133 public: |
153 |
134 |
154 HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
135 HTTPHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
155 } |
136 } |
156 |
137 |
209 auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName(); |
190 auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName(); |
210 |
191 |
211 if (attributeName == L"id") requestId = value; |
192 if (attributeName == L"id") requestId = value; |
212 else if (attributeName == L"url") request.url = convertor.to_bytes(value); |
193 else if (attributeName == L"url") request.url = convertor.to_bytes(value); |
213 else if (attributeName == L"method") request.method = parseMethod(value); |
194 else if (attributeName == L"method") request.method = parseMethod(value); |
214 else if (attributeName == L"text") request.body = convertor.to_bytes(value); |
195 else if (attributeName == L"text" && value.size()) request.body = convertor.to_bytes(value); |
215 else if (attributeName == L"data") request.body = "TODO: read binary data: " + convertor.to_bytes(value); // TODO: read hex/binary request body |
196 else if (attributeName == L"data" && value.size()) request.body = Hex::fromHex(value).str(); |
|
197 else if (attributeName == L"text"); // keep empty or value from 'data' |
|
198 else if (attributeName == L"data"); // keep empty or value from 'text' |
216 else if (isHeaderAttribute(attributeName)) appendRequestHeader(fetchHeaderName(attributeName), value); |
199 else if (isHeaderAttribute(attributeName)) appendRequestHeader(fetchHeaderName(attributeName), value); |
217 else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(attributeName + L" = " + value)); |
200 else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(attributeName + L" = " + value)); |
218 |
201 |
219 currentAttributeIndex++; |
202 currentAttributeIndex++; |
220 |
203 |
246 } |
229 } |
247 |
230 |
248 bool validText = false; |
231 bool validText = false; |
249 relationalWriter->writeAttribute(requestId); |
232 relationalWriter->writeAttribute(requestId); |
250 relationalWriter->writeAttribute(convertor.from_bytes(request.url)); |
233 relationalWriter->writeAttribute(convertor.from_bytes(request.url)); |
251 relationalWriter->writeAttribute(bodyToText(body, &validText)); |
234 relationalWriter->writeAttribute(Hex::toTxt(body, &validText)); |
252 relationalWriter->writeAttribute(bodyToHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal |
235 relationalWriter->writeAttribute(Hex::toHex(body)); // TODO: return as an octet-string (when supported) instead of hexadecimal |
253 relationalWriter->writeAttribute(&validText, typeid (validText)); |
236 relationalWriter->writeAttribute(&validText, typeid (validText)); |
254 relationalWriter->writeAttribute(&responseCode, typeid (responseCode)); |
237 relationalWriter->writeAttribute(&responseCode, typeid (responseCode)); |
255 |
238 |
256 request = HTTPClient::Request(); |
239 request = HTTPClient::Request(); |
257 requestId.clear(); |
240 requestId.clear(); |