-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
906 lines (886 loc) · 28.2 KB
/
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
package builtin_libs
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strings"
. "github.com/zgg-lang/zgg-go/runtime"
"github.com/gorilla/websocket"
)
var (
httpRequestContextClass ValueType
websocketContextClass ValueType
websocketClientClass ValueType
httpRequestClass ValueType
httpResponseClass ValueType
httpFormFileClass ValueType
)
func libHttp(*Context) ValueObject {
lib := NewObject()
// Client
lib.SetMember("get", httpGet, nil)
lib.SetMember("getJson", httpGetJson, nil)
lib.SetMember("postForm", httpPostForm, nil)
lib.SetMember("postJson", httpPostJson, nil)
lib.SetMember("Request", httpRequestClass, nil)
lib.SetMember("escape", NewNativeFunction("escape", func(c *Context, this Value, args []Value) Value {
if len(args) < 1 {
return NewStr("")
}
if as, ok := args[0].(ValueObject); ok {
rv := url.Values{}
as.Iterate(func(k string, v Value) {
if vs, ok := v.(ValueArray); ok {
for i := 0; i < vs.Len(); i++ {
item := vs.GetIndex(i, c)
rv.Add(k, item.ToString(c))
}
} else {
rv.Add(k, v.ToString(c))
}
})
return NewStr(rv.Encode())
}
a := args[0].ToString(c)
return NewStr(url.QueryEscape(a))
}), nil)
lib.SetMember("unescape", NewNativeFunction("unescape", func(c *Context, this Value, args []Value) Value {
if len(args) < 1 {
return NewStr("")
}
a := args[0].ToString(c)
rv, err := url.QueryUnescape(a)
if err != nil {
c.OnRuntimeError("unescape %s error: %s", a, err)
}
return NewStr(rv)
}), nil)
// Server
lib.SetMember("createServer", httpCreateServer, nil)
lib.SetMember("serve", NewNativeFunction("serve", func(c *Context, this Value, args []Value) Value {
if len(args) != 2 {
c.OnRuntimeError("http: serve requires 2 arguments")
return nil
}
addr := c.MustStr(args[0], "http.serve(addr, handleFunc): addr")
handleFunc := c.MustCallable(args[1], "http.serve(addr, handleFunc): function")
http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
newC := c.Clone()
w.Header().Set("server", "zgg simple server")
rv := NewGoValue(r)
wv := NewGoValue(w)
ctx := NewObjectAndInit(httpRequestContextClass, newC, wv, rv)
newC.Invoke(handleFunc, nil, Args(ctx))
}))
return nil
}), nil)
return lib
}
var httpGet = NewNativeFunction("http.get", func(c *Context, thisArg Value, args []Value) Value {
var (
url ValueStr
headers ValueObject
)
EnsureFuncParams(c, "http.get", args,
ArgRuleRequired{"url", TypeStr, &url},
ArgRuleOptional{"headers", TypeObject, &headers, NewObject()},
)
request, err := http.NewRequest("GET", url.Value(), nil)
headers.Each(func(key string, value Value) bool {
request.Header.Add(key, value.ToString(c))
return true
})
rsp, err := http.DefaultClient.Do(request)
if err != nil {
panic("http.get: " + err.Error())
}
defer rsp.Body.Close()
bs, err := ioutil.ReadAll(rsp.Body)
if err != nil {
panic("http.get: " + err.Error())
}
return NewBytes(bs)
}, "url", "headers")
var httpGetJson = NewNativeFunction("getJson", func(c *Context, thisArg Value, args []Value) Value {
c.AssertArgNum(len(args), 1, 2, "http.getJson")
url := c.MustStr(args[0])
request, err := http.NewRequest("GET", url, nil)
if err != nil {
c.OnRuntimeError("http.getJson: make request error %s", err)
return nil
}
if len(args) > 1 {
headers := c.MustObject(args[1], "http.getJson::headers")
headers.Each(func(key string, value Value) bool {
request.Header.Add(key, value.ToString(c))
return true
})
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
c.OnRuntimeError("http.getJson: " + err.Error())
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
var j interface{}
if err := json.Unmarshal(respBytes, &j); err != nil {
c.OnRuntimeError("http.getJson: " + err.Error())
}
return jsonToValue(j, c)
}, "url", "headers")
var httpPostForm = NewNativeFunction("postForm", func(c *Context, this Value, args []Value) Value {
c.AssertArgNum(len(args), 2, 3, "http.postForm")
postUrl := c.MustStr(args[0], "http.postForm::url")
formArgs := c.MustObject(args[1], "http.postForm::form")
form := url.Values{}
formArgs.Each(func(key string, value Value) bool {
if values, ok := value.(ValueArray); ok {
for i := 0; i < values.Len(); i++ {
elem := values.GetIndex(i, c)
form.Add(key, elem.ToString(c))
}
} else {
form.Add(key, value.ToString(c))
}
return true
})
bodyReader := strings.NewReader(form.Encode())
request, err := http.NewRequest("POST", postUrl, bodyReader)
if err != nil {
c.OnRuntimeError("http.postForm: make request error %s", err)
return nil
}
if len(args) > 2 {
headers := c.MustObject(args[2], "http.postForm::headers")
headers.Each(func(key string, value Value) bool {
request.Header.Add(key, value.ToString(c))
return true
})
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(request)
if err != nil {
c.OnRuntimeError("http.postForm: request error %s", err)
return nil
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.OnRuntimeError("http.postForm: read response error %s", err)
return nil
}
return NewBytes(content)
}, "url", "form", "headers")
var httpPostJson = NewNativeFunction("postJson", func(c *Context, this Value, args []Value) Value {
c.AssertArgNum(len(args), 2, 3, "http.postJson")
postUrl := c.MustStr(args[0], "http.postJson::url")
content := args[1]
contentBytes, err := json.Marshal(content.ToGoValue())
if err != nil {
c.OnRuntimeError("http.postJson::content encode to json error %s", err)
return nil
}
bodyReader := bytes.NewReader(contentBytes)
request, err := http.NewRequest("POST", postUrl, bodyReader)
if err != nil {
c.OnRuntimeError("http.postJson: make request error %s", err)
return nil
}
if len(args) > 2 {
headers := c.MustObject(args[2], "http.postJson::headers")
headers.Each(func(key string, value Value) bool {
request.Header.Add(key, value.ToString(c))
return true
})
}
request.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
if err != nil {
c.OnRuntimeError("http.postJson: request error %s", err)
return nil
}
defer resp.Body.Close()
rspContent, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.OnRuntimeError("http.postJson: read response error %s", err)
return nil
}
return NewBytes(rspContent)
}, "url", "content", "headers")
// type httpRequestContext struct {
// r *http.Request
// w http.ResponseWriter
// Method string
// }
// func httpNewRequestContext(w http.ResponseWriter, r *http.Request) *httpRequestContext {
// return &httpRequestContext{
// r: r,
// w: w,
// }
// }
// func (c *httpRequestContext) write
var httpCreateServer = NewNativeFunction("createServer", func(c *Context, thisArg Value, args []Value) Value {
serverName := "zgg http server"
if len(args) > 0 {
opts := c.MustObject(args[0], "http.createServer options")
if _serverName, ok := opts.GetMember("serverName", c).(ValueStr); ok {
serverName = _serverName.Value()
}
}
svr := http.NewServeMux()
rv := NewObject()
upgrader := &websocket.Upgrader{
ReadBufferSize: 65536,
WriteBufferSize: 65536,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
rv.SetMember("route", NewNativeFunction("route", func(c *Context, thisArg Value, args []Value) Value {
if len(args) < 2 {
c.OnRuntimeError("http.server: route requires at least 2 arguments")
return nil
}
path := c.MustStr(args[0], "http.server.route(path, handleFunc): path")
handleFunc := c.MustCallable(args[1], "http.server.route(path, handleFunc): function")
svr.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
newC := c.Clone()
w.Header().Set("server", serverName)
rv := NewGoValue(r)
wv := NewGoValue(w)
ctx := NewObjectAndInit(httpRequestContextClass, newC, wv, rv)
newC.Invoke(handleFunc, nil, Args(ctx))
if callable, ok := ctx.GetMember("close", c).(ValueCallable); ok {
c.Invoke(callable, ctx, NoArgs)
}
})
return thisArg
}, "path", "handleFunc"), nil)
rv.SetMember("routeWebsocket", NewNativeFunction("routeWebsocket", func(c *Context, thisArgs Value, args []Value) Value {
var (
routePath ValueStr
handleFunc ValueCallable
)
EnsureFuncParams(c, "routeWebsocket", args,
ArgRuleRequired{"path", TypeStr, &routePath},
ArgRuleRequired{"handleFunc", TypeFunc, &handleFunc},
)
svr.HandleFunc(routePath.Value(), func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
w.WriteHeader(500)
return
}
defer conn.Close()
newC := c.Clone()
w.Header().Set("server", serverName)
ctx := NewObjectAndInit(websocketContextClass, newC, NewGoValue(conn), NewGoValue(w), NewGoValue(r))
newC.Invoke(handleFunc, nil, Args(ctx))
})
return thisArg
}, "path", "handleFunc"), nil)
rv.SetMember("serve", NewNativeFunction("serve", func(c *Context, thisArgs Value, args []Value) Value {
if len(args) != 1 {
c.OnRuntimeError("http.server: serve requires 1 argument")
return nil
}
addr := c.MustStr(args[0], "http.server.serve(listenAddr): listenAddr")
if err := http.ListenAndServe(addr, svr); err != nil {
c.OnRuntimeError("http.server.serve fail: " + err.Error())
return nil
}
return Undefined()
}), nil)
return rv
})
func initHttpRequestContextClass() ValueType {
className := "http.RequestContext"
return NewClassBuilder("RequestContext").
Constructor(func(c *Context, this ValueObject, args []Value) {
// w := args[0].ToGoValue().(http.ResponseWriter)
r := args[1].ToGoValue().(*http.Request)
this.SetMember("_w", args[0], c)
this.SetMember("_r", args[1], c)
this.SetMember("method", NewStr(r.Method), c)
this.SetMember("path", NewStr(r.URL.Path), c)
this.SetMember("querystr", NewStr(r.URL.RawQuery), c)
}).
Method("getBody", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
c.OnRuntimeError("%s.getData error %s", className, err)
return nil
}
return NewBytes(body)
}).
Method("query", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var argName ValueStr
EnsureFuncParams(c, className+".query", args,
ArgRuleRequired{"name", TypeStr, &argName},
)
if arg, found := r.URL.Query()[argName.Value()]; found && len(arg) > 0 {
return NewStr(arg[0])
}
return Nil()
}).
Method("getHeader", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var argName ValueStr
EnsureFuncParams(c, className+".getHeader", args,
ArgRuleRequired{"name", TypeStr, &argName},
)
return NewStr(r.Header.Get(argName.Value()))
}).
Method("getHeaders", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var argName ValueStr
EnsureFuncParams(c, className+".getHeaders", args,
ArgRuleRequired{"name", TypeStr, &argName},
)
headers := r.Header.Values(argName.Value())
rv := NewArray(len(headers))
for _, h := range headers {
rv.PushBack(NewStr(h))
}
return rv
}).
Method("queryAll", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var argName ValueStr
EnsureFuncParams(c, className+".queryAll", args,
ArgRuleRequired{"name", TypeStr, &argName},
)
if arg, found := r.URL.Query()[argName.Value()]; found {
rv := NewArray(len(arg))
for _, a := range arg {
rv.PushBack(NewStr(a))
}
return rv
}
return NewArray()
}).
Method("parseMultipartForm", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var maxSize ValueInt
EnsureFuncParams(c, className+".parseMultipartForm", args,
ArgRuleOptional{"maxSize", TypeInt, &maxSize, NewInt(10 * 1024 * 1024)},
)
r.ParseMultipartForm(maxSize.Value())
return Nil()
}).
Method("file", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var (
name ValueStr
index ValueInt
)
EnsureFuncParams(c, className+".file", args,
ArgRuleRequired{"name", TypeStr, &name},
ArgRuleOptional{"index", TypeInt, &index, NewInt(0)},
)
if r.MultipartForm == nil {
c.OnRuntimeError("RequestContext.file: cannot get file without parseMultipartForm")
}
fhs := r.MultipartForm.File[name.Value()]
if len(fhs) <= index.AsInt() {
return Nil()
}
rv := NewObjectAndInit(httpFormFileClass, c, NewGoValue(fhs[index.AsInt()]))
return rv
}).
Method("files", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var (
name ValueStr
index ValueInt
)
EnsureFuncParams(c, className+".file", args,
ArgRuleRequired{"name", TypeStr, &name},
ArgRuleOptional{"index", TypeInt, &index, NewInt(0)},
)
if r.MultipartForm == nil {
c.OnRuntimeError("RequestContext.file: cannot get file without parseMultipartForm")
}
fhs := r.MultipartForm.File[name.Value()]
rv := NewArray(len(fhs))
for _, fh := range fhs {
rfh := NewObjectAndInit(httpFormFileClass, c, NewGoValue(fh))
rv.PushBack(rfh)
}
return rv
}).
Method("form", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
r.ParseForm()
var argName ValueStr
EnsureFuncParams(c, className+".form", args,
ArgRuleRequired{"name", TypeStr, &argName},
)
if arg, found := r.Form[argName.Value()]; found && len(arg) > 0 {
return NewStr(arg[0])
}
return Nil()
}).
Method("formAll", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
var argName ValueStr
EnsureFuncParams(c, className+".formAll", args,
ArgRuleRequired{"name", TypeStr, &argName},
)
if arg, found := r.Form[argName.Value()]; found {
rv := NewArray(len(arg))
for _, a := range arg {
rv.PushBack(NewStr(a))
}
return rv
}
return NewArray()
}).
Method("getBodyStr", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
c.OnRuntimeError("%s.getData error %s", className, err)
return nil
}
return NewStr(string(body))
}).
Method("addHeader", func(c *Context, this ValueObject, args []Value) Value {
var (
key ValueStr
val ValueStr
)
EnsureFuncParams(c, "http.RequestContext.addHeader", args,
ArgRuleRequired{"key", TypeStr, &key},
ArgRuleRequired{"val", TypeStr, &val},
)
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
w.Header().Add(key.Value(), val.Value())
return Undefined()
}).
Method("setHeader", func(c *Context, this ValueObject, args []Value) Value {
var (
key ValueStr
val ValueStr
)
EnsureFuncParams(c, "http.RequestContext.setHeader", args,
ArgRuleRequired{"key", TypeStr, &key},
ArgRuleRequired{"val", TypeStr, &val},
)
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
w.Header().Set(key.Value(), val.Value())
return Undefined()
}).
Method("write", func(c *Context, this ValueObject, args []Value) Value {
if len(args) < 1 {
c.OnRuntimeError("http.RequestContext.write requires at least one argument")
return nil
}
statusCode := c.MustInt(args[0])
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
w.WriteHeader(int(statusCode))
for i := 1; i < len(args); i++ {
arg := args[i]
if reader, ok := arg.ToGoValue().(io.Reader); ok {
io.Copy(w, reader)
continue
}
switch argVal := arg.(type) {
case ValueBytes:
{
bs := argVal.Value()
written := 0
for written < len(bs) {
n, err := w.Write(bs[written:])
if err != nil {
break
}
written += n
}
}
default:
io.WriteString(w, arg.ToString(c))
}
}
return Undefined()
}).
Method("writeJson", func(c *Context, this ValueObject, args []Value) Value {
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
statusCode := 200
contentAt := 0
switch len(args) {
case 2:
statusCode = int(c.MustInt(args[0]))
contentAt++
fallthrough
case 1:
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(statusCode)
if err := json.NewEncoder(w).Encode(args[contentAt].ToGoValue()); err != nil {
c.OnRuntimeError("writeJson: encode to json error %s", err)
}
default:
c.OnRuntimeError("writeJson usage: writeJson([statusCode,] contentValue)")
}
return Undefined()
}).
Method("sendFile", func(c *Context, this ValueObject, args []Value) Value {
var (
filename ValueStr
)
EnsureFuncParams(c, "http.RequestContext.sendFile", args,
ArgRuleRequired{"filename", TypeStr, &filename},
)
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
http.ServeFile(w, r, filename.Value())
return Undefined()
}).
Method("redirect", func(c *Context, this ValueObject, args []Value) Value {
var (
url ValueStr
code ValueInt
)
switch len(args) {
case 1:
EnsureFuncParams(c, "http.RequestContext.redirect", args,
ArgRuleRequired{"url", TypeStr, &url},
)
code = NewInt(302)
default:
EnsureFuncParams(c, "http.RequestContext.redirect", args,
ArgRuleRequired{"url", TypeStr, &url},
ArgRuleRequired{"code", TypeInt, &code},
)
}
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
http.Redirect(w, r, url.Value(), code.AsInt())
return Undefined()
}).
Method("forward", func(c *Context, this ValueObject, args []Value) Value {
var (
target ValueStr
options ValueObject
)
EnsureFuncParams(c, "http.RequestContext.forward", args,
ArgRuleRequired{"target", TypeStr, &target},
ArgRuleOptional{"options", TypeObject, &options, NewObject()},
)
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
w := this.GetMember("_w", c).ToGoValue().(http.ResponseWriter)
var (
targetUrl = target.Value()
host = targetUrl
)
if sp := strings.Index(targetUrl, "://"); sp >= 0 {
host = host[sp+3:]
} else {
targetUrl = r.URL.Scheme + "://" + targetUrl
}
targetUrl += r.URL.Path
if r.URL.RawQuery != "" {
targetUrl += "?" + r.URL.RawQuery
}
reqBody := r.Body
if reqBody != nil {
defer reqBody.Close()
}
request, err := http.NewRequest(r.Method, targetUrl, reqBody)
if err != nil {
c.OnRuntimeError("make forward request error: %s", err)
}
for field, values := range r.Header {
if strings.ToLower(field) == "host" {
request.Header.Set(field, host)
} else {
for _, value := range values {
request.Header.Add(field, value)
}
}
}
response, err := http.DefaultClient.Do(request)
if err != nil {
c.OnRuntimeError("do forward request error: %s", err)
}
defer response.Body.Close()
respHeader := w.Header()
for field, values := range response.Header {
for i, value := range values {
if i == 0 {
respHeader.Set(field, value)
} else {
respHeader.Add(field, value)
}
}
}
w.WriteHeader(response.StatusCode)
io.Copy(w, response.Body)
return Undefined()
}).
Method("close", func(c *Context, this ValueObject, args []Value) Value {
r := this.GetMember("_r", c).ToGoValue().(*http.Request)
r.Body.Close()
return Undefined()
}).
Build()
}
func initWebsocketContextClass() ValueType {
// className := "http.WebsocketContext"
return NewClassBuilder("WebsocketContext").
Constructor(func(c *Context, this ValueObject, args []Value) {
this.SetMember("_conn", args[0], c)
this.SetMember("w", args[1], c)
this.SetMember("r", args[2], c)
}).
Method("read", func(c *Context, this ValueObject, args []Value) Value {
conn := this.GetMember("_conn", c).ToGoValue().(*websocket.Conn)
mt, pkg, err := conn.ReadMessage()
if err != nil {
c.OnRuntimeError("websocket read message error %s", err)
}
switch mt {
case websocket.TextMessage:
return NewStr(string(pkg))
case websocket.BinaryMessage:
return NewBytes(pkg)
}
return nil
}).
Method("write", func(c *Context, this ValueObject, args []Value) Value {
conn := this.GetMember("_conn", c).ToGoValue().(*websocket.Conn)
var err error
for _, arg := range args {
switch pkg := arg.(type) {
case ValueBytes:
err = conn.WriteMessage(websocket.BinaryMessage, pkg.Value())
case ValueObject:
err = conn.WriteJSON(pkg.ToGoValue())
default:
err = conn.WriteMessage(websocket.TextMessage, []byte(pkg.ToString(c)))
}
if err != nil {
c.OnRuntimeError("websocket write message error %s", err)
}
}
return Undefined()
}).
Build()
}
func initHttpRequestClass() ValueType {
return NewClassBuilder("Request").
Constructor(func(c *Context, this ValueObject, args []Value) {
var (
method ValueStr
url ValueStr
)
if len(args) == 1 {
EnsureFuncParams(c, "Request.__init__", args,
ArgRuleRequired{"url", TypeStr, &url},
)
this.SetMember("method", NewStr("GET"), c)
} else {
EnsureFuncParams(c, "Request.__init__", args,
ArgRuleRequired{"method", TypeStr, &method},
ArgRuleRequired{"url", TypeStr, &url},
)
this.SetMember("method", method, c)
}
this.SetMember("url", url, c)
this.SetMember("__headers", NewArray(), c)
this.SetMember("__body", NewArray(), c)
}).
Method("data", func(c *Context, this ValueObject, args []Value) Value {
body := this.GetMember("__body", c).(ValueArray)
for _, arg := range args {
body.PushBack(arg)
}
return this
}).
Method("form", func(c *Context, this ValueObject, args []Value) Value {
var formArg ValueObject
EnsureFuncParams(c, "Request.form", args, ArgRuleRequired{"form", TypeObject, &formArg})
headers := this.GetMember("__headers", c).(ValueArray)
headers.PushBack(NewArrayByValues(NewStr("Content-Type"), NewStr("application/x-www-form-urlencoded")))
body := this.GetMember("__body", c).(ValueArray)
form := url.Values{}
formArg.Iterate(func(key string, val Value) {
form.Set(key, val.ToString(c))
})
body.PushBack(NewStr(form.Encode()))
return this
}).
Method("header", func(c *Context, this ValueObject, args []Value) Value {
headers := this.GetMember("__headers", c).(ValueArray)
switch len(args) {
case 1:
if argHeaders, ok := args[0].(ValueObject); ok {
argHeaders.Iterate(func(key string, val Value) {
headers.PushBack(NewArrayByValues(NewStr(key), val))
})
return this
}
case 2:
headers.PushBack(NewArrayByValues(args[0], args[1]))
return this
}
c.OnRuntimeError("Request.header: invalid argument(s)")
return nil
}).
Method("call", func(c *Context, this ValueObject, args []Value) Value {
body := this.GetMember("__body", c).(ValueArray)
var reqBody io.Reader
if bodyLen := body.Len(); bodyLen == 0 {
reqBody = nil
} else {
readers := make([]io.Reader, bodyLen)
for i := range readers {
v := body.GetIndex(i, c)
switch bodyVal := v.(type) {
case ValueBytes:
readers[i] = bytes.NewReader(bodyVal.Value())
default:
readers[i] = strings.NewReader(bodyVal.ToString(c))
}
}
reqBody = io.MultiReader(readers...)
}
method := c.MustStr(this.GetMember("method", c))
url := c.MustStr(this.GetMember("url", c))
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
c.OnRuntimeError("Request.build: make request error %s", err)
}
headers := this.GetMember("__headers", c).(ValueArray)
for i := 0; i < headers.Len(); i++ {
item := headers.GetIndex(i, c)
key := item.GetIndex(0, c)
val := item.GetIndex(1, c)
req.Header.Add(key.ToString(c), val.ToString(c))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.OnRuntimeError("http.Request.call: do request error %s", err)
}
return NewObjectAndInit(httpResponseClass, c, NewGoValue(resp))
// return NewGoValue(resp)
}).
Build()
}
func initHttpResponseClass() ValueType {
return NewClassBuilder("Response").
Constructor(func(c *Context, this ValueObject, args []Value) {
resp := args[0].ToGoValue().(*http.Response)
this.SetMember("__resp", args[0], c)
this.SetMember("statusCode", NewInt(int64(resp.StatusCode)), c)
headers := NewObject()
for k := range resp.Header {
headers.SetMember(k, NewStr(resp.Header.Get(k)), c)
}
this.SetMember("headers", headers, c)
}).
Method("close", func(c *Context, this ValueObject, args []Value) Value {
resp := this.GetMember("__resp", c).ToGoValue().(*http.Response)
resp.Body.Close()
return this
}).
Method("header", func(c *Context, this ValueObject, args []Value) Value {
resp := this.GetMember("__resp", c).ToGoValue().(*http.Response)
var h ValueStr
EnsureFuncParams(c, "http.Response.header", args, ArgRuleRequired{"header", TypeStr, &h})
return NewStr(resp.Header.Get(h.Value()))
}).
Method("bytes", func(c *Context, this ValueObject, args []Value) Value {
resp := this.GetMember("__resp", c).ToGoValue().(*http.Response)
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.OnRuntimeError("http.Response.bytes: read body error %s", err)
}
return NewBytes(bytes)
}).
Method("chunk", func(c *Context, this ValueObject, args []Value) Value {
resp := this.GetMember("__resp", c).ToGoValue().(*http.Response)
var buf [512 * 1024]byte
return NewNativeFunction("", func(c *Context, this Value, args []Value) Value {
n, err := resp.Body.Read(buf[:])
if err == io.EOF {
return NewArrayByValues(NewBytes(buf[:n]), NewBool(false))
}
if err != nil {
c.OnRuntimeError("Read chunk error: %s", err)
}
return NewArrayByValues(NewBytes(buf[:n]), NewBool(true))
})
}).
Method("text", func(c *Context, this ValueObject, args []Value) Value {
resp := this.GetMember("__resp", c).ToGoValue().(*http.Response)
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.OnRuntimeError("http.Response.text: read body error %s", err)
}
return NewStr(string(bytes))
}).
Method("json", func(c *Context, this ValueObject, args []Value) Value {
resp := this.GetMember("__resp", c).ToGoValue().(*http.Response)
var o interface{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&o); err != nil {
c.OnRuntimeError("http.Response.json: decode body error %s", err)
}
return jsonToValue(o, c)
}).
Build()
}
func initHttpFormFileClass() ValueType {
return NewClassBuilder("FormFile").
Constructor(func(c *Context, this ValueObject, args []Value) {
fh := args[0].ToGoValue().(*multipart.FileHeader)
this.SetMember("_fh", args[0], c)
this.SetMember("name", NewStr(fh.Filename), c)
this.SetMember("size", NewInt(fh.Size), c)
}).
Method("bytes", func(c *Context, this ValueObject, args []Value) Value {
_bs := this.GetMember("_bytes", c)
if bs, ok := _bs.(ValueBytes); ok {
return bs
}
fh := this.GetMember("_fh", c).ToGoValue().(*multipart.FileHeader)
file, err := fh.Open()
if err != nil {
c.OnRuntimeError("FormFile.bytes: open file error %s", err)
}
defer file.Close()
fileBs, err := ioutil.ReadAll(file)
if err != nil {
c.OnRuntimeError("FormFile.bytes: read file error %s", err)
}
bs := NewBytes(fileBs)
this.SetMember("_bytes", bs, c)
return bs
}).
Build()
}
// func initHttpWebsocketClientClass() ValueType {
// return NewClassBuilder("WebsocketClient").
// Constructor(func(c *Context, this ValueObject, args []Value) {
// var (
// url ValueStr
// )
// EnsureFuncParams(c, "http.WebSocket")
// websocket.Dial()
// }).
// Build()
// }
func init() {
httpRequestContextClass = initHttpRequestContextClass()
websocketContextClass = initWebsocketContextClass()
httpRequestClass = initHttpRequestClass()
httpResponseClass = initHttpResponseClass()
httpFormFileClass = initHttpFormFileClass()
}