Commit 11cf20bd authored by Matt Bell's avatar Matt Bell

commands: Added Length field to Response

squash! commands: Added Length field to Response

commands/http: client: Fixed error on unset length
parent 970b570f
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"strconv"
"strings" "strings"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
...@@ -137,9 +138,18 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error ...@@ -137,9 +138,18 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
var err error var err error
res := cmds.NewResponse(req) res := cmds.NewResponse(req)
contentType := httpRes.Header["Content-Type"][0] contentType := httpRes.Header.Get(contentTypeHeader)
contentType = strings.Split(contentType, ";")[0] 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 len(httpRes.Header.Get(streamHeader)) > 0 {
// if output is a stream, we can just use the body reader // if output is a stream, we can just use the body reader
res.SetOutput(httpRes.Body) res.SetOutput(httpRes.Body)
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strconv"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" 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) { ...@@ -92,6 +93,11 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(contentTypeHeader, mime) 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 response contains an error, write an HTTP error status code
if e := res.Error(); e != nil { if e := res.Error(); e != nil {
if e.Code == cmds.ErrClient { if e.Code == cmds.ErrClient {
......
...@@ -95,6 +95,10 @@ type Response interface { ...@@ -95,6 +95,10 @@ type Response interface {
SetOutput(interface{}) SetOutput(interface{})
Output() 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 // Marshal marshals out the response into a buffer. It uses the EncodingType
// on the Request to chose a Marshaler (Codec). // on the Request to chose a Marshaler (Codec).
Marshal() (io.Reader, error) Marshal() (io.Reader, error)
...@@ -104,10 +108,11 @@ type Response interface { ...@@ -104,10 +108,11 @@ type Response interface {
} }
type response struct { type response struct {
req Request req Request
err *Error err *Error
value interface{} value interface{}
out io.Reader out io.Reader
length uint64
} }
func (r *response) Request() Request { func (r *response) Request() Request {
...@@ -122,6 +127,14 @@ func (r *response) SetOutput(v interface{}) { ...@@ -122,6 +127,14 @@ func (r *response) SetOutput(v interface{}) {
r.value = v 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 { func (r *response) Error() *Error {
return r.err return r.err
} }
......
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