-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (119 loc) · 2.93 KB
/
main.go
File metadata and controls
143 lines (119 loc) · 2.93 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
timod "github.com/thingsdb/go-timod"
"github.com/vmihailenco/msgpack"
)
type reqData struct {
URL string `msgpack:"url"`
Method string `msgpack:"method"`
Body []byte `msgpack:"body"`
Headers [][2]string `msgpack:"headers"`
Params [][2]string `msgpack:"params"`
AllowRedirects bool `msgpack:"allow_redirects"`
}
type resData struct {
StatusCode int `msgpack:"status_code"`
Status string `msgpack:"status"`
Body []byte `msgpack:"body"`
}
func handleReqData(pkg *timod.Pkg, data *reqData) {
params := url.Values{}
reqURL, err := url.Parse(data.URL)
if err != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Error: Failed to parse URL (%s)", err))
return
}
body := bytes.NewReader(data.Body)
for i := 0; i < len(data.Params); i++ {
param := data.Params[i]
key, value := param[0], param[1]
params.Set(key, value)
}
reqURL.RawQuery = params.Encode()
req, err := http.NewRequest(data.Method, reqURL.String(), body)
if err != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Error: Failed to create HTTP request (%s)", err))
return
}
for i := 0; i < len(data.Headers); i++ {
header := data.Headers[i]
key, value := header[0], header[1]
req.Header.Set(key, value)
}
client := &http.Client{}
// The default client policy is to allow up to 10 redirects
if !data.AllowRedirects {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
res, err := client.Do(req)
if err != nil {
timod.WriteEx(
pkg.Pid,
timod.ExOperation,
fmt.Sprintf("Error: Failed to do the HTTP request (%s)", err))
return
}
var response resData
// Set the body
response.Body, err = ioutil.ReadAll(res.Body)
if err != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Error: Failed to read bytes from HTTP response (%s)", err))
return
}
// Set the status
response.Status = res.Status
response.StatusCode = res.StatusCode
timod.WriteResponse(pkg.Pid, &response)
}
func onModuleReq(pkg *timod.Pkg) {
var data reqData
err := msgpack.Unmarshal(pkg.Data, &data)
if err == nil {
handleReqData(pkg, &data)
} else {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Error: Failed to unpack request (%s)", err))
}
}
func handler(buf *timod.Buffer, quit chan bool) {
for {
select {
case pkg := <-buf.PkgCh:
switch timod.Proto(pkg.Tp) {
case timod.ProtoModuleConf:
log.Println("No configuration data is required for this module")
timod.WriteConfOk()
case timod.ProtoModuleReq:
onModuleReq(pkg)
default:
log.Printf("Error: Unexpected package type: %d", pkg.Tp)
}
case err := <-buf.ErrCh:
log.Printf("Error: %s", err)
quit <- true
return
}
}
}
func main() {
timod.StartModule("requests", handler)
}