-
Notifications
You must be signed in to change notification settings - Fork 24
/
internal_http.go
285 lines (250 loc) · 6.05 KB
/
internal_http.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package gscript
import (
"fmt"
"github.com/crossoverJie/gscript/log"
"github.com/crossoverJie/gscript/parser"
"github.com/crossoverJie/gscript/stack"
"github.com/crossoverJie/gscript/symbol"
"io"
"net"
"net/http"
"strings"
)
type httpTool struct {
w http.ResponseWriter
r *http.Request
}
// path 与 http 工具的映射关系
var path2HttpTool map[string]*httpTool
func httpHandle(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p := paramValues[0]
p0 := paramValues[1]
p1 := paramValues[2]
var (
method, path string
)
switch p.(type) {
case string:
method = fmt.Sprintf("%s", p)
}
switch p0.(type) {
case string:
path = fmt.Sprintf("%s", p0)
}
switch p1.(type) {
case *stack.FuncObject:
funcObject := p1.(*stack.FuncObject)
function := funcObject.GetFunction()
var h = func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case *log.Log:
http.Error(w, x.String(), http.StatusInternalServerError)
default:
panic(x)
}
}
}()
if r.Method != strings.ToUpper(method) {
// todo crossoverJie http panic 单独 recovery
log.RuntimePanic(ctx, fmt.Sprintf("http method %s not correct", method))
}
// 保存 path 与 HTTPTool 映射关系
if path2HttpTool == nil {
path2HttpTool = make(map[string]*httpTool)
}
path2HttpTool[path] = &httpTool{w: w, r: r}
// 注入 classObject 对象:HttpContext
// todo crossoverJie 基于该能力可以实现反射
class := symbol.NewClass(ctx, "HttpContext")
classObject := stack.NewClassObject(class)
pathVar := v.at.GetHttpPathVariable()
classObject.SetValue(pathVar, path)
v.executeFunctionCall(stack.NewFuncObject(function), []interface{}{classObject})
}
http.HandleFunc(path, h)
}
return nil
}
func httpRun(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
var addr string
switch p0.(type) {
case string:
addr = fmt.Sprintf("%s", p0)
}
if addr != "" {
ip := externalIP()
fmt.Println(fmt.Sprintf("http host %s on port%s", ip, addr))
err := http.ListenAndServe(addr, nil)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("httpRun function error occurred,error:%s", err))
}
} else {
log.RuntimePanic(ctx, fmt.Sprintf("addr cannot be empty"))
}
return nil
}
func fprintfJSON(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
code, path, json := v.getCodePathValue(paramValues)
tool, ok := path2HttpTool[path]
if !ok {
log.RuntimePanic(ctx, fmt.Sprintf("http handle path not fount"))
}
tool.w.Header().Set("Content-Type", "application/json")
if code != http.StatusOK {
tool.w.WriteHeader(code)
}
fmt.Fprintf(tool.w, json)
return nil
}
func fprintfHTML(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
code, path, html := v.getCodePathValue(paramValues)
tool, ok := path2HttpTool[path]
if !ok {
log.RuntimePanic(ctx, fmt.Sprintf("http handle path not fount"))
}
tool.w.Header().Set("Content-Type", "text/html")
if code != http.StatusOK {
tool.w.WriteHeader(code)
}
fmt.Fprintf(tool.w, html)
return nil
}
func requestBody(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
var path string
switch p0.(type) {
case string:
path = fmt.Sprintf("%s", p0)
}
tool, ok := path2HttpTool[path]
if !ok {
log.RuntimePanic(ctx, fmt.Sprintf("http handle path not fount"))
}
data, err := io.ReadAll(tool.r.Body)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("read body error:%s", err))
}
return string(data)
}
func (v *Visitor) getCodePathValue(paramValues []interface{}) (int, string, string) {
p0 := paramValues[0]
p1 := paramValues[1]
p2 := paramValues[2]
var (
path, value string
code int
)
switch p0.(type) {
case int:
code = p0.(int)
}
switch p1.(type) {
case string:
path = fmt.Sprintf("%s", p1)
}
switch p2.(type) {
case string:
value = fmt.Sprintf("%s", p2)
}
return code, path, value
}
func queryPath(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
var path string
switch p0.(type) {
case string:
path = fmt.Sprintf("%s", p0)
}
tool, ok := path2HttpTool[path]
if !ok {
log.RuntimePanic(ctx, fmt.Sprintf("http handle path not fount"))
}
return tool.r.URL.Path
}
func formValue(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
p1 := paramValues[1]
var (
path, key string
)
switch p0.(type) {
case string:
path = fmt.Sprintf("%s", p0)
}
switch p1.(type) {
case string:
key = fmt.Sprintf("%s", p1)
}
tool, ok := path2HttpTool[path]
if !ok {
log.RuntimePanic(ctx, fmt.Sprintf("http handle path not fount"))
}
return tool.r.FormValue(key)
}
func postFormValue(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
p1 := paramValues[1]
var (
path, key string
)
switch p0.(type) {
case string:
path = fmt.Sprintf("%s", p0)
}
switch p1.(type) {
case string:
key = fmt.Sprintf("%s", p1)
}
tool, ok := path2HttpTool[path]
if !ok {
log.RuntimePanic(ctx, fmt.Sprintf("http handle path not fount"))
}
return tool.r.PostFormValue(key)
}
func externalIP() string {
ifaces, err := net.Interfaces()
if err != nil {
return ""
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return ""
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String()
}
}
return ""
}