Skip to content

Instantly share code, notes, and snippets.

@lantins
Created January 5, 2015 20:11
Show Gist options
  • Save lantins/25a7464eb3f2068c22b4 to your computer and use it in GitHub Desktop.
Save lantins/25a7464eb3f2068c22b4 to your computer and use it in GitHub Desktop.
proxy_writer.go
// proxyWriter is used to track final status and size of the
// response sent to the client
type proxyWriter struct {
http.ResponseWriter
status int
size int
}
func (r *proxyWriter) Header() http.Header {
return r.ResponseWriter.Header()
}
func (r *proxyWriter) Write(b []byte) (int, error) {
if r.status == 0 {
// default if WriteHeader has not been called yet
r.status = http.StatusOK
}
size, err := r.ResponseWriter.Write(b)
r.size += size
return size, err
}
func (r *proxyWriter) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}
func (r *proxyWriter) Status() int {
return r.status
}
func (r *proxyWriter) Size() int {
return r.size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment