-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip_deflate.go
86 lines (68 loc) · 1.78 KB
/
gzip_deflate.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
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
var name string
type helloWorldResponse struct {
Message string `json:"message"`
}
func main() {
port := 8080
http.Handle("/helloworld",
NewGzipHandler(http.HandlerFunc(helloWorldHandler)),
)
log.Printf("Server starting on port %v\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
func helloWorldHandler(rw http.ResponseWriter, r *http.Request) {
response := helloWorldResponse{Message: "Hello " + name}
encoder := json.NewEncoder(rw)
encoder.Encode(response)
}
func NewGzipHandler(next http.Handler) http.Handler {
return &GZipHandler{next}
}
type GZipHandler struct {
next http.Handler
}
func (h *GZipHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
encodings := r.Header.Get("Accept-Encoding")
if strings.Contains(encodings, "gzip") {
h.serveGzipped(w, r)
} else if strings.Contains(encodings, "deflate") {
panic("Deflate not implemented")
} else {
h.servePlain(w, r)
}
}
func (h *GZipHandler) serveGzipped(w http.ResponseWriter, r *http.Request) {
gzw := gzip.NewWriter(w)
defer gzw.Close()
w.Header().Set("Content-Encoding", "gzip")
h.next.ServeHTTP(GzipResponseWriter{gzw, w}, r)
}
func (h *GZipHandler) servePlain(w http.ResponseWriter, r *http.Request) {
h.next.ServeHTTP(w, r)
}
type GzipResponseWriter struct {
gw *gzip.Writer
http.ResponseWriter
}
func (w GzipResponseWriter) Write(b []byte) (int, error) {
if _, ok := w.Header()["Content-Type"]; !ok {
// If content type is not set, infer it from the uncompressed body.
w.Header().Set("Content-Type", http.DetectContentType(b))
}
return w.gw.Write(b)
}
func (w GzipResponseWriter) Flush() {
w.gw.Flush()
if fw, ok := w.ResponseWriter.(http.Flusher); ok {
fw.Flush()
}
}