/
parse.go
168 lines (158 loc) · 3.96 KB
/
parse.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
package main
import (
"bufio"
"bytes"
"io/ioutil"
"mime"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/Unknwon/macaron"
"github.com/go-floki/jade"
"github.com/kballard/go-shellquote"
"github.com/microcosm-cc/bluemonday"
"github.com/qiniu/log"
"github.com/russross/blackfriday"
)
func detectCType(path string) string {
ctype := mime.TypeByExtension(filepath.Ext(path))
if ctype != "" {
// go return charset=utf8 even if the charset is not utf8
idx := strings.Index(ctype, "; ")
if idx > 0 {
// remove charset; anyway, browsers are very good at guessing it.
ctype = ctype[0:idx]
}
}
return ctype
}
func parseCfgFile(file string, m *macaron.Macaron) error {
fd, err := os.Open(file)
if err != nil {
return err
}
defer fd.Close()
rd := bufio.NewReader(fd)
for {
bline, _, err := rd.ReadLine() // Just ignore isPrefix(maybe not good)
if err != nil {
break
}
line := string(bline)
if strings.HasPrefix(line, "#") {
continue
}
fields, err := shellquote.Split(line)
if err != nil {
log.Printf("Shellquote parse error: %v", err)
continue
}
if !(len(fields) >= 3 && len(fields) <= 4) {
continue
}
method, patten, script := fields[0], fields[1], fields[2]
contentType := ""
if len(fields) == 4 {
contentType = fields[3]
} else {
}
addRoute(m, method, patten, script, contentType)
}
return nil
}
func addRoute(m *macaron.Macaron, method, patten, script, contentType string) {
log.Println("Add Route:", method, patten, script)
method = strings.ToUpper(method)
switch method {
case "GET":
m.Get(patten, NewScriptHandler(script, contentType))
case "POST":
m.Post(patten, NewScriptHandler(script, contentType))
}
}
func runShellScript(ctx *macaron.Context, script string) ([]byte, error) {
ctx.Req.ParseForm()
//log.Println(ctx.Req.Form)
envs := map[string]string{
"REQ_PATH": ctx.Req.URL.Path,
"REQ_URI": ctx.Req.RequestURI,
"REQ_METHOD": ctx.Req.Method,
}
for key, vals := range ctx.Req.Form {
log.Debug("Form value:", key, vals)
envs["FORM_"+key] = vals[0]
}
for key, vals := range ctx.Req.PostForm {
log.Debug("Form value:", key, vals)
envs["POST_FORM_"+key] = vals[0]
}
environ := os.Environ()
for key, val := range envs {
environ = append(environ, key+"="+val)
}
cmd := exec.Command("/bin/bash", script) //.Output()
cmd.Env = environ
return cmd.Output()
}
func render(ctx *macaron.Context, output []byte, err error, contentType string) {
if err != nil {
ctx.Error(500, err.Error()+"\n"+string(output))
return
}
if contentType == "" {
contentType = "text"
}
switch contentType {
case "html":
ctx.Header().Set("Content-Type", "text/html")
case "json":
ctx.Header().Set("Content-Type", "application/json; charset=UTF-8")
case "text":
ctx.Header().Set("Content-Type", "text/plain")
default:
ctx.Header().Set("Content-Type", contentType)
}
ctx.Write(output)
}
func NewScriptHandler(script string, contentType string) func(*macaron.Context) {
ext := filepath.Ext(script)
return func(ctx *macaron.Context) {
var err error
var output []byte
errquit := func(err error) {
ctx.Error(500, err.Error()+"\n"+string(output))
return
}
switch ext {
case ".sh":
output, err = runShellScript(ctx, script)
render(ctx, output, err, contentType)
case ".html":
output, err = ioutil.ReadFile(script)
render(ctx, output, err, contentType)
case ".md":
output, err = ioutil.ReadFile(script)
if err != nil {
errquit(err)
return
}
unsafe := blackfriday.MarkdownCommon(output)
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
render(ctx, html, nil, "html")
case ".jade":
tmpl, err := jade.CompileFile(script, jade.Options{})
if err != nil {
errquit(err)
return
}
buf := bytes.NewBuffer(nil)
err = tmpl.Execute(buf, nil)
render(ctx, buf.Bytes(), err, "html")
default:
log.Warn("Unknown script ext", script, ext)
output, err = ioutil.ReadFile(script)
render(ctx, output, err, detectCType(script))
}
}
}