Skip to content

Instantly share code, notes, and snippets.

@jordic
Created July 23, 2014 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordic/56632719fd6f326342b2 to your computer and use it in GitHub Desktop.
Save jordic/56632719fd6f326342b2 to your computer and use it in GitHub Desktop.
http.ResponseWriter embed
type ResponseWriterWatcher struct {
rw http.ResponseWriter
Status int
Size int
}
func WatchResponseWriter(rw http.ResponseWriter) *ResponseWriterWatcher {
return &ResponseWriterWatcher{
rw: rw,
}
}
func (l *ResponseWriterWatcher) Header() http.Header {
return l.rw.Header()
}
func (l *ResponseWriterWatcher) Write(b []byte) (int, error) {
if l.Status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
l.Status = http.StatusOK
}
size, err := l.rw.Write(b)
l.Size += size
return size, err
}
func (l *ResponseWriterWatcher) WriteHeader(s int) {
l.rw.WriteHeader(s)
l.Status = s
}
func (l *ResponseWriterWatcher) StatusSet() bool {
return (l.Status != 0 && l.Status != 200)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment