forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
72 lines (62 loc) · 1.38 KB
/
fetch.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
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
package deno
import (
"github.com/golang/protobuf/proto"
"io/ioutil"
"net/http"
)
func InitFetch() {
Sub("fetch", func(buf []byte) []byte {
msg := &Msg{}
check(proto.Unmarshal(buf, msg))
switch msg.Command {
case Msg_FETCH_REQ:
return Fetch(
msg.FetchReqId,
msg.FetchReqUrl)
default:
panic("[fetch] Unexpected message " + string(buf))
}
})
}
func Fetch(id int32, targetUrl string) []byte {
logDebug("Fetch %d %s", id, targetUrl)
async(func() {
resMsg := &Msg{
Command: Msg_FETCH_RES,
FetchResId: id,
}
if !Perms.Net {
resMsg.Error = "Network access denied."
PubMsg("fetch", resMsg)
return
}
resp, err := http.Get(targetUrl)
if err != nil {
resMsg.Error = err.Error()
PubMsg("fetch", resMsg)
return
}
if resp == nil {
resMsg.Error = "resp is nil "
PubMsg("fetch", resMsg)
return
}
resMsg.FetchResStatus = int32(resp.StatusCode)
logDebug("fetch success %d %s", resMsg.FetchResStatus, targetUrl)
PubMsg("fetch", resMsg)
// Now we read the body and send another message0
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp == nil {
resMsg.Error = "resp is nil "
PubMsg("fetch", resMsg)
return
}
resMsg.FetchResBody = body
PubMsg("fetch", resMsg)
// TODO streaming.
})
return nil
}