summaryrefslogtreecommitdiff
path: root/src/scm/webid-oidc/server/resource/path.scm
blob: a637d6067d41f5fa2d904bd9b7d7c2cbda242d97 (plain)
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
(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 refresh-token) #:prefix refresh:)
  #: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 (rnrs bytevectors)
  #:use-module (oop goops)
  #:export
  (

   read-path
   update-path

   ))

(define (default-dir)
  (string-append (refresh:default-dir) "/server"))

(define (hash-path path)
  (let ((h (stubs:hash 'SHA-256 path))
        (dir (default-dir)))
    (let ((first-char (substring h 0 1))
          (rest (substring h 1)))
      (format #f "~a/path/~a/~a" dir first-char rest))))

(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
                (raise-exception
                 (make-exception
                  (make-path-not-found path)
                  (make-uri-slash-semantics-error path with-slash))))
               (without-slash-exists
                (raise-exception
                 (make-exception
                  (make-path-not-found path)
                  (make-uri-slash-semantics-error path with-slash))))
               (else
                (raise-exception (make-path-not-found path)))))))
      (lambda ()
        (call-with-input-file h
          (lambda (port)
            (let ((main-etag (read port)))
              (let ((auxiliary (read port)))
                (values main-etag
                        (map (lambda (cell)
                               (let ((key (string->uri (car cell)))
                                     (value (string->uri (cdr cell))))
                                 (cons key value)))
                             auxiliary))))))))))

(define* (update-path path f content-type contained static-content create delete
                      #:key (create-intermediate-containers? #f))
  (let ((h (hash-path 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
     (lambda (port)
       (receive (etag 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 etag
           (hash-set! garbage etag #t))
         (when auxiliary
           (for-each
            (lambda (cell)
              (when (cdr cell)
                (hash-set! garbage (cdr cell) #t)))
            auxiliary))
         (call-with-values
             (lambda ()
               (f etag auxiliary))
           (case-lambda
             ((false)
              (when false
                (error "You’re using the API wrong."))
              ;; Delete the resource
              (unless (null? (contained etag))
                (raise-exception (make-container-not-empty path)))
              (when (equal? path "/")
                (raise-exception (make-cannot-delete-root)))
              (set! has-been-deleted? #t)
              #f)
             ((new-etag new-auxiliary)
              (unless (and (string? new-etag) (list? new-auxiliary))
                (error "You’re using the API wrong."))
              (hash-remove! garbage new-etag)
              (when new-auxiliary
                (for-each
                 (lambda (cell)
                   (hash-remove! garbage (cdr cell)))
                 new-auxiliary))
              (write new-etag port)
              (write (map (lambda (cell)
                            (cons (uri->string (car cell))
                                  (cdr cell)))
                          new-auxiliary)
                     port)
              #t))))))
    (when (and parent-path has-been-created? (not has-been-deleted?))
      (update-path
       parent-path
       (lambda (etag auxiliary)
         ;; Add path as a child of the resource at etag
         (unless create-intermediate-containers?
           (unless etag
             ;; Typically, POST to a non-existing path
             (raise-exception (make-path-not-found parent-path))))
         (unless auxiliary
           (set! auxiliary '()))
         (let ((content-type (if etag (content-type etag) 'text/turtle))
               (other-children (if etag (contained etag) '()))
               (static-content (if etag (static-content etag) (string->utf8 ""))))
           (unless (eq? content-type 'text/turtle)
             (raise-exception (make-not-a-container parent-path content-type)))
           (let ((new-etag
                  (create content-type (cons path other-children) static-content)))
             (values new-etag auxiliary))))
       content-type contained static-content create delete
       #:create-intermediate-containers? create-intermediate-containers?))
    (when (and parent-path has-been-deleted? (not has-been-created?))
      (update-path
       parent-path
       (lambda (etag auxiliary)
         (unless etag
           (raise-exception (make-path-not-found parent-path)))
         (let ((content-type (content-type etag))
               (all-children (contained etag))
               (static-content (static-content etag)))
           (values
            (create content-type
                    (filter (lambda (x)
                              (not (equal? x path)))
                            all-children)
                    static-content)
            auxiliary)))
       content-type contained static-content create delete
       #:create-intermediate-containers? create-intermediate-containers?))
    (for-each
     delete
     (hash-map->list (lambda (garbage false) garbage) garbage))))