some commenting
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jul 26, 2015 at 11:28 UTC
1ce310be8b45607216918f39ec2ead783dd322e9
2 files changed
+9
commands/http/client.go
+7
@@ -242,6 +242,8 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
242
return res, nil
243
}
244
245
+// read json objects off of the given stream, and write the objects out to
246
+// the 'out' channel
247
func readStreamedJson(req cmds.Request, httpRes *http.Response, out chan<- interface{}) {
248
defer close(out)
249
dec := json.NewDecoder(&httpResponseReader{httpRes})
@@ -270,6 +272,8 @@ func readStreamedJson(req cmds.Request, httpRes *http.Response, out chan<- inter
272
}
273
}
274
275
+// decode a value of the given type, if the type is nil, attempt to decode into
276
+// an interface{} anyways
277
func decodeTypedVal(t reflect.Type, dec *json.Decoder) (interface{}, error) {
278
var v interface{}
279
var err error
@@ -283,6 +287,9 @@ func decodeTypedVal(t reflect.Type, dec *json.Decoder) (interface{}, error) {
287
return v, err
288
}
289
290
+// httpResponseReader reads from the response body, and checks for an error
291
+// in the http trailer upon EOF, this error if present is returned instead
292
+// of the EOF.
293
type httpResponseReader struct {
294
resp *http.Response
295
}
commands/http/handler.go
+2
@@ -193,6 +193,7 @@ func sendResponse(w http.ResponseWriter, req cmds.Request, res cmds.Response) {
193
mime = applicationJson
194
}
195
}
196
+
197
if mime != "" {
198
h.Set(contentTypeHeader, mime)
199
}
@@ -207,6 +208,7 @@ func sendResponse(w http.ResponseWriter, req cmds.Request, res cmds.Response) {
208
// Copies from an io.Reader to a http.ResponseWriter.
209
// Flushes chunks over HTTP stream as they are read (if supported by transport).
210
func copyChunks(status int, w http.ResponseWriter, out io.Reader) error {
211
+ // hijack the connection so we can write our own chunked output and trailers
212
hijacker, ok := w.(http.Hijacker)
213
if !ok {
214
log.Error("Failed to create hijacker! cannot continue!")