From f598fd0f4b59d35e2dcdd0e5c84ba91e4ce1ce29 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 15 Mar 2021 12:05:26 +0100 Subject: [PATCH] gitserver: fix fetchfile error handling since the git command output is redirected to the http response body, the golang http server automatically adds a 200 status before sending the body. If the git command fails we cannot return an http error anymore but must close the connection to let the client know that the request failed. --- internal/git-handler/handler.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/git-handler/handler.go b/internal/git-handler/handler.go index e25398d..e799b74 100644 --- a/internal/git-handler/handler.go +++ b/internal/git-handler/handler.go @@ -266,8 +266,13 @@ func (h *FetchFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if err := gitFetchFile(ctx, w, r.Body, repoAbsPath, fetchData.Ref, fetchData.Path); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - // we cannot return any http error since the http header has already been written h.log.Errorf("git command error: %v", err) + + // since we already answered with a 200 we cannot return another error code + // So abort the connection and the client will detect the missing ending chunk + // and consider this an error + // + // this is the way to force close a request without logging the panic + panic(http.ErrAbortHandler) } }