client.go 6.64 KB
Newer Older
1 2 3
package http

import (
Jan Winkelmann's avatar
Jan Winkelmann committed
4
	"context"
5
	"encoding/json"
6
	"errors"
7
	"fmt"
8
	"io"
9
	"io/ioutil"
10
	"net/http"
11
	"net/url"
12
	"reflect"
13
	"strconv"
14 15
	"strings"

16 17
	cmds "github.com/ipfs/go-ipfs/commands"
	config "github.com/ipfs/go-ipfs/repo/config"
18

Jan Winkelmann's avatar
Jan Winkelmann committed
19
	"gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
20 21
)

22
const (
23
	ApiUrlFormat = "http://%s%s/%s?%s"
24 25
	ApiPath      = "/api/v0" // TODO: make configurable
)
26

Jeromy's avatar
Jeromy committed
27 28 29 30
var OptionSkipMap = map[string]bool{
	"api": true,
}

31 32 33 34 35 36 37
// Client is the commands HTTP client interface.
type Client interface {
	Send(req cmds.Request) (cmds.Response, error)
}

type client struct {
	serverAddress string
rht's avatar
rht committed
38
	httpClient    *http.Client
39
}
40

41
func NewClient(address string) Client {
42 43
	return &client{
		serverAddress: address,
rht's avatar
rht committed
44
		httpClient:    http.DefaultClient,
45
	}
46
}
47

48
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
49

50 51
	if req.Context() == nil {
		log.Warningf("no context set in request")
52
		if err := req.SetRootContext(context.TODO()); err != nil {
53 54 55 56
			return nil, err
		}
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
57
	// save user-provided encoding
Jan Winkelmann's avatar
Jan Winkelmann committed
58
	previousUserProvidedEncoding, found, err := req.Option(cmdkit.EncShort).String()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
59 60 61 62 63
	if err != nil {
		return nil, err
	}

	// override with json to send to server
Jan Winkelmann's avatar
Jan Winkelmann committed
64
	req.SetOption(cmdkit.EncShort, cmds.JSON)
65

66
	// stream channel output
Jan Winkelmann's avatar
Jan Winkelmann committed
67
	req.SetOption(cmdkit.ChanOpt, "true")
68

69
	query, err := getQuery(req)
70 71 72 73
	if err != nil {
		return nil, err
	}

74
	var fileReader *MultiFileReader
75 76
	var reader io.Reader

77
	if req.Files() != nil {
78
		fileReader = NewMultiFileReader(req.Files(), true)
79
		reader = fileReader
80 81
	}

Jeromy's avatar
Jeromy committed
82 83
	path := strings.Join(req.Path(), "/")
	url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)
84

85
	httpReq, err := http.NewRequest("POST", url, reader)
86 87 88 89 90 91
	if err != nil {
		return nil, err
	}

	// TODO extract string consts?
	if fileReader != nil {
92
		httpReq.Header.Set(contentTypeHeader, "multipart/form-data; boundary="+fileReader.Boundary())
93
	} else {
94
		httpReq.Header.Set(contentTypeHeader, applicationOctetStream)
95
	}
rht's avatar
rht committed
96
	httpReq.Header.Set(uaHeader, config.ApiVersion)
97

rht's avatar
rht committed
98
	httpReq.Cancel = req.Context().Done()
rht's avatar
rht committed
99
	httpReq.Close = true
100

rht's avatar
rht committed
101 102 103 104
	httpRes, err := c.httpClient.Do(httpReq)
	if err != nil {
		return nil, err
	}
105

rht's avatar
rht committed
106 107 108
	// using the overridden JSON encoding in request
	res, err := getResponse(httpRes, req)
	if err != nil {
rht's avatar
rht committed
109
		return nil, err
110
	}
rht's avatar
rht committed
111 112 113 114 115

	if found && len(previousUserProvidedEncoding) > 0 {
		// reset to user provided encoding after sending request
		// NB: if user has provided an encoding but it is the empty string,
		// still leave it as JSON.
Jan Winkelmann's avatar
Jan Winkelmann committed
116
		req.SetOption(cmdkit.EncShort, previousUserProvidedEncoding)
rht's avatar
rht committed
117 118 119
	}

	return res, nil
120 121
}

122
func getQuery(req cmds.Request) (string, error) {
123
	query := url.Values{}
124
	for k, v := range req.Options() {
Jeromy's avatar
Jeromy committed
125
		if OptionSkipMap[k] {
Jeromy's avatar
Jeromy committed
126 127
			continue
		}
128
		str := fmt.Sprintf("%v", v)
129
		query.Set(k, str)
130
	}
131

132
	args := req.StringArguments()
133 134
	argDefs := req.Command().Arguments

135 136 137 138 139
	argDefIndex := 0

	for _, arg := range args {
		argDef := argDefs[argDefIndex]
		// skip ArgFiles
Jan Winkelmann's avatar
Jan Winkelmann committed
140
		for argDef.Type == cmdkit.ArgFile {
141 142
			argDefIndex++
			argDef = argDefs[argDefIndex]
143 144
		}

145 146 147 148
		query.Add("arg", arg)

		if len(argDefs) > argDefIndex+1 {
			argDefIndex++
149
		}
150
	}
151

152
	return query.Encode(), nil
153
}
154

155 156 157
// getResponse decodes a http.Response to create a cmds.Response
func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error) {
	var err error
158 159
	res := cmds.NewResponse(req)

160
	contentType := httpRes.Header.Get(contentTypeHeader)
161 162
	contentType = strings.Split(contentType, ";")[0]

163
	lengthHeader := httpRes.Header.Get(extraContentLengthHeader)
164 165 166 167 168 169 170 171
	if len(lengthHeader) > 0 {
		length, err := strconv.ParseUint(lengthHeader, 10, 64)
		if err != nil {
			return nil, err
		}
		res.SetLength(length)
	}

172 173
	rr := &httpResponseReader{httpRes}
	res.SetCloser(rr)
174

175
	if contentType != applicationJson {
Jeromy's avatar
Jeromy committed
176
		// for all non json output types, just stream back the output
177
		res.SetOutput(rr)
178
		return res, nil
179 180 181 182

	} else if len(httpRes.Header.Get(channelHeader)) > 0 {
		// if output is coming from a channel, decode each chunk
		outChan := make(chan interface{})
Jeromy's avatar
Jeromy committed
183

184
		go readStreamedJson(req, rr, outChan, res)
185

186
		res.SetOutput((<-chan interface{})(outChan))
187
		return res, nil
188 189
	}

190
	dec := json.NewDecoder(rr)
191

Jeromy's avatar
Jeromy committed
192
	// If we ran into an error
193
	if httpRes.StatusCode >= http.StatusBadRequest {
Jan Winkelmann's avatar
Jan Winkelmann committed
194
		var e *cmdkit.Error
195

Jeromy's avatar
Jeromy committed
196 197
		switch {
		case httpRes.StatusCode == http.StatusNotFound:
198
			// handle 404s
Jan Winkelmann's avatar
Jan Winkelmann committed
199
			e = &cmdkit.Error{Message: "Command not found.", Code: cmdkit.ErrClient}
200

201
		case contentType == plainText:
202
			// handle non-marshalled errors
203 204 205 206
			mes, err := ioutil.ReadAll(rr)
			if err != nil {
				return nil, err
			}
207

Jan Winkelmann's avatar
Jan Winkelmann committed
208
			e = &cmdkit.Error{Message: string(mes), Code: cmdkit.ErrNormal}
Jeromy's avatar
Jeromy committed
209
		default:
210
			// handle marshalled errors
Jan Winkelmann's avatar
Jan Winkelmann committed
211 212
			var rxErr cmdkit.Error
			err = dec.Decode(&rxErr)
213 214 215
			if err != nil {
				return nil, err
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
216
			e = &rxErr
217 218 219 220
		}

		res.SetError(e, e.Code)

Jeromy's avatar
Jeromy committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234
		return res, nil
	}

	outputType := reflect.TypeOf(req.Command().Type)
	v, err := decodeTypedVal(outputType, dec)
	if err != nil && err != io.EOF {
		return nil, err
	}

	res.SetOutput(v)

	return res, nil
}

Jeromy's avatar
Jeromy committed
235 236
// read json objects off of the given stream, and write the objects out to
// the 'out' channel
237
func readStreamedJson(req cmds.Request, rr io.Reader, out chan<- interface{}, resp cmds.Response) {
Jeromy's avatar
Jeromy committed
238
	defer close(out)
239
	dec := json.NewDecoder(rr)
Jeromy's avatar
Jeromy committed
240 241 242 243 244 245 246
	outputType := reflect.TypeOf(req.Command().Type)

	ctx := req.Context()

	for {
		v, err := decodeTypedVal(outputType, dec)
		if err != nil {
247
			if err != io.EOF {
Jeromy's avatar
Jeromy committed
248
				log.Error(err)
Jan Winkelmann's avatar
Jan Winkelmann committed
249
				resp.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
250 251
			}
			return
252
		}
Jeromy's avatar
Jeromy committed
253 254 255 256 257

		select {
		case <-ctx.Done():
			return
		case out <- v:
258
		}
259
	}
Jeromy's avatar
Jeromy committed
260
}
261

Jeromy's avatar
Jeromy committed
262 263
// decode a value of the given type, if the type is nil, attempt to decode into
// an interface{} anyways
Jeromy's avatar
Jeromy committed
264 265 266 267 268 269 270 271 272 273 274
func decodeTypedVal(t reflect.Type, dec *json.Decoder) (interface{}, error) {
	var v interface{}
	var err error
	if t != nil {
		v = reflect.New(t).Interface()
		err = dec.Decode(v)
	} else {
		err = dec.Decode(&v)
	}

	return v, err
275
}
276

Jeromy's avatar
Jeromy committed
277 278 279
// httpResponseReader reads from the response body, and checks for an error
// in the http trailer upon EOF, this error if present is returned instead
// of the EOF.
280 281 282 283 284 285
type httpResponseReader struct {
	resp *http.Response
}

func (r *httpResponseReader) Read(b []byte) (int, error) {
	n, err := r.resp.Body.Read(b)
286 287 288 289 290

	// reading on a closed response body is as good as an io.EOF here
	if err != nil && strings.Contains(err.Error(), "read on closed response body") {
		err = io.EOF
	}
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	if err == io.EOF {
		_ = r.resp.Body.Close()
		trailerErr := r.checkError()
		if trailerErr != nil {
			return n, trailerErr
		}
	}
	return n, err
}

func (r *httpResponseReader) checkError() error {
	if e := r.resp.Trailer.Get(StreamErrHeader); e != "" {
		return errors.New(e)
	}
	return nil
}

func (r *httpResponseReader) Close() error {
	return r.resp.Body.Close()
}