aboutsummaryrefslogtreecommitdiff
path: root/compress.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-31 12:09:21 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-31 12:09:21 +0200
commit95456fa9d7097e92348c9aa58674a85d09208251 (patch)
treef844dc87711e033e88b67b4f29b108c0399646d2 /compress.go
parentDo not read request body if both 'Content-Length' and 'Transfer-Encoding' hea... (diff)
downloadfasthttp-95456fa9d7097e92348c9aa58674a85d09208251.tar.gz
fasthttp-95456fa9d7097e92348c9aa58674a85d09208251.tar.bz2
fasthttp-95456fa9d7097e92348c9aa58674a85d09208251.zip
Added AppendGzipBytes and AppendGunzipBytes for simplifying gzipping/gunzipping request/response bodies
Diffstat (limited to 'compress.go')
-rw-r--r--compress.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/compress.go b/compress.go
index 18e2d8d..033bb60 100644
--- a/compress.go
+++ b/compress.go
@@ -108,6 +108,50 @@ var gzipWriterPoolMap = func() map[int]*sync.Pool {
return m
}()
+// AppendGzipBytes appends gzipped src to dst and returns the resulting dst.
+func AppendGzipBytes(dst, src []byte) []byte {
+ w := &byteSliceWriter{dst}
+ zw := acquireGzipWriter(w, CompressDefaultCompression)
+ zw.Write(src)
+ releaseGzipWriter(zw)
+ return w.b
+}
+
+// AppendGunzipBytes append gunzipped src to dst and returns the resulting dst.
+func AppendGunzipBytes(dst, src []byte) ([]byte, error) {
+ r := &byteSliceReader{src}
+ zr, err := acquireGzipReader(r)
+ if err != nil {
+ return dst, err
+ }
+ w := &byteSliceWriter{dst}
+ _, err = io.Copy(w, zr)
+ releaseGzipReader(zr)
+ return w.b, err
+}
+
+type byteSliceWriter struct {
+ b []byte
+}
+
+func (w *byteSliceWriter) Write(p []byte) (int, error) {
+ w.b = append(w.b, p...)
+ return len(p), nil
+}
+
+type byteSliceReader struct {
+ b []byte
+}
+
+func (r *byteSliceReader) Read(p []byte) (int, error) {
+ if len(r.b) == 0 {
+ return 0, io.EOF
+ }
+ n := copy(p, r.b)
+ r.b = r.b[n:]
+ return n, nil
+}
+
func acquireFlateWriter(w io.Writer, level int) *flateWriter {
p := flateWriterPoolMap[level]
if p == nil {