1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-29 15:57:09 +00:00
Former-commit-id: 8a8a25ab8fb33a342c8d05fc7eae7cafd5bb02b2
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-13 01:40:57 +02:00
parent 5667bfb9f0
commit d76d9b1ec6
10 changed files with 1017 additions and 71 deletions

View File

@@ -13,7 +13,9 @@ import (
type gzipResponseWriter struct {
ResponseWriter
gzipWriter *gzip.Writer
gzipWriter *gzip.Writer // it contains the underline writer too
chunks []byte
disabled bool
}
var gzpool = sync.Pool{New: func() interface{} { return &gzipResponseWriter{} }}
@@ -22,6 +24,8 @@ func acquireGzipResponseWriter(underline ResponseWriter) *gzipResponseWriter {
w := gzpool.Get().(*gzipResponseWriter)
w.ResponseWriter = underline
w.gzipWriter = acquireGzipWriter(w.ResponseWriter)
w.chunks = w.chunks[0:0]
w.disabled = false
return w
}
@@ -32,7 +36,32 @@ func releaseGzipResponseWriter(w *gzipResponseWriter) {
// Write compresses and writes that data to the underline response writer
func (w *gzipResponseWriter) Write(contents []byte) (int, error) {
return w.gzipWriter.Write(contents)
// save the contents to serve them (only gzip data here)
w.chunks = append(w.chunks, contents...)
return len(w.chunks), nil
}
func (w *gzipResponseWriter) flushResponse() {
if w.disabled {
w.ResponseWriter.Write(w.chunks)
} else {
// if it's not disable write all chunks gzip compressed
w.gzipWriter.Write(w.chunks) // it writes to the underline responseWriter (look acquireGzipResponseWriter)
}
w.ResponseWriter.flushResponse()
}
func (w *gzipResponseWriter) ResetBody() {
w.chunks = w.chunks[0:0]
}
func (w *gzipResponseWriter) Disable() {
w.disabled = true
}
func (w *gzipResponseWriter) releaseMe() {
releaseGzipResponseWriter(w)
w.ResponseWriter.releaseMe()
}
var rpool = sync.Pool{New: func() interface{} { return &responseWriter{statusCode: StatusOK} }}