diff --git a/commands/http/client.go b/commands/http/client.go index a32974a74..903565797 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -42,6 +42,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) { // override with json to send to server req.SetOption(cmds.EncShort, cmds.JSON) + // stream channel output + req.SetOption(cmds.ChanOpt, "true") + query, err := getQuery(req) if err != nil { return nil, err diff --git a/commands/http/handler.go b/commands/http/handler.go index e5013f6c6..329e8fa29 100644 --- a/commands/http/handler.go +++ b/commands/http/handler.go @@ -84,13 +84,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set(contentTypeHeader, mime) } - // if the res output is a channel, set a custom header for it - isChan := false - if _, ok := res.Output().(chan interface{}); ok { - w.Header().Set(channelHeader, "1") - isChan = true - } - // if response contains an error, write an HTTP error status code if e := res.Error(); e != nil { if e.Code == cmds.ErrClient { @@ -108,11 +101,14 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - if isChan { + // if output is a channel and user requested streaming channels, + // use chunk copier for the output + _, isChan := res.Output().(chan interface{}) + streamChans, _, _ := req.Option("stream-channels").Bool() + if isChan && streamChans { err = copyChunks(w, out) if err != nil { log.Error(err) - fmt.Println(err) } return } diff --git a/commands/option.go b/commands/option.go index 78f01a7aa..a78511e57 100644 --- a/commands/option.go +++ b/commands/option.go @@ -158,15 +158,18 @@ const ( EncLong = "encoding" RecShort = "r" RecLong = "recursive" + ChanOpt = "stream-channels" ) // options that are used by this package var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)") var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively") +var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output") // global options, added to every command var globalOptions = []Option{ OptionEncodingType, + OptionStreamChannels, } // the above array of Options, wrapped in a Command