Skip to content

Commit

Permalink
proxy: add HTTPErrorHandler that allows for custom handling error han…
Browse files Browse the repository at this point in the history
…dling

The idea is taken from the Stripe fork [1] but instead of context it's set globally for proxy.
It gives much flexibility to error handling.

[1] https://github.com/stripe/goproxy/blob/3f1dfba6d1a4747c78fee7069e2f28fd6b703917/ctx.go#L21
  • Loading branch information
mmatczuk committed Nov 9, 2022
1 parent a696aef commit b7a3b82
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
}

func httpError(w io.WriteCloser, ctx *ProxyCtx, err error) {
if ctx.Proxy.HTTPErrorHandler != nil {
ctx.Proxy.HTTPErrorHandler(w, ctx, err)
return
}

if _, err := io.WriteString(w, "HTTP/1.1 502 Bad Gateway\r\n\r\n"); err != nil {
ctx.Warnf("Error responding to client: %s", err)
}
Expand Down
2 changes: 2 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ProxyHttpServer struct {
respHandlers []RespHandler
httpsHandlers []HttpsHandler
Tr *http.Transport
// Will be invoked to return a custom response to clients when goproxy fails to connect to a proxy target
HTTPErrorHandler func(io.WriteCloser, *ProxyCtx, error)
// ConnectDial will be used to create TCP connections for CONNECT requests
// if nil Tr.Dial will be used
ConnectDial func(network string, addr string) (net.Conn, error)
Expand Down

0 comments on commit b7a3b82

Please sign in to comment.