Skip to content

Instantly share code, notes, and snippets.

@tiborvass
Last active August 29, 2015 14:18
Show Gist options
  • Save tiborvass/35df501ecd73bb32203b to your computer and use it in GitHub Desktop.
Save tiborvass/35df501ecd73bb32203b to your computer and use it in GitHub Desktop.
package httputils
import (
"net/http"
"sync"
)
type StatusResponseWriter interface {
http.ResponseWriter
StatusSent() bool
}
func ToStatusResponseWriter(w http.ResponseWriter) StatusResponseWriter {
return &statusResponseWriter{w: w}
}
type statusResponseWriter struct {
sync.Mutex
w http.ResponseWriter
used bool
}
func (srw *statusResponseWriter) Header() http.Header {
return srw.w.Header()
}
func (srw *statusResponseWriter) Write(p []byte) (n int, err error) {
srw.Lock()
srw.used = true
srw.Unlock()
return srw.w.Write(p)
}
func (srw *statusResponseWriter) WriteHeader(code int) {
srw.Lock()
srw.used = true
srw.Unlock()
srw.w.WriteHeader(code)
}
func (srw *statusResponseWriter) StatusSent() bool {
srw.Lock()
used := srw.used
srw.Unlock()
return used
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment