1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00
denoland-deno/fetch.go

73 lines
1.4 KiB
Go
Raw Normal View History

2018-05-28 21:50:44 -04:00
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
2018-05-29 04:28:32 -04:00
package deno
2018-05-27 03:46:18 -04:00
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."
2018-05-29 05:27:41 -04:00
PubMsg("fetch", resMsg)
return
}
2018-05-27 03:46:18 -04:00
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
}