Repository: envoyproxy/envoy
File: source/extensions/geoip_providers/maxmind/geoip_provider.cc (C++)

Code before:
          populateGeoLookupResult(mmdb_lookup_result, lookup_result,
                                  config_->anonTorHeader().value(), MMDB_ANON_LOOKUP_ARGS[3]);
        }
        if (config_->isLookupEnabledForHeader(config_->anonProxyHeader())) {
          populateGeoLookupResult(mmdb_lookup_result, lookup_result,
                                  config_->anonProxyHeader().value(), MMDB_ANON_LOOKUP_ARGS[4]);
        }
        if (lookup_result.size() > n_prev_hits) {
          config_->incHit(ANON_DB_TYPE);
        }
        MMDB_free_entry_data_list(entry_data_list);
      } else {
        config_->incLookupError(ANON_DB_TYPE);
      }
    }
    config_->incTotal(ANON_DB_TYPE);
  }
}

void GeoipProvider::lookupInIspDb(
    const Network::Address::InstanceConstSharedPtr& remote_address,
    absl::flat_hash_map<std::string, std::string>& lookup_result) const {
  if (config_->isLookupEnabledForHeader(config_->ispHeader()) ||
      config_->isLookupEnabledForHeader(config_->applePrivateRelayHeader()) ||
      (!config_->isAsnDbPathSet() && config_->isLookupEnabledForHeader(config_->asnHeader())) ||
      (!config_->isAsnDbPathSet() && config_->isLookupEnabledForHeader(config_->asnOrgHeader()))) {
    int mmdb_error;
    auto isp_db_ptr = getIspDb();
    // Used for testing.
    synchronizer_.syncPoint(std::string(ISP_DB_TYPE).append("_lookup_pre_complete"));
    if (!isp_db_ptr) {
      IS_ENVOY_BUG("Maxmind isp database must be initialised for performing lookups");
      return;
    }
    auto isp_db = isp_db_ptr.get();
    MMDB_lookup_result_s mmdb_lookup_result = MMDB_lookup_sockaddr(
        isp_db->mmdb(), reinterpret_cast<const sockaddr*>(remote_address->sockAddr()), &mmdb_error);
    const uint32_t n_prev_hits = lookup_result.size();
    if (!mmdb_error && mmdb_lookup_result.found_entry) {
      MMDB_entry_data_list_s* entry_data_list;
      int status = MMDB_get_entry_data_list(&mmdb_lookup_result.entry, &entry_data_list);
      if (status == MMDB_SUCCESS) {
        if (config_->isLookupEnabledForHeader(config_->ispHeader())) {
          populateGeoLookupResult(mmdb_lookup_result, lookup_result, config_->ispHeader().value(),
                                  MMDB_ISP_LOOKUP_ARGS[0]);
        }
        if (config_->isLookupEnabledForHeader(config_->applePrivateRelayHeader())) {
          populateGeoLookupResult(mmdb_lookup_result, lookup_result,
                                  config_->applePrivateRelayHeader().value(),
                                  MMDB_ISP_LOOKUP_ARGS[0]);
          if (lookup_result.find(config_->applePrivateRelayHeader().value()) !=

Review comment (suggestion):
this can be simplified to:
```
!config_->isAsnDbPathSet() && (config_->isLookupEnabledForHeader(config_->asnHeader()) || config_->isLookupEnabledForHeader(config_->asnOrgHeader()))
```

Code after:
          populateGeoLookupResult(mmdb_lookup_result, lookup_result,
                                  config_->anonTorHeader().value(), MMDB_ANON_LOOKUP_ARGS[3]);
        }
        if (config_->isLookupEnabledForHeader(config_->anonProxyHeader())) {
          populateGeoLookupResult(mmdb_lookup_result, lookup_result,
                                  config_->anonProxyHeader().value(), MMDB_ANON_LOOKUP_ARGS[4]);
        }
        if (lookup_result.size() > n_prev_hits) {
          config_->incHit(ANON_DB_TYPE);
        }
        MMDB_free_entry_data_list(entry_data_list);
      } else {
        config_->incLookupError(ANON_DB_TYPE);
      }
    }
    config_->incTotal(ANON_DB_TYPE);
  }
}

void GeoipProvider::lookupInIspDb(
    const Network::Address::InstanceConstSharedPtr& remote_address,
    absl::flat_hash_map<std::string, std::string>& lookup_result) const {
  if (config_->isLookupEnabledForHeader(config_->ispHeader()) ||
      config_->isLookupEnabledForHeader(config_->applePrivateRelayHeader()) ||
      (!config_->isAsnDbPathSet() &&
       (config_->isLookupEnabledForHeader(config_->asnHeader()) ||
        config_->isLookupEnabledForHeader(config_->asnOrgHeader())))) {
    int mmdb_error;
    auto isp_db_ptr = getIspDb();
    // Used for testing.
    synchronizer_.syncPoint(std::string(ISP_DB_TYPE).append("_lookup_pre_complete"));
    if (!isp_db_ptr) {
      IS_ENVOY_BUG("Maxmind isp database must be initialised for performing lookups");
      return;
    }
    auto isp_db = isp_db_ptr.get();
    MMDB_lookup_result_s mmdb_lookup_result = MMDB_lookup_sockaddr(
        isp_db->mmdb(), reinterpret_cast<const sockaddr*>(remote_address->sockAddr()), &mmdb_error);
    const uint32_t n_prev_hits = lookup_result.size();
    if (!mmdb_error && mmdb_lookup_result.found_entry) {
      MMDB_entry_data_list_s* entry_data_list;
      int status = MMDB_get_entry_data_list(&mmdb_lookup_result.entry, &entry_data_list);
      if (status == MMDB_SUCCESS) {
        if (config_->isLookupEnabledForHeader(config_->ispHeader())) {
          populateGeoLookupResult(mmdb_lookup_result, lookup_result, config_->ispHeader().value(),
                                  MMDB_ISP_LOOKUP_ARGS[0]);
        }
        if (config_->isLookupEnabledForHeader(config_->applePrivateRelayHeader())) {
          populateGeoLookupResult(mmdb_lookup_result, lookup_result,
                                  config_->applePrivateRelayHeader().value(),
                                  MMDB_ISP_LOOKUP_ARGS[0]);