-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathminifier.go
More file actions
36 lines (26 loc) · 781 Bytes
/
minifier.go
File metadata and controls
36 lines (26 loc) · 781 Bytes
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
package minifier
import (
"io"
"strings"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
type responseMinifier struct {
*caddyhttp.ResponseWriterWrapper
handler *Middleware
}
func (fw *responseMinifier) WriteHeader(status int) {
// we don't know the length after replacements since
// we're not buffering it all to find out
fw.Header().Del("Content-Length")
fw.ResponseWriterWrapper.WriteHeader(status)
}
func (fw *responseMinifier) Write(d []byte) (int, error) {
var writer io.WriteCloser
var mediatype = fw.ResponseWriter.Header().Get("Content-Type")
if strings.Contains(mediatype, "text/html") {
writer = fw.handler.minify.Writer(mediatype, fw.ResponseWriter)
defer writer.Close()
return writer.Write(d)
}
return fw.ResponseWriter.Write(d)
}