-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
360 lines (311 loc) · 8.26 KB
/
endpoint.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
359
360
package rmsgo
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
. "github.com/cvanloo/rmsgo/mock"
)
// @todo: https://datatracker.ietf.org/doc/html/draft-dejong-remotestorage-21#section-6
// keep multiple versions of files around, option to restore deleted files
// > A provider MAY offer version rollback functionality to its users,
// > but this specification does not define the interface for that.
type (
Middleware func(next http.Handler) http.Handler
HandlerWithError func(w http.ResponseWriter, r *http.Request) error
MuxWithError struct {
http.ServeMux
}
ErrorResponder interface {
RespondError(w http.ResponseWriter, r *http.Request) (wasHandled bool)
}
)
func MiddlewareStack(middlewares ...Middleware) Middleware {
return func(next http.Handler) http.Handler {
for i := len(middlewares) - 1; i >= 0; i-- {
m := middlewares[i]
next = m(next)
}
return next
}
}
func (h HandlerWithError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
if err, ok := err.(ErrorResponder); ok {
if err.RespondError(w, r) {
return
}
}
status := http.StatusInternalServerError
http.Error(w, http.StatusText(status), status)
g.unhandled(err)
}
}
func (m *MuxWithError) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request) error) {
m.ServeMux.Handle(pattern, HandlerWithError(handler))
}
func RMSRouter() http.Handler {
folderMux := &MuxWithError{}
folderMux.HandleFunc("GET /", getFolder)
folderMux.Handle("/", HandlerWithError(func(http.ResponseWriter, *http.Request) error {
return BadRequest("method not allowed on folders")
}))
documentMux := &MuxWithError{}
documentMux.HandleFunc("GET /", getDocument)
documentMux.HandleFunc("PUT /", putDocument)
documentMux.HandleFunc("DELETE /", deleteDocument)
documentMux.Handle("/", HandlerWithError(func(http.ResponseWriter, *http.Request) error {
return BadRequest("method not allowed on documents")
}))
mux := &MuxWithError{}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) error {
path := r.URL.Path
isFolder := path[len(path)-1] == '/' // @fixme: is this the path without query parameters and stuff?
if isFolder {
folderMux.ServeHTTP(w, r)
} else {
documentMux.ServeHTTP(w, r)
}
return nil
})
return mux
}
func getFolder(w http.ResponseWriter, r *http.Request) error {
n, err := Retrieve(r.URL.Path)
if err != nil {
return MaybeNotFound(err)
}
if !n.isFolder {
return NotAFolder(n.rname)
}
etag, err := n.Version()
if err != nil {
return err // internal server error
}
if condStr := r.Header.Get("If-None-Match"); condStr != "" { // @todo: extract into its own type/functionality?
conds := strings.Split(condStr, ",")
for _, cond := range conds {
cond = strings.TrimSpace(cond)
rev, err := ParseETag(cond)
if err != nil {
// @note(#orig_error): it's ok to lose the original error,
// since it can only be caused by a malformed ETag.
return InvalidIfNonMatch(cond) // @todo: pass original err?
}
if rev.Equal(etag) {
return NotModified()
}
}
}
items := LDjson{}
for _, child := range n.children {
desc := LDjson{}
etag, err := child.Version()
if err != nil {
return err // internal server error
}
desc["ETag"] = etag.String()
if !child.isFolder {
desc["Content-Type"] = child.mime
desc["Content-Length"] = child.length
desc["Last-Modified"] = child.lastMod.Format(timeFormat)
}
items[child.name] = desc
}
desc := LDjson{
"@context": "http://remotestorage.io/spec/folder-description",
"items": items,
}
hs := w.Header()
hs.Set("Content-Type", "application/ld+json")
hs.Set("Cache-Control", "no-cache")
hs.Set("ETag", etag.String())
return json.NewEncoder(w).Encode(desc)
}
func getDocument(w http.ResponseWriter, r *http.Request) error {
n, err := Retrieve(r.URL.Path)
if err != nil {
return MaybeNotFound(err)
}
if n.isFolder {
return NotADocument(n.rname)
}
etag, err := n.Version()
if err != nil {
return err // internal server error
}
if condStr := r.Header.Get("If-None-Match"); condStr != "" {
conds := strings.Split(condStr, ",")
for _, cond := range conds {
cond = strings.TrimSpace(cond)
rev, err := ParseETag(cond)
if err != nil {
// @note(#orig_error): it's ok to lose the original error,
// since it can only have been caused by a malformed ETag.
return InvalidIfNonMatch(cond) // @todo: parse all and report all errors instead of only the first?
}
if rev.Equal(etag) {
return NotModified()
}
}
}
fd, err := FS.Open(n.sname)
if err != nil {
return err // internal server error
}
hs := w.Header()
hs.Set("Cache-Control", "no-cache")
hs.Set("ETag", etag.String())
hs.Set("Content-Type", n.mime)
hs.Set("Content-Length", fmt.Sprintf("%d", n.length))
w.WriteHeader(http.StatusOK)
if r.Method != http.MethodHead {
_, err = io.Copy(w, fd)
}
return err
}
func putDocument(w http.ResponseWriter, r *http.Request) error {
rpath := r.URL.Path
n, err := Retrieve(rpath)
found := !errors.Is(err, ErrNotExist)
if found { // err is /not/ ErrNotExist
if err != nil {
return err // internal server error
}
if n.isFolder {
return Conflict(n.rname)
}
}
if cond := r.Header.Get("If-None-Match"); cond == "*" && found {
etag, err := n.Version()
if err != nil {
return err // internal server error
}
w.Header().Set("ETag", etag.String())
return DocExists(n.rname)
}
if cond := r.Header.Get("If-Match"); cond != "" {
rev, err := ParseETag(cond)
if err != nil {
// @note(#orig_error): it's ok to lose the original error, since
// it can only be caused by a malformed ETag.
return InvalidIfMatch(cond)
}
etag, err := n.Version()
if err != nil {
return err // internal server error
}
w.Header().Set("ETag", etag.String())
if !etag.Equal(rev) {
return VersionMismatch(rev, etag)
}
}
mime := r.Header.Get("Content-Type")
if found {
fd, err := FS.Create(n.sname)
if err != nil {
return err // internal server error
}
fsize, err := io.Copy(fd, r.Body)
if err != nil {
return err // internal server error
}
if mime == "" {
_, err := fd.Seek(0, io.SeekStart)
if err != nil {
return err // internal server error
}
bs := make([]byte, 512)
_, err = fd.Read(bs)
if err != nil {
return err // internal server error
}
mime = http.DetectContentType(bs)
}
UpdateDocument(n, mime, fsize)
err = fd.Close()
if err != nil {
return err // internal server error
}
} else {
u, err := UUID()
if err != nil {
return err // internal server error
}
sname := filepath.Join(g.sroot, u.String())
fd, err := FS.Create(sname)
if err != nil {
return err // internal server error
}
fsize, err := io.Copy(fd, r.Body)
if err != nil {
return err // internal server error
}
if mime == "" {
_, err := fd.Seek(0, io.SeekStart)
if err != nil {
return err // internal server error
}
bs := make([]byte, 512)
_, err = fd.Read(bs)
if err != nil {
return err // internal server error
}
mime = http.DetectContentType(bs)
}
n, err = AddDocument(rpath, sname, fsize, mime)
if err != nil {
return MaybeAncestorConflict(err, rpath)
}
err = fd.Close()
if err != nil {
return err // internal server error
}
}
etag, err := n.Version()
if err != nil {
return err // internal server error
}
hs := w.Header()
hs.Set("ETag", etag.String())
w.WriteHeader(http.StatusCreated)
return nil
}
func deleteDocument(w http.ResponseWriter, r *http.Request) error {
rpath := r.URL.Path
n, err := Retrieve(rpath)
if err != nil {
return MaybeNotFound(err)
}
if n.isFolder {
return NotADocument(n.rname)
}
etag, err := n.Version()
if err != nil {
return err // internal server error
}
if cond := r.Header.Get("If-Match"); cond != "" {
rev, err := ParseETag(cond)
if err != nil {
// @note(#orig_error): it's ok to lose the original error, since
// it can only be caused by a malformed ETag.
return InvalidIfMatch(cond)
}
if !etag.Equal(rev) {
w.Header().Set("ETag", etag.String())
return VersionMismatch(rev, etag)
}
}
RemoveDocument(n)
err = FS.Remove(n.sname)
if err != nil {
return err // internal server error
}
hs := w.Header()
hs.Set("ETag", etag.String())
w.WriteHeader(http.StatusOK)
return nil
}