From 6adebfad11d452226def4ffdd9cfbd4bbdfb7d8a Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Tue, 20 Jan 2015 19:03:03 -0800 Subject: [PATCH] commands: Added Length field to Response squash! commands: Added Length field to Response commands/http: client: Fixed error on unset length --- commands/http/client.go | 12 +++++++++++- commands/http/handler.go | 6 ++++++ commands/response.go | 21 +++++++++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/commands/http/client.go b/commands/http/client.go index 6f4015a30..ba7c5b764 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "reflect" + "strconv" "strings" cmds "github.com/jbenet/go-ipfs/commands" @@ -137,9 +138,18 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error var err error res := cmds.NewResponse(req) - contentType := httpRes.Header["Content-Type"][0] + contentType := httpRes.Header.Get(contentTypeHeader) contentType = strings.Split(contentType, ";")[0] + lengthHeader := httpRes.Header.Get(contentLengthHeader) + if len(lengthHeader) > 0 { + length, err := strconv.ParseUint(lengthHeader, 10, 64) + if err != nil { + return nil, err + } + res.SetLength(length) + } + if len(httpRes.Header.Get(streamHeader)) > 0 { // if output is a stream, we can just use the body reader res.SetOutput(httpRes.Body) diff --git a/commands/http/handler.go b/commands/http/handler.go index cfca7500d..c16541504 100644 --- a/commands/http/handler.go +++ b/commands/http/handler.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "strconv" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" @@ -92,6 +93,11 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set(contentTypeHeader, mime) } + // set the Content-Length from the response length + if res.Length() > 0 { + w.Header().Set(contentLengthHeader, strconv.FormatUint(res.Length(), 10)) + } + // if response contains an error, write an HTTP error status code if e := res.Error(); e != nil { if e.Code == cmds.ErrClient { diff --git a/commands/response.go b/commands/response.go index e727b574f..71d49b69b 100644 --- a/commands/response.go +++ b/commands/response.go @@ -95,6 +95,10 @@ type Response interface { SetOutput(interface{}) Output() interface{} + // Sets/Returns the length of the output + SetLength(uint64) + Length() uint64 + // Marshal marshals out the response into a buffer. It uses the EncodingType // on the Request to chose a Marshaler (Codec). Marshal() (io.Reader, error) @@ -104,10 +108,11 @@ type Response interface { } type response struct { - req Request - err *Error - value interface{} - out io.Reader + req Request + err *Error + value interface{} + out io.Reader + length uint64 } func (r *response) Request() Request { @@ -122,6 +127,14 @@ func (r *response) SetOutput(v interface{}) { r.value = v } +func (r *response) Length() uint64 { + return r.length +} + +func (r *response) SetLength(l uint64) { + r.length = l +} + func (r *response) Error() *Error { return r.err }