-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandlers.go
286 lines (254 loc) · 10.1 KB
/
handlers.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
package stremio
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"math"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"github.com/cespare/xxhash/v2"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
)
type customEndpoint struct {
method string
path string
handler fiber.Handler
}
func createHealthHandler(logger *zap.Logger) fiber.Handler {
return func(c *fiber.Ctx) error {
logger.Debug("healthHandler called")
return c.SendString("OK")
}
}
func createManifestHandler(manifest Manifest, logger *zap.Logger, manifestCallback ManifestCallback, userDataType reflect.Type, userDataIsBase64 bool) fiber.Handler {
// When there's user data we want Stremio to show the "Install" button, which it only does when "configurationRequired" is false.
// To not change the boolean value of the manifest object on the fly and thus mess with a single object across concurrent goroutines, we copy it and return two different objects.
// Note that this manifest copy has some values shallowly copied, but `BehaviorHints.ConfigurationRequired` is a simple type and thus a real copy.
configuredManifest := manifest
configuredManifest.BehaviorHints.ConfigurationRequired = false
manifestBody, err := json.Marshal(manifest)
if err != nil {
logger.Fatal("Couldn't marshal manifest", zap.Error(err))
}
configuredManifestBody, err := json.Marshal(configuredManifest)
if err != nil {
logger.Fatal("Couldn't marshal configured manifest", zap.Error(err))
}
return func(c *fiber.Ctx) error {
logger.Debug("manifestHandler called")
// First call the callback so the SDK user can prevent further processing
var userData interface{}
userDataString := c.Params("userData")
configured := false
if userDataString == "" {
if userDataType == nil {
userData = ""
} else {
userData = nil
}
} else {
configured = true
if userDataType == nil {
userData = userDataString
} else {
var err error
if userData, err = decodeUserData(userDataString, userDataType, logger, userDataIsBase64); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
}
}
if manifestCallback != nil {
manifestClone := manifest.clone()
if status := manifestCallback(c.Context(), &manifestClone, userData); status >= 400 {
return c.SendStatus(status)
}
// Similar to what we do before returning this handler func, we need to set `ConfigurationRequired` to false so that Stremio shows an install button at all
if configured {
manifestClone.BehaviorHints.ConfigurationRequired = false
}
// Probably no performance gain when checking deep equality of original vs cloned manifest to skip potentially unnecessary JSON encoding.
clonedManifestBody, err := json.Marshal(manifestClone)
if err != nil {
logger.Fatal("Couldn't marshal cloned manifest", zap.Error(err))
}
logger.Debug("Responding", zap.ByteString("body", clonedManifestBody))
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
return c.Send(clonedManifestBody)
}
if configured {
logger.Debug("Responding", zap.ByteString("body", configuredManifestBody))
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
return c.Send(configuredManifestBody)
} else {
logger.Debug("Responding", zap.ByteString("body", manifestBody))
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
return c.Send(manifestBody)
}
}
}
func createCatalogHandler(catalogHandlers map[string]CatalogHandler, cacheAge time.Duration, cachePublic, handleEtag bool, logger *zap.Logger, userDataType reflect.Type, userDataIsBase64 bool) fiber.Handler {
handlers := make(map[string]handler, len(catalogHandlers))
for k, v := range catalogHandlers {
handlers[k] = convertCatalogHandler(v)
}
return createHandler("catalog", handlers, []byte("metas"), cacheAge, cachePublic, handleEtag, logger, userDataType, userDataIsBase64)
}
func createStreamHandler(streamHandlers map[string]StreamHandler, cacheAge time.Duration, cachePublic, handleEtag bool, logger *zap.Logger, userDataType reflect.Type, userDataIsBase64 bool) fiber.Handler {
handlers := make(map[string]handler, len(streamHandlers))
for k, v := range streamHandlers {
handlers[k] = convertStreamHandler(v)
}
return createHandler("stream", handlers, []byte("streams"), cacheAge, cachePublic, handleEtag, logger, userDataType, userDataIsBase64)
}
func convertCatalogHandler(h CatalogHandler) handler {
return func(ctx context.Context, id string, userData interface{}) (interface{}, error) {
return h(ctx, id, userData)
}
}
func convertStreamHandler(h StreamHandler) handler {
return func(ctx context.Context, id string, userData interface{}) (interface{}, error) {
return h(ctx, id, userData)
}
}
// Common handler (same signature as both catalog and stream handler)
type handler func(ctx context.Context, id string, userData interface{}) (interface{}, error)
func createHandler(handlerName string, handlers map[string]handler, jsonArrayKey []byte, cacheAge time.Duration, cachePublic, handleEtag bool, logger *zap.Logger, userDataType reflect.Type, userDataIsBase64 bool) fiber.Handler {
handlerName = handlerName + "Handler"
handlerLogMsg := handlerName + " called"
var cacheHeaderVal string
if cacheAge != 0 {
cacheAgeSeconds := strconv.FormatFloat(math.Round(cacheAge.Seconds()), 'f', 0, 64)
cacheHeaderVal = "max-age=" + cacheAgeSeconds
if cachePublic {
cacheHeaderVal += ", public"
} else {
cacheHeaderVal += ", private"
}
}
logger = logger.With(zap.String("handler", handlerName))
return func(c *fiber.Ctx) error {
logger.Debug(handlerLogMsg)
requestedType := c.Params("type")
requestedID := c.Params("id")
requestedID, err := url.PathUnescape(requestedID)
if err != nil {
logger.Error("Requested ID couldn't be unescaped", zap.String("requestedID", requestedID))
return c.SendStatus(fiber.StatusBadRequest)
}
zapLogType, zapLogID := zap.String("requestedType", requestedType), zap.String("requestedID", requestedID)
// Check if we have a handler for the type
handler, ok := handlers[requestedType]
if !ok {
logger.Warn("Got request for unhandled type; returning 404")
return c.SendStatus(fiber.StatusNotFound)
}
// Decode user data
var userData interface{}
userDataString := c.Params("userData")
if userDataType == nil {
userData = userDataString
} else if userDataString == "" {
userData = nil
} else {
var err error
if userData, err = decodeUserData(userDataString, userDataType, logger, userDataIsBase64); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
}
res, err := handler(c.Context(), requestedID, userData)
if err != nil {
switch err {
case NotFound:
logger.Warn("Got request for unhandled media ID; returning 404")
return c.SendStatus(fiber.StatusNotFound)
case BadRequest:
logger.Warn("Got bad request; returning 400")
return c.SendStatus(fiber.StatusBadRequest)
default:
logger.Error("Addon returned error", zap.Error(err), zapLogType, zapLogID)
return c.SendStatus(fiber.StatusInternalServerError)
}
}
resBody, err := json.Marshal(res)
if err != nil {
logger.Error("Couldn't marshal response", zap.Error(err), zapLogType, zapLogID)
return c.SendStatus(fiber.StatusInternalServerError)
}
// Handle ETag
var eTag string
if handleEtag {
hash := xxhash.Sum64(resBody)
eTag = strconv.FormatUint(hash, 16)
ifNoneMatch := c.Get("If-None-Match")
zapLogIfNoneMatch, zapLogETagServer := zap.String("If-None-Match", ifNoneMatch), zap.String("ETag", eTag)
modified := false
if ifNoneMatch == "*" {
logger.Debug("If-None-Match is \"*\", responding with 304", zapLogIfNoneMatch, zapLogETagServer, zapLogType, zapLogID)
} else if ifNoneMatch != eTag {
logger.Debug("If-None-Match != ETag", zapLogIfNoneMatch, zapLogETagServer, zapLogType, zapLogID)
modified = true
} else {
logger.Debug("ETag matches, responding with 304", zapLogIfNoneMatch, zapLogETagServer, zapLogType, zapLogID)
}
if !modified {
c.Set(fiber.HeaderCacheControl, cacheHeaderVal) // Required according to https://tools.ietf.org/html/rfc7232#section-4.1
c.Set(fiber.HeaderETag, eTag) // We set it to make sure a client doesn't overwrite its cached ETag with an empty string or so.
return c.SendStatus(fiber.StatusNotModified)
}
}
if len(jsonArrayKey) > 0 {
prefix := append([]byte(`{"`), jsonArrayKey...)
prefix = append(prefix, '"', ':')
resBody = append(prefix, resBody...)
resBody = append(resBody, '}')
}
logger.Debug("Responding", zap.ByteString("body", resBody), zapLogType, zapLogID)
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
if cacheHeaderVal != "" {
c.Set(fiber.HeaderCacheControl, cacheHeaderVal)
if handleEtag {
c.Set(fiber.HeaderETag, eTag)
}
}
return c.Send(resBody)
}
}
func createRootHandler(redirectURL string, logger *zap.Logger) fiber.Handler {
return func(c *fiber.Ctx) error {
logger.Debug("rootHandler called")
logger.Debug("Responding with redirect", zap.String("redirectURL", redirectURL))
c.Set(fiber.HeaderLocation, redirectURL)
return c.SendStatus(fiber.StatusMovedPermanently)
}
}
func decodeUserData(data string, t reflect.Type, logger *zap.Logger, userDataIsBase64 bool) (interface{}, error) {
logger.Debug("Decoding user data", zap.String("userData", data))
var userDataDecoded []byte
var err error
if userDataIsBase64 {
// Remove padding so that both Base64URL values with and without padding work.
data = strings.TrimSuffix(data, "=")
userDataDecoded, err = base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(data)
} else {
var userDataDecodedString string
userDataDecodedString, err = url.PathUnescape(data)
userDataDecoded = []byte(userDataDecodedString)
}
if err != nil {
// We use WARN instead of ERROR because it's most likely an *encoding* error on the client side
logger.Warn("Couldn't decode user data", zap.Error(err))
return nil, err
}
userData := reflect.New(t).Interface()
if err := json.Unmarshal(userDataDecoded, userData); err != nil {
logger.Warn("Couldn't unmarshal user data", zap.Error(err))
return nil, err
}
logger.Debug("Decoded user data", zap.String("userData", fmt.Sprintf("%+v", userData)))
return userData, nil
}