/
gzip.go
165 lines (164 loc) · 4.19 KB
/
gzip.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
157
158
159
160
161
162
163
164
165
package httpub
//import (
// "bytes"
// "compress/gzip"
// "io"
// "net/http"
// "path"
// "strings"
//
// filesystem "github.com/influx6/npkg/nfs"
//)
//
//// GzipServe returns a Handler which handles the necessary bits to gzip or ungzip
//// file resonses from a http.FileSystem.
//func GzipServe(fs filesystem.FileSystem, gzipped bool) ContextHandler {
// return func(ctx *Ctx) error {
// reqURL := path.Clean(ctx.Path())
// if reqURL == "./" || reqURL == "." {
// ctx.Redirect(http.StatusMovedPermanently, "/")
// return nil
// }
//
// if !strings.HasPrefix(reqURL, "/") {
// reqURL = "/" + reqURL
// }
//
// file, err := fs.Open(reqURL)
// if err != nil {
// return err
// }
//
// stat, err := file.Stat()
// if err != nil {
// return err
// }
//
// mime := GetFileMimeType(stat.Name())
// ctx.AddHeader("Content-Type", mime)
//
// if ctx.HasHeader("Accept-Encoding", "gzip") && gzipped {
// ctx.SetHeader("Content-Encoding", "gzip")
// defer ctx.Status(http.StatusOK)
// http.ServeContent(ctx.Response(), ctx.Request(), stat.Name(), stat.ModTime(), file)
// return nil
// }
//
// if ctx.HasHeader("Accept-Encoding", "gzip") && !gzipped {
// ctx.SetHeader("Content-Encoding", "gzip")
//
// gwriter := gzip.NewWriter(ctx.Response())
// defer gwriter.Close()
//
// _, err := io.Copy(gwriter, file)
// if err != nil && err != io.EOF {
// return err
// }
//
// ctx.Status(http.StatusOK)
//
// return nil
// }
//
// if !ctx.HasHeader("Accept-Encoding", "gzip") && gzipped {
// gzreader, err := gzip.NewReader(file)
// if err != nil {
// return err
// }
//
// var bu bytes.Buffer
// _, err = io.Copy(&bu, gzreader)
// if err != nil && err != io.EOF {
// return err
// }
//
// defer ctx.Status(http.StatusOK)
// http.ServeContent(ctx.Response(), ctx.Request(), stat.Name(), stat.ModTime(), bytes.NewReader(bu.Bytes()))
// return nil
// }
//
// defer ctx.Status(http.StatusOK)
// http.ServeContent(ctx.Response(), ctx.Request(), stat.Name(), stat.ModTime(), file)
// return nil
// }
//}
//
//// HTTPGzipServer returns a http.Handler which handles the necessary bits to gzip or ungzip
//// file resonses from a http.FileSystem.
//func HTTPGzipServer(fs http.FileSystem, gzipped bool) http.Handler {
// zipper := HTTPGzipServe(fs, gzipped)
// return handlerImpl{ContextHandler: zipper}
//}
//
//// HTTPGzipServe returns a Handler which handles the necessary bits to gzip or ungzip
//// file resonses from a http.FileSystem.
//func HTTPGzipServe(fs http.FileSystem, gzipped bool) ContextHandler {
// return func(ctx *Ctx) error {
// reqURL := path.Clean(ctx.Path())
// if reqURL == "./" || reqURL == "." {
// ctx.Redirect(http.StatusMovedPermanently, "/")
// return nil
// }
//
// if !strings.HasPrefix(reqURL, "/") {
// reqURL = "/" + reqURL
// }
//
// file, err := fs.Open(reqURL)
// if err != nil {
// return err
// }
//
// stat, err := file.Stat()
// if err != nil {
// return err
// }
//
// mime := GetFileMimeType(stat.Name())
// ctx.AddHeader("Content-Type", mime)
//
// if ctx.HasHeader("Accept-Encoding", "gzip") && gzipped {
// ctx.SetHeader("Content-Encoding", "gzip")
// defer ctx.Status(http.StatusOK)
// http.ServeContent(ctx.Response(), ctx.Request(), stat.Name(), stat.ModTime(), file)
// return nil
// }
//
// if ctx.HasHeader("Accept-Encoding", "gzip") && !gzipped {
// ctx.SetHeader("Content-Encoding", "gzip")
//
// gwriter := gzip.NewWriter(ctx.Response())
// defer gwriter.Close()
//
// _, err := io.Copy(gwriter, file)
// if err != nil && err != io.EOF {
// return err
// }
//
// ctx.Status(http.StatusOK)
//
// return nil
// }
//
// if !ctx.HasHeader("Accept-Encoding", "gzip") && gzipped {
// gzreader, err := gzip.NewReader(file)
// if err != nil {
// return err
// }
//
// var bu bytes.Buffer
// _, err = io.Copy(&bu, gzreader)
// if err != nil && err != io.EOF {
// return err
// }
//
// defer ctx.Status(http.StatusOK)
// http.ServeContent(ctx.Response(), ctx.Request(), stat.Name(), stat.ModTime(), bytes.NewReader(bu.Bytes()))
// return nil
// }
//
// defer ctx.Status(http.StatusOK)
// http.ServeContent(ctx.Response(), ctx.Request(), stat.Name(), stat.ModTime(), file)
// return nil
// }
//}