Repository: envoyproxy/envoy
File: source/common/router/config_impl.cc (C++)

Code before:
        duplicate_found = !wildcard_virtual_host_prefixes_[domain.size() - 1]
                               .emplace(domain.substr(0, domain.size() - 1), virtual_host)
                               .second;
      } else {
        duplicate_found = !virtual_hosts_.emplace(domain, virtual_host).second;
      }
      if (duplicate_found) {
        creation_status = absl::InvalidArgumentError(
            fmt::format("Only unique values for domains are permitted. Duplicate "
                        "entry of domain {} in route {}",
                        domain, route_config.name()));
        return;
      }
    }
  }
}

const VirtualHostImpl* RouteMatcher::findVirtualHost(const Http::RequestHeaderMap& headers) const {
  // Fast path the case where we only have a default virtual host.
  if (virtual_hosts_.empty() && wildcard_virtual_host_suffixes_.empty() &&
      wildcard_virtual_host_prefixes_.empty()) {
    return default_virtual_host_.get();
  }

  absl::string_view host_header_value;
  if (vhost_header_ != "") {
    auto result = headers.get(Http::LowerCaseString(vhost_header_));
    // If using an alternate header, it must not be empty.
    if (result.empty()) {
      return nullptr;
    }
    host_header_value = result[0]->value().getStringView();
  } else {
    // There may be no authority in early reply paths in the HTTP connection manager.
    if (headers.Host() == nullptr) {
      return nullptr;
    }
    host_header_value = headers.getHostValue();
  }

  // If 'ignore_port_in_host_matching' is set, ignore the port number in the host header(if any).
  if (ignorePortInHostMatching()) {
    if (const absl::string_view::size_type port_start =
            Http::HeaderUtility::getPortStart(host_header_value);
        port_start != absl::string_view::npos) {
      host_header_value = host_header_value.substr(0, port_start);
    }
  }
  // TODO (@rshriram) Match Origin header in WebSocket
  // request with VHost, using wildcard match
  // Lower-case the value of the host header, as hostnames are case insensitive.

Review comment (suggestion):
```suggestion
  if (!vhost_header_.get().empty()) {
```

Code after:
        duplicate_found = !wildcard_virtual_host_prefixes_[domain.size() - 1]
                               .emplace(domain.substr(0, domain.size() - 1), virtual_host)
                               .second;
      } else {
        duplicate_found = !virtual_hosts_.emplace(domain, virtual_host).second;
      }
      if (duplicate_found) {
        creation_status = absl::InvalidArgumentError(
            fmt::format("Only unique values for domains are permitted. Duplicate "
                        "entry of domain {} in route {}",
                        domain, route_config.name()));
        return;
      }
    }
  }
}

const VirtualHostImpl* RouteMatcher::findVirtualHost(const Http::RequestHeaderMap& headers) const {
  // Fast path the case where we only have a default virtual host.
  if (virtual_hosts_.empty() && wildcard_virtual_host_suffixes_.empty() &&
      wildcard_virtual_host_prefixes_.empty()) {
    return default_virtual_host_.get();
  }

  absl::string_view host_header_value;
  if (!vhost_header_.get().empty()) {
    auto result = headers.get(vhost_header_);
    // If using an alternate header, it must not be empty.
    if (result.empty()) {
      return nullptr;
    }
    host_header_value = result[0]->value().getStringView();
  } else {
    // There may be no authority in early reply paths in the HTTP connection manager.
    if (headers.Host() == nullptr) {
      return nullptr;
    }
    host_header_value = headers.getHostValue();
  }

  // If 'ignore_port_in_host_matching' is set, ignore the port number in the host header(if any).
  if (ignorePortInHostMatching()) {
    if (const absl::string_view::size_type port_start =
            Http::HeaderUtility::getPortStart(host_header_value);
        port_start != absl::string_view::npos) {
      host_header_value = host_header_value.substr(0, port_start);
    }
  }
  // TODO (@rshriram) Match Origin header in WebSocket
  // request with VHost, using wildcard match
  // Lower-case the value of the host header, as hostnames are case insensitive.