/
hook.go
115 lines (108 loc) · 2.3 KB
/
hook.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
package ghoko
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type hook struct {
id string
isJson bool
isSync bool
w http.ResponseWriter
r *http.Request
params Params
name string
handler *Handler
}
func newHook(handler *Handler, w http.ResponseWriter, r *http.Request) (*hook, error) {
id := r.Header.Get("Ghoko-Id")
if id == "" {
id = handler.idgen.Id().(string)
}
if !strings.HasPrefix(r.URL.Path, handler.rootUrl) {
return nil, ErrNotFound
}
name := strings.TrimPrefix(r.URL.Path, handler.rootUrl)
h := &hook{
w: w,
r: r,
params: make(Params),
isJson: strings.Contains(r.Header.Get("Content-Type"), "json"),
isSync: r.Header.Get("Ghoko-Sync") == "true",
name: name,
handler: handler,
id: id,
}
if h.isJson {
u, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
return nil, err
}
h.params.AddValues(u.Query())
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
defer r.Body.Close()
if err := h.params.AddJSON(data); err != nil {
return nil, err
}
} else {
if err := r.ParseForm(); err != nil {
return nil, err
}
h.params.AddValues(r.Form)
}
return h, nil
}
func (h *hook) exec() (int, []byte) {
f := func() (int, []byte, error) {
ipt := h.handler.iptPool.Get()
defer h.handler.iptPool.Put(ipt)
var buf bytes.Buffer
var status int
ipt.Bind("Id", h.id)
ipt.Bind("WriteBody", func(str string) error {
if !h.isSync {
return ErrSyncNeeded
}
_, err := buf.WriteString(str)
return err
})
ipt.Bind("WriteHeader", func(s int) error {
if !h.isSync {
return ErrSyncNeeded
}
status = s
return nil
})
if err := ipt.Exec(h.name, h.params); err != nil {
if !h.isSync {
writeAndLogError(nil, h.r, err)
}
return http.StatusInternalServerError, nil, err
}
return http.StatusOK, buf.Bytes(), nil
}
if h.isSync {
h.w.Header().Set("Ghoko-Id", h.id)
status, data, err := f()
if err != nil {
return http.StatusInternalServerError, []byte(err.Error())
}
return status, data
}
go f()
return http.StatusOK, h.data(h.id)
}
func (h *hook) data(data string) []byte {
if h.isJson {
buf := bytes.NewBufferString("\"")
buf.WriteString(data)
buf.WriteString("\"")
return buf.Bytes()
}
return []byte(data)
}