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.
This commit is contained in:
Simone Gotti 2021-03-15 12:05:26 +01:00
parent 097caaf871
commit f598fd0f4b
1 changed files with 7 additions and 2 deletions

View File

@ -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)
}
}