Browse Source

Add BytesWritten() method to WriterProxy.

This makes it possible to show content length when writing the access log.
Marcus Redivo 11 years ago
parent
commit
22c4c96f3b
1 changed files with 7 additions and 0 deletions
  1. +7
    -0
      web/util/writer_proxy.go

+ 7
- 0
web/util/writer_proxy.go View File

@ -14,6 +14,8 @@ type WriterProxy interface {
// Status returns the HTTP status of the request, or 0 if one has not // Status returns the HTTP status of the request, or 0 if one has not
// yet been sent. // yet been sent.
Status() int Status() int
// BytesWritten returns the total number of bytes sent to the client.
BytesWritten() int
// Tee causes the response body to be written to the given io.Writer in // Tee causes the response body to be written to the given io.Writer in
// addition to proxying the writes through. Only one io.Writer can be // addition to proxying the writes through. Only one io.Writer can be
// tee'd to at once: setting a second one will overwrite the first. // tee'd to at once: setting a second one will overwrite the first.
@ -46,6 +48,7 @@ type basicWriter struct {
http.ResponseWriter http.ResponseWriter
wroteHeader bool wroteHeader bool
code int code int
bytes int
tee io.Writer tee io.Writer
} }
@ -66,6 +69,7 @@ func (b *basicWriter) Write(buf []byte) (int, error) {
err = err2 err = err2
} }
} }
b.bytes += n
return n, err return n, err
} }
func (b *basicWriter) maybeWriteHeader() { func (b *basicWriter) maybeWriteHeader() {
@ -76,6 +80,9 @@ func (b *basicWriter) maybeWriteHeader() {
func (b *basicWriter) Status() int { func (b *basicWriter) Status() int {
return b.code return b.code
} }
func (b *basicWriter) BytesWritten() int {
return b.bytes
}
func (b *basicWriter) Tee(w io.Writer) { func (b *basicWriter) Tee(w io.Writer) {
b.tee = w b.tee = w
} }


Loading…
Cancel
Save