Copilot commented on code in PR #12234:
URL: https://github.com/apache/trafficserver/pull/12234#discussion_r2160417331


##########
plugins/header_rewrite/conditions.cc:
##########
@@ -800,41 +800,95 @@ ConditionGeo::initialize(Parser &p)
     match->set(p.get_arg(), mods());
     _matcher = std::move(match);
   }
+  require_resources(RSRC_CLIENT_REQUEST_HEADERS);
 }
 
 void
 ConditionGeo::set_qualifier(const std::string &q)
 {
-  Condition::set_qualifier(q);
+  std::string qualifier = q;
+  std::size_t colon_pos = q.find(':');
+
+  if (colon_pos != std::string::npos) {
+    std::string header;
+
+    qualifier = q.substr(0, colon_pos);
+    header    = q.substr(colon_pos + 1);
 
-  Dbg(pi_dbg_ctl, "\tParsing %%{GEO:%s} qualifier", q.c_str());
+    if (header.empty() || header.find_first_of(" \t") != std::string::npos) {
+      TSError("[%s] Invalid header name in GEO qualifier: '%s'", PLUGIN_NAME, 
header.c_str());
+      return;
+    }
+
+    _ip_header = header;
+  }
 
-  if (q == "COUNTRY") {
+  Condition::set_qualifier(qualifier);
+
+  if (_ip_header.empty()) {
+    Dbg(pi_dbg_ctl, "\tParsing %%{GEO:%s} qualifier", qualifier.c_str());
+  } else {
+    Dbg(pi_dbg_ctl, "\tParsing %%{GEO:%s:%s} qualifier", qualifier.c_str(), 
_ip_header.c_str());
+  }
+
+  if (qualifier == "COUNTRY") {
     _geo_qual = GEO_QUAL_COUNTRY;
     is_int_type(false);
-  } else if (q == "COUNTRY-ISO") {
+  } else if (qualifier == "COUNTRY-ISO") {
     _geo_qual = GEO_QUAL_COUNTRY_ISO;
     is_int_type(true);
-  } else if (q == "ASN") {
+  } else if (qualifier == "ASN") {
     _geo_qual = GEO_QUAL_ASN;
     is_int_type(true);
-  } else if (q == "ASN-NAME") {
+  } else if (qualifier == "ASN-NAME") {
     _geo_qual = GEO_QUAL_ASN_NAME;
     is_int_type(false);
   } else {
-    TSError("[%s] Unknown Geo() qualifier: %s", PLUGIN_NAME, q.c_str());
+    TSError("[%s] Unknown Geo() qualifier: %s", PLUGIN_NAME, 
qualifier.c_str());
+  }
+}
+
+static const sockaddr *
+_geo_ip_helper(const std::string &header, const Resources &res, struct 
sockaddr &addr_buf)
+{
+  if (header.empty()) {
+    return TSHttpTxnClientAddrGet(res.txnp);
+  } else {
+    int len = 0;
+
+    if (res.client_bufp && res.client_hdr_loc) {
+      TSMLoc field_loc = TSMimeHdrFieldFind(res.client_bufp, 
res.client_hdr_loc, header.c_str(), header.size());
+
+      if (field_loc) {
+        const char *value = TSMimeHdrFieldValueStringGet(res.client_bufp, 
res.client_hdr_loc, field_loc, -1, &len);
+
+        if (len > 0 && value) {
+          if (TSIpStringToAddr(value, len, &addr_buf) == TS_SUCCESS) {

Review Comment:
   Potential resource leak: the field location obtained from TSMimeHdrFieldFind 
is not released if TSIpStringToAddr succeeds. Consider calling 
TSHandleMLocRelease before returning the address.
   ```suggestion
             if (TSIpStringToAddr(value, len, &addr_buf) == TS_SUCCESS) {
               TSHandleMLocRelease(res.client_bufp, res.client_hdr_loc, 
field_loc);
   ```



-- 
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: github-unsubscr...@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to