Skip to content

Commit

Permalink
commands/http/handler: Write a trailer with any error messages
Browse files Browse the repository at this point in the history
For streaming responses (e.g. 'ipfs add -r /path/to/root/directory'),
we may hit errors later on in the stream, after we've already written
the initial 200 response and started in on the streaming body
(e.g. /path/to/root/directory/a/b/c is an unsupported file type).  We
should check for that sort of error, and set a trailer [1] if there
were any errors.  Unfortunately, it's a bit awkward to set these
headers in Go 1.4 and earlier [2], but [2] does have a workaround for
Go 1.4 (it's not clear if the workaround is compatible with Go 1.3).

[1]: http://tools.ietf.org/html/rfc2616#section-14.40
[2]: golang/go#7759

License: MIT
Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Jun 24, 2015
1 parent 9b8e737 commit 46b7889
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions commands/http/client.go
Expand Up @@ -206,6 +206,11 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error

if err == io.EOF {
close(outChan)
errorString := httpRes.Trailer.Get("Error")
if errorString != "" {
err = fmt.Errorf(errorString)
res.SetError(err, cmds.ErrNormal)
}
return
}
outChan <- v
Expand Down
26 changes: 20 additions & 6 deletions commands/http/handler.go
Expand Up @@ -176,14 +176,14 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// w.WriteString(transferEncodingHeader + ": chunked\r\n")
// w.Header().Set(channelHeader, "1")
// w.WriteHeader(200)
err = copyChunks(applicationJson, w, out)
err = copyChunks(res, applicationJson, w, out)
if err != nil {
log.Debug(err)
}
return
}

flushCopy(w, out)
flushCopy(nil, w, out)
}

func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -193,9 +193,9 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// flushCopy Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func flushCopy(w http.ResponseWriter, out io.Reader) error {
func flushCopy(res cmds.Response, w http.ResponseWriter, out io.Reader) error {
if _, ok := w.(http.Flusher); !ok {
return copyChunks("", w, out)
return copyChunks(res, "", w, out)
}

io.Copy(&flushResponse{w}, out)
Expand All @@ -204,7 +204,7 @@ func flushCopy(w http.ResponseWriter, out io.Reader) error {

// Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func copyChunks(contentType string, w http.ResponseWriter, out io.Reader) error {
func copyChunks(res cmds.Response, contentType string, w http.ResponseWriter, out io.Reader) error {
hijacker, ok := w.(http.Hijacker)
if !ok {
return errors.New("Could not create hijacker")
Expand All @@ -216,6 +216,9 @@ func copyChunks(contentType string, w http.ResponseWriter, out io.Reader) error
defer conn.Close()

writer.WriteString("HTTP/1.1 200 OK\r\n")
if res != nil {
writer.WriteString("Trailer: Error\r\n")
}
if contentType != "" {
writer.WriteString(contentTypeHeader + ": " + contentType + "\r\n")
}
Expand Down Expand Up @@ -248,7 +251,18 @@ func copyChunks(contentType string, w http.ResponseWriter, out io.Reader) error
}
}

writer.WriteString("0\r\n\r\n")
writer.WriteString("0\r\n") // eof

if res != nil {
err := res.Error()
if err != nil {
t := http.Header{}
t.Set("Error", err.Error())
t.Write(writer)
}
}

writer.WriteString("\r\n") // end of trailers
writer.Flush()

return nil
Expand Down

0 comments on commit 46b7889

Please sign in to comment.