src/HTTPHandler.h
branchv_0
changeset 18 d8efcefdf906
parent 17 aa43616375c6
child 19 0fc76872a921
equal deleted inserted replaced
17:aa43616375c6 18:d8efcefdf906
    19 #include <memory>
    19 #include <memory>
    20 #include <string>
    20 #include <string>
    21 #include <vector>
    21 #include <vector>
    22 #include <codecvt>
    22 #include <codecvt>
    23 #include <regex>
    23 #include <regex>
       
    24 #include <locale>
    24 #include <stdexcept>
    25 #include <stdexcept>
    25 
    26 
    26 #include <curl/curl.h>
    27 #include <curl/curl.h>
    27 
    28 
    28 #include <relpipe/common/type/typedefs.h>
    29 #include <relpipe/common/type/typedefs.h>
    45 class HTTPHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    46 class HTTPHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    46 private:
    47 private:
    47 
    48 
    48 	class HeaderDefinition {
    49 	class HeaderDefinition {
    49 	public:
    50 	public:
    50 		// TODO: filters/patterns/condition
    51 		std::wregex url = std::wregex(L".*");
    51 		relpipe::common::type::StringX name;
    52 		relpipe::common::type::StringX name;
    52 		relpipe::common::type::StringX value;
    53 		relpipe::common::type::StringX value;
       
    54 
       
    55 		bool matches(const relpipe::common::type::StringX& url) const {
       
    56 			return std::regex_match(url, this->url);
       
    57 		}
    53 	};
    58 	};
    54 
    59 
    55 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    60 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    56 	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
    61 	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
    57 	Configuration configuration;
    62 	Configuration configuration;
   148 	void headerAttribute(const relpipe::common::type::StringX& value) {
   153 	void headerAttribute(const relpipe::common::type::StringX& value) {
   149 		auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName();
   154 		auto attributeName = currentReaderMetadata[currentAttributeIndex].getAttributeName();
   150 
   155 
   151 		if (attributeName == L"name") requestHeader.name = value;
   156 		if (attributeName == L"name") requestHeader.name = value;
   152 		else if (attributeName == L"value") requestHeader.value = value;
   157 		else if (attributeName == L"value") requestHeader.value = value;
       
   158 		else if (attributeName == L"url") requestHeader.url = std::wregex(value.size() ? value : L".*"); // TODO: null instead of empty value (when supported)
   153 		else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(attributeName + L" = " + value));
   159 		else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(attributeName + L" = " + value));
   154 
   160 
   155 		currentAttributeIndex++;
   161 		currentAttributeIndex++;
   156 
   162 
   157 		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
   163 		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
   174 
   180 
   175 		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
   181 		if (currentAttributeIndex % currentReaderMetadata.size() == 0) {
   176 			currentAttributeIndex = 0;
   182 			currentAttributeIndex = 0;
   177 			std::shared_ptr<HTTPClient> http(HTTPClient::open());
   183 			std::shared_ptr<HTTPClient> http(HTTPClient::open());
   178 
   184 
   179 			for (const HeaderDefinition& h : requestHeaders) appendRequestHeader(h.name, h.value);
   185 			for (const HeaderDefinition& h : requestHeaders) if (h.matches(convertor.from_bytes(request.url))) appendRequestHeader(h.name, h.value);
   180 
   186 
   181 			std::string body;
   187 			std::string body;
   182 			relpipe::common::type::Integer responseCode = -1;
   188 			relpipe::common::type::Integer responseCode = -1;
   183 
   189 
   184 			try {
   190 			try {