/
request.go
194 lines (167 loc) · 4.67 KB
/
request.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
package fetch
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"net/url"
"reflect"
"strings"
"text/template"
_ "unsafe"
"github.com/shiroyk/cloudcat"
)
// NewRequest returns a new RequestConfig given a method, URL, optional body, optional headers.
func NewRequest(method, u string, body any, headers map[string]string) (*http.Request, error) {
var reqBody io.Reader = http.NoBody
if body != nil {
// Convert body to io.Reader
switch data := body.(type) {
default:
kind := reflect.ValueOf(body).Kind()
if kind != reflect.Struct && kind != reflect.Map {
break
}
j, err := json.Marshal(body)
if err != nil {
return nil, err
}
if headers == nil {
headers = make(map[string]string)
}
if _, ok := headers["Content-Type"]; !ok {
headers["Content-Type"] = "application/json"
}
reqBody = bytes.NewReader(j)
case io.Reader:
reqBody = data
case string:
reqBody = bytes.NewBufferString(data)
case []byte:
reqBody = bytes.NewBuffer(data)
}
}
req, err := http.NewRequest(method, u, reqBody)
if err != nil {
return nil, err
}
// set headers
for k, v := range headers {
req.Header.Set(k, v)
}
return req, nil
}
// NewTemplateRequest returns a new RequestConfig given a http template with argument.
func NewTemplateRequest(funcs template.FuncMap, tpl string, arg any) (*http.Request, error) {
tmp, err := template.New("url").Funcs(funcs).Parse(tpl)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err = tmp.Execute(buf, arg); err != nil {
return nil, err
}
// https://github.com/golang/go/issues/24963
novalue := strings.ReplaceAll(buf.String(), "<no value>", "")
tp := newTextprotoReader(bufio.NewReader(strings.NewReader(novalue)))
// First line: GET /index.html HTTP/1.0
var s string
if s, err = tp.ReadLine(); err != nil {
return nil, err
}
defer func() {
putTextprotoReader(tp)
if errors.Is(err, io.EOF) {
err = io.ErrUnexpectedEOF
}
}()
req := new(http.Request)
var rawURI string
req.Method, rawURI, req.Proto = parseRequestLine(s)
if !validMethod(req.Method) {
return nil, fmt.Errorf("invalid method %s", req.Method)
}
var ok bool
if req.ProtoMajor, req.ProtoMinor, ok = http.ParseHTTPVersion(req.Proto); !ok {
return nil, fmt.Errorf("malformed HTTP version %s", req.Proto)
}
if req.URL, err = url.ParseRequestURI(rawURI); err != nil {
return nil, err
}
// Subsequent lines: Key: value.
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
req.Header = http.Header(mimeHeader)
if len(req.Header["Host"]) > 1 {
return nil, fmt.Errorf("too many Host headers")
}
// RFC 7230, section 5.3: Must treat
// GET /index.html HTTP/1.1
// Host: www.google.com
// and
// GET http://www.google.com/index.html HTTP/1.1
// Host: doesntmatter
// the same. In the second case, any Host line is ignored.
req.Host = req.URL.Host
fixPragmaCacheControl(req.Header)
req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header)
if req.Method != http.MethodHead || req.Body == nil {
// Read body and fix content-length
body := new(bytes.Buffer)
if _, err = tp.R.WriteTo(body); err != nil {
return nil, err
}
if body.Len() == 0 {
req.Body = http.NoBody
} else {
req.ContentLength = int64(body.Len())
req.Body = io.NopCloser(body)
}
}
return req, nil
}
// DefaultTemplateFuncMap The default template function map
func DefaultTemplateFuncMap(cache cloudcat.Cache) template.FuncMap {
return template.FuncMap{
"get": func(key string) (ret string) {
if v, ok := cache.Get(key); ok {
return string(v)
}
return
},
"set": func(key string, value string) (ret string) {
cache.Set(key, []byte(value))
return
},
}
}
// parseRequestLine parses "GET /foo HTTP/1.1" into its three parts.
// Default proto HTTP/1.1.
func parseRequestLine(line string) (method, requestURI, proto string) {
method, rest, ok1 := strings.Cut(line, " ")
requestURI, proto, ok2 := strings.Cut(rest, " ")
if !ok1 {
// default GET request
return "GET", line, "HTTP/1.1"
}
if !ok2 {
return method, requestURI, "HTTP/1.1"
}
return method, requestURI, proto
}
//go:linkname newTextprotoReader net/http.newTextprotoReader
func newTextprotoReader(br *bufio.Reader) *textproto.Reader
//go:linkname putTextprotoReader net/http.putTextprotoReader
func putTextprotoReader(r *textproto.Reader)
//go:linkname validMethod net/http.validMethod
func validMethod(method string) bool
//go:linkname shouldClose net/http.shouldClose
func shouldClose(major, minor int, header http.Header) bool
//go:linkname fixPragmaCacheControl net/http.fixPragmaCacheControl
func fixPragmaCacheControl(header http.Header)