This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
compress.go
156 lines (130 loc) · 3.38 KB
/
compress.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"bufio"
"compress/flate"
"compress/gzip"
"fmt"
"io"
"net"
"net/http"
"path"
"strings"
)
// some http headers
const (
HeaderAcceptEncoding = "Accept-Encoding"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderVary = "Vary"
)
// Compresser defines the interface return compress type
type Compresser interface {
CompressType() string
}
// GZip implements gzip Compresser
type GZip struct{}
// CompressType returns compress type
func (GZip) CompressType() string {
return "gzip"
}
// Deflate implements deflate Compresser
type Deflate struct{}
// CompressType returns compress type
func (Deflate) CompressType() string {
return "deflate"
}
// Compress implements auto Compresser
type Compress struct{}
// CompressType returns compress type
func (Compress) CompressType() string {
return "auto"
}
// Compresses defines a middleware to compress HTTP response
func Compresses(exts []string) HandlerFunc {
extsmap := make(map[string]bool)
for _, ext := range exts {
extsmap[strings.ToLower(ext)] = true
}
return func(ctx *Context) {
ae := ctx.Req().Header.Get("Accept-Encoding")
if ae == "" {
ctx.Next()
return
}
if len(extsmap) > 0 {
ext := strings.ToLower(path.Ext(ctx.Req().URL.Path))
if _, ok := extsmap[ext]; ok {
compress(ctx, "auto")
return
}
}
if action := ctx.Action(); action != nil {
if c, ok := action.(Compresser); ok {
compress(ctx, c.CompressType())
return
}
}
// if blank, then no compress
ctx.Next()
}
}
func compress(ctx *Context, compressType string) {
ae := ctx.Req().Header.Get("Accept-Encoding")
acceptCompress := strings.SplitN(ae, ",", -1)
var writer io.Writer
var val string
for _, val = range acceptCompress {
val = strings.TrimSpace(val)
if compressType == "auto" || val == compressType {
if val == "gzip" {
ctx.Header().Set("Content-Encoding", "gzip")
writer = gzip.NewWriter(ctx.ResponseWriter)
break
} else if val == "deflate" {
ctx.Header().Set("Content-Encoding", "deflate")
writer, _ = flate.NewWriter(ctx.ResponseWriter, flate.BestSpeed)
break
}
}
}
// not supported compress method, then ignore
if writer == nil {
ctx.Next()
return
}
// for cache server
ctx.Header().Add(HeaderVary, "Accept-Encoding")
gzw := &compressWriter{writer, ctx.ResponseWriter}
ctx.ResponseWriter = gzw
ctx.Next()
// delete content length after we know we have been written to
gzw.Header().Del(HeaderContentLength)
ctx.ResponseWriter = gzw.ResponseWriter
switch writer.(type) {
case *gzip.Writer:
writer.(*gzip.Writer).Close()
case *flate.Writer:
writer.(*flate.Writer).Close()
}
}
type compressWriter struct {
w io.Writer
ResponseWriter
}
func (grw *compressWriter) Write(p []byte) (int, error) {
if len(grw.Header().Get(HeaderContentType)) == 0 {
grw.Header().Set(HeaderContentType, http.DetectContentType(p))
}
return grw.w.Write(p)
}
func (grw *compressWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := grw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}