-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
read.go
156 lines (134 loc) · 3.79 KB
/
read.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
package http_ns
import (
"errors"
"fmt"
"io"
"github.com/inoxlang/inox/internal/commonfmt"
"github.com/inoxlang/inox/internal/core"
)
func HttpGet(ctx *core.Context, u core.URL, args ...core.Value) (*Response, error) {
var contentType core.Mimetype
var requestOptionArgs []core.Value
for _, arg := range args {
switch argVal := arg.(type) {
case core.URL:
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("url")
case core.Mimetype:
if contentType != "" {
return nil, commonfmt.FmtErrXProvidedAtLeastTwice("mime type")
}
contentType = argVal
case core.Option, core.QuantityRange:
requestOptionArgs = append(requestOptionArgs, argVal)
default:
return nil, fmt.Errorf("invalid argument, type = %T ", arg)
}
}
//checks
if u == "" {
return nil, errors.New(MISSING_URL_ARG)
}
client, opts, err := getClientAndOptions(ctx, u, requestOptionArgs...)
if err != nil {
return nil, err
}
req, err := client.MakeRequest(ctx, "GET", u, nil, string(contentType), opts)
if err != nil {
return nil, err
}
return client.DoRequest(ctx, req)
}
func HttpRead(ctx *core.Context, u core.URL, args ...core.Value) (result core.Value, finalErr error) {
var contentType core.Mimetype
var b []byte
var requestOptionArgs []core.Value
doParse := true
validateRaw := false
for _, arg := range args {
switch v := arg.(type) {
case core.Mimetype:
if contentType != "" {
finalErr = commonfmt.FmtErrXProvidedAtLeastTwice("content type")
return
}
contentType = v
case core.Option:
if v.Name == "raw" {
if v.Value == core.True {
doParse = false
}
} else {
requestOptionArgs = append(requestOptionArgs, v)
}
case core.QuantityRange:
requestOptionArgs = append(requestOptionArgs, v)
default:
return nil, fmt.Errorf("invalid argument %#v", arg)
}
}
var httpGetArgs []core.Value
if contentType != "" {
httpGetArgs = append(httpGetArgs, contentType)
}
if requestOptionArgs != nil {
httpGetArgs = append(httpGetArgs, requestOptionArgs...)
}
resp, err := HttpGet(ctx, u, httpGetArgs...)
if err != nil {
return nil, fmt.Errorf("http network error: %w", err)
}
defer resp.wrapped.Body.Close()
if resp.StatusCode(ctx) >= 400 {
return nil, fmt.Errorf("http: status code %d: %s", resp.StatusCode(ctx), resp.Status(ctx))
}
b, err = io.ReadAll(resp.wrapped.Body)
if err != nil {
return nil, fmt.Errorf("http: error while reading body: %w", err)
}
respContentType, hasRespContentType, err := resp.ContentType(ctx)
if err == nil && contentType == "" && hasRespContentType {
contentType = respContentType
}
result, _, finalErr = core.ParseOrValidateResourceContent(ctx, b, contentType, doParse, validateRaw)
return
}
// httpExists returns true if the argument is a reachable core.Host or a core.URL returning a status code in the range 200-399
func httpExists(ctx *core.Context, args ...core.Value) core.Bool {
var url core.URL
for _, arg := range args {
switch a := arg.(type) {
case core.Host:
if url != "" {
panic(commonfmt.FmtErrArgumentProvidedAtLeastTwice("entity"))
}
if a.HasScheme() && !a.HasHttpScheme() {
panic(errors.New("only http(s) hosts are accepted"))
} else {
url = core.URL(a + "/")
}
case core.URL:
if url != "" {
panic(commonfmt.FmtErrArgumentProvidedAtLeastTwice("entity"))
}
url = a
default:
panic(errors.New("core.URL or core.Host argument expected"))
}
}
if url.UnderlyingString() == "" {
panic(errors.New("missing argument"))
}
client, opts, err := getClientAndOptions(ctx, url)
if err != nil {
panic(err)
}
req, err := client.MakeRequest(ctx, "HEAD", url, nil, "", opts)
if err != nil {
panic(err)
}
resp, err := client.DoRequest(ctx, req)
if err == nil {
defer resp.wrapped.Body.Close()
}
return err == nil && resp.wrapped.StatusCode <= 399
}