Repository navigation
Expand file tree
/
Copy pathhttputil.go
More file actions
173 lines (159 loc) · 5.31 KB
/
Copy pathhttputil.go
File metadata and controls
173 lines (159 loc) · 5.31 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"sync"
"time"
)
// decodeJSON decodes a JSON request body into v.
func decodeJSON(r *http.Request, v any) error {
defer func() { _ = r.Body.Close() }()
dec := json.NewDecoder(io.LimitReader(r.Body, 4<<20))
return dec.Decode(v)
}
// httpClientSingleton is a process-wide http.Client with a reused transport
// (connection pooling). Creating a client/transport per call defeats keep-alive
// and kills throughput, which matters at this request rate.
var (
httpClientOnce sync.Once
httpClientShared *http.Client
)
// GetHttpClient returns the shared http.Client honoring the global PROXY
// setting. The transport is built once and reused: connections to the local
// wrapper-lite instances and to Apple are kept alive and pooled.
func GetHttpClient() *http.Client {
httpClientOnce.Do(func() {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 200,
MaxIdleConnsPerHost: 32,
// wrapper-lite closes idle keep-alive connections after 30s
// (httplib set_keep_alive_timeout(30)), so the client must retire
// idle connections sooner than that. With a longer client timeout
// the pool hands out connections the server has already closed,
// producing a burst of "EOF" transport errors.
IdleConnTimeout: 20 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
}
if PROXY != "" {
proxyUrl, err := url.Parse(PROXY)
if err == nil {
tr.Proxy = http.ProxyURL(proxyUrl)
}
}
httpClientShared = &http.Client{
Transport: tr,
Timeout: 60 * time.Second,
}
})
return httpClientShared
}
// LiteReply is the wire envelope used by wrapper-lite and by wrapper-manager:
// {"code":0,"msg":"SUCCESS","data":{...}}.
type LiteReply struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data json.RawMessage `json:"data"`
}
// Cache-Control headers used to steer Cloudflare's cache for the proxied
// wrapper-lite API. Success (code 0) on cacheable endpoints may be cached;
// anything else must NOT be cached (a cached failure would poison the edge
// cache until the TTL expires).
const (
headerCacheControl = "Cache-Control"
// cacheableTTLSeconds is a reference max-age on cacheable successes. When
// Cloudflare is configured with "ignore origin Cache-Control and use this
// TTL", the edge TTL wins; otherwise this value is used.
cacheableTTLSeconds = 3600
cacheControlPublic = "public, max-age=3600"
cacheControlNoStore = "no-store"
)
// setCacheHeader sets Cache-Control based on whether the endpoint is cacheable
// (part of the media/catalog API surface) and whether the business code is a
// success (0).
func setCacheHeader(w http.ResponseWriter, cacheableEndpoint bool, code int) {
if !cacheableEndpoint {
w.Header().Set(headerCacheControl, cacheControlNoStore)
return
}
if code == 0 {
w.Header().Set(headerCacheControl, cacheControlPublic)
} else {
w.Header().Set(headerCacheControl, cacheControlNoStore)
}
}
// WriteEnvelope writes a unified envelope to the HTTP response. cacheable
// marks endpoints that Cloudflare is allowed to cache on success.
func WriteEnvelope(w http.ResponseWriter, code int, msg string, data any) {
setCacheHeader(w, false, code)
w.Header().Set("Content-Type", "application/json")
payload := map[string]any{
"code": code,
"msg": msg,
"data": data,
}
body, err := json.Marshal(payload)
if err != nil {
body = []byte(`{"code":-1,"msg":"internal error","data":null}`)
}
_, _ = w.Write(body)
}
// WriteEnvelopeCacheable is like WriteEnvelope but marks the endpoint as
// cacheable, so successful (code 0) responses get a public Cache-Control and
// failures are still no-store.
func WriteEnvelopeCacheable(w http.ResponseWriter, code int, msg string, data any) {
setCacheHeader(w, true, code)
w.Header().Set("Content-Type", "application/json")
payload := map[string]any{
"code": code,
"msg": msg,
"data": data,
}
body, err := json.Marshal(payload)
if err != nil {
body = []byte(`{"code":-1,"msg":"internal error","data":null}`)
}
_, _ = w.Write(body)
}
// WriteLiteSuccess writes code:0 with the given data object.
func WriteLiteSuccess(w http.ResponseWriter, data any) {
WriteEnvelope(w, 0, "SUCCESS", data)
}
// WriteLiteError writes a non-zero envelope error.
func WriteLiteError(w http.ResponseWriter, msg string) {
WriteEnvelope(w, -1, msg, nil)
}
// fetchLite performs a request against a running wrapper-lite instance and
// returns the raw body. The response envelope is passed through untouched so
// that clients see exactly the lite contract.
func fetchLite(port int, method, path string, query url.Values, body io.Reader, contentType string) ([]byte, error) {
u := url.URL{
Scheme: "http",
Host: fmt.Sprintf("127.0.0.1:%d", port),
Path: path,
RawQuery: query.Encode(),
}
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
resp, err := GetHttpClient().Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
return io.ReadAll(resp.Body)
}