-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.go
More file actions
140 lines (115 loc) · 3.22 KB
/
Copy pathfunc.go
File metadata and controls
140 lines (115 loc) · 3.22 KB
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
package lightweigit
import (
"bytes"
"compress/flate"
"encoding/binary"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io"
"net/http"
"net/url"
"runtime"
"strings"
"github.com/voluminor/lightweigit-loader/target"
)
// // // // // // // // // // // // // // // //
func UserAgent(obj ProviderInterface) string {
return fmt.Sprintf("%s %s; %s (Goland %s %s)", target.Name, target.Version, obj.Type(), runtime.GOOS, runtime.GOARCH)
}
func GetJSON(obj ProviderInterface, u string, out any) error {
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", UserAgent(obj))
req.Header.Set("Accept", "application/json")
resp, err := HttpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return ErrNotFound
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
detail := strings.TrimSpace(string(b))
switch resp.StatusCode {
case http.StatusForbidden:
return fmt.Errorf("%s api error: %s: %s: %w", obj.Type(), resp.Status, detail, ErrForbidden)
case http.StatusTooManyRequests:
return fmt.Errorf("%s api error: %s: %s: %w", obj.Type(), resp.Status, detail, ErrTooManyRequests)
}
return fmt.Errorf("%s api error: %s: %s", obj.Type(), resp.Status, detail)
}
// Read one byte past the cap: hitting it means the body was cut, so
// decoding would fail with a misleading JSON error. Report it explicitly.
b, err := io.ReadAll(io.LimitReader(resp.Body, maxJSONBody+1))
if err != nil {
return err
}
if len(b) > maxJSONBody {
return fmt.Errorf("%s api: body over %d bytes: %w", obj.Type(), maxJSONBody, ErrResponseTooLarge)
}
return json.Unmarshal(b, out)
}
// //
func BuildURL(scheme, host, path, query string) *url.URL {
return &url.URL{
Scheme: scheme,
Host: host,
Path: path,
RawPath: (&url.URL{Path: path}).EscapedPath(),
OmitHost: host == "",
RawQuery: query,
}
}
func AddURL(u *url.URL, addToPath, setQuery string) *url.URL {
u.Path += addToPath
u.RawPath = (&url.URL{Path: u.Path}).EscapedPath()
u.RawQuery = setQuery
return u
}
// //
func Marshal(m target.ModType, a any) []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(a)
dataBuf := buf.Bytes()
crc := crc32.ChecksumIEEE(dataBuf)
dst := make([]byte, 4)
binary.LittleEndian.PutUint32(dst, crc)
buf.Reset()
w, _ := flate.NewWriter(&buf, flate.BestCompression)
w.Write(dataBuf)
w.Close()
var out bytes.Buffer
out.WriteByte(byte(m))
io.Copy(&out, bytes.NewReader(buf.Bytes()))
out.Write(dst[:])
return out.Bytes()
}
func Unmarshal(data []byte, a any) (target.ModType, error) {
if len(data) < 5 {
return 0, errors.New("not enough data to unmarshal")
}
m := target.ModType(data[0])
if m.String() == "unknown" {
return m, fmt.Errorf("unknown mod type")
}
crc := binary.LittleEndian.Uint32(data[len(data)-4:])
r := flate.NewReader(bytes.NewReader(data[1 : len(data)-4]))
var out bytes.Buffer
io.Copy(&out, r)
r.Close()
data = out.Bytes()
if crc32.ChecksumIEEE(data) != crc {
return m, fmt.Errorf("invalid checksum")
}
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return m, dec.Decode(a)
}