Last active
August 29, 2015 14:18
-
-
Save tiborvass/35df501ecd73bb32203b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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