commands: Added Length field to Response
squash! commands: Added Length field to Response commands/http: client: Fixed error on unset length
Matt Bell committed
Jan 20, 2015 at 19:03 UTC
6adebfad11d452226def4ffdd9cfbd4bbdfb7d8a
3 files changed
+34
-5
commands/http/client.go
+11
-1
@@ -8,6 +8,7 @@ import (
8
"net/http"
9
"net/url"
10
"reflect"
11
+ "strconv"
12
"strings"
13
14
cmds "github.com/jbenet/go-ipfs/commands"
@@ -137,9 +138,18 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
138
var err error
139
res := cmds.NewResponse(req)
140
140
- contentType := httpRes.Header["Content-Type"][0]
141
+ contentType := httpRes.Header.Get(contentTypeHeader)
142
contentType = strings.Split(contentType, ";")[0]
143
144
+ lengthHeader := httpRes.Header.Get(contentLengthHeader)
145
+ if len(lengthHeader) > 0 {
146
+ length, err := strconv.ParseUint(lengthHeader, 10, 64)
147
+ if err != nil {
148
+ return nil, err
149
+ }
150
+ res.SetLength(length)
151
+ }
152
+
153
if len(httpRes.Header.Get(streamHeader)) > 0 {
154
// if output is a stream, we can just use the body reader
155
res.SetOutput(httpRes.Body)
commands/http/handler.go
+6
@@ -5,6 +5,7 @@ import (
5
"fmt"
6
"io"
7
"net/http"
8
+ "strconv"
9
10
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11
@@ -92,6 +93,11 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
93
w.Header().Set(contentTypeHeader, mime)
94
}
95
96
+ // set the Content-Length from the response length
97
+ if res.Length() > 0 {
98
+ w.Header().Set(contentLengthHeader, strconv.FormatUint(res.Length(), 10))
99
+ }
100
+
101
// if response contains an error, write an HTTP error status code
102
if e := res.Error(); e != nil {
103
if e.Code == cmds.ErrClient {
commands/response.go
+17
-4
@@ -95,6 +95,10 @@ type Response interface {
95
SetOutput(interface{})
96
Output() interface{}
97
98
+ // Sets/Returns the length of the output
99
+ SetLength(uint64)
100
+ Length() uint64
101
+
102
// Marshal marshals out the response into a buffer. It uses the EncodingType
103
// on the Request to chose a Marshaler (Codec).
104
Marshal() (io.Reader, error)
@@ -104,10 +108,11 @@ type Response interface {
108
}
109
110
type response struct {
107
- req Request
108
- err *Error
109
- value interface{}
110
- out io.Reader
111
+ req Request
112
+ err *Error
113
+ value interface{}
114
+ out io.Reader
115
+ length uint64
116
}
117
118
func (r *response) Request() Request {
@@ -122,6 +127,14 @@ func (r *response) SetOutput(v interface{}) {
127
r.value = v
128
}
129
130
+func (r *response) Length() uint64 {
131
+ return r.length
132
+}
133
+
134
+func (r *response) SetLength(l uint64) {
135
+ r.length = l
136
+}
137
+
138
func (r *response) Error() *Error {
139
return r.err
140
}