client.go 3.73 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 11 12
	"strings"

	cmds "github.com/jbenet/go-ipfs/commands"
13
	config "github.com/jbenet/go-ipfs/config"
14
	u "github.com/jbenet/go-ipfs/util"
15 16
)

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

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

type client struct {
	serverAddress string
}
30

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

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

	// 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
44
	req.SetOption(cmds.EncShort, cmds.JSON)
45

46
	query, err := getQuery(req)
47 48 49 50
	if err != nil {
		return nil, err
	}

51 52
	var fileReader *MultiFileReader
	if req.Files() != nil {
53
		fileReader = NewMultiFileReader(req.Files(), true)
54 55
	}

56 57 58
	path := strings.Join(req.Path(), "/")
	url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	httpReq, err := http.NewRequest("POST", url, fileReader)
	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)
75 76 77 78
	if err != nil {
		return nil, err
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
79
	// using the overridden JSON encoding in request
80 81 82 83 84
	res, err := getResponse(httpRes, req)
	if err != nil {
		return nil, err
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
85 86 87 88 89
	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)
90 91 92 93 94
	}

	return res, nil
}

95
func getQuery(req cmds.Request) (string, error) {
96
	query := url.Values{}
97
	for k, v := range req.Options() {
98
		str := fmt.Sprintf("%v", v)
99
		query.Set(k, str)
100
	}
101 102

	args := req.Arguments()
103 104 105
	argDefs := req.Command().Arguments
	var argDef cmds.Argument

106
	for i, arg := range args {
107 108 109 110 111
		if i < len(argDefs) {
			argDef = argDefs[i]
		}

		if argDef.Type == cmds.ArgString {
112 113
			str, ok := arg.(string)
			if !ok {
114
				return "", u.ErrCast()
115 116
			}
			query.Add("arg", str)
117
		}
118
	}
119

120
	return query.Encode(), nil
121
}
122

123 124 125
// getResponse decodes a http.Response to create a cmds.Response
func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error) {
	var err error
126 127 128 129 130
	res := cmds.NewResponse(req)

	contentType := httpRes.Header["Content-Type"][0]
	contentType = strings.Split(contentType, ";")[0]

131
	if len(httpRes.Header.Get(streamHeader)) > 0 {
132
		res.SetOutput(httpRes.Body)
133 134 135 136 137 138 139
		return res, nil
	}

	dec := json.NewDecoder(httpRes.Body)

	if httpRes.StatusCode >= http.StatusBadRequest {
		e := cmds.Error{}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

		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
			}
159 160 161 162 163
		}

		res.SetError(e, e.Code)

	} else {
164
		v := req.Command().Type
165
		err = dec.Decode(&v)
166
		if err != nil && err != io.EOF {
167 168 169
			return nil, err
		}

170
		res.SetOutput(v)
171 172
	}

173 174
	return res, nil
}