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

Code before:
    return generateNewPath(path_with_query, path_only, new_path_only.value());
  }

  // Handle the case where prefix_rewrite or regex_rewrite is configured.
  return rewritePathByPrefixOrRegex(path_with_query, matched_path, prefix_rewrite_,
                                    regex_rewrite_.get(), regex_rewrite_substitution_);
}

std::string RouteEntryImplBase::newUri(const Http::RequestHeaderMap& headers) const {
  ASSERT(isDirectResponse());
  return ::Envoy::Http::Utility::newUri(
      ::Envoy::makeOptRefFromPtr(
          const_cast<const ::Envoy::Http::Utility::RedirectConfig*>(redirect_config_.get())),
      headers);
}

std::string& RouteEntryImplBase::formatBody(const Http::RequestHeaderMap& request_headers,
                                            const Http::ResponseHeaderMap& response_headers,
                                            const StreamInfo::StreamInfo& stream_info,
                                            std::string& body) const {
  if (direct_response_body_provider_ != nullptr) {
    if (direct_response_body_formatter_ == nullptr) {
      // No formatting, return the body from the provider directly.
      return direct_response_body_provider_->data();
    }
    // Get a copy of the body from the provider for formatting.
    body = direct_response_body_provider_->data();
  }

  // If there is a formatter with or without a body, apply it.
  if (direct_response_body_formatter_ != nullptr) {
    body = direct_response_body_formatter_->format(
        {&request_headers, &response_headers, nullptr, body}, stream_info);
  }
  return body;
}

std::multimap<std::string, std::string>
RouteEntryImplBase::parseOpaqueConfig(const envoy::config::route::v3::Route& route) {
  std::multimap<std::string, std::string> ret;
  if (route.has_metadata()) {
    auto filter_metadata = route.metadata().filter_metadata().find("envoy.filters.http.router");
    if (filter_metadata == route.metadata().filter_metadata().end()) {
      return ret;
    }
    for (const auto& it : filter_metadata->second.fields()) {
      if (it.second.kind_case() == Protobuf::Value::kStringValue) {
        ret.emplace(it.first, it.second.string_value());
      }
    }
  }

Review comment (bug):
Hm, there's still an unnecessary copy in this path, when body (a string) is set to ->data() (a string-ref) it has to be copied - you can't update the ref.

Suggest rolling the behavior up into less branches using a local reference:
```
absl::string_view direct_body = direct_response_body_provider_ ? direct_response_body_provider_->data() : "";
if (direct_response_body_formatter_ == nullptr) {
  return direct_body;
}
body_out = direct_response_body_formatter_->format(&request_headers, &response_headers, nullptr, direct_body}, stream_info);
return body_out;
```


Code after:
    return generateNewPath(path_with_query, path_only, new_path_only.value());
  }

  // Handle the case where prefix_rewrite or regex_rewrite is configured.
  return rewritePathByPrefixOrRegex(path_with_query, matched_path, prefix_rewrite_,
                                    regex_rewrite_.get(), regex_rewrite_substitution_);
}

std::string RouteEntryImplBase::newUri(const Http::RequestHeaderMap& headers) const {
  ASSERT(isDirectResponse());
  return ::Envoy::Http::Utility::newUri(
      ::Envoy::makeOptRefFromPtr(
          const_cast<const ::Envoy::Http::Utility::RedirectConfig*>(redirect_config_.get())),
      headers);
}

absl::string_view RouteEntryImplBase::formatBody(const Http::RequestHeaderMap& request_headers,
                                                 const Http::ResponseHeaderMap& response_headers,
                                                 const StreamInfo::StreamInfo& stream_info,
                                                 std::string& body_out) const {
  absl::string_view direct_body = direct_response_body_provider_ != nullptr
                                      ? direct_response_body_provider_->data()
                                      : EMPTY_STRING;
  if (direct_response_body_formatter_ == nullptr) {
    return direct_body;
  }

  body_out = direct_response_body_formatter_->format(
      {&request_headers, &response_headers, nullptr, direct_body}, stream_info);
  return body_out;
}

std::multimap<std::string, std::string>
RouteEntryImplBase::parseOpaqueConfig(const envoy::config::route::v3::Route& route) {
  std::multimap<std::string, std::string> ret;
  if (route.has_metadata()) {
    auto filter_metadata = route.metadata().filter_metadata().find("envoy.filters.http.router");
    if (filter_metadata == route.metadata().filter_metadata().end()) {
      return ret;
    }
    for (const auto& it : filter_metadata->second.fields()) {
      if (it.second.kind_case() == Protobuf::Value::kStringValue) {
        ret.emplace(it.first, it.second.string_value());
      }
    }
  }
  return ret;
}

std::unique_ptr<HedgePolicyImpl> RouteEntryImplBase::buildHedgePolicy(
    HedgePolicyConstOptRef vhost_hedge_policy,