Skip to content

Commit 24feecc

Browse files
authored
Merge pull request #640 from kinvolk/imran/http-connect-log-improvement
http-connect: error to be written on connection
2 parents 707e0c9 + 3a4f42e commit 24feecc

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pkg/server/tunnel.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,21 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5353
http.Error(w, "hijacking not supported", http.StatusInternalServerError)
5454
return
5555
}
56-
w.WriteHeader(http.StatusOK)
5756

5857
conn, bufrw, err := hijacker.Hijack()
5958
if err != nil {
6059
http.Error(w, err.Error(), http.StatusInternalServerError)
6160
return
6261
}
62+
63+
// Send the HTTP 200 OK status after a successful hijack
64+
_, err = conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
65+
if err != nil {
66+
klog.ErrorS(err, "failed to send 200 connection established")
67+
conn.Close()
68+
return
69+
}
70+
6371
var closeOnce sync.Once
6472
defer closeOnce.Do(func() { conn.Close() })
6573

@@ -78,7 +86,9 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7886
klog.V(4).Infof("Set pending(rand=%d) to %v", random, w)
7987
backend, err := t.Server.getBackend(r.Host)
8088
if err != nil {
81-
http.Error(w, fmt.Sprintf("currently no tunnels available: %v", err), http.StatusInternalServerError)
89+
klog.ErrorS(err, "no tunnels available")
90+
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)))
91+
conn.Close()
8292
return
8393
}
8494
closed := make(chan struct{})

0 commit comments

Comments
 (0)