-
Notifications
You must be signed in to change notification settings - Fork 6
/
staticfile.go
233 lines (210 loc) · 6.1 KB
/
staticfile.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package foolgo
import (
"bytes"
"compress/flate"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
const (
COMPRESS_CLOSE = -1
COMPRESS_GZIP = 1
COMPRESS_FLATE = 2
)
var (
GzipExt = []string{".css", ".js", ".html"}
)
func AddCompressType(ext string) {
if ext != "" {
GzipExt = append(GzipExt, ext)
}
}
func OutStaticFile(response *Response, request *Request, file string) { /*{{{*/
file_path := response.server_config.Root + file
fi, err := os.Stat(file_path)
if err != nil && os.IsNotExist(err) {
OutErrorHtml(response, request, http.StatusNotFound)
return
} else if fi.IsDir() == true {
OutErrorHtml(response, request, http.StatusForbidden)
return
}
file_size := fi.Size()
mod_time := fi.ModTime()
if CompressType == COMPRESS_CLOSE || file_size < int64(CompressMinSize) || (strings.Index(request.Header("Accept-Encoding"), "gzip") < 0 && strings.Index(request.Header("Accept-Encoding"), "flate") < 0) {
http.ServeFile(response.Writer, request.request, file_path)
return
}
is_gzip := false
for _, ext := range GzipExt {
if strings.HasSuffix(strings.ToLower(file), strings.ToLower(ext)) {
is_gzip = true
break
}
}
if is_gzip == false {
http.ServeFile(response.Writer, request.request, file_path)
return
}
osfile, err := os.Open(file_path)
if err != nil {
OutErrorHtml(response, request, http.StatusNotFound)
return
}
var b bytes.Buffer
switch CompressType {
case COMPRESS_GZIP:
output_writer, err := gzip.NewWriterLevel(&b, gzip.BestCompression)
if err != nil {
OutErrorHtml(response, request, http.StatusNotFound)
return
}
_, err = io.Copy(output_writer, osfile)
output_writer.Close()
case COMPRESS_FLATE:
output_writer, err := flate.NewWriter(&b, flate.BestSpeed)
if err != nil {
OutErrorHtml(response, request, http.StatusNotFound)
return
}
_, err = io.Copy(output_writer, osfile)
output_writer.Close()
}
if err != nil {
OutErrorHtml(response, request, http.StatusNotFound)
return
}
content, err := ioutil.ReadAll(&b)
if err != nil {
OutErrorHtml(response, request, http.StatusNotFound)
return
}
cfi := &memFileInfo{fi, mod_time, content, int64(len(content)), file_size}
mf := &memFile{cfi, 0}
switch CompressType {
case COMPRESS_GZIP:
response.Header("Content-Encoding", "gzip")
case COMPRESS_FLATE:
response.Header("Content-Encoding", "deflate")
}
http.ServeContent(response.Writer, request.request, file_path, mod_time, mf)
} /*}}}*/
func OutErrorHtml(response *Response, request *Request, http_code int) { /*{{{*/
response.Header("Status", fmt.Sprintf("%d", http_code))
if err_html, ok := response.server_config.HttpErrorHtml[http_code]; ok == true {
if fi, err := os.Stat(err_html); (err == nil || os.IsExist(err)) && fi.IsDir() != true {
http.ServeFile(response.Writer, request.request, err_html)
return
}
}
error_list := make(map[int]string)
error_list[403] = `
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>foolgo/1.0.0</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
`
error_list[404] = `
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>foolgo/1.0.0</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
`
error_list[500] = `
<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>foolgo/1.0.0</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
`
response.Header("Content-Type", "text/html; charset=utf-8")
response.Header("X-Content-Type-Options", "nosniff")
response.Writer.WriteHeader(http_code)
fmt.Fprintln(response.Writer, error_list[http_code])
} /*}}}*/
type memFile struct {
fi *memFileInfo
offset int64
}
// Close memfile.
func (f *memFile) Close() error {
return nil
}
// Get os.FileInfo of memfile.
func (f *memFile) Stat() (os.FileInfo, error) {
return f.fi, nil
}
// read os.FileInfo of files in directory of memfile.
// it returns empty slice.
func (f *memFile) Readdir(count int) ([]os.FileInfo, error) {
infos := []os.FileInfo{}
return infos, nil
}
func (f *memFile) Read(p []byte) (n int, err error) {
if len(f.fi.content)-int(f.offset) >= len(p) {
n = len(p)
} else {
n = len(f.fi.content) - int(f.offset)
err = io.EOF
}
copy(p, f.fi.content[f.offset:f.offset+int64(n)])
f.offset += int64(n)
return
}
var errWhence = errors.New("Seek: invalid whence")
var errOffset = errors.New("Seek: invalid offset")
func (f *memFile) Seek(offset int64, whence int) (ret int64, err error) {
switch whence {
default:
return 0, errWhence
case os.SEEK_SET:
case os.SEEK_CUR:
offset += f.offset
case os.SEEK_END:
offset += int64(len(f.fi.content))
}
if offset < 0 || int(offset) > len(f.fi.content) {
return 0, errOffset
}
f.offset = offset
return f.offset, nil
}
type memFileInfo struct {
os.FileInfo
modTime time.Time
content []byte
contentSize int64
fileSize int64
}