Repository: envoyproxy/envoy
File: source/extensions/filters/http/ext_proc/ext_proc.cc (C++)

Code before:
// If an error response is received, sends an immediate response with an error message.
void Filter::handleErrorResponse(absl::Status processing_status) {
  ENVOY_STREAM_LOG(debug, "Sending immediate response: {}", *decoder_callbacks_,
                   processing_status.message());
  processing_complete_ = true;
  onFinishProcessorCalls(processing_status.raw_code());
  closeStream();
  ImmediateResponse invalid_mutation_response;
  invalid_mutation_response.mutable_status()->set_code(
      static_cast<StatusCode>(static_cast<uint32_t>(config_->statusOnError())));
  invalid_mutation_response.set_details(std::string(processing_status.message()));
  sendImmediateResponse(invalid_mutation_response);
}

namespace {

// DEFAULT header modes in a ProcessingResponse mode_override have no effect (they are considered
// unset). Body modes are always explicit.
ProcessingMode effectiveModeOverride(const ProcessingMode& target_override,
                                     const ProcessingMode& existing_override) {
  auto mode_override = existing_override;
  if (target_override.request_header_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_request_header_mode(target_override.request_header_mode());
  }
  if (target_override.response_header_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_response_header_mode(target_override.response_header_mode());
  }
  if (target_override.request_trailer_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_request_trailer_mode(target_override.request_trailer_mode());
  }
  if (target_override.response_trailer_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_response_trailer_mode(target_override.response_trailer_mode());
  }
  mode_override.set_request_body_mode(target_override.request_body_mode());
  mode_override.set_response_body_mode(target_override.response_body_mode());
  return mode_override;
}

bool isLastBodyResponse(ProcessorState& state,
                        const envoy::service::ext_proc::v3::ProcessingResponse& response) {
  switch (state.bodyMode()) {
  case ProcessingMode::BUFFERED:
  case ProcessingMode::BUFFERED_PARTIAL:
    // TODO: - skip stream closing optimization for BUFFERED and BUFFERED_PARTIAL for now.
    break;
  case ProcessingMode::STREAMED:
    if (!state.chunkQueue().empty()) {
      return state.chunkQueue().queue().front()->end_stream;
    }
    break;
  case ProcessingMode::FULL_DUPLEX_STREAMED: {

Review comment (suggestion):
much appreciate if you could add a method comment talking about what this method is about, and comment heavily on which modes are not supported yet. 


Code after:
  if (target_override.request_header_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_request_header_mode(target_override.request_header_mode());
  }
  if (target_override.response_header_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_response_header_mode(target_override.response_header_mode());
  }
  if (target_override.request_trailer_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_request_trailer_mode(target_override.request_trailer_mode());
  }
  if (target_override.response_trailer_mode() != ProcessingMode::DEFAULT) {
    mode_override.set_response_trailer_mode(target_override.response_trailer_mode());
  }
  mode_override.set_request_body_mode(target_override.request_body_mode());
  mode_override.set_response_body_mode(target_override.response_body_mode());
  return mode_override;
}

// Returns true if this body response is the last message in the current direction (request or
// response path). This means no further body chunks or trailers are expected in this direction.
// For now, such check is only done for STREAMED or FULL_DUPLEX_STREAMED body mode. For any
// other body mode, it always return false.
bool isLastBodyResponse(ProcessorState& state,
                        const envoy::service::ext_proc::v3::BodyResponse& body_response) {
  switch (state.bodyMode()) {
  case ProcessingMode::BUFFERED:
  case ProcessingMode::BUFFERED_PARTIAL:
    // TODO: - skip stream closing optimization for BUFFERED and BUFFERED_PARTIAL for now.
    return false;
  case ProcessingMode::STREAMED:
    if (!state.chunkQueue().empty()) {
      return state.chunkQueue().queue().front()->end_stream;
    }
    return false;
  case ProcessingMode::FULL_DUPLEX_STREAMED: {
    if (body_response.has_response() && body_response.response().has_body_mutation()) {
      const auto& body_mutation = body_response.response().body_mutation();
      if (body_mutation.has_streamed_response()) {
        return body_mutation.streamed_response().end_of_stream();
      }
    }
    return false;
  }
  default:
    break;
  }
  return false;
}

} // namespace

void Filter::closeGrpcStreamIfLastRespReceived(const ProcessingResponse& response,