AtlatestRepositorysigil-cloudflare
1;;; Cloudflare R2's S3 API. Calls return a buffered result and do not retry.
2;;; Body I/O yields under the scheduler; DNS/connect/TLS may still block it.
3(define-library (cloudflare r2)
4 (import (sigil io)
5 (sigil string)
6 (sigil list)
7 (sigil time)
8 (sigil http client)
9 (cloudflare r2 signing))
11 (export r2-client
12 r2-put-object
13 r2-get-object
14 r2-head-object
15 r2-delete-object
16 r2-public-url)
18 (begin
19 ;; A closure deliberately keeps credentials out of printed client values.
20 (define (r2-client account-id access-key-id secret-access-key bucket
21 (keys: (public-base-url #f)
22 (timeout 30)
23 (test-port #f)))
24 (unless (and (string? account-id)
25 (= (string-length account-id) 32)
26 (every (lambda (c)
27 (or (and (char<=? #\0 c) (char<=? c #\9))
28 (and (char<=? #\a c) (char<=? c #\f))))
29 (string->list account-id)))
30 (error "R2 account ID must be 32 lowercase hex characters"))
31 (unless (and (string? bucket)
32 (<= 3 (string-length bucket) 63)
33 (every (lambda (c)
34 (or (and (char<=? #\a c) (char<=? c #\z))
35 (and (char<=? #\0 c) (char<=? c #\9))
36 (char=? c #\-)))
37 (string->list bucket))
38 (not (char=? (string-ref bucket 0) #\-))
39 (not (char=? (string-ref bucket (- (string-length bucket) 1))
40 #\-)))
41 (error "Invalid R2 bucket name"))
42 (unless (and (string? access-key-id)
43 (> (string-length access-key-id) 0)
44 (every char-alphabetic-or-numeric? (string->list access-key-id))
45 (string? secret-access-key)
46 (> (string-length secret-access-key) 0)
47 (number? timeout)
48 (> timeout 0))
49 (error "Invalid R2 credentials or timeout"))
50 (when test-port
51 (unless (and (integer? test-port) (<= 1 test-port 65535))
52 (error "Invalid R2 loopback test port")))
53 (when public-base-url
54 (validate-public-origin public-base-url))
55 (let* ((host (if test-port
56 (string-append "127.0.0.1:" (number->string test-port))
57 (string-append account-id ".r2.cloudflarestorage.com")))
58 (origin (string-append (if test-port "http://" "https://") host)))
59 (lambda (action key body headers)
60 (validate-key key)
61 (if (eq? action 'public-url)
62 (and public-base-url
63 (string-append public-base-url "/"
64 (r2-uri-encode key preserve-slashes?: #t)))
65 (let* ((now (current-second))
66 (stamp (string-append
67 (string-join (string-split (format-date now #t) "-") "")
68 "T"
69 (string-join (string-split (format-time now #t) ":") "")
70 "Z"))
71 (signed (r2-sign-request
72 action (string-append "/" bucket "/" key) '()
73 (cons (cons "host" host) headers)
74 (or body (make-bytevector 0))
75 access-key-id secret-access-key stamp))
76 ;; The HTTP client supplies Host itself. Sending it twice can
77 ;; invalidate the signature; sign once, send once.
78 (wire-headers (filter (lambda (h)
79 (not (string=? (car h) "host")))
80 (dict-ref signed headers:)))
81 (result (http-fetch-bytes
82 action (string-append origin (dict-ref signed path:))
83 headers: wire-headers body: body timeout: timeout)))
84 (when result
85 (let* ((length-header (assoc "content-length"
86 (dict-ref result headers:)))
87 (expected (and length-header
88 (string->number (cdr length-header)))))
89 (when (and (not (eq? action 'HEAD))
90 expected
91 (not (= expected
92 (bytevector-length (dict-ref result body:)))))
93 (set! result #f))))
94 result)))))
96 (define (char-alphabetic-or-numeric? c)
97 (or (and (char<=? #\a c) (char<=? c #\z))
98 (and (char<=? #\A c) (char<=? c #\Z))
99 (and (char<=? #\0 c) (char<=? c #\9))))
101 (define (validate-public-origin origin)
102 ;; A public URL is a separate R2 custom-domain origin, never the signed API.
103 (let ((url (and (string? origin) (parse-url origin))))
104 (unless (and url
105 (string=? (url-scheme url) "https")
106 (not (url-query url))
107 (string=? (url-path url) "/")
108 (not (string-contains? origin "@"))
109 (not (string-contains? origin "#"))
110 (not (string-contains? origin "\r"))
111 (not (string-contains? origin "\n"))
112 (not (char=? (string-ref origin (- (string-length origin) 1))
113 #\/)))
114 (error "R2 public base URL must be an HTTPS origin without a trailing slash"))))
116 (define (validate-key key)
117 (unless (and (string? key)
118 (<= 1 (bytevector-length (string->utf8 key)) 1024))
119 (error "R2 object key must contain 1 to 1024 UTF-8 bytes")))
121 (define (r2-put-object client key bytes
122 (keys: (content-type "application/octet-stream")
123 (cache-control #f)))
124 (unless (bytevector? bytes)
125 (error "R2 upload body must be a bytevector"))
126 (client 'PUT key bytes
127 (append (list (cons "content-type" content-type))
128 (if cache-control
129 (list (cons "cache-control" cache-control))
130 '()))))
132 (define (r2-get-object client key)
133 (client 'GET key #f '()))
135 (define (r2-head-object client key)
136 (client 'HEAD key #f '()))
138 (define (r2-delete-object client key)
139 (client 'DELETE key #f '()))
141 (define (r2-public-url client key)
142 (client 'public-url key #f '()))))