Repository: envoyproxy/envoy
File: source/extensions/filters/http/geoip/geoip_filter.h (C/C++)

Code before:
 */
class GeoipFilterConfig {
public:
  GeoipFilterConfig(const envoy::extensions::filters::http::geoip::v3::Geoip& config,
                    const std::string& stat_prefix, Stats::Scope& scope);

  void incTotal() { incCounter(stat_name_set_->getBuiltin("total", unknown_hit_)); }

  bool useXff() const { return use_xff_; }
  uint32_t xffNumTrustedHops() const { return xff_num_trusted_hops_; }

  // Returns true if a custom header should be used to extract the IP address.
  bool useIpAddressHeader() const { return !ip_address_header_.get().empty(); }
  // Returns the header name to use for extracting the IP address.
  const Http::LowerCaseString& ipAddressHeader() const { return ip_address_header_; }

private:
  void incCounter(Stats::StatName name);

  Stats::Scope& scope_;
  Stats::StatNameSetPtr stat_name_set_;
  const Stats::StatName stats_prefix_;
  const Stats::StatName unknown_hit_;
  bool use_xff_;
  const uint32_t xff_num_trusted_hops_;
  const Http::LowerCaseString ip_address_header_;
};

using GeoipFilterConfigSharedPtr = std::shared_ptr<GeoipFilterConfig>;

class GeoipFilter : public Http::StreamDecoderFilter,
                    public Logger::Loggable<Logger::Id::filter>,
                    public std::enable_shared_from_this<GeoipFilter> {
public:
  GeoipFilter(GeoipFilterConfigSharedPtr config, Geolocation::DriverSharedPtr driver);
  ~GeoipFilter() override;

  // Http::StreamFilterBase
  void onDestroy() override;

  // Http::StreamDecoderFilter
  Http::FilterHeadersStatus decodeHeaders(Http::RequestHeaderMap& headers,
                                          bool end_stream) override;
  Http::FilterDataStatus decodeData(Buffer::Instance& data, bool end_stream) override;
  Http::FilterTrailersStatus decodeTrailers(Http::RequestTrailerMap& trailers) override;
  void setDecoderFilterCallbacks(Http::StreamDecoderFilterCallbacks& callbacks) override;
  // Callbacks for geolocation filter when lookup is complete.
  void onLookupComplete(Geolocation::LookupResult&& result);

private:
  // Allow the unit test to have access to private members.

Review comment (suggestion):
would better make it optional and set to nullopt when not configured

Code after:
 */
class GeoipFilterConfig {
public:
  GeoipFilterConfig(const envoy::extensions::filters::http::geoip::v3::Geoip& config,
                    const std::string& stat_prefix, Stats::Scope& scope);

  void incTotal() { incCounter(stat_name_set_->getBuiltin("total", unknown_hit_)); }

  bool useXff() const { return use_xff_; }
  uint32_t xffNumTrustedHops() const { return xff_num_trusted_hops_; }

  // Returns the custom header name to use for extracting the IP address, if configured.
  const absl::optional<Http::LowerCaseString>& ipAddressHeader() const {
    return ip_address_header_;
  }

private:
  void incCounter(Stats::StatName name);

  Stats::Scope& scope_;
  Stats::StatNameSetPtr stat_name_set_;
  const Stats::StatName stats_prefix_;
  const Stats::StatName unknown_hit_;
  bool use_xff_;
  const uint32_t xff_num_trusted_hops_;
  absl::optional<Http::LowerCaseString> ip_address_header_;
};

using GeoipFilterConfigSharedPtr = std::shared_ptr<GeoipFilterConfig>;

class GeoipFilter : public Http::StreamDecoderFilter,
                    public Logger::Loggable<Logger::Id::filter>,
                    public std::enable_shared_from_this<GeoipFilter> {
public:
  GeoipFilter(GeoipFilterConfigSharedPtr config, Geolocation::DriverSharedPtr driver);
  ~GeoipFilter() override;

  // Http::StreamFilterBase
  void onDestroy() override;

  // Http::StreamDecoderFilter
  Http::FilterHeadersStatus decodeHeaders(Http::RequestHeaderMap& headers,
                                          bool end_stream) override;
  Http::FilterDataStatus decodeData(Buffer::Instance& data, bool end_stream) override;
  Http::FilterTrailersStatus decodeTrailers(Http::RequestTrailerMap& trailers) override;
  void setDecoderFilterCallbacks(Http::StreamDecoderFilterCallbacks& callbacks) override;
  // Callbacks for geolocation filter when lookup is complete.
  void onLookupComplete(Geolocation::LookupResult&& result);

private:
  // Allow the unit test to have access to private members.