lib/httpserver: call Request.Header() only once instead of calling it each time a new request header is set

This is a follow-up for ad839aa492
This commit is contained in:
Aliaksandr Valialkin 2023-10-31 18:33:40 +01:00
parent 68f82b1c06
commit efb6ac27c2
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -242,20 +242,21 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
} }
}() }()
h := w.Header()
if *headerHSTS != "" { if *headerHSTS != "" {
w.Header().Add("Strict-Transport-Security", *headerHSTS) h.Add("Strict-Transport-Security", *headerHSTS)
} }
if *headerFrameOptions != "" { if *headerFrameOptions != "" {
w.Header().Add("X-Frame-Options", *headerFrameOptions) h.Add("X-Frame-Options", *headerFrameOptions)
} }
if *headerCSP != "" { if *headerCSP != "" {
w.Header().Add("Content-Security-Policy", *headerCSP) h.Add("Content-Security-Policy", *headerCSP)
} }
w.Header().Add("X-Server-Hostname", hostname) h.Add("X-Server-Hostname", hostname)
requestsTotal.Inc() requestsTotal.Inc()
if whetherToCloseConn(r) { if whetherToCloseConn(r) {
connTimeoutClosedConns.Inc() connTimeoutClosedConns.Inc()
w.Header().Set("Connection", "close") h.Set("Connection", "close")
} }
path := r.URL.Path path := r.URL.Path
prefix := GetPathPrefix() prefix := GetPathPrefix()
@ -280,7 +281,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
} }
switch r.URL.Path { switch r.URL.Path {
case "/health": case "/health":
w.Header().Set("Content-Type", "text/plain; charset=utf-8") h.Set("Content-Type", "text/plain; charset=utf-8")
deadline := atomic.LoadInt64(&s.shutdownDelayDeadline) deadline := atomic.LoadInt64(&s.shutdownDelayDeadline)
if deadline <= 0 { if deadline <= 0 {
w.Write([]byte("OK")) w.Write([]byte("OK"))
@ -315,7 +316,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
return return
} }
startTime := time.Now() startTime := time.Now()
w.Header().Set("Content-Type", "text/plain; charset=utf-8") h.Set("Content-Type", "text/plain; charset=utf-8")
appmetrics.WritePrometheusMetrics(w) appmetrics.WritePrometheusMetrics(w)
metricsHandlerDuration.UpdateDuration(startTime) metricsHandlerDuration.UpdateDuration(startTime)
return return
@ -323,7 +324,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
if !CheckAuthFlag(w, r, *flagsAuthKey, "flagsAuthKey") { if !CheckAuthFlag(w, r, *flagsAuthKey, "flagsAuthKey") {
return return
} }
w.Header().Set("Content-Type", "text/plain; charset=utf-8") h.Set("Content-Type", "text/plain; charset=utf-8")
flagutil.WriteFlags(w) flagutil.WriteFlags(w)
return return
case "/-/healthy": case "/-/healthy":