From a696aefb3319e6ae24783e119ef243712ea7f668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Wed, 9 Nov 2022 10:39:33 +0100 Subject: [PATCH] http: unify error handling with https If response is nil call httpError like in https code. --- http.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/http.go b/http.go index 400219f3..6cd88106 100644 --- a/http.go +++ b/http.go @@ -47,18 +47,19 @@ func (proxy *ProxyHttpServer) handleHttp(w http.ResponseWriter, r *http.Request) resp = proxy.filterResponse(resp, ctx) if resp == nil { - var errorString string - if ctx.Error != nil { - errorString = "error read response " + r.URL.Host + " : " + ctx.Error.Error() - ctx.Logf(errorString) - http.Error(w, ctx.Error.Error(), 500) - } else { - errorString = "error read response " + r.URL.Host - ctx.Logf(errorString) - http.Error(w, errorString, 500) + hij, ok := w.(http.Hijacker) + if !ok { + panic("httpserver does not support hijacking") } + + proxyClient, _, e := hij.Hijack() + if e != nil { + panic("Cannot hijack connection " + e.Error()) + } + httpError(proxyClient, ctx, ctx.Error) return } + ctx.Logf("Copying response to client %v [%d]", resp.Status, resp.StatusCode) // http.ResponseWriter will take care of filling the correct response length // Setting it now, might impose wrong value, contradicting the actual new