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

import (
4
	"bytes"
5 6
	"encoding/json"
	"fmt"
7
	"io"
8
	"net/http"
9
	"net/url"
10
	"reflect"
11
	"strconv"
12 13 14
	"strings"

	cmds "github.com/jbenet/go-ipfs/commands"
15
	config "github.com/jbenet/go-ipfs/repo/config"
16 17
)

18
const (
19
	ApiUrlFormat = "http://%s%s/%s?%s"
20 21
	ApiPath      = "/api/v0" // TODO: make configurable
)
22

23 24 25 26 27 28 29 30
// Client is the commands HTTP client interface.
type Client interface {
	Send(req cmds.Request) (cmds.Response, error)
}

type client struct {
	serverAddress string
}
31

32 33 34
func NewClient(address string) Client {
	return &client{address}
}
35

36
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37 38 39 40 41 42 43 44

	// save user-provided encoding
	previousUserProvidedEncoding, found, err := req.Option(cmds.EncShort).String()
	if err != nil {
		return nil, err
	}

	// override with json to send to server
45
	req.SetOption(cmds.EncShort, cmds.JSON)
46

47 48 49
	// stream channel output
	req.SetOption(cmds.ChanOpt, "true")

50
	query, err := getQuery(req)
51 52 53 54
	if err != nil {
		return nil, err
	}

55
	var fileReader *MultiFileReader
56 57
	var reader io.Reader

58
	if req.Files() != nil {
59
		fileReader = NewMultiFileReader(req.Files(), true)
60 61 62 63 64
		reader = fileReader
	} else {
		// if we have no file data, use an empty Reader
		// (http.NewRequest panics when a nil Reader is used)
		reader = strings.NewReader("")
65 66
	}

67 68 69
	path := strings.Join(req.Path(), "/")
	url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)

70
	httpReq, err := http.NewRequest("POST", url, reader)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	if err != nil {
		return nil, err
	}

	// TODO extract string consts?
	if fileReader != nil {
		httpReq.Header.Set("Content-Type", "multipart/form-data; boundary="+fileReader.Boundary())
		httpReq.Header.Set("Content-Disposition", "form-data: name=\"files\"")
	} else {
		httpReq.Header.Set("Content-Type", "application/octet-stream")
	}
	version := config.CurrentVersionNumber
	httpReq.Header.Set("User-Agent", fmt.Sprintf("/go-ipfs/%s/", version))

	httpRes, err := http.DefaultClient.Do(httpReq)
86 87 88 89
	if err != nil {
		return nil, err
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
90
	// using the overridden JSON encoding in request
91 92 93 94 95
	res, err := getResponse(httpRes, req)
	if err != nil {
		return nil, err
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
96 97 98 99 100
	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.
		req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
101 102 103 104 105
	}

	return res, nil
}

106
func getQuery(req cmds.Request) (string, error) {
107
	query := url.Values{}
108
	for k, v := range req.Options() {
109
		str := fmt.Sprintf("%v", v)
110
		query.Set(k, str)
111
	}
112 113

	args := req.Arguments()
114 115
	argDefs := req.Command().Arguments

116 117 118 119 120 121 122 123
	argDefIndex := 0

	for _, arg := range args {
		argDef := argDefs[argDefIndex]
		// skip ArgFiles
		for argDef.Type == cmds.ArgFile {
			argDefIndex++
			argDef = argDefs[argDefIndex]
124 125
		}

126 127 128 129
		query.Add("arg", arg)

		if len(argDefs) > argDefIndex+1 {
			argDefIndex++
130
		}
131
	}
132

133
	return query.Encode(), nil
134
}
135

136 137 138
// getResponse decodes a http.Response to create a cmds.Response
func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error) {
	var err error
139 140
	res := cmds.NewResponse(req)

141
	contentType := httpRes.Header.Get(contentTypeHeader)
142 143
	contentType = strings.Split(contentType, ";")[0]

144 145 146 147 148 149 150 151 152
	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)
	}

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)
156
		return res, nil
157 158 159 160 161 162

	} else if len(httpRes.Header.Get(channelHeader)) > 0 {
		// if output is coming from a channel, decode each chunk
		outChan := make(chan interface{})
		go func() {
			dec := json.NewDecoder(httpRes.Body)
163
			outputType := reflect.TypeOf(req.Command().Type)
164 165

			for {
166 167 168 169 170 171 172 173
				var v interface{}
				var err error
				if outputType != nil {
					v = reflect.New(outputType).Interface()
					err = dec.Decode(v)
				} else {
					err = dec.Decode(&v)
				}
174 175 176 177 178 179
				if err != nil && err != io.EOF {
					fmt.Println(err.Error())
					return
				}
				if err == io.EOF {
					close(outChan)
180 181 182 183 184 185
					return
				}
				outChan <- v
			}
		}()

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

	dec := json.NewDecoder(httpRes.Body)

	if httpRes.StatusCode >= http.StatusBadRequest {
		e := cmds.Error{}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

		if httpRes.StatusCode == http.StatusNotFound {
			// handle 404s
			e.Message = "Command not found."
			e.Code = cmds.ErrClient

		} else if contentType == "text/plain" {
			// handle non-marshalled errors
			buf := bytes.NewBuffer(nil)
			io.Copy(buf, httpRes.Body)
			e.Message = string(buf.Bytes())
			e.Code = cmds.ErrNormal

		} else {
			// handle marshalled errors
			err = dec.Decode(&e)
			if err != nil {
				return nil, err
			}
213 214 215 216 217
		}

		res.SetError(e, e.Code)

	} else {
218
		outputType := reflect.TypeOf(req.Command().Type)
219 220 221 222 223 224 225 226
		var v interface{}

		if outputType != nil {
			v = reflect.New(outputType).Interface()
			err = dec.Decode(v)
		} else {
			err = dec.Decode(&v)
		}
227
		if err != nil && err != io.EOF {
228 229
			return nil, err
		}
230 231 232
		if v != nil {
			res.SetOutput(v)
		}
233 234
	}

235 236
	return res, nil
}