/
file_filter.go
66 lines (60 loc) · 1.64 KB
/
file_filter.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
package static_file
import (
"github.com/ngmoco/falcore"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
)
// A falcore RequestFilter for serving static files
// from the filesystem.
type Filter struct {
// File system base path for serving files
BasePath string
// Prefix in URL path
PathPrefix string
}
func (f *Filter) FilterRequest(req *falcore.Request) (res *http.Response) {
// Clean asset path
asset_path := filepath.Clean(filepath.FromSlash(req.HttpRequest.URL.Path))
// Resolve PathPrefix
if strings.HasPrefix(asset_path, f.PathPrefix) {
asset_path = asset_path[len(f.PathPrefix):]
} else {
falcore.Debug("%v doesn't match prefix %v", asset_path, f.PathPrefix)
res = falcore.SimpleResponse(req.HttpRequest, 404, nil, "Not found.")
return
}
// Resolve FSBase
if f.BasePath != "" {
asset_path = filepath.Join(f.BasePath, asset_path)
} else {
falcore.Error("file_filter requires a BasePath")
return falcore.SimpleResponse(req.HttpRequest, 500, nil, "Server Error\n")
}
// Open File
if file, err := os.Open(asset_path); err == nil {
// Make sure it's an actual file
if stat, err := file.Stat(); err == nil && stat.Mode()&os.ModeType == 0 {
res = &http.Response{
Request: req.HttpRequest,
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: file,
Header: make(http.Header),
ContentLength: stat.Size(),
}
if ct := mime.TypeByExtension(filepath.Ext(asset_path)); ct != "" {
res.Header.Set("Content-Type", ct)
}
} else {
file.Close()
}
} else {
falcore.Finest("Can't open %v: %v", asset_path, err)
}
return
}