-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrows.go
468 lines (413 loc) Β· 12.6 KB
/
arrows.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
// Copyright (C) 2019 - 2023 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/gurl
//
// Package send defines a pure computations to compose HTTP request senders
package send
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"github.com/fogfish/gurl/v2"
"github.com/fogfish/gurl/v2/http"
)
// Method defines HTTP Method/Verb to the request
func Method(verb string) http.Arrow {
return func(ctx *http.Context) error {
ctx.Method = verb
return nil
}
}
// Authority is part of URL, use the type to prevent escaping
type Authority string
// Path is part of URL, use the type to prevent path escaping
type Path string
// URI defines destination URI
// use Params arrow if you need to supply URL query params.
func URI(url string, args ...any) http.Arrow {
return func(ctx *http.Context) error {
if len(args) != 0 {
url = mkURI(url, args)
}
if !strings.HasPrefix(url, "http") {
url = ctx.Host + url
}
if !strings.HasPrefix(url, "http") {
return &gurl.NotSupported{URL: url}
}
req, err := http.NewRequest(ctx.Method, url)
if err != nil {
return err
}
ctx.Request = req
return nil
}
}
func mkURI(uri string, args []any) string {
opts := []any{}
for _, x := range args {
switch v := x.(type) {
case *url.URL:
v.Path = strings.TrimSuffix(v.Path, "/")
opts = append(opts, v.String())
case *Path:
opts = append(opts, *v)
case Path:
opts = append(opts, v)
case *Authority:
opts = append(opts, *v)
case Authority:
opts = append(opts, v)
case string:
opts = append(opts, url.PathEscape(v))
case *string:
opts = append(opts, url.PathEscape(*v))
case int:
opts = append(opts, v)
case *int:
opts = append(opts, *v)
default:
opts = append(opts, url.PathEscape(urlSegment(x)))
}
}
return fmt.Sprintf(uri, opts...)
}
func urlSegment(arg any) string {
val := reflect.ValueOf(arg)
if val.Kind() == reflect.Ptr {
return fmt.Sprintf("%v", val.Elem())
}
return fmt.Sprintf("%v", val)
}
// Params appends query params to request URL. The arrow takes a struct and
// converts it to map[string]string. The function fails if input is not convertable
// to map of strings (e.g. contains nested struct).
func Params[T any](query T) http.Arrow {
return func(cat *http.Context) error {
bytes, err := json.Marshal(query)
if err != nil {
return err
}
var req map[string]string
err = json.Unmarshal(bytes, &req)
if err != nil {
return err
}
uri := cat.Request.URL
q := uri.Query()
for k, v := range req {
q.Add(k, v)
}
uri.RawQuery = q.Encode()
cat.Request.URL = uri
return nil
}
}
// Param appends query params to request URL.
func Param[T interface{ string | int }](key string, val T) http.Arrow {
return func(ctx *http.Context) error {
uri := ctx.Request.URL
q := uri.Query()
switch v := any(val).(type) {
case string:
q.Add(key, v)
case int:
q.Add(key, strconv.Itoa(v))
}
uri.RawQuery = q.Encode()
ctx.Request.URL = uri
return nil
}
}
// Header defines HTTP headers to the request
//
// ΓΈ.Header("User-Agent", "gurl"),
func Header[T http.ReadableHeaderValues](header string, value T) http.Arrow {
return HeaderOf[T](header).Set(value)
}
// Type of HTTP Header
//
// const Host = HeaderOf[string]("Host")
// ΓΈ.Host.Set("example.com")
type HeaderOf[T http.ReadableHeaderValues] string
// Sets value of HTTP header
func (h HeaderOf[T]) Set(value T) http.Arrow {
switch v := any(value).(type) {
case string:
return func(cat *http.Context) error {
cat.Request.Header.Add(string(h), v)
return nil
}
case int:
return func(cat *http.Context) error {
cat.Request.Header.Add(string(h), strconv.Itoa(v))
return nil
}
case time.Time:
return func(cat *http.Context) error {
cat.Request.Header.Add(string(h), v.UTC().Format(time.RFC1123))
return nil
}
default:
panic("invalid type")
}
}
// Type of HTTP Header, Content-Type enumeration
//
// const ContentType = HeaderEnumContent("Content-Type")
// ΓΈ.ContentType.JSON
type HeaderEnumContent string
// Sets value of HTTP header
func (h HeaderEnumContent) Set(value string) http.Arrow {
return func(cat *http.Context) error {
cat.Request.Header.Add(string(h), value)
return nil
}
}
// ApplicationJSON defines header `???: application/json`
func (h HeaderEnumContent) ApplicationJSON(cat *http.Context) error {
cat.Request.Header.Add(string(h), "application/json")
return nil
}
// JSON defines header `???: application/json`
func (h HeaderEnumContent) JSON(cat *http.Context) error {
cat.Request.Header.Add(string(h), "application/json")
return nil
}
// Form defined Header `???: application/x-www-form-urlencoded`
func (h HeaderEnumContent) Form(cat *http.Context) error {
cat.Request.Header.Add(string(h), "application/x-www-form-urlencoded")
return nil
}
// TextPlain defined Header `???: text/plain`
func (h HeaderEnumContent) TextPlain(cat *http.Context) error {
cat.Request.Header.Add(string(h), "text/plain")
return nil
}
// Text defined Header `???: text/plain`
func (h HeaderEnumContent) Text(cat *http.Context) error {
cat.Request.Header.Add(string(h), "text/plain")
return nil
}
// TextHTML defined Header `???: text/html`
func (h HeaderEnumContent) TextHTML(cat *http.Context) error {
cat.Request.Header.Add(string(h), "text/html")
return nil
}
// HTML defined Header `???: text/html`
func (h HeaderEnumContent) HTML(cat *http.Context) error {
cat.Request.Header.Add(string(h), "text/html")
return nil
}
// Type of HTTP Header, Connection enumeration
//
// const Connection = HeaderEnumConnection("Connection")
// ΓΈ.Connection.KeepAlive
type HeaderEnumConnection string
// Sets value of HTTP header
func (h HeaderEnumConnection) Set(value string) http.Arrow {
return func(cat *http.Context) error {
cat.Request.Header.Add(string(h), value)
return nil
}
}
// KeepAlive defines header `???: keep-alive`
func (h HeaderEnumConnection) KeepAlive(cat *http.Context) error {
cat.Request.Header.Add(string(h), "keep-alive")
cat.Request.Close = false
return nil
}
// Close defines header `???: close`
func (h HeaderEnumConnection) Close(cat *http.Context) error {
cat.Request.Header.Add(string(h), "close")
cat.Request.Close = true
return nil
}
// Type of HTTP Header, Transfer-Encoding enumeration
//
// const TransferEncoding = HeaderEnumTransferEncoding("Transfer-Encoding")
// ΓΈ.TransferEncoding.Chunked
type HeaderEnumTransferEncoding string
// Sets value of HTTP header
func (h HeaderEnumTransferEncoding) Set(value string) http.Arrow {
return func(cat *http.Context) error {
cat.Request.TransferEncoding = strings.Split(value, ",")
return nil
}
}
// Chunked defines header `Transfer-Encoding: chunked`
func (h HeaderEnumTransferEncoding) Chunked(cat *http.Context) error {
cat.Request.TransferEncoding = []string{"chunked"}
return nil
}
// Identity defines header `Transfer-Encoding: identity`
func (h HeaderEnumTransferEncoding) Identity(cat *http.Context) error {
cat.Request.TransferEncoding = []string{"identity"}
return nil
}
// Header Content-Length
//
// const ContentLength = HeaderEnumContentLength("Content-Length")
// ΓΈ.ContentLength.Set(1024)
type HeaderEnumContentLength string
// Is sets a literal value of HTTP header
func (h HeaderEnumContentLength) Set(value int64) http.Arrow {
return func(cat *http.Context) error {
cat.Request.ContentLength = value
return nil
}
}
// List of supported HTTP header constants
// https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields
const (
Accept = HeaderEnumContent("Accept")
AcceptCharset = HeaderOf[string]("Accept-Charset")
AcceptEncoding = HeaderOf[string]("Accept-Encoding")
AcceptLanguage = HeaderOf[string]("Accept-Language")
Authorization = HeaderOf[string]("Authorization")
CacheControl = HeaderOf[string]("Cache-Control")
Connection = HeaderEnumConnection("Connection")
ContentEncoding = HeaderOf[string]("Content-Encoding")
ContentLength = HeaderEnumContentLength("Content-Length")
ContentType = HeaderEnumContent("Content-Type")
Cookie = HeaderOf[string]("Cookie")
Date = HeaderOf[time.Time]("Date")
From = HeaderOf[string]("From")
Host = HeaderOf[string]("Host")
IfMatch = HeaderOf[string]("If-Match")
IfModifiedSince = HeaderOf[time.Time]("If-Modified-Since")
IfNoneMatch = HeaderOf[string]("If-None-Match")
IfRange = HeaderOf[string]("If-Range")
IfUnmodifiedSince = HeaderOf[time.Time]("If-Unmodified-Since")
Origin = HeaderOf[string]("Origin")
Range = HeaderOf[string]("Range")
Referer = HeaderOf[string]("Referer")
TransferEncoding = HeaderEnumTransferEncoding("Transfer-Encoding")
UserAgent = HeaderOf[string]("User-Agent")
Upgrade = HeaderOf[string]("Upgrade")
)
// Send payload to destination URL. You can also use native Go data types
// (e.g. maps, struct, etc) as egress payload. The library implicitly encodes
// input structures to binary using Content-Type as a hint. The function fails
// if content type is not supported by the library.
//
// The function accept a "classical" data container such as string, []bytes or
// io.Reader interfaces.
func Send(data any) http.Arrow {
return func(cat *http.Context) error {
chunked := cat.Request.Header.Get(string(TransferEncoding)) == "chunked"
content := cat.Request.Header.Get(string(ContentType))
if content == "" {
return fmt.Errorf("unknown Content-Type")
}
switch stream := data.(type) {
case string:
cat.Request.Body = io.NopCloser(bytes.NewBuffer([]byte(stream)))
// cat.Request.GetBody = func() (io.ReadCloser, error) {
// return io.NopCloser(bytes.NewBuffer([]byte(stream))), nil
// }
if !chunked && cat.Request.ContentLength == 0 {
cat.Request.ContentLength = int64(len(stream))
}
case *strings.Reader:
cat.Request.Body = io.NopCloser(stream)
// snapshot := *stream
// cat.Request.GetBody = func() (io.ReadCloser, error) {
// r := snapshot
// return io.NopCloser(&r), nil
// }
if !chunked && cat.Request.ContentLength == 0 {
cat.Request.ContentLength = int64(stream.Len())
}
case []byte:
cat.Request.Body = io.NopCloser(bytes.NewBuffer(stream))
// cat.Request.GetBody = func() (io.ReadCloser, error) {
// return io.NopCloser(bytes.NewBuffer(stream)), nil
// }
if !chunked && cat.Request.ContentLength == 0 {
cat.Request.ContentLength = int64(len(stream))
}
case *bytes.Buffer:
cat.Request.Body = io.NopCloser(stream)
// snapshot := stream.Bytes()
// cat.Request.GetBody = func() (io.ReadCloser, error) {
// return io.NopCloser(bytes.NewBuffer(snapshot)), nil
// }
if !chunked && cat.Request.ContentLength == 0 {
cat.Request.ContentLength = int64(stream.Len())
}
case *bytes.Reader:
cat.Request.Body = io.NopCloser(stream)
// snapshot := *stream
// cat.Request.GetBody = func() (io.ReadCloser, error) {
// r := snapshot
// return io.NopCloser(&r), nil
// }
if !chunked && cat.Request.ContentLength == 0 {
cat.Request.ContentLength = int64(stream.Len())
}
case io.Reader:
rc, ok := stream.(io.ReadCloser)
if !ok {
rc = io.NopCloser(stream)
}
cat.Request.Body = rc
default:
pkt, err := encode(content, data)
if err != nil {
return err
}
cat.Request.Body = io.NopCloser(pkt)
snapshot := pkt.Bytes()
cat.Request.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewBuffer(snapshot)), nil
}
if !chunked && cat.Request.ContentLength == 0 {
cat.Request.ContentLength = int64(pkt.Len())
}
}
return nil
}
}
func encode(content string, data interface{}) (buf *bytes.Buffer, err error) {
switch {
// "application/json" and other variants
case strings.Contains(content, "json"):
buf, err = encodeJSON(data)
// "application/x-www-form-urlencoded"
case strings.Contains(content, "www-form"):
buf, err = encodeForm(data)
default:
err = fmt.Errorf("unsupported Content-Type %v", content)
}
return
}
func encodeJSON(data interface{}) (*bytes.Buffer, error) {
json, err := json.Marshal(data)
return bytes.NewBuffer(json), err
}
func encodeForm(data interface{}) (*bytes.Buffer, error) {
bin, err := json.Marshal(data)
if err != nil {
return nil, err
}
var req map[string]string
err = json.Unmarshal(bin, &req)
if err != nil {
return nil, fmt.Errorf("encode application/x-www-form-urlencoded: %w", err)
}
var payload url.Values = make(map[string][]string)
for key, val := range req {
payload[key] = []string{val}
}
return bytes.NewBuffer([]byte(payload.Encode())), nil
}