Repository: envoyproxy/envoy
File: source/extensions/transport_sockets/http_11_proxy/connect.cc (C++)

Code before:
  // The absence of proxy info from the transport socket options means that we should use the host
  // address of the provided HostDescription if it has the appropriate metadata set.
  for (auto& metadata : {host->metadata(), host->localityMetadata()}) {
    if (metadata == nullptr) {
      continue;
    }

    const bool has_proxy_addr = metadata->typed_filter_metadata().contains(
        Config::MetadataFilters::get().ENVOY_HTTP11_PROXY_TRANSPORT_SOCKET_ADDR);
    if (has_proxy_addr) {
      if (!Runtime::runtimeFeatureEnabled(
              "envoy.reloadable_features.http_11_proxy_connect_legacy_format")) {
        // Prefer <host-name>:<port> for RFC 9110 compliance, unless URI is <host-ip>:<port>.
        std::string target;
        if (!host->hostname().empty()) {
          const uint32_t port = host->address()->ip()->port();
          target = absl::StrCat(host->hostname(), ":", port);
        } else {
          target = host->address()->asStringView();
        }
        header_buffer_.add(formatConnectRequest(target));
      } else {
        // Legacy behavior: <host-ip>:<port> format, no Host header for backward compatibility.
        header_buffer_.add(
            absl::StrCat("CONNECT ", host->address()->asStringView(), " HTTP/1.1\r\n\r\n"));
      }
      need_to_strip_connect_response_ = true;
    }
  }
}

void UpstreamHttp11ConnectSocket::setTransportSocketCallbacks(
    Network::TransportSocketCallbacks& callbacks) {
  transport_socket_->setTransportSocketCallbacks(callbacks);
  callbacks_ = &callbacks;
}

Network::IoResult UpstreamHttp11ConnectSocket::doWrite(Buffer::Instance& buffer, bool end_stream) {
  if (header_buffer_.length() > 0) {
    return writeHeader();
  }
  if (!need_to_strip_connect_response_) {
    // Don't pass events up until the connect response is read because TLS reads
    // kick off writes which don't pass through the transport socket.
    return transport_socket_->doWrite(buffer, end_stream);
  }
  return Network::IoResult{Network::PostIoAction::KeepOpen, 0, false};
}

Network::IoResult UpstreamHttp11ConnectSocket::doRead(Buffer::Instance& buffer) {
  if (need_to_strip_connect_response_) {

Review comment (refactor):
Can you address this nesting by either refactoring or breaking some of the logic into another function? It's just too deep.

Code after:

inline void UpstreamHttp11ConnectSocket::handleProxyInfoConnect() {
  if (transport_socket_->ssl()) {
    std::string target = absl::StrCat(
        options_->http11ProxyInfo()->hostname,
        Http::HeaderUtility::hostHasPort(options_->http11ProxyInfo()->hostname) ? "" : ":443");

    if (!Runtime::runtimeFeatureEnabled(
            "envoy.reloadable_features.http_11_proxy_connect_legacy_format")) {
      // RFC 9110 compliant CONNECT format that includes Host header.
      header_buffer_.add(formatConnectRequest(target));
    } else {
      // Legacy behavior: no Host header for backward compatibility.
      header_buffer_.add(absl::StrCat("CONNECT ", target, " HTTP/1.1\r\n\r\n"));
    }
    need_to_strip_connect_response_ = true;
  }
}

inline void UpstreamHttp11ConnectSocket::handleHostMetadataConnect(
    std::shared_ptr<const Upstream::HostDescription> host) {
  if (!Runtime::runtimeFeatureEnabled(
          "envoy.reloadable_features.http_11_proxy_connect_legacy_format")) {
    // Prefer <host-name>:<port> for RFC 9110 compliance, unless URI is <host-ip>:<port>.
    std::string target;
    if (!host->hostname().empty()) {
      const uint32_t port = host->address()->ip()->port();
      target = absl::StrCat(host->hostname(), ":", port);
    } else {
      target = host->address()->asStringView();
    }
    header_buffer_.add(formatConnectRequest(target));
  } else {
    // Legacy behavior: <host-ip>:<port> format, no Host header for backward compatibility.
    header_buffer_.add(
        absl::StrCat("CONNECT ", host->address()->asStringView(), " HTTP/1.1\r\n\r\n"));
  }
  need_to_strip_connect_response_ = true;
}

void UpstreamHttp11ConnectSocket::setTransportSocketCallbacks(
    Network::TransportSocketCallbacks& callbacks) {
  transport_socket_->setTransportSocketCallbacks(callbacks);
  callbacks_ = &callbacks;
}

Network::IoResult UpstreamHttp11ConnectSocket::doWrite(Buffer::Instance& buffer, bool end_stream) {
  if (header_buffer_.length() > 0) {
    return writeHeader();
  }
  if (!need_to_strip_connect_response_) {