commands/http: Client: decode chunked streaming output
Matt Bell committed
Dec 17, 2014 at 16:20 UTC
cf1e770e1e13dee1428edcbf74079a53ac9e88f0
1 file changed
+23
commands/http/client.go
+23
@@ -137,8 +137,31 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
137
contentType = strings.Split(contentType, ";")[0]
138
139
if len(httpRes.Header.Get(streamHeader)) > 0 {
140
+ // if output is a stream, we can just use the body reader
141
res.SetOutput(httpRes.Body)
142
return res, nil
143
+
144
+ } else if len(httpRes.Header.Get(channelHeader)) > 0 {
145
+ // if output is coming from a channel, decode each chunk
146
+ outChan := make(chan interface{})
147
+ go func() {
148
+ dec := json.NewDecoder(httpRes.Body)
149
+ v := req.Command().Type
150
+
151
+ for {
152
+ err := dec.Decode(&v)
153
+ if err != nil {
154
+ if err != io.EOF {
155
+ fmt.Println(err.Error())
156
+ }
157
+ return
158
+ }
159
+ outChan <- v
160
+ }
161
+ }()
162
+
163
+ res.SetOutput(outChan)
164
+ return res, nil
165
}
166
167
dec := json.NewDecoder(httpRes.Body)