Abstract adding Connection: close header to avoid triggering H2 draining logic (#8178)

diff --git a/proxy/ProxyTransaction.cc b/proxy/ProxyTransaction.cc
index 3f1ace9..cb80b3c 100644
--- a/proxy/ProxyTransaction.cc
+++ b/proxy/ProxyTransaction.cc
@@ -240,3 +240,11 @@
 {
   return false;
 }
+
+// Most protocols will not want to set the Connection: header
+// For H2 it will initiate the drain logic.  So we make do nothing
+// the default action.
+void
+ProxyTransaction::set_close_connection(HTTPHdr &hdr) const
+{
+}
diff --git a/proxy/ProxyTransaction.h b/proxy/ProxyTransaction.h
index 82a07d6..261af68 100644
--- a/proxy/ProxyTransaction.h
+++ b/proxy/ProxyTransaction.h
@@ -87,6 +87,10 @@
   // Returns true if there is a request body for this request
   virtual bool has_request_body(int64_t content_length, bool is_chunked_set) const;
 
+  // Worker function to set Connection:close header if appropriate for
+  // underlying protocol
+  virtual void set_close_connection(HTTPHdr &hdr) const;
+
   sockaddr const *get_remote_addr() const;
 
   virtual HTTPVersion get_version(HTTPHdr &hdr) const;
diff --git a/proxy/http/Http1Transaction.h b/proxy/http/Http1Transaction.h
index be0a174..aeb1d3a 100644
--- a/proxy/http/Http1Transaction.h
+++ b/proxy/http/Http1Transaction.h
@@ -43,6 +43,7 @@
   // Methods
   int get_transaction_id() const override;
   void set_reader(IOBufferReader *reader);
+  void set_close_connection(HTTPHdr &hdr) const override;
 
   ////////////////////
   // Variables
@@ -71,3 +72,9 @@
 {
   _reader = reader;
 }
+
+inline void
+Http1Transaction::set_close_connection(HTTPHdr &hdr) const
+{
+  hdr.value_set(MIME_FIELD_CONNECTION, MIME_LEN_CONNECTION, "close", 5);
+}
diff --git a/proxy/http/HttpSM.cc b/proxy/http/HttpSM.cc
index 67d18b8..4f6e06d 100644
--- a/proxy/http/HttpSM.cc
+++ b/proxy/http/HttpSM.cc
@@ -5873,7 +5873,7 @@
 
 close_connection:
   t_state.client_info.keep_alive = HTTP_NO_KEEPALIVE;
-  response.value_set(MIME_FIELD_CONNECTION, MIME_LEN_CONNECTION, "close", 5);
+  ua_txn->set_close_connection(response);
 }
 
 void
diff --git a/proxy/http/HttpTransact.cc b/proxy/http/HttpTransact.cc
index 6e66aa4..ce2d7fa 100644
--- a/proxy/http/HttpTransact.cc
+++ b/proxy/http/HttpTransact.cc
@@ -6891,7 +6891,10 @@
         if (s->current.request_to == PARENT_PROXY && parent_is_proxy(s)) {
           heads->value_set(MIME_FIELD_PROXY_CONNECTION, MIME_LEN_PROXY_CONNECTION, "close", 5);
         } else {
-          heads->value_set(MIME_FIELD_CONNECTION, MIME_LEN_CONNECTION, "close", 5);
+          ProxyTransaction *svr = s->state_machine->get_server_txn();
+          if (svr) {
+            svr->set_close_connection(*heads);
+          }
         }
       }
       // Note: if we are 1.1, we always need to send the close
@@ -7050,7 +7053,11 @@
   case KA_CLOSE:
   case KA_DISABLED:
     if (s->client_info.keep_alive != HTTP_NO_KEEPALIVE || (ver == HTTP_1_1)) {
-      heads->value_set(c_hdr_field_str, c_hdr_field_len, "close", 5);
+      if (s->client_info.proxy_connect_hdr) {
+        heads->value_set(c_hdr_field_str, c_hdr_field_len, "close", 5);
+      } else if (s->state_machine->ua_txn != nullptr) {
+        s->state_machine->ua_txn->set_close_connection(*heads);
+      }
       s->client_info.keep_alive = HTTP_NO_KEEPALIVE;
     }
     // Note: if we are 1.1, we always need to send the close