mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 03:17:43 +08:00
commands: Added Length field to Response
squash! commands: Added Length field to Response commands/http: client: Fixed error on unset length
This commit is contained in:
parent
7b4de230eb
commit
6adebfad11
@ -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)
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user