support header filtering also by request ID pattern, not only URL pattern v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 24 Mar 2022 02:02:48 +0100
branchv_0
changeset 20 cad9f6d421ee
parent 19 0fc76872a921
child 21 1a4174f4693a
support header filtering also by request ID pattern, not only URL pattern
src/HTTPHandler.h
--- a/src/HTTPHandler.h	Tue Mar 22 22:08:03 2022 +0100
+++ b/src/HTTPHandler.h	Thu Mar 24 02:02:48 2022 +0100
@@ -48,12 +48,16 @@
 
 	class HeaderDefinition {
 	public:
+		std::wregex request = std::wregex(L".*");
 		std::wregex url = std::wregex(L".*");
 		relpipe::common::type::StringX name;
 		relpipe::common::type::StringX value;
 
-		bool matches(const relpipe::common::type::StringX& url) const {
-			return std::regex_match(url, this->url);
+		bool matches(const relpipe::common::type::StringX& request, const relpipe::common::type::StringX& url) const {
+			bool result = false;
+			result |= std::regex_match(request, this->request);
+			result |= std::regex_match(url, this->url);
+			return result;
 		}
 	};
 
@@ -156,6 +160,7 @@
 
 		if (attributeName == L"name") requestHeader.name = value;
 		else if (attributeName == L"value") requestHeader.value = value;
+		else if (attributeName == L"request") requestHeader.request = std::wregex(value.size() ? value : L".*"); // TODO: null instead of empty value (when supported)
 		else if (attributeName == L"url") requestHeader.url = std::wregex(value.size() ? value : L".*"); // TODO: null instead of empty value (when supported)
 		else throw std::invalid_argument("Unsupported attribute in the header relation: " + convertor.to_bytes(attributeName + L" = " + value));
 
@@ -184,7 +189,7 @@
 			currentAttributeIndex = 0;
 			std::shared_ptr<HTTPClient> http(HTTPClient::open());
 
-			for (const HeaderDefinition& h : requestHeaders) if (h.matches(convertor.from_bytes(request.url))) appendRequestHeader(h.name, h.value);
+			for (const HeaderDefinition& h : requestHeaders) if (h.matches(requestId, convertor.from_bytes(request.url))) appendRequestHeader(h.name, h.value);
 
 			std::string body;
 			relpipe::common::type::Integer responseCode = -1;