-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
123 lines (114 loc) · 2.46 KB
/
server.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
package httpfilter
import (
"bytes"
"io/ioutil"
"mime"
"net/http"
"path/filepath"
"strings"
)
var filterFileName = "_filters.txt"
type Server struct {
root string
filter string
ops map[string]OpFunc
}
func NewServer(root string, filter string, ops ...map[string]OpFunc) *Server {
sv := &Server{
root: root,
filter: filter,
}
sv.ops = map[string]OpFunc{
"serve": sv.serveFile,
"ignore": ignore,
"redirect": redirect,
"proxy": proxy,
"request": request,
}
for _, m := range ops {
for k, v := range m {
sv.ops[k] = v
}
}
return sv
}
func (sv *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
wr := wrapWriter(w)
name := filepath.Join(sv.root, req.URL.Path)
dir, name := filepath.Split(name)
var filter string
if sv.filter == "" {
filter = filepath.Join(dir, filterFileName)
} else {
filter = sv.filter
}
if p, err := ioutil.ReadFile(filter); err == nil {
sv.parseFilter(p, wr, req)
}
if _, ok := <-wr.ok; ok {
sv.serveFile(w, req, name)
}
}
func (sv *Server) serveFile(w http.ResponseWriter, req *http.Request, args ...string) {
path := filepath.Join(sv.root, filepath.Dir(req.URL.Path), args[0])
name := filepath.Base(path)
if name == filterFileName {
http.Error(w, "Not found.", 404)
return
}
b, err := ioutil.ReadFile(path)
if err != nil {
http.Error(w, "Not found.", 404)
return
}
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(name)))
w.Write(b)
}
func match(req *http.Request, s string) bool {
q := ""
if s[0] == byte('@') {
q = req.Host
s = s[1:]
} else {
q = filepath.Base(req.URL.Path)
}
re := matchExtensions(q, s)
return re
}
func matchExtensions(q string, s string) bool {
qsplit := strings.Split(q, ".")
ssplit := strings.Split(s, ".")
if len(ssplit) == 1 && ssplit[0] == "*" {
return true
}
if len(ssplit) != len(qsplit) {
return false;
}
for i := 0; i < len(ssplit); i++ {
if ssplit[i] != "*" && ssplit[i] != qsplit[i] {
return false
}
}
return true
}
func (sv *Server) parseFilter(p []byte, wr *writerWrapper, req *http.Request) {
lines := bytes.Split(p, []byte{'\n'})
var op []byte
for _, line := range lines {
vals := make([]string, 0)
for _, word := range bytes.Fields(line) {
if word[0] == '#' {
op = word[1:]
} else {
vals = append(vals, string(word))
}
}
if len(vals) > 0 && match(req, vals[0]) {
sv.ops[string(op)](wr, req, vals[1:]...)
if _, ok := <-wr.ok; !ok {
break
}
wr.ok <- 0
}
}
}