feat: add flush and finish for compression writer
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"errors"
|
||||||
"mime"
|
"mime"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -378,3 +379,44 @@ func (w *compressionWriter) Write(data []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
return w.gzip.Write(data)
|
return w.gzip.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:unused
|
||||||
|
func (w *compressionWriter) Flush() {
|
||||||
|
if w.mode == compressionUndecided {
|
||||||
|
if w.canCompress(w.buffer.Bytes()) {
|
||||||
|
w.startGzip()
|
||||||
|
_ = w.flushBuffer()
|
||||||
|
} else {
|
||||||
|
w.startPlain()
|
||||||
|
_ = w.flushBuffer()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if w.mode == compressionGzip {
|
||||||
|
_ = w.gzip.Flush()
|
||||||
|
}
|
||||||
|
w.ResponseWriter.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:unused
|
||||||
|
func (w *compressionWriter) finish() error {
|
||||||
|
if w.mode == compressionUndecided {
|
||||||
|
if w.Status() == http.StatusNotModified {
|
||||||
|
addVary(w.Header(), "Accept-Encoding")
|
||||||
|
}
|
||||||
|
if w.buffer.Len() > 0 || w.status != 0 {
|
||||||
|
w.startPlain()
|
||||||
|
if err := w.flushBuffer(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.ResponseWriter.WriteHeaderNow()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if w.mode == compressionPlain {
|
||||||
|
return w.flushBuffer()
|
||||||
|
}
|
||||||
|
if w.gzip == nil {
|
||||||
|
return errors.New("gzip writer was not initialized")
|
||||||
|
}
|
||||||
|
return w.gzip.Close()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user