Commit 356c353e authored by Juan Benet's avatar Juan Benet

Merge pull request #1882 from ipfs/fix/streaming-output

fix streaming output to flush per write
parents 795e2422 5dc2c7e0
...@@ -245,13 +245,45 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req ...@@ -245,13 +245,45 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
} }
w.WriteHeader(status) w.WriteHeader(status)
_, err = io.Copy(w, out) err = flushCopy(w, out)
if err != nil { if err != nil {
log.Error("err: ", err) log.Error("err: ", err)
w.Header().Set(StreamErrHeader, sanitizedErrStr(err)) w.Header().Set(StreamErrHeader, sanitizedErrStr(err))
} }
} }
func flushCopy(w io.Writer, r io.Reader) error {
buf := make([]byte, 4096)
f, ok := w.(http.Flusher)
if !ok {
_, err := io.Copy(w, r)
return err
}
for {
n, err := r.Read(buf)
switch err {
case io.EOF:
return nil
case nil:
// continue
default:
return err
}
nw, err := w.Write(buf[:n])
if err != nil {
return err
}
if nw != n {
return fmt.Errorf("http write failed to write full amount: %d != %d", nw, n)
}
f.Flush()
}
return nil
}
func sanitizedErrStr(err error) string { func sanitizedErrStr(err error) string {
s := err.Error() s := err.Error()
s = strings.Split(s, "\n")[0] s = strings.Split(s, "\n")[0]
......
...@@ -109,6 +109,17 @@ test_expect_success "transport should be encrypted" ' ...@@ -109,6 +109,17 @@ test_expect_success "transport should be encrypted" '
test_fsh cat swarmnc test_fsh cat swarmnc
' '
test_expect_success "output from streaming commands works" '
test_expect_code 28 curl -m 2 http://localhost:5001/api/v0/stats/bw\?poll=true > statsout
'
test_expect_success "output looks good" '
grep TotalIn statsout > /dev/null &&
grep TotalOut statsout > /dev/null &&
grep RateIn statsout > /dev/null &&
grep RateOut statsout >/dev/null
'
# end same as in t0010 # end same as in t0010
test_expect_success "daemon is still running" ' test_expect_success "daemon is still running" '
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment