forked from yaitoo/xun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_writer_deflate.go
More file actions
35 lines (30 loc) · 1.04 KB
/
Copy pathresponse_writer_deflate.go
File metadata and controls
35 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package xun
import (
"compress/flate"
)
// deflateResponseWriter is a custom http.ResponseWriter that wraps the standard
// ResponseWriter and compresses the response using the deflate algorithm.
type deflateResponseWriter struct {
*stdResponseWriter
w *flate.Writer
}
// Write writes the data to the underlying gzip writer.
// It implements the io.Writer interface.
func (rw *deflateResponseWriter) Write(p []byte) (int, error) {
n, err := rw.w.Write(p)
rw.bodySentBytes += n
return n, err
}
// Close closes the underlying writer, flushing any buffered data to the client.
// It is important to call this method to ensure all data is properly sent.
func (rw *deflateResponseWriter) Close() {
rw.w.Close()
}
// Flush writes any buffered data to the underlying writer and ensures that
// the response is sent to the client. It locks the response writer to
// prevent concurrent writes, flushes the compressed data, and then
// flushes the standard response writer.
func (rw *deflateResponseWriter) Flush() {
rw.w.Flush()
rw.stdResponseWriter.Flush()
}