serrislew commented on code in PR #12813:
URL: https://github.com/apache/trafficserver/pull/12813#discussion_r2766598937


##########
src/proxy/http/remap/RemapYamlConfig.cc:
##########
@@ -0,0 +1,1128 @@
+/** @file
+ *
+ *  YAML remap configuration file parsing implementation.
+ *
+ *  @section license License
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+#include "proxy/http/remap/RemapYamlConfig.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <filesystem>
+#include <algorithm>
+#include <string>
+#include <string_view>
+#include <vector>
+
+#include "tscore/Diags.h"
+#include "tscore/ink_string.h"
+#include "tsutil/ts_errata.h"
+#include "tsutil/PostScript.h"
+#include "swoc/bwf_base.h"
+#include "swoc/bwf_ex.h"
+#include "swoc/swoc_file.h"
+
+#include "proxy/http/remap/UrlRewrite.h"
+#include "proxy/http/remap/UrlMapping.h"
+#include "proxy/http/remap/RemapConfig.h"
+#include "proxy/http/remap/AclFiltering.h"
+#include "records/RecCore.h"
+
+namespace
+{
+DbgCtl dbg_ctl_remap_yaml{"remap_yaml"};
+DbgCtl dbg_ctl_url_rewrite{"url_rewrite"};
+
+/** will process the regex mapping configuration and create objects in
+    output argument reg_map. It assumes existing data in reg_map is
+    inconsequential and will be perfunctorily null-ed;
+*/
+static bool
+process_regex_mapping_config(const char *from_host_lower, url_mapping 
*new_mapping, UrlRewrite::RegexMapping *reg_map)
+{
+  std::string_view to_host{};
+  int              to_host_len;
+  int              substitution_id;
+  int32_t          captures;
+
+  reg_map->to_url_host_template     = nullptr;
+  reg_map->to_url_host_template_len = 0;
+  reg_map->n_substitutions          = 0;
+
+  reg_map->url_map = new_mapping;
+
+  // using from_host_lower (and not new_mapping->fromURL.host_get())
+  // as this one will be nullptr-terminated (required by pcre_compile)
+  if (reg_map->regular_expression.compile(from_host_lower) == false) {
+    Warning("pcre_compile failed! Regex has error starting at %s", 
from_host_lower);
+    goto lFail;
+  }
+
+  captures = reg_map->regular_expression.get_capture_count();
+  if (captures == -1) {
+    Warning("pcre_fullinfo failed!");
+    goto lFail;
+  }
+  if (captures >= UrlRewrite::MAX_REGEX_SUBS) { // off by one for $0 (implicit 
capture)
+    Warning("regex has %d capturing subpatterns (including entire regex); Max 
allowed: %d", captures + 1,
+            UrlRewrite::MAX_REGEX_SUBS);
+    goto lFail;
+  }
+
+  to_host     = new_mapping->toURL.host_get();
+  to_host_len = static_cast<int>(to_host.length());
+  for (int i = 0; i < to_host_len - 1; ++i) {
+    if (to_host[i] == '$') {
+      substitution_id = to_host[i + 1] - '0';
+      if ((substitution_id < 0) || (substitution_id > captures)) {
+        Warning("Substitution id [%c] has no corresponding capture pattern in 
regex [%s]", to_host[i + 1], from_host_lower);
+        goto lFail;
+      }
+      reg_map->substitution_markers[reg_map->n_substitutions] = i;
+      reg_map->substitution_ids[reg_map->n_substitutions]     = 
substitution_id;
+      ++reg_map->n_substitutions;
+    }
+  }
+
+  // so the regex itself is stored in fromURL.host; string to match
+  // will be in the request; string to use for substitutions will be
+  // in this buffer
+  reg_map->to_url_host_template_len = to_host_len;
+  reg_map->to_url_host_template     = static_cast<char 
*>(ats_malloc(to_host_len));
+  memcpy(reg_map->to_url_host_template, to_host.data(), to_host_len);
+
+  return true;
+
+lFail:
+  ats_free(reg_map->to_url_host_template);
+  reg_map->to_url_host_template     = nullptr;
+  reg_map->to_url_host_template_len = 0;
+
+  return false;
+}
+} // end anonymous namespace
+
+swoc::Errata
+parse_yaml_url(const YAML::Node &node, URL &url, bool host_check, 
std::string_view &url_str)
+{
+  if (!node || !node.IsMap()) {
+    return swoc::Errata("URL must be a map");
+  }
+  url.create(nullptr);
+
+  // Use url first if defined
+  ParseResult rparse;
+  if (node["url"]) {
+    url_str = node["url"].as<std::string_view>();
+    if (host_check) {
+      rparse = url.parse_regex(url_str);
+    } else {
+      rparse = url.parse_no_host_check(url_str);
+    }
+    if (rparse != ParseResult::DONE) {
+      return swoc::Errata("malformed URL: {}", url_str);
+    }
+
+    return {};
+  }
+
+  // Build URL string from components
+  if (node["scheme"]) {
+    url.scheme_set(node["scheme"].as<std::string>());
+  }
+
+  if (node["host"]) {
+    url.host_set(node["host"].as<std::string>());
+  }
+
+  if (node["port"]) {
+    url.port_set(node["port"].as<int>());
+  }
+
+  if (node["path"]) {
+    url.path_set(node["path"].as<std::string>());
+  }
+
+  return {};
+}
+
+swoc::Errata
+remap_validate_yaml_filter_args(acl_filter_rule **rule_pp, const YAML::Node 
&node, ACLBehaviorPolicy behavior_policy)
+{
+  acl_filter_rule *rule;
+  int              j;
+  bool             new_rule_flg = false;
+
+  if (!rule_pp) {
+    Dbg(dbg_ctl_url_rewrite, "[validate_filter_args] Invalid argument(s)");
+    return swoc::Errata("Invalid argument(s)");
+  }
+
+  if (dbg_ctl_url_rewrite.on()) {
+    printf("validate_filter_args: ");
+    for (const auto &rule : node) {
+      printf("\"%s\" ", rule.first.as<std::string>().c_str());
+    }
+    printf("\n");
+  }

Review Comment:
   Yeah this was also copied from `RemapConfig.cc`. I will update, thanks!



-- 
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]

Reply via email to