Skip to content

Instantly share code, notes, and snippets.

@lantins
Created January 5, 2015 20:11
Show Gist options
  • Select an option

  • Save lantins/25a7464eb3f2068c22b4 to your computer and use it in GitHub Desktop.

Select an option

Save lantins/25a7464eb3f2068c22b4 to your computer and use it in GitHub Desktop.

Revisions

  1. lantins created this gist Jan 5, 2015.
    34 changes: 34 additions & 0 deletions proxy_writer.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // 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
    }