From 3a4f42e59e2ac6570a4de8e73c0bb20d34b2ae07 Mon Sep 17 00:00:00 2001 From: Imran Pochi Date: Tue, 16 Jul 2024 17:58:59 +0530 Subject: [PATCH] http-connect: error to be written on connection Incorrect error mesage from server to client because of hijack and error message being written to http.Error or w.Write, instead the error message should be written to the connection fixes: 630 Signed-off-by: Imran Pochi --- pkg/server/tunnel.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/server/tunnel.go b/pkg/server/tunnel.go index 5fb59a667..3d3ec18d2 100644 --- a/pkg/server/tunnel.go +++ b/pkg/server/tunnel.go @@ -53,13 +53,21 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, "hijacking not supported", http.StatusInternalServerError) return } - w.WriteHeader(http.StatusOK) conn, bufrw, err := hijacker.Hijack() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + + // Send the HTTP 200 OK status after a successful hijack + _, err = conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) + if err != nil { + klog.ErrorS(err, "failed to send 200 connection established") + conn.Close() + return + } + var closeOnce sync.Once defer closeOnce.Do(func() { conn.Close() }) @@ -78,7 +86,9 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) { klog.V(4).Infof("Set pending(rand=%d) to %v", random, w) backend, err := t.Server.getBackend(r.Host) if err != nil { - http.Error(w, fmt.Sprintf("currently no tunnels available: %v", err), http.StatusInternalServerError) + klog.ErrorS(err, "no tunnels available") + conn.Write([]byte(fmt.Sprintf("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\ncurrently no tunnels available: %v", err))) + conn.Close() return } closed := make(chan struct{})