summaryrefslogtreecommitdiff
path: root/fetch.go
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-27 03:46:18 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-27 03:46:51 -0400
commita831d1e2391e67f6d6ad3284ab9858d9b8d610c1 (patch)
tree4c8c5cf44f050f1c5e8875bf24958c92139ea476 /fetch.go
parentef00cf3e38b76395abe42d406c2bb06380754091 (diff)
Implement fetch
Diffstat (limited to 'fetch.go')
-rw-r--r--fetch.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/fetch.go b/fetch.go
new file mode 100644
index 000000000..d0d0ac1b3
--- /dev/null
+++ b/fetch.go
@@ -0,0 +1,64 @@
+package main
+
+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,
+ }
+
+ 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
+}