mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 01:52:26 +08:00
commands/http: Client: decode chunked streaming output
This commit is contained in:
@ -137,8 +137,31 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
|
|||||||
contentType = strings.Split(contentType, ";")[0]
|
contentType = strings.Split(contentType, ";")[0]
|
||||||
|
|
||||||
if len(httpRes.Header.Get(streamHeader)) > 0 {
|
if len(httpRes.Header.Get(streamHeader)) > 0 {
|
||||||
|
// if output is a stream, we can just use the body reader
|
||||||
res.SetOutput(httpRes.Body)
|
res.SetOutput(httpRes.Body)
|
||||||
return res, nil
|
return res, nil
|
||||||
|
|
||||||
|
} else if len(httpRes.Header.Get(channelHeader)) > 0 {
|
||||||
|
// if output is coming from a channel, decode each chunk
|
||||||
|
outChan := make(chan interface{})
|
||||||
|
go func() {
|
||||||
|
dec := json.NewDecoder(httpRes.Body)
|
||||||
|
v := req.Command().Type
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := dec.Decode(&v)
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
outChan <- v
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
res.SetOutput(outChan)
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dec := json.NewDecoder(httpRes.Body)
|
dec := json.NewDecoder(httpRes.Body)
|
||||||
|
Reference in New Issue
Block a user