-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpclient.go
195 lines (178 loc) · 4.28 KB
/
httpclient.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
package installer
import (
"encoding/json"
"encoding/xml"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
type HTTPClient struct {
http.Client
UserAgent string
}
var DefaultHTTPClient = &HTTPClient{
Client: http.Client{
Timeout: time.Second * 10,
},
UserAgent: "github.com/kmcsr/server-installer/" + PkgVersion,
}
func (c *HTTPClient) NewRequest(method string, url string, body io.Reader) (req *http.Request, err error) {
if req, err = http.NewRequest("GET", url, nil); err != nil {
return
}
req.Header.Set("User-Agent", c.UserAgent)
return
}
func (c *HTTPClient) Do(req *http.Request) (res *http.Response, err error) {
if ua := req.Header.Get("User-Agent"); ua == "" {
req.Header.Set("User-Agent", c.UserAgent)
}
if res, err = c.Client.Do(req); err != nil {
return
}
return
}
func (c *HTTPClient) Get(url string) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("GET", url, nil); err != nil {
return
}
return c.Do(req)
}
func (c *HTTPClient) GetJson(url string, obj any) (err error) {
var req *http.Request
if req, err = c.NewRequest("GET", url, nil); err != nil {
return
}
req.Header.Set("Accept", "application/json, */*;q=0.1")
var res *http.Response
if res, err = c.Do(req); err != nil {
return
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return &HttpStatusError{
Code: res.StatusCode,
}
}
return json.NewDecoder(res.Body).Decode(obj)
}
func (c *HTTPClient) GetXml(url string, obj any) (err error) {
var req *http.Request
if req, err = c.NewRequest("GET", url, nil); err != nil {
return
}
req.Header.Set("Accept", "application/xml, */*;q=0.1")
var res *http.Response
if res, err = c.Do(req); err != nil {
return
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return &HttpStatusError{
Code: res.StatusCode,
}
}
return xml.NewDecoder(res.Body).Decode(obj)
}
type progressReader struct {
Reader io.Reader
read int64
size int64
cb DlCallback
}
func newProgressReader(r io.Reader, size int64, cb DlCallback) *progressReader {
cb(0, size)
return &progressReader{
Reader: r,
read: 0,
size: size,
cb: cb,
}
}
func (r *progressReader) Read(buf []byte) (n int, err error) {
n, err = r.Reader.Read(buf)
if n > 0 {
r.read += (int64)(n)
r.cb(r.read, r.size)
}
return
}
func (c *HTTPClient) DownloadTmp(url string, pattern string, mode os.FileMode, hashes StringMap, size int64, cb DlCallback) (path string, err error) {
var res *http.Response
if res, err = c.Get(url); err != nil {
return
}
if res.StatusCode != http.StatusOK {
err = &HttpStatusError{
Code: res.StatusCode,
}
return
}
if size < 0 {
size = res.ContentLength
} else if res.ContentLength < 0 && res.ContentLength != size {
err = &ContentLengthNotMatchErr{
ContentLength: res.ContentLength,
Expect: size,
}
return
}
var r io.Reader = res.Body
if cb != nil {
r = newProgressReader(r, size, cb)
}
dir, base := filepath.Split(pattern)
var fd *os.File
if fd, err = os.CreateTemp(dir, base); err != nil {
return
}
defer func(fd *os.File) {
fd.Close()
if err != nil {
os.Remove(fd.Name())
}
}(fd)
if _, err = checkHashStream(r, hashes, fd); err != nil {
return
}
if mode != 0 {
if err = fd.Chmod(mode); err != nil {
return
}
}
path = fd.Name()
return
}
func (c *HTTPClient) Download(url string, path string, mode os.FileMode, hashes StringMap, size int64, cb DlCallback) (err error) {
var tmppath string
tmppath, err = c.DownloadTmp(url, path+".*.downloading", mode, hashes, size, cb)
if err = renameIfNotExist(tmppath, path, 0644); err != nil {
return
}
return
}
func (c *HTTPClient) Head(url string) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("HEAD", url, nil); err != nil {
return
}
return c.Do(req)
}
func (c *HTTPClient) Post(url string, contentType string, body io.Reader) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("POST", url, body); err != nil {
return
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}
func (c *HTTPClient) PostForm(url string, form url.Values) (res *http.Response, err error) {
formStr := form.Encode()
return c.Post(url, "application/x-www-form-urlencoded",
strings.NewReader(formStr))
}