-
Notifications
You must be signed in to change notification settings - Fork 8
/
init.go
40 lines (33 loc) · 908 Bytes
/
init.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
package gcurl
import (
"compress/flate"
"compress/gzip"
"io"
"log"
"net/http"
"strings"
)
var gserver *http.ServeMux
func init() {
log.SetFlags(log.Llongfile)
gserver = http.NewServeMux()
gserver.HandleFunc("/get/body-compressed", func(w http.ResponseWriter, r *http.Request) {
var writer io.Writer = w
encodings := r.Header.Get("Accept-Encoding")
if strings.Contains(encodings, "gzip") {
w.Header().Set("Content-Encoding", "gzip")
writer = gzip.NewWriter(writer)
writer.Write([]byte("hello compress"))
defer writer.(*gzip.Writer).Close()
} else if strings.Contains(encodings, "deflate") {
w.Header().Set("Content-Encoding", "deflate")
writer, err := flate.NewWriter(writer, flate.DefaultCompression)
if err != nil {
panic(err)
}
writer.Write([]byte("hello compress"))
defer writer.Close()
}
// Use r.Body to read uncompressed request body
})
}