From 03f590d98e67b7b1d57bb0f9a4cca7e735384128 Mon Sep 17 00:00:00 2001 From: Omri Bahumi Date: Sun, 22 Aug 2021 17:04:35 +0300 Subject: [PATCH] httpError: write error string as body in 502 response --- https.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/https.go b/https.go index 6fcf17a9..e98c6cef 100644 --- a/https.go +++ b/https.go @@ -4,6 +4,7 @@ import ( "bufio" "crypto/tls" "errors" + "fmt" "io" "io/ioutil" "net" @@ -108,6 +109,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request } targetSiteCon, err := proxy.connectDial("tcp", host) if err != nil { + ctx.Warnf("Error dialing to %s: %s", host, err.Error()) httpError(proxyClient, ctx, err) return } @@ -293,7 +295,8 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request } func httpError(w io.WriteCloser, ctx *ProxyCtx, err error) { - if _, err := io.WriteString(w, "HTTP/1.1 502 Bad Gateway\r\n\r\n"); err != nil { + errStr := fmt.Sprintf("HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s", len(err.Error()), err.Error()) + if _, err := io.WriteString(w, errStr); err != nil { ctx.Warnf("Error responding to client: %s", err) } if err := w.Close(); err != nil {