Skip to content

Commit

Permalink
http-connect: error to be written on connection
Browse files Browse the repository at this point in the history
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 <imranpochi@microsoft.com>
  • Loading branch information
ipochi committed Jul 16, 2024
1 parent 94bd4ac commit 3a4f42e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/server/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() })

Expand All @@ -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{})
Expand Down

0 comments on commit 3a4f42e

Please sign in to comment.