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
|
;; disfluid, implementation of the Solid specification
;; Copyright (C) 2020, 2021 Vivien Kraus
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(define-module (webid-oidc server resource path)
#:use-module (webid-oidc errors)
#:use-module ((webid-oidc stubs) #:prefix stubs:)
#:use-module (webid-oidc rdf-index)
#:use-module (webid-oidc web-i18n)
#:use-module (webid-oidc server resource content)
#:use-module ((webid-oidc refresh-token) #:prefix refresh:)
#:use-module ((webid-oidc parameters) #:prefix p:)
#:use-module (web uri)
#:use-module (rnrs bytevectors)
#:use-module (ice-9 exceptions)
#:use-module (ice-9 receive)
#:use-module (ice-9 optargs)
#:use-module (ice-9 iconv)
#:use-module (ice-9 textual-ports)
#:use-module (ice-9 binary-ports)
#:use-module (ice-9 threads)
#:use-module (ice-9 match)
#:use-module (rnrs bytevectors)
#:use-module (srfi srfi-26)
#:use-module (oop goops)
#:declarative? #t
#:export
(
&path-not-found
make-path-not-found
path-not-found?
path-not-found-path
&uri-slash-semantics-error
make-uri-slash-semantics-error
uri-slash-semantics-error?
uri-slash-semantics-error-requested
uri-slash-semantics-error-existing
&container-not-empty
make-container-not-empty
container-not-empty?
container-not-empty-path
&cannot-delete-root
make-cannot-delete-root
cannot-delete-root?
&path-is-auxiliary
make-path-is-auxiliary
path-is-auxiliary?
path-is-auxiliary-path
read-path
update-path
base-path
derive-path
auxiliary-path?
acl-path?
container-path?
root-path?
))
(define-exception-type
&path-not-found
&external-error
make-path-not-found
path-not-found?
(path path-not-found-path))
(define-exception-type
&uri-slash-semantics-error
&external-error
make-uri-slash-semantics-error
uri-slash-semantics-error?
(requested uri-slash-semantics-error-requested)
(existing uri-slash-semantics-error-existing))
(define-exception-type
&container-not-empty
&external-error
make-container-not-empty
container-not-empty?
(path container-not-empty-path))
(define-exception-type
&cannot-delete-root
&external-error
make-cannot-delete-root
cannot-delete-root?)
(define-exception-type
&path-is-auxiliary
&external-error
make-path-is-auxiliary
path-is-auxiliary?
(path path-is-auxiliary-path))
(define (hash-path/lock path)
(let ((h (stubs:hash 'SHA-256 path))
(dir (p:data-home)))
(let ((first-char (substring h 0 1))
(rest (substring h 1)))
(values
(format #f "~a/server/path/~a/~a" dir first-char rest)
(format #f "~a/server/path/~a/.lock" dir first-char)))))
(define (hash-path path)
(receive (h lock) (hash-path/lock path)
h))
(define (lock-file-name path)
(receive (h lock) (hash-path/lock path)
lock))
(define (read-path path)
(let ((h (hash-path path)))
(with-exception-handler
(lambda (error)
(let ((with-slash (string-append path "/"))
(without-slash
(if (string-suffix? "/" path)
(substring path 0 (- (string-length path) (string-length "/")))
path)))
(let ((with-slash-exists (file-exists? (hash-path with-slash)))
(without-slash-exists (file-exists? (hash-path without-slash))))
(cond
(with-slash-exists
(let ((final-message
(format #f (G_ "incorrect slash semantics: path ~s should have a slash")
path)))
(raise-exception
(make-exception
(make-path-not-found path)
(make-uri-slash-semantics-error path with-slash)
(make-exception-with-message final-message)))))
(without-slash-exists
(let ((final-message
(format #f (G_ "incorrect slash semantics: path ~s should not have a slash")
path)))
(raise-exception
(make-exception
(make-path-not-found path)
(make-uri-slash-semantics-error path without-slash)
(make-exception-with-message final-message)))))
(else
(let ((final-message
(format #f (G_ "path ~s does not exist") path)))
(raise-exception
(make-exception
(make-path-not-found path)
(make-exception-with-message final-message)))))))))
(lambda ()
(call-with-input-file h
(lambda (port)
(let* ((main-etag (read port))
(auxiliary (read port)))
(values (make <content> #:etag main-etag)
(map
(match-lambda
(((= string->uri key) . etag)
`(,key . ,(make <content> #:etag etag))))
auxiliary)))))))))
(define* (update-path path f
#:key (create-intermediate-containers? #f))
(let ((h (hash-path path))
(lock (lock-file-name path))
(garbage (make-hash-table))
(has-been-created? #f)
(has-been-deleted? #f)
(parent-path
(let ((components (split-and-decode-uri-path path)))
(cond
((null? components)
#f)
((null? (cdr components))
"/")
(else
(string-append
"/"
(encode-and-join-uri-path
(reverse
(cdr
(reverse components))))
"/"))))))
(stubs:atomically-update-file
h
lock
(lambda (port)
(receive (main auxiliary)
(with-exception-handler
(lambda (error)
(unless (path-not-found? error)
(raise-exception error))
(set! has-been-created? #t)
(values #f #f))
(lambda ()
(read-path path))
#:unwind? #t
#:unwind-for-type &path-not-found)
(when main
(hash-set! garbage (etag main) #t))
(for-each
(match-lambda
((_ . content)
(hash-set! garbage (etag content) #t)))
(or auxiliary '()))
(call-with-values
(lambda ()
(f main auxiliary))
(match-lambda*
((#f)
(unless (or (not main)
(not (contained main))
(null? (contained main)))
(raise-exception
(make-exception
(make-container-not-empty path)
(make-exception-with-message
(format #f (G_ "the path ~s exists, it has contained paths, and it is not empty")
path)))))
(when (equal? path "/")
(raise-exception
(make-exception
(make-cannot-delete-root)
(make-exception-with-message
(format #f (G_ "you cannot delete the root"))))))
(set! has-been-deleted? #t)
#f)
(((? (cute is-a? <> <content>) new-main)
new-auxiliary)
(hash-remove! garbage (etag new-main))
(for-each
(match-lambda
((_ . content)
(hash-remove! garbage (etag content))))
(or new-auxiliary '()))
(write (etag new-main) port)
(write (map (match-lambda
(((= uri->string key) . (= etag etag))
`(,key . ,etag)))
(or new-auxiliary '()))
port)
#t)
(else
(fail (G_ "you must return either #f to delete the path, or a new main content and alist from URI types to auxiliary content"))))))))
(when (and parent-path has-been-created? (not has-been-deleted?))
(update-path
parent-path
(lambda (main auxiliary)
;; Add path as a child of the resource at etag
(unless create-intermediate-containers?
(unless main
;; Typically, POST to a non-existing path
(raise-exception (make-path-not-found parent-path))))
(unless auxiliary
(set! auxiliary '()))
(let ((content-type (if main (content-type main) 'text/turtle))
(other-children (if main (contained main) '()))
(static-content (if main (static-content main) (string->utf8 ""))))
(let ((new-content
(make <content>
#:content-type content-type
#:contained (cons path other-children)
#:static-content static-content)))
(values new-content auxiliary))))
#:create-intermediate-containers? create-intermediate-containers?))
(when (and parent-path has-been-deleted? (not has-been-created?))
(update-path
parent-path
(lambda (main auxiliary)
(unless main
(raise-exception (make-path-not-found parent-path)))
(let ((content-type (content-type main))
(all-children (contained main))
(static-content (static-content main)))
(values
(make <content>
#:content-type content-type
#:contained
(filter (lambda (x) (not (equal? x path))) all-children)
#:static-content static-content)
auxiliary)))
#:create-intermediate-containers? create-intermediate-containers?))
(for-each
delete-content
(hash-map->list (lambda (garbage _) garbage) garbage))))
(define (base-path path)
(define (check-suffix suffix type)
(let ((total-length (string-length path))
(suffix-length (string-length suffix)))
(if (string-suffix? suffix path)
(values
(substring path 0 (- total-length suffix-length))
type)
(values #f #f))))
(define (all-checks candidates)
(if (null? candidates)
(values path #f)
(receive (base type)
(check-suffix (caar candidates) (cdar candidates))
(if base
(values base type)
(all-checks (cdr candidates))))))
(all-checks
`((".acl"
. ,(string->uri "http://www.w3.org/ns/auth/acl#accessControl"))
(".meta"
. ,(string->uri "https://www.w3.org/ns/iana/link-relations/relation#describedby")))))
(define (derive-path path type)
(receive (base base-type)
(base-path path)
(cond
((equal? type (string->uri "http://www.w3.org/ns/auth/acl#accessControl"))
(string-append base ".acl"))
((equal? type (string->uri "https://www.w3.org/ns/iana/link-relations/relation#describedby"))
(string-append base ".meta")))))
(define (auxiliary-path? path)
(receive (base type)
(base-path path)
(values type base)))
(define (acl-path? path)
(receive (base type)
(base-path path)
(and type
(equal? type (string->uri "http://www.w3.org/ns/auth/acl#accessControl")))))
(define (container-path? path)
(string-suffix? "/" path))
(define (root-path? path)
(equal? path "/"))
|