-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers.go
340 lines (293 loc) · 9.75 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
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
package processing
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
json "github.com/go-ap/jsonld"
)
// Typer is the static package variable that determines a CollectionPath type for a particular request
// It can be overloaded from outside packages.
// @TODO(marius): This should be moved as a property on an instantiable package object, instead of keeping it here
var Typer CollectionTyper = pathTyper{}
// CollectionTyper allows external packages to tell us which CollectionPath the current HTTP request addresses
type CollectionTyper interface {
Type(r *http.Request) vocab.CollectionPath
}
type pathTyper struct{}
func (d pathTyper) Type(r *http.Request) vocab.CollectionPath {
if r.URL == nil || len(r.URL.Path) == 0 {
return vocab.Unknown
}
col := vocab.Unknown
pathElements := strings.Split(r.URL.Path[1:], "/") // Skip first /
for i := len(pathElements) - 1; i >= 0; i-- {
col = vocab.CollectionPath(pathElements[i])
if vocab.ValidObjectCollection(col) || vocab.ValidActivityCollection(col) {
return col
}
}
return col
}
// MethodValidator is the interface need to be implemented to specify if an HTTP request's method
// is supported by the implementor object
type MethodValidator interface {
ValidMethod(r *http.Request) bool
}
// RequestValidator is the interface need to be implemented to specify if the whole HTTP request
// is valid in the context of the implementor object
type RequestValidator interface {
ValidateRequest(r *http.Request) (int, error)
}
// ActivityHandlerFn is the type that we're using to represent handlers that process requests containing
// an ActivityStreams Activity. It needs to implement the http.Handler interface.
//
// It is considered that following the execution of the handler, we return a pair formed of an HTTP status together with
// an IRI representing a new Object - in the case of transitive activities that had a side effect, or an error.
// In the case of intransitive activities the iri will always be empty.
type ActivityHandlerFn func(vocab.IRI, *http.Request) (vocab.Item, int, error)
// ValidMethod validates if the current handler can process the current request
func (a ActivityHandlerFn) ValidMethod(r *http.Request) bool {
return r.Method == http.MethodPost
}
// ValidateRequest validates if the current handler can process the current request
func (a ActivityHandlerFn) ValidateRequest(r *http.Request) (int, error) {
if !a.ValidMethod(r) {
return http.StatusNotAcceptable, errors.MethodNotAllowedf("Invalid HTTP method %s", r.Method)
}
return http.StatusOK, nil
}
func reqIRI(r *http.Request) vocab.IRI {
proto := "https"
if r.TLS == nil {
proto = "http"
}
return vocab.IRI(fmt.Sprintf("%s://%s%s", proto, r.Host, r.RequestURI))
}
// ServeHTTP implements the http.Handler interface for the ActivityHandlerFn type
func (a ActivityHandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var dat []byte
var it vocab.Item
var err error
var status = http.StatusInternalServerError
if status, err = a.ValidateRequest(r); err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
if it, status, err = a(reqIRI(r), r); err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
typ := it.GetType()
if vocab.ActivityTypes.Contains(typ) {
err = vocab.OnActivity(it, func(act *vocab.Activity) error {
if vocab.IsIRI(act.Object) {
return nil
}
// For activities that contain an object which is not just an IRI we want to return it in the response
if dat, err = vocab.MarshalJSON(act.Object); err != nil {
return err
}
return nil
})
} else if vocab.IntransitiveActivityTypes.Contains(typ) {
status = http.StatusNoContent
} else {
err = errors.BadRequestf("Invalid activity type %s received", typ)
}
if err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
switch status {
case http.StatusCreated:
if len(it.GetLink()) > 0 {
w.Header().Set("Location", it.GetLink().String())
}
case http.StatusGone:
if len(it.GetLink()) > 0 {
w.Header().Set("Location", it.GetLink().String())
}
case http.StatusNoContent:
if len(it.GetLink()) > 0 {
w.Header().Set("Location", it.GetLink().String())
}
default:
w.Header().Set("Content-Type", json.ContentType)
dat, _ = vocab.MarshalJSON(it)
}
w.WriteHeader(status)
w.Write(dat)
}
// CollectionHandlerFn is the type that we're using to represent handlers that will return ActivityStreams
// Collection or OrderedCollection objects. It needs to implement the http.Handler interface.
type CollectionHandlerFn func(vocab.CollectionPath, *http.Request) (vocab.CollectionInterface, error)
// ValidMethod validates if the current handler can process the current request
func (c CollectionHandlerFn) ValidMethod(r *http.Request) bool {
return r.Method == http.MethodGet || r.Method == http.MethodHead
}
// ValidateRequest validates if the current handler can process the current request
func (c CollectionHandlerFn) ValidateRequest(r *http.Request) (int, error) {
if !c.ValidMethod(r) {
return http.StatusMethodNotAllowed, errors.MethodNotAllowedf("Invalid HTTP method %s", r.Method)
}
return http.StatusOK, nil
}
// ServeHTTP implements the http.Handler interface for the CollectionHandlerFn type
func (c CollectionHandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var dat []byte
var status = http.StatusInternalServerError
var err error
status, err = c.ValidateRequest(r)
if err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
fromCache := false
col, err := c(Typer.Type(r), r)
if err != nil {
if errors.IsNotModified(err) {
fromCache = true
} else {
errors.HandleError(err).ServeHTTP(w, r)
return
}
}
_ = vocab.OnObject(col, func(o *vocab.Object) error {
updatedAt := o.Published
if !o.Updated.IsZero() {
updatedAt = o.Updated
}
if !updatedAt.IsZero() {
w.Header().Set("Last-Modified", updatedAt.Format(time.RFC1123))
}
return nil
})
w.Header().Set("Content-Type", json.ContentType)
if w.Header().Get("Cache-Control") == "" {
cacheType := "public"
if r.Header.Get("Authorization") != "" {
cacheType = "private"
}
w.Header().Set("Cache-Control", fmt.Sprintf("%s, max-age=%d", cacheType, int(24*time.Hour.Seconds())))
}
if dat, err = json.WithContext(json.IRI(vocab.ActivityBaseURI), json.IRI(vocab.SecurityContextURI)).Marshal(col); err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
if r.Method == http.MethodHead {
w.Header().Set("Content-Length", strconv.Itoa(len(dat)))
dat = nil
}
status = http.StatusOK
if fromCache && ObjectNotUpdatedSince(r.Header, col) {
status = http.StatusNotModified
}
w.WriteHeader(status)
_, _ = w.Write(dat)
}
// ItemHandlerFn is the type that we're using to represent handlers that return ActivityStreams
// objects. It needs to implement the http.Handler interface
type ItemHandlerFn func(*http.Request) (vocab.Item, error)
// ValidMethod validates if the current handler can process the current request
func (i ItemHandlerFn) ValidMethod(r *http.Request) bool {
return r.Method == http.MethodGet || r.Method == http.MethodHead
}
func ObjectNotUpdatedSince(h http.Header, it vocab.Item) bool {
ifModifiedSince := h.Get("If-Modified-Since")
if len(ifModifiedSince) == 0 {
return false
}
hdrUpdated, err := time.Parse(http.TimeFormat, ifModifiedSince)
if err != nil {
return false
}
var colUpdated time.Time
_ = vocab.OnObject(it, func(ob *vocab.Object) error {
colUpdated = ob.Published
if !ob.Updated.IsZero() {
colUpdated = ob.Updated
}
return nil
})
return colUpdated.Equal(hdrUpdated) || colUpdated.Before(hdrUpdated)
}
// ValidateRequest validates if the current handler can process the current request
func (i ItemHandlerFn) ValidateRequest(r *http.Request) (int, error) {
if !i.ValidMethod(r) {
return http.StatusMethodNotAllowed, errors.MethodNotAllowedf("Invalid HTTP method %s", r.Method)
}
return http.StatusOK, nil
}
const (
day = 24 * time.Hour
year = 8766 * time.Hour
)
var NotModified = errors.NotModified("from cache")
// ServeHTTP implements the http.Handler interface for the ItemHandlerFn type
func (i ItemHandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var dat []byte
var err error
status := http.StatusInternalServerError
status, err = i.ValidateRequest(r)
if err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
fromCache := false
it, err := i(r)
if err != nil {
if errors.IsNotModified(err) {
fromCache = true
} else {
errors.HandleError(err).ServeHTTP(w, r)
return
}
}
if vocab.IsNil(it) {
errors.HandleError(errors.NotFoundf("")).ServeHTTP(w, r)
return
}
_ = vocab.OnObject(it, func(o *vocab.Object) error {
updatedAt := o.Published
if !o.Updated.IsZero() {
updatedAt = o.Updated
}
if !updatedAt.IsZero() {
w.Header().Set("Last-Modified", updatedAt.Format(time.RFC1123))
}
if w.Header().Get("Cache-Control") == "" {
cacheType := "public"
if r.Header.Get("Authorization") != "" {
cacheType = "private"
}
if vocab.ActivityTypes.Contains(o.Type) {
w.Header().Set("Cache-Control", fmt.Sprintf("%s, max-age=%d, immutable", cacheType, int(year.Seconds())))
} else {
w.Header().Set("Cache-Control", fmt.Sprintf("%s, max-age=%d", cacheType, int(day.Seconds())))
}
}
return nil
})
w.Header().Set("Content-Type", json.ContentType)
if dat, err = json.WithContext(json.IRI(vocab.ActivityBaseURI), json.IRI(vocab.SecurityContextURI)).Marshal(it); err != nil {
errors.HandleError(err).ServeHTTP(w, r)
return
}
status = http.StatusOK
switch r.Method {
case http.MethodGet:
if it.GetType() == vocab.TombstoneType {
status = http.StatusGone
}
case http.MethodHead:
if fromCache && ObjectNotUpdatedSince(r.Header, it) {
status = http.StatusNotModified
}
}
w.WriteHeader(status)
_, _ = w.Write(dat)
}