// KeyListTextMarshaler outputs a KeyList as plaintext, one key per line func KeyListTextMarshaler(res cmds.Response) (io.Reader, error) { output := res.Output().(*KeyList) buf := new(bytes.Buffer) for _, key := range output.Keys { buf.WriteString(key.B58String() + "\n") } return buf, nil }
func bootstrapMarshaler(res cmds.Response) (io.Reader, error) { v, ok := res.Output().(*BootstrapOutput) if !ok { return nil, u.ErrCast() } buf := new(bytes.Buffer) err := bootstrapWritePeers(buf, "", v.Peers) return buf, err }
func stringListMarshaler(res cmds.Response) (io.Reader, error) { list, ok := res.Output().(*stringList) if !ok { return nil, errors.New("failed to cast []string") } buf := new(bytes.Buffer) for _, s := range list.Strings { buf.WriteString(s) buf.WriteString("\n") } return buf, nil }
func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req cmds.Request) { mime, err := guessMimeType(res) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } status := http.StatusOK // if response contains an error, write an HTTP error status code if e := res.Error(); e != nil { if e.Code == cmds.ErrClient { status = http.StatusBadRequest } else { status = http.StatusInternalServerError } // NOTE: The error will actually be written out by the reader below } out, err := res.Reader() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } h := w.Header() if res.Length() > 0 { h.Set(contentLengthHeader, strconv.FormatUint(res.Length(), 10)) } if _, ok := res.Output().(io.Reader); ok { // we don't set the Content-Type for streams, so that browsers can MIME-sniff the type themselves // we set this header so clients have a way to know this is an output stream // (not marshalled command output) mime = "" h.Set(streamHeader, "1") } // if output is a channel and user requested streaming channels, // use chunk copier for the output _, isChan := res.Output().(chan interface{}) if !isChan { _, isChan = res.Output().(<-chan interface{}) } if isChan { h.Set(channelHeader, "1") } if mime != "" { h.Set(contentTypeHeader, mime) } h.Set(transferEncodingHeader, "chunked") if r.Method == "HEAD" { // after all the headers. return } if err := writeResponse(status, w, out); err != nil { if strings.Contains(err.Error(), "broken pipe") { log.Info("client disconnect while writing stream ", err) return } log.Error("error while writing stream ", err) } }
func MessageTextMarshaler(res cmds.Response) (io.Reader, error) { return strings.NewReader(res.Output().(*MessageOutput).Message), nil }