Skip to content

Commit

Permalink
httpError: write error string as body in 502 response
Browse files Browse the repository at this point in the history
  • Loading branch information
omribahumi authored and elazarl committed Oct 31, 2023
1 parent 1fe6677 commit 3ec0782
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion https.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -129,6 +130,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
}
targetSiteCon, err := proxy.connectDial(ctx, "tcp", host)
if err != nil {
ctx.Warnf("Error dialing to %s: %s", host, err.Error())
httpError(proxyClient, ctx, err)
return
}
Expand Down Expand Up @@ -333,7 +335,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 {
Expand Down

0 comments on commit 3ec0782

Please sign in to comment.