-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
vulcain.go
358 lines (296 loc) · 10.6 KB
/
vulcain.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Package vulcain helps implementing the Vulcain protocol (https://vulcain.rocks) in Go projects.
// It provides helper functions to parse HTTP requests containing "preload" and "fields" directives,
// to extract and push the relations of a JSON document matched by the "preload" directive,
// and to modify the JSON document according to both directives.
//
// This package can be used in any HTTP handler as well as with httputil.ReverseProxy.
package vulcain
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"github.com/dunglas/httpsfv"
"github.com/getkin/kin-openapi/routers"
"go.uber.org/zap"
)
var (
jsonRe = regexp.MustCompile(`(?i)\bjson\b`)
preferRe = regexp.MustCompile(`\s*selector="?json-pointer"?`)
notransformRe = regexp.MustCompile(`\bno-transform\b`)
)
// Option instances allow to configure the library
type Option func(o *opt)
// WithOpenAPIFile sets the path to an OpenAPI definition (in YAML or JSON) documenting the relations between resources
// This option is only useful for non-hypermedia APIs
func WithOpenAPIFile(openAPIFile string) Option {
return func(o *opt) {
o.openAPIFile = openAPIFile
}
}
// WithEarlyHints instructs the gateway server to send Preload hints in 103 Early Hints response.
// Enabling this setting is usually useless because the gateway server doesn't supports JSON streaming yet,
// consequently the server will have to wait for the full JSON response to be received from upstream before being able
// to compute the Link headers to send.
// When the full response is available, we can send the final response directly.
// Better send Early Hints responses as soon as possible, directly from the upstream application.
// The proxy will forward them even if this option is not enabled.
func WithEarlyHints() Option {
return func(o *opt) {
o.enableEarlyHints = true
}
}
// WithMaxPushes sets the maximum number of resources to push
// There is no limit by default
func WithMaxPushes(maxPushes int) Option {
return func(o *opt) {
o.maxPushes = maxPushes
}
}
// WithLogger sets the logger to use
func WithLogger(logger *zap.Logger) Option {
return func(o *opt) {
o.logger = logger
}
}
type opt struct {
openAPIFile string
enableEarlyHints bool
maxPushes int
logger *zap.Logger
}
// Vulcain is the entrypoint of the library
// Use New() to create an instance
type Vulcain struct {
enableEarlyHints bool
pushers *pushers
openAPI *openAPI
logger *zap.Logger
}
// New creates a Vulcain instance
func New(options ...Option) *Vulcain {
opt := &opt{
maxPushes: -1,
}
for _, o := range options {
o(opt)
}
if opt.logger == nil {
opt.logger = zap.NewNop()
}
var o *openAPI
if opt.openAPIFile != "" {
o = newOpenAPI(opt.openAPIFile, opt.logger)
}
return &Vulcain{
opt.enableEarlyHints,
&pushers{maxPushes: opt.maxPushes, pusherMap: make(map[string]*waitPusher), logger: opt.logger},
o,
opt.logger,
}
}
// extractFromRequest extracts the "fields" and "preload" directives from the appropriate HTTP headers and query parameters
func extractFromRequest(req *http.Request) (fields, preload httpsfv.List, fieldsHeader, fieldsQuery, preloadHeader, preloadQuery bool) {
query := req.URL.Query()
var err error
if len(req.Header["Fields"]) > 0 {
if fields, err = httpsfv.UnmarshalList(req.Header["Fields"]); err == nil {
fieldsHeader = true
}
}
if !fieldsHeader && len(query["fields"]) > 0 {
if fields, err = httpsfv.UnmarshalList(query["fields"]); err == nil {
fieldsQuery = true
}
}
if len(req.Header["Preload"]) > 0 {
if preload, err = httpsfv.UnmarshalList(req.Header["Preload"]); err == nil {
preloadHeader = true
}
}
if !preloadHeader && len(query["preload"]) > 0 {
if preload, err = httpsfv.UnmarshalList(query["preload"]); err == nil {
preloadQuery = true
}
}
return fields, preload, fieldsHeader, fieldsQuery, preloadHeader, preloadQuery
}
// getOpenAPIRoute gets the routers.Route instance corresponding to the given URL
func (v *Vulcain) getOpenAPIRoute(url *url.URL, route *routers.Route, routeTested bool) *routers.Route {
if routeTested || v.openAPI == nil {
return route
}
return v.openAPI.getRoute(url)
}
// CreateRequestContext assign the waitPusher used by other functions to the request context.
// CreateRequestContext must always be called first.
func (v *Vulcain) CreateRequestContext(rw http.ResponseWriter, req *http.Request) context.Context {
return context.WithValue(req.Context(), ctxKey{}, v.pushers.getPusherForRequest(rw, req))
}
// IsValidRequest tells if this request contains at least one Vulcain directive.
// IsValidRequest must always be called before Apply.
func (v *Vulcain) IsValidRequest(req *http.Request) bool {
query := req.URL.Query()
// No Vulcain hints: don't modify the response
return req.Header.Get("Preload") != "" ||
req.Header.Get("Fields") != "" ||
query.Get("preload") != "" ||
query.Get("fields") != ""
}
// IsValidResponse checks if Apply will be able to deal with this response.
func (v *Vulcain) IsValidResponse(req *http.Request, responseStatus int, responseHeaders http.Header) bool {
// Not a success, marked as no-transform or not JSON: don't modify the response
if responseStatus < 200 ||
responseStatus > 300 ||
!jsonRe.MatchString(responseHeaders.Get("Content-Type")) ||
notransformRe.MatchString(responseHeaders.Get("Cache-Control")) {
return false
}
prefers, ok := req.Header["Prefer"]
if !ok {
return true
}
for _, p := range prefers {
if preferRe.MatchString(p) {
return true
}
}
return false
}
// Apply pushes the requested relations, modifies the response headers and returns a modified response to send to the client.
// It's the responsibility of the caller to use the updated response body.
// Apply must not be called if IsValidRequest or IsValidResponse return false.
func (v *Vulcain) Apply(req *http.Request, rw http.ResponseWriter, responseBody io.Reader, responseHeaders http.Header) ([]byte, error) {
f, p, fieldsHeader, fieldsQuery, preloadHeader, preloadQuery := extractFromRequest(req)
currentBody, err := io.ReadAll(responseBody)
if err != nil {
return nil, err
}
tree := &node{}
tree.importPointers(preload, p)
tree.importPointers(fields, f)
var (
oaRoute *routers.Route
oaRouteTested, usePreloadLinks bool
)
newBody := v.traverseJSON(currentBody, tree, len(f) > 0, func(n *node, val string) string {
var (
u *url.URL
useOA bool
newValue string
)
oaRoute, oaRouteTested = v.getOpenAPIRoute(req.URL, oaRoute, oaRouteTested), true
if u, useOA, err = v.parseRelation(n.String(), val, oaRoute); err != nil {
return ""
}
// Never rewrite values when using OpenAPI, use headers instead of query parameters
if (preloadQuery || fieldsQuery) && !useOA {
urlRewriter(u, n)
newValue = u.String()
}
if n.preload {
usePreloadLinks = !v.push(u, rw, req, responseHeaders, n, preloadHeader, fieldsHeader)
}
return newValue
})
if usePreloadLinks {
if v.enableEarlyHints {
h := rw.Header()
// If responseHeaders is not the same as rw.Header() (e.g. when using the built-in reverse proxy)
// temporarly copy Link headers to send the 103 response
_, ok := h["Link"]
if !ok {
h["Link"] = responseHeaders["Link"]
}
rw.WriteHeader(http.StatusEarlyHints)
if !ok {
delete(h, "Link")
}
}
responseHeaders.Add("Vary", "Preload")
}
responseHeaders.Set("Content-Length", strconv.Itoa(len(newBody)))
if fieldsHeader {
responseHeaders.Add("Vary", "Fields")
}
return newBody, nil
}
// Finish cleanups the waitPusher and, if it's the explicit response, waits for all PUSH_PROMISEs to be sent before returning.
// Finish must always be called, even if IsValidRequest or IsValidResponse returns false.
// If the current response is the explicit one and wait is false, then the body is sent instantly, even if all PUSH_PROMISEs haven't been sent yet.
func (v *Vulcain) Finish(req *http.Request, wait bool) {
v.pushers.finish(req, wait)
}
// addPreloadHeader sets preload Link rel=preload headers as fallback when Server Push isn't available (https://www.w3.org/TR/preload/).
func (v *Vulcain) addPreloadHeader(h http.Header, link string, nopush bool) {
var suffix string
if nopush {
suffix = "; nopush"
}
h.Add("Link", "<"+link+">; rel=preload; as=fetch"+suffix)
v.logger.Debug("link preload header added", zap.String("relation", link))
}
// push pushes a relation or adds a Link rel=preload header as a fallback.
// TODO: allow to set the nopush attribute using the configuration (https://www.w3.org/TR/preload/#server-push-http-2)
func (v *Vulcain) push(u *url.URL, rw http.ResponseWriter, req *http.Request, newHeaders http.Header, n *node, preloadHeader, fieldsHeader bool) bool {
url := u.String()
if v.pushers.maxPushes == 0 || u.IsAbs() {
v.addPreloadHeader(newHeaders, url, true)
return false
}
pusher := req.Context().Value(ctxKey{}).(*waitPusher)
if pusher == nil {
v.addPreloadHeader(newHeaders, url, false)
return false
}
pushOptions := &http.PushOptions{Header: req.Header.Clone()}
pushOptions.Header.Set(internalRequestHeader, pusher.id)
pushOptions.Header.Del("Preload")
pushOptions.Header.Del("Fields")
pushOptions.Header.Del("Te") // Trailing headers aren't supported by Firefox for pushes, and we don't use them
if preloadHeader {
if preload := n.httpList(preload, ""); len(preload) > 0 {
if v, err := httpsfv.Marshal(preload); err == nil {
pushOptions.Header.Set("Preload", v)
}
}
}
if fieldsHeader {
if f := n.httpList(fields, ""); len(f) > 0 {
if v, err := httpsfv.Marshal(f); err == nil {
pushOptions.Header.Set("Fields", v)
}
}
}
// HTTP/2, and relative relation, push!
if err := pusher.Push(url, pushOptions); err != nil {
// Don't add the preload header for something already pushed
if errors.Is(err, errRelationAlreadyPushed) {
return true
}
v.addPreloadHeader(newHeaders, url, false)
v.logger.Debug("failed to push", zap.Stringer("node", n), zap.String("relation", url), zap.Error(err))
return false
}
v.logger.Debug("relation pushed", zap.String("relation", url))
return true
}
// parseRelation returns the URL of a relation, using OpenAPI to build it if necessary.
func (v *Vulcain) parseRelation(selector, rel string, oaRoute *routers.Route) (*url.URL, bool, error) {
var useOA bool
if oaRoute != nil {
if oaRel := v.openAPI.getRelation(oaRoute, selector, rel); oaRel != "" {
rel = oaRel
useOA = true
}
}
u, err := url.Parse(rel)
if err == nil {
return u, useOA, nil
}
v.logger.Debug("the relation is an invalid URL", zap.String("node", selector), zap.String("relation", rel), zap.Error(err))
return nil, useOA, err
}