logger: avoid writing audit log response header twice (#10642)

This commit fixes a misuse of the `http.ResponseWriter.WriteHeader`.
A caller should **either** call `WriteHeader` exactly once **or**
write to the response writer and causing an implicit 200 OK.

Writing the response headers more than once causes a `http: superfluous
response.WriteHeader call` log message. This commit fixes this
by preventing a 2nd `WriteHeader` call being forwarded to the underlying
`ResponseWriter`.

Updates #10587
master
Andreas Auernhammer 4 years ago committed by GitHub
parent effe131090
commit ed6d2a100f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      cmd/logger/audit.go

@ -68,8 +68,7 @@ func (lrw *ResponseWriter) Write(p []byte) (int, error) {
if !lrw.headersLogged { if !lrw.headersLogged {
// We assume the response code to be '200 OK' when WriteHeader() is not called, // We assume the response code to be '200 OK' when WriteHeader() is not called,
// that way following Golang HTTP response behavior. // that way following Golang HTTP response behavior.
lrw.writeHeaders(&lrw.headers, http.StatusOK, lrw.Header()) lrw.WriteHeader(http.StatusOK)
lrw.headersLogged = true
} }
if (lrw.LogErrBody && lrw.StatusCode >= http.StatusBadRequest) || lrw.LogAllBody { if (lrw.LogErrBody && lrw.StatusCode >= http.StatusBadRequest) || lrw.LogAllBody {
// Always logging error responses. // Always logging error responses.
@ -107,12 +106,12 @@ func (lrw *ResponseWriter) Body() []byte {
// WriteHeader - writes http status code // WriteHeader - writes http status code
func (lrw *ResponseWriter) WriteHeader(code int) { func (lrw *ResponseWriter) WriteHeader(code int) {
lrw.StatusCode = code
if !lrw.headersLogged { if !lrw.headersLogged {
lrw.StatusCode = code
lrw.writeHeaders(&lrw.headers, code, lrw.ResponseWriter.Header()) lrw.writeHeaders(&lrw.headers, code, lrw.ResponseWriter.Header())
lrw.headersLogged = true lrw.headersLogged = true
lrw.ResponseWriter.WriteHeader(code)
} }
lrw.ResponseWriter.WriteHeader(code)
} }
// Flush - Calls the underlying Flush. // Flush - Calls the underlying Flush.

Loading…
Cancel
Save