forked from nojima/httpie-go
/
build.go
313 lines (278 loc) · 7.16 KB
/
build.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package exchange
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path"
"strings"
"github.com/HexmosTech/httpie-go/input"
"github.com/HexmosTech/httpie-go/version"
"github.com/pkg/errors"
)
type bodyTuple struct {
body io.ReadCloser
getBody func() (io.ReadCloser, error)
contentLength int64
contentType string
}
func BuildHTTPRequest(in *input.Input, options *Options) (*http.Request, error) {
u, err := buildURL(in)
if err != nil {
return nil, err
}
header, err := buildHTTPHeader(in)
if err != nil {
return nil, err
}
bodyTuple, err := buildHTTPBody(in)
if err != nil {
return nil, err
}
if header.Get("Content-Type") == "" && bodyTuple.contentType != "" {
header.Set("Content-Type", bodyTuple.contentType)
}
if header.Get("User-Agent") == "" {
header.Set("User-Agent", fmt.Sprintf("httpie-go/%s", version.Current()))
}
r := http.Request{
Method: string(in.Method),
URL: u,
Header: header,
Host: header.Get("Host"),
Body: bodyTuple.body,
GetBody: bodyTuple.getBody,
ContentLength: bodyTuple.contentLength,
}
if options.Auth.Enabled {
r.SetBasicAuth(options.Auth.UserName, options.Auth.Password)
}
return &r, nil
}
func buildURL(in *input.Input) (*url.URL, error) {
q, err := url.ParseQuery(in.URL.RawQuery)
if err != nil {
return nil, errors.Wrap(err, "parsing query string")
}
for _, field := range in.Parameters {
value, err := resolveFieldValue(field)
if err != nil {
return nil, err
}
q.Add(field.Name, value)
}
u := *in.URL
u.RawQuery = q.Encode()
return &u, nil
}
func buildHTTPHeader(in *input.Input) (http.Header, error) {
header := make(http.Header)
for _, field := range in.Header.Fields {
value, err := resolveFieldValue(field)
if err != nil {
return nil, err
}
header.Add(field.Name, value)
}
return header, nil
}
func buildHTTPBody(in *input.Input) (bodyTuple, error) {
switch in.Body.BodyType {
case input.EmptyBody:
return bodyTuple{}, nil
case input.JSONBody:
return buildJSONBody(in)
case input.FormBody:
return buildFormBody(in)
case input.RawBody:
return buildRawBody(in)
default:
return bodyTuple{}, errors.Errorf("unknown body type: %v", in.Body.BodyType)
}
}
func buildJSONBody(in *input.Input) (bodyTuple, error) {
obj := map[string]interface{}{}
for _, field := range in.Body.Fields {
value, err := resolveFieldValue(field)
if err != nil {
return bodyTuple{}, err
}
obj[field.Name] = value
}
for _, field := range in.Body.RawJSONFields {
value, err := resolveFieldValue(field)
if err != nil {
return bodyTuple{}, err
}
var v interface{}
if err := json.Unmarshal([]byte(value), &v); err != nil {
return bodyTuple{}, errors.Wrapf(err, "parsing JSON value of '%s'", field.Name)
}
obj[field.Name] = v
}
body, err := json.Marshal(obj)
if err != nil {
return bodyTuple{}, errors.Wrap(err, "marshaling JSON of HTTP body")
}
return bodyTuple{
body: ioutil.NopCloser(bytes.NewReader(body)),
getBody: func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(body)), nil
},
contentLength: int64(len(body)),
contentType: "application/json",
}, nil
}
func buildFormBody(in *input.Input) (bodyTuple, error) {
if len(in.Body.Files) > 0 {
return buildMultipartBody(in)
} else {
return buildURLEncodedBody(in)
}
}
func buildURLEncodedBody(in *input.Input) (bodyTuple, error) {
form := url.Values{}
for _, field := range in.Body.Fields {
value, err := resolveFieldValue(field)
if err != nil {
return bodyTuple{}, err
}
form.Add(field.Name, value)
}
body := form.Encode()
return bodyTuple{
body: ioutil.NopCloser(strings.NewReader(body)),
getBody: func() (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader(body)), nil
},
contentLength: int64(len(body)),
contentType: "application/x-www-form-urlencoded; charset=utf-8",
}, nil
}
func buildMultipartBody(in *input.Input) (bodyTuple, error) {
var buffer bytes.Buffer
multipartWriter := multipart.NewWriter(&buffer)
for _, field := range in.Body.Fields {
if err := buildInlinePart(field, multipartWriter); err != nil {
return bodyTuple{}, err
}
}
for _, field := range in.Body.Files {
if err := buildFilePart(field, multipartWriter); err != nil {
return bodyTuple{}, err
}
}
multipartWriter.Close()
body := buffer.Bytes()
return bodyTuple{
body: ioutil.NopCloser(bytes.NewReader(body)),
getBody: func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(body)), nil
},
contentLength: int64(len(body)),
contentType: multipartWriter.FormDataContentType(),
}, nil
}
func buildInlinePart(field input.Field, multipartWriter *multipart.Writer) error {
value, err := resolveFieldValue(field)
if err != nil {
return err
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", buildContentDisposition(field.Name, ""))
w, err := multipartWriter.CreatePart(h)
if err != nil {
return err
}
if _, err := w.Write([]byte(value)); err != nil {
return err
}
return nil
}
func buildFilePart(field input.Field, multipartWriter *multipart.Writer) error {
h := make(textproto.MIMEHeader)
var filename string
if field.IsFile {
filename = path.Base(field.Value)
}
h.Set("Content-Disposition", buildContentDisposition(field.Name, filename))
w, err := multipartWriter.CreatePart(h)
if err != nil {
return err
}
if field.IsFile {
file, err := os.Open(field.Value)
if err != nil {
return errors.Wrapf(err, "failed to open '%s'", field.Value)
}
defer file.Close()
if _, err := io.Copy(w, file); err != nil {
return errors.Wrapf(err, "failed to read from '%s'", field.Value)
}
} else {
if _, err := io.Copy(w, strings.NewReader(field.Value)); err != nil {
return errors.Wrap(err, "failed to write to multipart writer")
}
}
return nil
}
func buildContentDisposition(name string, filename string) string {
var buffer bytes.Buffer
buffer.WriteString("form-data")
if name != "" {
if needEscape(name) {
fmt.Fprintf(&buffer, `; name*=utf-8''%s`, url.PathEscape(name))
} else {
fmt.Fprintf(&buffer, `; name="%s"`, name)
}
}
if filename != "" {
if needEscape(filename) {
fmt.Fprintf(&buffer, `; filename*=utf-8''%s`, url.PathEscape(filename))
} else {
fmt.Fprintf(&buffer, `; filename="%s"`, filename)
}
}
return buffer.String()
}
func needEscape(s string) bool {
for _, c := range s {
if c > 127 {
return true
}
if c < 32 && c != '\t' {
return true
}
if c == '"' || c == '\\' {
return true
}
}
return false
}
func buildRawBody(in *input.Input) (bodyTuple, error) {
return bodyTuple{
body: ioutil.NopCloser(bytes.NewReader(in.Body.Raw)),
getBody: func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(in.Body.Raw)), nil
},
contentLength: int64(len(in.Body.Raw)),
contentType: "application/json",
}, nil
}
func resolveFieldValue(field input.Field) (string, error) {
if field.IsFile {
data, err := ioutil.ReadFile(field.Value)
if err != nil {
return "", errors.Wrapf(err, "reading field value of '%s'", field.Name)
}
return string(data), nil
} else {
return field.Value, nil
}
}