Created
July 23, 2014 13:34
-
-
Save jordic/56632719fd6f326342b2 to your computer and use it in GitHub Desktop.
http.ResponseWriter embed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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