adamdebreceni commented on code in PR #1331:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1331#discussion_r874625456
##########
extensions/http-curl/processors/InvokeHTTP.cpp:
##########
@@ -264,18 +269,37 @@ bool InvokeHTTP::shouldEmitFlowFile() const {
return ("POST" == method_ || "PUT" == method_ || "PATCH" == method_);
}
-std::optional<std::map<std::string, std::string>>
InvokeHTTP::validateAttributesAgainstHTTPHeaderRules(const
std::map<std::string, std::string>& attributes) const {
- std::map<std::string, std::string> result;
- for (const auto& [attribute_name, attribute_value] : attributes) {
- if (utils::HTTPClient::isValidHttpHeaderField(attribute_name)) {
- result.emplace(attribute_name, attribute_value);
- } else if (invalid_http_header_field_handling_strategy_ ==
InvalidHTTPHeaderFieldHandlingOption::TRANSFORM) {
-
result.emplace(utils::HTTPClient::replaceInvalidCharactersInHttpHeaderFieldName(attribute_name),
attribute_value);
- } else if (invalid_http_header_field_handling_strategy_ ==
InvalidHTTPHeaderFieldHandlingOption::FAIL) {
- return std::nullopt;
- }
+/**
+ * Calls append_header with valid HTTP header keys, based on
attributes_to_send_
+ * @param flow_file
+ * @param append_header Callback to append HTTP header to the request
+ * @return false when the flow file should be routed to failure, true otherwise
+ */
+bool InvokeHTTP::appendHeaders(const core::FlowFile& flow_file,
/*std::invocable<std::string, std::string>*/ auto append_header) {
+ static_assert(std::is_invocable_v<decltype(append_header), std::string,
std::string>);
+ if (!attributes_to_send_) return true;
+ const auto key_fn = [](const std::pair<std::string, std::string>& pair) {
return pair.first; };
+ const auto original_attributes = flow_file.getAttributes();
+ // non-const views, because otherwise it doesn't satisfy viewable_range, and
transform would fail
+ ranges::viewable_range auto matching_attributes = original_attributes
+ | ranges::views::filter([this](const auto& key) { return
utils::regexMatch(key, *attributes_to_send_); }, key_fn);
+ switch (invalid_http_header_field_handling_strategy_.value()) {
+ case InvalidHTTPHeaderFieldHandlingOption::FAIL:
+ if (ranges::any_of(matching_attributes,
std::not_fn(&utils::HTTPClient::isValidHttpHeaderField), key_fn)) return false;
+ for (const auto& header: matching_attributes)
append_header(header.first, header.second);
+ return true;
+ case InvalidHTTPHeaderFieldHandlingOption::DROP:
+ for (const auto& header: matching_attributes |
ranges::views::filter(&utils::HTTPClient::isValidHttpHeaderField, key_fn)) {
+ append_header(header.first, header.second);
+ }
+ return true;
+ case InvalidHTTPHeaderFieldHandlingOption::TRANSFORM:
+ for (const auto& header: matching_attributes) {
+
append_header(utils::HTTPClient::replaceInvalidCharactersInHttpHeaderFieldName(header.first),
header.second);
+ }
+ return true;
}
- return result;
+ return true;
Review Comment:
could you expand on the benefits of this change (usage of ranges)? it feels
more complex
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]