forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
293 lines (241 loc) · 6.58 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package http
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/boxo/files"
)
const (
ApiUrlFormat = "%s%s/%s?%s"
)
var OptionSkipMap = map[string]bool{
"api": true,
}
type client struct {
serverAddress string
httpClient *http.Client
ua string
apiPrefix string
headers map[string]string
fallback cmds.Executor
rawAbsPath bool
}
// ClientOpt is an option that can be passed to the HTTP client constructor.
type ClientOpt func(*client)
// ClientWithUserAgent specifies the HTTP user agent for the client.
func ClientWithUserAgent(ua string) ClientOpt {
return func(c *client) {
c.ua = ua
}
}
// ClientWithHeader adds an HTTP header to the client.
func ClientWithHeader(key, value string) ClientOpt {
return func(c *client) {
if c.headers == nil {
c.headers = map[string]string{}
}
c.headers[key] = value
}
}
// ClientWithHTTPClient specifies a custom http.Client. Defaults to
// http.DefaultClient.
func ClientWithHTTPClient(hc *http.Client) ClientOpt {
return func(c *client) {
c.httpClient = hc
}
}
// ClientWithAPIPrefix specifies an API URL prefix.
func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
return func(c *client) {
c.apiPrefix = apiPrefix
}
}
// ClientWithFallback adds a fallback executor to the client.
//
// Note: This may run the PreRun function twice.
func ClientWithFallback(exe cmds.Executor) ClientOpt {
return func(c *client) {
c.fallback = exe
}
}
// ClientWithRawAbsPath enables the rawAbspath for [files.NewMultiFileReader].
func ClientWithRawAbsPath(rawAbsPath bool) ClientOpt {
return func(c *client) {
c.rawAbsPath = rawAbsPath
}
}
// NewClient constructs a new HTTP-backed command executor.
func NewClient(address string, opts ...ClientOpt) cmds.Executor {
if strings.Contains(address, "443") {
address = "https://" + address
} else if !strings.HasPrefix(address, "http://") {
address = "http://" + address
}
c := &client{
serverAddress: address,
httpClient: http.DefaultClient,
ua: "go-ipfs-cmds/http",
}
for _, opt := range opts {
opt(c)
}
return c
}
func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
cmd := req.Command
err := cmd.CheckArguments(req)
if err != nil {
return err
}
if cmd.PreRun != nil {
err := cmd.PreRun(req, env)
if err != nil {
return err
}
}
res, err := c.send(req)
if err != nil {
// Unwrap any URL errors. We don't really need to expose the
// underlying HTTP nonsense to the user.
if urlerr, ok := err.(*url.Error); ok {
err = urlerr.Err
}
if netoperr, ok := err.(*net.OpError); ok && netoperr.Op == "dial" {
// Connection refused.
if c.fallback != nil {
// XXX: this runs the PreRun twice
return c.fallback.Execute(req, re, env)
}
err = fmt.Errorf("cannot connect to the api. Is the daemon running? To run as a standalone CLI command remove the api file in `$IPFS_PATH/api`")
}
return err
}
if cmd.PostRun != nil {
if typer, ok := re.(interface {
Type() cmds.PostRunType
}); ok && cmd.PostRun[typer.Type()] != nil {
err := cmd.PostRun[typer.Type()](res, re)
closeErr := re.CloseWithError(err)
if closeErr == cmds.ErrClosingClosedEmitter {
// ignore double close errors
return nil
}
return closeErr
}
}
return cmds.Copy(re, res)
}
func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
query, err := getQuery(req)
if err != nil {
return nil, err
}
var fileReader *files.MultiFileReader
var reader io.Reader // in case we have no body to send we need to provide
// untyped nil to http.NewRequest
if bodyArgs := req.BodyArgs(); bodyArgs != nil {
// In the end, this wraps a file reader in a file reader.
// However, such is life.
fileReader = files.NewMultiFileReader(files.NewMapDirectory(map[string]files.Node{
"stdin": files.NewReaderFile(bodyArgs),
}), true, c.rawAbsPath)
reader = fileReader
} else if req.Files != nil {
fileReader = files.NewMultiFileReader(req.Files, true, c.rawAbsPath)
reader = fileReader
}
path := strings.Join(req.Path, "/")
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, c.apiPrefix, path, query)
httpReq, err := http.NewRequest("POST", url, reader)
if err != nil {
return nil, err
}
// TODO extract string consts?
if fileReader != nil {
httpReq.Header.Set(contentTypeHeader, "multipart/form-data; boundary="+fileReader.Boundary())
} else {
httpReq.Header.Set(contentTypeHeader, applicationOctetStream)
}
httpReq.Header.Set(uaHeader, c.ua)
for key, val := range c.headers {
httpReq.Header.Set(key, val)
}
httpReq = httpReq.WithContext(req.Context)
httpReq.Close = true
return httpReq, nil
}
func (c *client) send(req *cmds.Request) (cmds.Response, error) {
if req.Context == nil {
log.Warnf("no context set in request")
req.Context = context.Background()
}
// save user-provided encoding
previousUserProvidedEncoding, found := req.Options[cmds.EncLong].(string)
// override with json to send to server
req.SetOption(cmds.EncLong, cmds.JSON)
// stream channel output
req.SetOption(cmds.ChanOpt, true)
// build http request
httpReq, err := c.toHTTPRequest(req)
if err != nil {
return nil, err
}
// send http request
httpRes, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, err
}
// parse using the overridden JSON encoding in request
res, err := parseResponse(httpRes, req)
if err != nil {
return nil, err
}
// reset request encoding to what it was before
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.EncLong, previousUserProvidedEncoding)
}
return res, nil
}
func getQuery(req *cmds.Request) (string, error) {
query := url.Values{}
for k, v := range req.Options {
if OptionSkipMap[k] {
continue
}
switch val := v.(type) {
case []string:
for _, o := range val {
query.Add(k, o)
}
case bool, int, int64, uint, uint64, float64, string:
str := fmt.Sprintf("%v", v)
query.Set(k, str)
default:
return "", fmt.Errorf("unsupported query parameter type. key: %s, value: %v", k, v)
}
}
args := req.Arguments
argDefs := req.Command.Arguments
argDefIndex := 0
for _, arg := range args {
argDef := argDefs[argDefIndex]
// skip ArgFiles
for argDef.Type == cmds.ArgFile {
argDefIndex++
argDef = argDefs[argDefIndex]
}
query.Add("arg", arg)
if len(argDefs) > argDefIndex+1 {
argDefIndex++
}
}
return query.Encode(), nil
}