Fixes responseWriter.Size()

- "written" boolean is not longer needed
- size is reseted
- addition instead of assignation
This commit is contained in:
Manu Mtz-Almeida 2014-09-03 04:25:08 +02:00
parent daedc0bc17
commit 21e53c0db6
1 changed files with 12 additions and 9 deletions

View File

@ -12,6 +12,10 @@ import (
"net/http" "net/http"
) )
const (
NoWritten = -1
)
type ( type (
ResponseWriter interface { ResponseWriter interface {
http.ResponseWriter http.ResponseWriter
@ -27,30 +31,29 @@ type (
responseWriter struct { responseWriter struct {
http.ResponseWriter http.ResponseWriter
status int status int
size int size int
written bool
} }
) )
func (w *responseWriter) reset(writer http.ResponseWriter) { func (w *responseWriter) reset(writer http.ResponseWriter) {
w.ResponseWriter = writer w.ResponseWriter = writer
w.status = 200 w.status = 200
w.written = false w.size = NoWritten
} }
func (w *responseWriter) WriteHeader(code int) { func (w *responseWriter) WriteHeader(code int) {
if code > 0 { if code > 0 {
w.status = code w.status = code
if w.written { if w.Written() {
log.Println("[GIN] WARNING. Headers were already written!") log.Println("[GIN] WARNING. Headers were already written!")
} }
} }
} }
func (w *responseWriter) WriteHeaderNow() { func (w *responseWriter) WriteHeaderNow() {
if !w.written { if !w.Written() {
w.written = true w.size = 0
w.ResponseWriter.WriteHeader(w.status) w.ResponseWriter.WriteHeader(w.status)
} }
} }
@ -58,7 +61,7 @@ func (w *responseWriter) WriteHeaderNow() {
func (w *responseWriter) Write(data []byte) (n int, err error) { func (w *responseWriter) Write(data []byte) (n int, err error) {
w.WriteHeaderNow() w.WriteHeaderNow()
n, err = w.ResponseWriter.Write(data) n, err = w.ResponseWriter.Write(data)
w.size = n w.size += n
return return
} }
@ -71,7 +74,7 @@ func (w *responseWriter) Size() int {
} }
func (w *responseWriter) Written() bool { func (w *responseWriter) Written() bool {
return w.written return w.size != NoWritten
} }
// Implements the http.Hijacker interface // Implements the http.Hijacker interface