github.com/thanos-io/thanos@v0.32.5/pkg/server/http/utils.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package http
     5  
     6  import "net/http"
     7  
     8  // ResponseWriterWithStatus wraps around http.ResponseWriter to capture the status code of the response.
     9  type ResponseWriterWithStatus struct {
    10  	http.ResponseWriter
    11  	status          int
    12  	isHeaderWritten bool
    13  }
    14  
    15  // WrapResponseWriterWithStatus wraps the http.ResponseWriter for extracting status.
    16  func WrapResponseWriterWithStatus(w http.ResponseWriter) *ResponseWriterWithStatus {
    17  	return &ResponseWriterWithStatus{ResponseWriter: w}
    18  }
    19  
    20  // Status returns http response status.
    21  func (r *ResponseWriterWithStatus) Status() int {
    22  	return r.status
    23  }
    24  
    25  // WriteHeader writes the header.
    26  func (r *ResponseWriterWithStatus) WriteHeader(code int) {
    27  	if !r.isHeaderWritten {
    28  		r.status = code
    29  		r.ResponseWriter.WriteHeader(code)
    30  		r.isHeaderWritten = true
    31  	}
    32  }