substrate-lang 0.9.3 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/README.md +1 -1
  2. package/build/compiler/binary.2d43b43d1600@0+0.wasm +0 -0
  3. package/build/compiler/checker.4dd4556dd421@0+0.wasm +0 -0
  4. package/build/compiler/compile.5d8f6407af2a@0+0.wasm +0 -0
  5. package/build/compiler/{core.fc3dab9d1846@0+0.wasm → core.f2defb75e00b@0+0.wasm} +0 -0
  6. package/build/compiler/expander.967ce443d379@0+0.wasm +0 -0
  7. package/build/compiler/fs.01b8aab4a9a8@0+0.wasm +0 -0
  8. package/build/compiler/{leb128.748b1c37277a@0+0.wasm → leb128.15d814b7b4f2@0+0.wasm} +0 -0
  9. package/build/compiler/{link.11b643d35670@0+0.wasm → link.b826ea7c8f17@0+0.wasm} +0 -0
  10. package/build/compiler/{macros.4083601c00db@0+0.wasm → macros.b269e0c42784@0+0.wasm} +0 -0
  11. package/build/compiler/{main.ed62cce43000@0+0.wasm → main.4fc52f204d54@0+0.wasm} +0 -0
  12. package/build/compiler/{ops.b361e858d8e1@0+0.wasm → ops.515b5e05f646@0+0.wasm} +0 -0
  13. package/build/compiler/{path.dcc4712bcc7b@0+0.wasm → path.5a6286472cd4@0+0.wasm} +0 -0
  14. package/build/compiler/program +21 -21
  15. package/build/compiler/prune.406215c3e4f8@0+0.wasm +0 -0
  16. package/build/compiler/reader.694b03770bf0@0+0.wasm +0 -0
  17. package/build/compiler/roots.cefd10e93684@0+0.wasm +0 -0
  18. package/build/compiler/{running.d73f7826cb74@0+0.wasm → running.0d25dbaec018@0+0.wasm} +0 -0
  19. package/build/compiler/serialize.b0d82f3d63d2@0+0.wasm +0 -0
  20. package/build/compiler/{sha256.5fc9d60f8bb2@0+0.wasm → sha256.a6c32b04d799@0+0.wasm} +0 -0
  21. package/build/compiler/substrate.5ee5d0b4d3e7@0+0.wasm +0 -0
  22. package/build/compiler/syntax.32f3e40b61c7@0+0.wasm +0 -0
  23. package/build/compiler/{writer.806bc85aa684@0+0.wasm → writer.00b7802cbe39@0+0.wasm} +0 -0
  24. package/build/sub/{base64.8fba03e1be0a@0+0.wasm → base64.a26bdcf60a2d@0+0.wasm} +0 -0
  25. package/build/sub/build.459c5b9412bd@0+0.wasm +0 -0
  26. package/build/sub/chromium.3d5818f2707d@0+0.wasm +0 -0
  27. package/build/sub/common.1edc613607a0@0+0.wasm +0 -0
  28. package/build/sub/deflate.23f7c4679260@0+0.wasm +0 -0
  29. package/build/sub/doc.0775df5de6fe@0+0.wasm +0 -0
  30. package/build/sub/{doc.566f9861828e@0+0.wasm → doc.ef9965d5d402@0+0.wasm} +0 -0
  31. package/build/sub/env.e5451da6e747@0+0.wasm +0 -0
  32. package/build/sub/events.bf5473ebf220@0+0.wasm +0 -0
  33. package/build/sub/fetch.7790d4943758@0+0.wasm +0 -0
  34. package/build/sub/fetch.96b2d10f025e@0+0.wasm +0 -0
  35. package/build/sub/files.ad0469fdf6fa@0+0.wasm +0 -0
  36. package/build/sub/fs.01b8aab4a9a8@0+0.wasm +0 -0
  37. package/build/sub/git.dc4c0e757ebe@0+0.wasm +0 -0
  38. package/build/sub/grants.c54776c0d7e3@0+0.wasm +0 -0
  39. package/build/sub/gzip.16e372eda1cf@0+0.wasm +0 -0
  40. package/build/sub/hash.660682e4f491@0+0.wasm +0 -0
  41. package/build/sub/{html.20baa1dc7ca6@0+0.wasm → html.a23126eb949a@0+0.wasm} +0 -0
  42. package/build/sub/{html.c1671a102b4d@0+0.wasm → html.e9ed37f34977@0+0.wasm} +0 -0
  43. package/build/sub/{inflate.8899e0933792@0+0.wasm → inflate.59bb61f14001@0+0.wasm} +0 -0
  44. package/build/sub/json.55590d2f518a@0+0.wasm +0 -0
  45. package/build/sub/manifest.ef317480deb0@0+0.wasm +0 -0
  46. package/build/sub/map.f455cca6a42a@0+0.wasm +0 -0
  47. package/build/sub/pack.e02836264f6a@0+0.wasm +0 -0
  48. package/build/sub/{path.dcc4712bcc7b@0+0.wasm → path.5a6286472cd4@0+0.wasm} +0 -0
  49. package/build/sub/path.6891ffea73ca@0+0.wasm +0 -0
  50. package/build/sub/process.7106571fa723@0+0.wasm +0 -0
  51. package/build/sub/program +48 -48
  52. package/build/sub/read.0b93933d62a2@0+0.wasm +0 -0
  53. package/build/sub/{reader.b535e61f99f4@0+0.wasm → reader.694b03770bf0@0+0.wasm} +0 -0
  54. package/build/sub/{root.9b713ec588c1@0+0.wasm → root.f10a4906f460@0+0.wasm} +0 -0
  55. package/build/sub/{roots.6d2942578055@0+0.wasm → roots.cefd10e93684@0+0.wasm} +0 -0
  56. package/build/sub/{sha256.5fc9d60f8bb2@0+0.wasm → sha256.a6c32b04d799@0+0.wasm} +0 -0
  57. package/build/sub/{sha512.b988b9dc6114@0+0.wasm → sha512.c24a792d7891@0+0.wasm} +0 -0
  58. package/build/sub/site.1732b58ec0e8@0+0.wasm +0 -0
  59. package/build/sub/site.ae3913b0b0f4@0+0.wasm +0 -0
  60. package/build/sub/store.a24d7a25daa6@0+0.wasm +0 -0
  61. package/build/sub/stream.126e3bd5d626@0+0.wasm +0 -0
  62. package/build/sub/sub.2e3092140706@0+0.wasm +0 -0
  63. package/build/sub/substrate.5ee5d0b4d3e7@0+0.wasm +0 -0
  64. package/build/sub/{syntax.9857f6784d63@0+0.wasm → syntax.32f3e40b61c7@0+0.wasm} +0 -0
  65. package/build/sub/tar.2a633861e654@0+0.wasm +0 -0
  66. package/build/sub/test.9bd59c73f65e@0+0.wasm +0 -0
  67. package/build/sub/{text.4ab70ac35b5a@0+0.wasm → text.1bc90482bd43@0+0.wasm} +0 -0
  68. package/build/sub/{timer.3809bb17a7a1@0+0.wasm → timer.f35eb62ae446@0+0.wasm} +0 -0
  69. package/build/sub/upgrade.39ee449b00d5@0+0.wasm +0 -0
  70. package/build/sub/{utf8.524c475ecd40@0+0.wasm → utf8.e7216bd054e1@0+0.wasm} +0 -0
  71. package/build/sub/worker.762a9d642af0@0+0.wasm +0 -0
  72. package/build/sub/writer.00b7802cbe39@0+0.wasm +0 -0
  73. package/package.json +1 -1
  74. package/std/archive/tar.sub +44 -44
  75. package/std/browser/chromium.sub +5 -4
  76. package/std/browser/devtools.sub +24 -19
  77. package/std/compress/deflate.sub +10 -12
  78. package/std/compress/gzip.sub +18 -16
  79. package/std/compress/inflate.sub +7 -4
  80. package/std/crypto/password.sub +3 -3
  81. package/std/doc/doc.sub +2 -2
  82. package/std/encoding/json.sub +64 -73
  83. package/std/encoding/wire.sub +15 -15
  84. package/std/http/checks.sub +3 -3
  85. package/std/http/conditional.sub +2 -4
  86. package/std/http/cookie.sub +5 -7
  87. package/std/http/form.sub +9 -12
  88. package/std/http/http.sub +17 -24
  89. package/std/http/multipart.sub +13 -15
  90. package/std/http/route.sub +18 -13
  91. package/std/http/server.sub +21 -23
  92. package/std/http/session.sub +3 -6
  93. package/std/http/sse-client.sub +9 -14
  94. package/std/http/static.sub +5 -5
  95. package/std/http/url.sub +10 -10
  96. package/std/http/websocket.sub +2 -5
  97. package/std/meta/derive.sub +0 -2
  98. package/std/meta/method.sub +41 -0
  99. package/std/meta/type-info.sub +30 -37
  100. package/std/package/files.sub +24 -25
  101. package/std/package/git.sub +26 -33
  102. package/std/package/hash.sub +9 -9
  103. package/std/package/manifest.sub +81 -57
  104. package/std/package/map.sub +5 -9
  105. package/std/package/pack.sub +13 -13
  106. package/std/package/path.sub +1 -4
  107. package/std/package/read.sub +10 -14
  108. package/std/package/site.sub +11 -11
  109. package/std/package/store.sub +16 -12
  110. package/std/package.sub +6 -6
  111. package/std/test/browser.sub +9 -8
  112. package/std/test/build.sub +2 -2
  113. package/std/test/compare.sub +9 -10
  114. package/std/test/page.sub +32 -27
  115. package/std/test/run.sub +9 -8
  116. package/std/test/test.sub +36 -30
  117. package/std/vector/vector.sub +75 -0
  118. package/substrate/VERSION +1 -1
  119. package/substrate/compiler/binary.sub +5 -14
  120. package/substrate/compiler/checker.sub +235 -222
  121. package/substrate/compiler/compile.sub +11 -11
  122. package/substrate/compiler/expander.sub +52 -78
  123. package/substrate/compiler/link.sub +2 -2
  124. package/substrate/compiler/main.sub +9 -9
  125. package/substrate/compiler/ops.sub +1 -1
  126. package/substrate/compiler/prune.sub +12 -17
  127. package/substrate/compiler/reader.sub +4 -4
  128. package/substrate/compiler/roots.sub +11 -10
  129. package/substrate/compiler/running.sub +1 -1
  130. package/substrate/compiler/serialize.sub +57 -50
  131. package/substrate/compiler/substrate.sub +60 -23
  132. package/substrate/compiler/syntax.sub +8 -21
  133. package/substrate/host/errors.mjs +3 -2
  134. package/substrate/host/events.sub +3 -16
  135. package/substrate/host/fs.mjs +5 -4
  136. package/substrate/host/fs.sub +142 -112
  137. package/substrate/host/grants.mjs +1 -1
  138. package/substrate/host/grants.sub +33 -33
  139. package/substrate/host/modules/fake.mjs +1 -1
  140. package/substrate/host/modules/websocket.mjs +4 -4
  141. package/substrate/host/modules/websocket.sub +8 -6
  142. package/substrate/host/node/env.mjs +12 -6
  143. package/substrate/host/node/env.sub +21 -16
  144. package/substrate/host/node/fs.mjs +1 -1
  145. package/substrate/host/node/net.mjs +2 -2
  146. package/substrate/host/node/net.sub +13 -6
  147. package/substrate/host/node/process.mjs +4 -4
  148. package/substrate/host/node/process.sub +28 -28
  149. package/substrate/host/node/signal.mjs +3 -2
  150. package/substrate/host/node/signal.sub +9 -6
  151. package/substrate/host/node/watch.mjs +1 -1
  152. package/substrate/host/node/watch.sub +11 -9
  153. package/substrate/host/stream.sub +2 -0
  154. package/substrate/host/subhost-core.mjs +9 -1
  155. package/substrate/host/web/clipboard.mjs +2 -2
  156. package/substrate/host/web/clipboard.sub +10 -3
  157. package/substrate/host/web/files.mjs +2 -2
  158. package/substrate/host/web/files.sub +6 -2
  159. package/substrate/host/web/fs.mjs +3 -3
  160. package/substrate/host/web/location.mjs +3 -1
  161. package/substrate/host/web/location.sub +8 -4
  162. package/substrate/host/worker-side.mjs +3 -1
  163. package/substrate/host/worker.mjs +6 -5
  164. package/substrate/host/worker.sub +10 -10
  165. package/build/compiler/binary.3823f89ce414@0+0.wasm +0 -0
  166. package/build/compiler/checker.592d37dcbec9@0+0.wasm +0 -0
  167. package/build/compiler/compile.86a2b03b0c9b@0+0.wasm +0 -0
  168. package/build/compiler/expander.689d326e5a1b@0+0.wasm +0 -0
  169. package/build/compiler/fs.563034be70c3@0+0.wasm +0 -0
  170. package/build/compiler/prune.d4a3f9834095@0+0.wasm +0 -0
  171. package/build/compiler/reader.b535e61f99f4@0+0.wasm +0 -0
  172. package/build/compiler/roots.6d2942578055@0+0.wasm +0 -0
  173. package/build/compiler/serialize.b2196de75bc8@0+0.wasm +0 -0
  174. package/build/compiler/substrate.4c780d6dd6d4@0+0.wasm +0 -0
  175. package/build/compiler/syntax.9857f6784d63@0+0.wasm +0 -0
  176. package/build/sub/build.84ac996eda63@0+0.wasm +0 -0
  177. package/build/sub/chromium.e4db7d29897c@0+0.wasm +0 -0
  178. package/build/sub/common.91ddee6b7a67@0+0.wasm +0 -0
  179. package/build/sub/deflate.fc8075e3aed1@0+0.wasm +0 -0
  180. package/build/sub/doc.0b48bbac404b@0+0.wasm +0 -0
  181. package/build/sub/env.f051f8166526@0+0.wasm +0 -0
  182. package/build/sub/events.f952172735e0@0+0.wasm +0 -0
  183. package/build/sub/fetch.097f1cc9502d@0+0.wasm +0 -0
  184. package/build/sub/fetch.5d2f427c995e@0+0.wasm +0 -0
  185. package/build/sub/files.30e55182c5df@0+0.wasm +0 -0
  186. package/build/sub/fs.563034be70c3@0+0.wasm +0 -0
  187. package/build/sub/git.48aefa1644be@0+0.wasm +0 -0
  188. package/build/sub/grants.5bf25700d47c@0+0.wasm +0 -0
  189. package/build/sub/gzip.69588e97c35a@0+0.wasm +0 -0
  190. package/build/sub/hash.3f0d118da63f@0+0.wasm +0 -0
  191. package/build/sub/json.f15a29d99d8d@0+0.wasm +0 -0
  192. package/build/sub/manifest.cf7471880377@0+0.wasm +0 -0
  193. package/build/sub/map.dc516eec27ff@0+0.wasm +0 -0
  194. package/build/sub/pack.55992e8cc09c@0+0.wasm +0 -0
  195. package/build/sub/path.b7a8e1cf3aaa@0+0.wasm +0 -0
  196. package/build/sub/process.aea2c88cb7a0@0+0.wasm +0 -0
  197. package/build/sub/read.cd1e693f4fb4@0+0.wasm +0 -0
  198. package/build/sub/site.7c6d8c7747af@0+0.wasm +0 -0
  199. package/build/sub/site.81548474c323@0+0.wasm +0 -0
  200. package/build/sub/store.d4e845ff24ac@0+0.wasm +0 -0
  201. package/build/sub/stream.4c07247477f7@0+0.wasm +0 -0
  202. package/build/sub/sub.b57e17b6876b@0+0.wasm +0 -0
  203. package/build/sub/substrate.4c780d6dd6d4@0+0.wasm +0 -0
  204. package/build/sub/tar.e482cf873cfc@0+0.wasm +0 -0
  205. package/build/sub/test.0d9c079bb309@0+0.wasm +0 -0
  206. package/build/sub/upgrade.18a5aa0dae89@0+0.wasm +0 -0
  207. package/build/sub/worker.f1190de21b21@0+0.wasm +0 -0
  208. package/build/sub/writer.806bc85aa684@0+0.wasm +0 -0
@@ -3,9 +3,9 @@
3
3
 
4
4
  (provide wire wire-error)
5
5
 
6
- (struct wire-error [message string] [offset u32])
6
+ (struct wire-error [error error] [offset u32])
7
7
 
8
- (struct wire-reader [bytes (slice u8)] [at u32] [error wire-error])
8
+ (struct wire-reader [bytes (slice u8)] [at u32] [message string] [offset u32])
9
9
 
10
10
  ;; ---- writing ----
11
11
 
@@ -28,12 +28,12 @@
28
28
  (define (start! [r (ptr wire-reader)] [bytes (slice u8)])
29
29
  (set! (-> r bytes) bytes)
30
30
  (set! (-> r at) 0)
31
- (set! (-> r error message) "")
32
- (set! (-> r error offset) 0))
31
+ (set! (-> r message) "")
32
+ (set! (-> r offset) 0))
33
33
 
34
34
  (define (fail! [r (ptr wire-reader)] [message string]) -> bool
35
- (set! (-> r error message) message)
36
- (set! (-> r error offset) (-> r at))
35
+ (set! (-> r message) message)
36
+ (set! (-> r offset) (-> r at))
37
37
  #f)
38
38
 
39
39
  (define (refuse! [r (ptr wire-reader)] [at u32] [message string]) -> bool
@@ -45,7 +45,9 @@
45
45
  (define (end! [r (ptr wire-reader)]) -> bool (or (== (left r) 0) (fail! r "bytes follow the message")))
46
46
 
47
47
  (define (read-failed [r (ptr wire-reader)] [err (ptr wire-error)]) -> bool
48
- (when (!= err 0) (set! (@ err) (-> r error)))
48
+ (unless (== err 0)
49
+ (error-fail! (& (-> err error)) error-invalid (-> r message))
50
+ (set! (-> err offset) (-> r offset)))
49
51
  #f)
50
52
 
51
53
  (define (take-byte! [r (ptr wire-reader)] [to (ptr u8)]) -> bool
@@ -96,11 +98,9 @@
96
98
  (unless (take-count! r (& n)) (return #f))
97
99
  (let ([s (slice (-> r bytes) (-> r at) (+ (-> r at) n))])
98
100
  (unless (utf-8? s) (return (refuse! r start "a string is not UTF-8")))
99
- (let ([p (cast (ptr u8) (arena-alloc a n))])
100
- (memcpy p (-> s items) n)
101
- (set! (@ to) (cast string (slice p n)))
102
- (+= (-> r at) n)
103
- #t))))
101
+ (set! (@ to) (cast string (copy a s)))
102
+ (+= (-> r at) n)
103
+ #t)))
104
104
 
105
105
  ;; ---- the wire form ----
106
106
 
@@ -210,7 +210,7 @@
210
210
  #`(let ([#,present #f])
211
211
  (unless (take-flag! r (& #,present) "an option's tag is neither 0 nor 1") (return #f))
212
212
  (if #,present
213
- (let ([#,q (cast (ptr #,u) (arena-alloc a (sizeof #,u)))])
213
+ (let ([#,q (new a #,u)])
214
214
  #,(read-code (cadr type) #`(@ #,q) where)
215
215
  (set! #,p #,q))
216
216
  (set! #,p 0))))]
@@ -218,9 +218,9 @@
218
218
  (let ([n (derive-fresh 'n)] [items (derive-fresh 'items)] [i (derive-fresh 'i)] [u (type-syntax (cadr type))])
219
219
  #`(let ([#,n u32 0])
220
220
  (unless (take-count! r (& #,n)) (return #f))
221
- (let ([#,items (cast (ptr #,u) (arena-alloc a (* #,n (sizeof #,u))))])
221
+ (let ([#,items (new a #,u #,n)])
222
222
  (for ([#,i u32 0]) (< #,i #,n) (+= #,i 1) #,(read-code (cadr type) #`(@ #,items #,i) where))
223
- (set! #,p (slice #,items #,n)))))]
223
+ (set! #,p #,items))))]
224
224
  [(== (car type) 'array)
225
225
  (let ([i (derive-fresh 'i)])
226
226
  #`(for ([#,i u32 0]) (< #,i #,(literal (caddr type))) (+= #,i 1)
@@ -200,12 +200,12 @@
200
200
 
201
201
  ;; The table starts with 64 buckets and doubles, up to most, when three quarters full.
202
202
  (define (grow! [l (ptr http-limit)] [size u32])
203
- (let ([old (-> l buckets)] [n (-> l size)] [new (cast (ptr bucket) (calloc size (sizeof bucket)))])
203
+ (let ([old (-> l buckets)] [n (-> l size)] [more (-> (new 0 bucket size) items)])
204
204
  (for ([i u32 0]) (< i n) (+= i 1)
205
205
  (let ([b (& (@ old i))])
206
- (when (-> b used) (memcpy (slot new size (slice (cast (ptr u8) (-> b key)) (-> b length))) b (sizeof bucket)))))
206
+ (when (-> b used) (memcpy (slot more size (slice (cast (ptr u8) (-> b key)) (-> b length))) b (sizeof bucket)))))
207
207
  (free old)
208
- (set! (-> l buckets) new)
208
+ (set! (-> l buckets) more)
209
209
  (set! (-> l size) size)))
210
210
 
211
211
  (define (bucket-of [l (ptr http-limit)] [key (slice u8)]) -> (ptr bucket)
@@ -53,13 +53,11 @@
53
53
  (define (second-of [ms f64]) -> f64 (* (floor (/ ms 1000.0)) 1000.0))
54
54
 
55
55
  (define (http-conditional! [r (ptr http-request)] [etag string] [modified f64]) -> bool
56
- (let* ([a (& (-> r arena))] [copy (cast (ptr u8) (arena-alloc a (-> etag length)))]
57
- [read? (or (== (-> r method) "GET") (== (-> r method) "HEAD"))]
56
+ (let* ([a (& (-> r arena))] [read? (or (== (-> r method) "GET") (== (-> r method) "HEAD"))]
58
57
  [if-match (http-request-header r "if-match")] [if-none-match (http-request-header r "if-none-match")]
59
58
  [unmodified (date-of r "if-unmodified-since")] [since (date-of r "if-modified-since")]
60
59
  [changed (second-of modified)] [status i32 0])
61
- (memcpy copy (-> etag bytes) (-> etag length))
62
- (set! (-> r etag) (cast string (slice copy (-> etag length))))
60
+ (set! (-> r etag) (copy a etag))
63
61
  (set! (-> r modified) modified)
64
62
  (cond [(> (-> if-match length) 0) (unless (matches? if-match etag #t) (set! status 412))]
65
63
  [(and (>= unmodified 0.0) (> modified 0.0) (> changed unmodified)) (set! status 412)])
@@ -3,16 +3,14 @@
3
3
  (require-unqualified "http.sub")
4
4
  (require-unqualified "server.sub")
5
5
 
6
- (provide http-cookie http-request-cookie http-set-cookie! http-same-site-lax http-same-site-strict
7
- http-same-site-none)
6
+ (provide http-cookie http-request-cookie http-set-cookie! http-same-site http-same-site-default http-same-site-lax
7
+ http-same-site-strict http-same-site-none)
8
8
 
9
- (const http-same-site-lax i32 1)
10
- (const http-same-site-strict i32 2)
11
- (const http-same-site-none i32 3)
9
+ (enum http-same-site http-same-site-default http-same-site-lax http-same-site-strict http-same-site-none)
12
10
 
13
- ;; max-age is in seconds: 0 leaves it out, and below 0 ends the cookie now. same-site 0 leaves it out.
11
+ ;; max-age is in seconds: 0 leaves it out, and below 0 ends the cookie now. http-same-site-default leaves SameSite out.
14
12
  (struct http-cookie [name string] [value string] [path string] [domain string] [max-age i64] [secure bool]
15
- [http-only bool] [same-site i32])
13
+ [http-only bool] [same-site http-same-site])
16
14
 
17
15
  (define separators string "()<>@,;:\\\"/[]?={}")
18
16
 
package/std/http/form.sub CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
  (struct http-upload [filename string] [type string] [bytes (slice u8)])
12
12
 
13
- (struct form-error [message string] [field string])
13
+ (struct form-error [error error] [field string])
14
14
 
15
15
  (define (refuse! [err (ptr form-error)] [message string] [field string]) -> bool
16
16
  (unless (== err 0)
17
- (set! (-> err message) message)
17
+ (error-fail! (& (-> err error)) error-invalid message)
18
18
  (set! (-> err field) field))
19
19
  #f)
20
20
 
@@ -111,11 +111,11 @@
111
111
  (check! (cadr type) name)
112
112
  (let ([e (type-syntax (cadr type))])
113
113
  (if (upload? (cadr type))
114
- #`(let ([#,q (cast (ptr http-upload) (arena-alloc a (sizeof http-upload)))])
114
+ #`(let ([#,q (new a http-upload)])
115
115
  (set! #,p (if (file parts #,key 0 #,q) #,q 0)))
116
116
  #`(let ([#,s string])
117
117
  (if (text pairs parts #,key 0 (& #,s))
118
- (let ([#,q (cast (ptr #,e) (arena-alloc a (sizeof #,e)))])
118
+ (let ([#,q (new a #,e)])
119
119
  #,(value-code (cadr type) s #`(@ #,q) key)
120
120
  (set! #,p #,q))
121
121
  (set! #,p 0)))))]
@@ -123,17 +123,14 @@
123
123
  (check! (cadr type) name)
124
124
  (let ([e (type-syntax (cadr type))])
125
125
  (if (upload? (cadr type))
126
- #`(let* ([#,n (files parts #,key)]
127
- [#,items (cast (ptr http-upload) (arena-alloc a (* #,n (sizeof http-upload))))])
128
- (for ([#,k u32 0]) (< #,k #,n) (+= #,k 1) (file parts #,key #,k (+ #,items #,k)))
129
- (set! #,p (slice #,items #,n)))
130
- #`(let* ([#,n (texts pairs parts #,key)]
131
- [#,items (cast (ptr #,e) (arena-alloc a (* #,n (sizeof #,e))))]
132
- [#,s string])
126
+ #`(let* ([#,n (files parts #,key)] [#,items (new a http-upload #,n)])
127
+ (for ([#,k u32 0]) (< #,k #,n) (+= #,k 1) (file parts #,key #,k (& (@ #,items #,k))))
128
+ (set! #,p #,items))
129
+ #`(let* ([#,n (texts pairs parts #,key)] [#,items (new a #,e #,n)] [#,s string])
133
130
  (for ([#,k u32 0]) (< #,k #,n) (+= #,k 1)
134
131
  (text pairs parts #,key #,k (& #,s))
135
132
  #,(value-code (cadr type) s #`(@ #,items #,k) key))
136
- (set! #,p (slice #,items #,n)))))]
133
+ (set! #,p #,items))))]
137
134
  [(== type 'bool)
138
135
  #`(let ([#,s string])
139
136
  (if (text pairs parts #,key 0 (& #,s))
package/std/http/http.sub CHANGED
@@ -18,18 +18,11 @@
18
18
  (struct http-head [line buffer] [names (ptr string)] [values (ptr string)] [room u32] [count u32]
19
19
  [method string] [target string] [version string] [status i32] [reason string])
20
20
 
21
- (const state-head i32 0)
22
- (const state-length i32 1)
23
- (const state-chunk-size i32 2)
24
- (const state-chunk-data i32 3)
25
- (const state-chunk-end i32 4)
26
- (const state-trailers i32 5)
27
- (const state-until-close i32 6)
28
- (const state-stopped i32 7)
29
- (const state-refused i32 8)
30
- (const state-no-body i32 9)
31
-
32
- (struct http-parser [head http-head] [line buffer] [state i32] [remaining i64] [excess i64] [user (ptr void)]
21
+ (enum parser-state
22
+ state-head state-length state-chunk-size state-chunk-data state-chunk-end state-trailers state-until-close
23
+ state-stopped state-refused state-no-body)
24
+
25
+ (struct http-parser [head http-head] [line buffer] [state parser-state] [remaining i64] [excess i64] [user (ptr void)]
33
26
  [on-head (fn ((ptr http-parser)))] [on-body (fn ((ptr http-parser) (slice u8)))] [on-end (fn ((ptr http-parser)))])
34
27
 
35
28
  ;; ---- text ----
@@ -102,14 +95,14 @@
102
95
  (when (and (< i n) (== (@ value i) #\=))
103
96
  (+= i 1)
104
97
  (if (and (< i n) (== (@ value i) #\"))
105
- (let ([p (cast (ptr u8) (arena-alloc a (- n i)))] [k u32 0])
98
+ (let ([p (new a u8 (- n i))] [k u32 0])
106
99
  (+= i 1)
107
100
  (while (and (< i n) (!= (@ value i) #\"))
108
101
  (when (and (== (@ value i) #\\) (< (+ i 1) n)) (+= i 1))
109
102
  (set! (@ p k) (@ value i))
110
103
  (+= k 1)
111
104
  (+= i 1))
112
- (set! found (cast string (slice p k))))
105
+ (set! found (cast string (slice p 0 k))))
113
106
  (begin
114
107
  (set! start i)
115
108
  (while (and (< i n) (!= (@ value i) #\;)) (+= i 1))
@@ -201,22 +194,22 @@
201
194
 
202
195
  ;; to becomes a copy of from with its own text and room for just its headers, which http-head-free! frees.
203
196
  (define (http-head-copy! [to (ptr http-head)] [from (ptr http-head)])
204
- (let ([old (-> from line bytes)] [new (ptr u8) 0] [n (-> from count)])
197
+ (let ([old (-> from line bytes)] [text (ptr u8) 0] [n (-> from count)])
205
198
  (set! (-> to line) (make-buffer))
206
199
  (buffer-add! (& (-> to line)) (buffer-bytes (& (-> from line))))
207
- (set! new (-> to line bytes))
208
- (set! (-> to names) (cast (ptr string) (malloc (* n (sizeof string)))))
209
- (set! (-> to values) (cast (ptr string) (malloc (* n (sizeof string)))))
200
+ (set! text (-> to line bytes))
201
+ (set! (-> to names) (-> (new 0 string n) items))
202
+ (set! (-> to values) (-> (new 0 string n) items))
210
203
  (set! (-> to room) n)
211
204
  (for ([i u32 0]) (< i (-> from count)) (+= i 1)
212
- (set! (@ (-> to names) i) (moved (@ (-> from names) i) old new))
213
- (set! (@ (-> to values) i) (moved (@ (-> from values) i) old new)))
205
+ (set! (@ (-> to names) i) (moved (@ (-> from names) i) old text))
206
+ (set! (@ (-> to values) i) (moved (@ (-> from values) i) old text)))
214
207
  (set! (-> to count) (-> from count))
215
- (set! (-> to method) (moved (-> from method) old new))
216
- (set! (-> to target) (moved (-> from target) old new))
217
- (set! (-> to version) (moved (-> from version) old new))
208
+ (set! (-> to method) (moved (-> from method) old text))
209
+ (set! (-> to target) (moved (-> from target) old text))
210
+ (set! (-> to version) (moved (-> from version) old text))
218
211
  (set! (-> to status) (-> from status))
219
- (set! (-> to reason) (moved (-> from reason) old new))))
212
+ (set! (-> to reason) (moved (-> from reason) old text))))
220
213
 
221
214
  (define (moved [s string] [old (ptr u8)] [new (ptr u8)]) -> string
222
215
  (if (== (-> s length) 0) "" (cast string (slice (+ new (- (cast u32 (-> s bytes)) (cast u32 old))) (-> s length)))))
@@ -11,9 +11,7 @@
11
11
  (define (multipart-boundary [a (ptr arena)] [content-type string]) -> string
12
12
  (if (http-media-type? content-type "multipart/form-data") (http-media-parameter a content-type "boundary") ""))
13
13
 
14
- (define (refused [why (ptr string)] [message string]) -> bool
15
- (set! (@ why) message)
16
- #f)
14
+ (define (refused [err (ptr error)] [message string]) -> bool (error-fail! err error-invalid message))
17
15
 
18
16
  (define (at? [body (slice u8)] [i u32] [s string]) -> bool
19
17
  (and (<= (+ i (-> s length)) (-> body length)) (== (cast string (slice body i (+ i (-> s length)))) s)))
@@ -27,10 +25,10 @@
27
25
  -1))
28
26
 
29
27
  (define (joined [a (ptr arena)] [front string] [back string]) -> string
30
- (let ([p (cast (ptr u8) (arena-alloc a (+ (-> front length) (-> back length))))])
31
- (memcpy p (-> front bytes) (-> front length))
32
- (memcpy (+ p (-> front length)) (-> back bytes) (-> back length))
33
- (cast string (slice p (+ (-> front length) (-> back length))))))
28
+ (let ([p (new a u8 (+ (-> front length) (-> back length)))])
29
+ (memcpy (-> p items) (-> front bytes) (-> front length))
30
+ (memcpy (+ (-> p items) (-> front length)) (-> back bytes) (-> back length))
31
+ (cast string p)))
34
32
 
35
33
  (define (base-name [s string]) -> string
36
34
  (let ([i (-> s length)])
@@ -66,28 +64,28 @@
66
64
  [(http-same? name "content-type") (set! (-> part type) (http-trimmed value))])))
67
65
  (set! start (+ i 1)))))))
68
66
 
69
- ;; A body's named parts, in the arena; #f with the reason in why when the body is malformed.
67
+ ;; A body's named parts, in the arena.
70
68
  (define (multipart-parse [a (ptr arena)] [body (slice u8)] [boundary string] [to (ptr (slice multipart-part))]
71
- [why (ptr string)]) -> bool
69
+ [err (ptr error)]) -> bool
72
70
  (set! (@ to) 0)
73
71
  (when (or (== (-> boundary length) 0) (> (-> boundary length) 70))
74
- (return (refused why "the boundary is missing or longer than 70 bytes")))
72
+ (return (refused err "the boundary is missing or longer than 70 bytes")))
75
73
  (let* ([dash (joined a "--" boundary)] [delimiter (joined a "\r\n" dash)] [n (-> body length)]
76
74
  [first (if (at? body 0 dash) 0 (find body delimiter 0))]
77
75
  [at u32 0] [items (ptr multipart-part) 0] [count u32 0] [room u32 0])
78
- (when (< first 0) (return (refused why "the body has no boundary")))
76
+ (when (< first 0) (return (refused err "the body has no boundary")))
79
77
  (set! at (+ (cast u32 first) (if (at? body 0 dash) 0 2) (-> dash length)))
80
78
  (while #t
81
79
  (when (at? body at "--") (break))
82
80
  (while (and (< at n) (or (== (@ body at) 32) (== (@ body at) 9))) (+= at 1))
83
- (unless (at? body at "\r\n") (return (refused why "a boundary's line goes on past it")))
81
+ (unless (at? body at "\r\n") (return (refused err "a boundary's line goes on past it")))
84
82
  (+= at 2)
85
83
  (let* ([empty (at? body at "\r\n")] [end (if empty (cast i64 at) (find body "\r\n\r\n" at))]
86
84
  [part multipart-part] [start u32 0] [next i64 0])
87
- (when (< end 0) (return (refused why "a part's head does not end")))
85
+ (when (< end 0) (return (refused err "a part's head does not end")))
88
86
  (set! start (+ (cast u32 end) (if empty 2 4)))
89
87
  (set! next (find body delimiter start))
90
- (when (< next 0) (return (refused why "the body ends inside a part")))
88
+ (when (< next 0) (return (refused err "the body ends inside a part")))
91
89
  (set! (-> part name) "")
92
90
  (set! (-> part filename) "")
93
91
  (set! (-> part type) "")
@@ -97,7 +95,7 @@
97
95
  (when (> (-> part name length) 0)
98
96
  (when (== count room)
99
97
  (set! room (if (== room 0) 4 (* 2 room)))
100
- (let ([more (cast (ptr multipart-part) (arena-alloc a (* room (sizeof multipart-part))))])
98
+ (let ([more (-> (new a multipart-part room) items)])
101
99
  (when (> count 0) (memcpy more items (* count (sizeof multipart-part))))
102
100
  (set! items more)))
103
101
  (set! (@ items count) part)
@@ -18,20 +18,20 @@
18
18
  (define (route-rest [a (ptr arena)] [segments (slice string)] [i u32]) -> string
19
19
  (let ([n u32 0] [at u32 0])
20
20
  (for ([k u32 i]) (< k (-> segments length)) (+= k 1) (+= n (+ (-> (@ segments k) length) 1)))
21
- (let ([p (cast (ptr u8) (arena-alloc a n))])
21
+ (let ([p (new a u8 n)])
22
22
  (for ([k u32 i]) (< k (-> segments length)) (+= k 1)
23
23
  (when (> k i)
24
24
  (set! (@ p at) #\/)
25
25
  (+= at 1))
26
- (memcpy (+ p at) (-> (@ segments k) bytes) (-> (@ segments k) length))
26
+ (memcpy (+ (-> p items) at) (-> (@ segments k) bytes) (-> (@ segments k) length))
27
27
  (+= at (-> (@ segments k) length)))
28
- (cast string (slice p at)))))
28
+ (cast string (slice p 0 at)))))
29
29
 
30
30
  (define (route-slashed [a (ptr arena)] [segments (slice string)]) -> (slice string)
31
- (let* ([n (-> segments length)] [items (cast (ptr string) (arena-alloc a (* (+ n 1) (sizeof string))))])
31
+ (let* ([n (-> segments length)] [items (new a string (+ n 1))])
32
32
  (for ([i u32 0]) (< i n) (+= i 1) (set! (@ items i) (@ segments i)))
33
33
  (set! (@ items n) "")
34
- (slice items (+ n 1))))
34
+ items))
35
35
 
36
36
  (define (route-moved! [r (ptr http-request)] [path string])
37
37
  (let ([b (make-buffer)] [target (-> r target)])
@@ -76,8 +76,9 @@
76
76
  (buffer-add-string! (& b) "field ")
77
77
  (buffer-add-string! (& b) (-> err field))
78
78
  (buffer-add-string! (& b) ": ")
79
- (buffer-add-string! (& b) (-> err message))
79
+ (buffer-add-string! (& b) (error-text (& (-> err error))))
80
80
  (http-answer-text! r 400 (buffer-string (& b)))
81
+ (error-free! (& (-> err error)))
81
82
  (buffer-free! (& b))))
82
83
 
83
84
  (define (route-json-refused! [r (ptr http-request)] [err (ptr json-error)])
@@ -87,8 +88,9 @@
87
88
  (buffer-add-byte! (& b) #\:)
88
89
  (write-digits (& b) (-> err column) 10)
89
90
  (buffer-add-string! (& b) ": ")
90
- (buffer-add-string! (& b) (-> err message))
91
+ (buffer-add-string! (& b) (error-text (& (-> err error))))
91
92
  (http-answer-text! r 400 (buffer-string (& b)))
93
+ (error-free! (& (-> err error)))
92
94
  (buffer-free! (& b))))
93
95
 
94
96
  (define (route-typed? [r (ptr http-request)] [json bool]) -> bool
@@ -103,12 +105,15 @@
103
105
 
104
106
  (define (route-form-fields [r (ptr http-request)] [body (slice u8)] [pairs (ptr (slice url-pair))]
105
107
  [parts (ptr (slice multipart-part))]) -> bool
106
- (let* ([a (& (-> r arena))] [t (http-request-header r "content-type")] [why string])
108
+ (let* ([a (& (-> r arena))] [t (http-request-header r "content-type")] [why error])
107
109
  (set! (@ pairs) 0)
108
110
  (set! (@ parts) 0)
109
111
  (if (http-media-type? t "multipart/form-data")
110
112
  (or (multipart-parse a body (multipart-boundary a t) parts (& why))
111
- (begin (http-answer-text! r 400 why) #f))
113
+ (begin
114
+ (http-answer-text! r 400 (error-text (& why)))
115
+ (error-free! (& why))
116
+ #f))
112
117
  (begin (set! (@ pairs) (url-form-parse a (cast string body))) #t))))
113
118
 
114
119
  (define (route-add-text! [b (ptr buffer)] [s string])
@@ -440,7 +445,7 @@
440
445
  #,@(if (== body 0)
441
446
  `(,#`(unless (route-typed? r #,(literal (boolean (!= json 0)))) (return)))
442
447
  nil)
443
- (let ([#,args (cast (ptr #,given) (arena-alloc (& (-> r arena)) (sizeof #,given)))])
448
+ (let ([#,args (new (& (-> r arena)) #,given)])
444
449
  #,@(for/list ([x values]) #`(set! (-> #,args #,x) #,x))
445
450
  (set! (-> r route) (cast (ptr void) #,args))
446
451
  (http-read-body! r #,(if (== limit 0) (number 0) limit) #,read)))
@@ -448,7 +453,7 @@
448
453
  [start #`(define (#,(start-of r) [r (ptr http-request)] #,@(formals ps))
449
454
  #,(if (== query 0)
450
455
  ending
451
- #`(let ([#,q (cast (ptr #,query) (arena-alloc (& (-> r arena)) (sizeof #,query)))]
456
+ #`(let ([#,q (new (& (-> r arena)) #,query)]
452
457
  [#,e form-error])
453
458
  (unless (#,(derived query '"form-read" 'form where) (& (-> r arena))
454
459
  (url-form-parse (& (-> r arena)) (url-query (-> r target))) no-parts #,q
@@ -462,14 +467,14 @@
462
467
  (let* ([#,args (cast (ptr #,given) (-> r route))] #,@(for/list ([x values]) #`[#,x (-> #,args #,x)]))
463
468
  #,(cond [(!= body 0) (handler-call handler values `(,#'bytes))]
464
469
  [(!= json 0)
465
- #`(let ([#,value (cast (ptr #,json) (arena-alloc (& (-> r arena)) (sizeof #,json)))]
470
+ #`(let ([#,value (new (& (-> r arena)) #,json)]
466
471
  [#,e json-error])
467
472
  (unless (#,(derived json '"json-read" 'json where) (& (-> r arena)) (cast string bytes)
468
473
  #,value (& #,e))
469
474
  (return (route-json-refused! r (& #,e))))
470
475
  #,(handler-call handler values `(,value)))]
471
476
  [else
472
- #`(let ([#,value (cast (ptr #,form) (arena-alloc (& (-> r arena)) (sizeof #,form)))]
477
+ #`(let ([#,value (new (& (-> r arena)) #,form)]
473
478
  [#,e form-error] [pairs (slice url-pair)] [parts (slice multipart-part)])
474
479
  (unless (route-form-fields r bytes (& pairs) (& parts)) (return))
475
480
  (unless (#,(derived form '"form-read" 'form where) (& (-> r arena)) pairs parts #,value
@@ -3,7 +3,8 @@
3
3
  ;; in ms, the default at 0 and none below 0: idle-timeout 5 s (130 s behind proxies), head-timeout 60 s,
4
4
  ;; request-timeout 300 s, send-timeout 60 s; body-limit for http-read-body!, 1 MiB at 0; compress, a gzip level for
5
5
  ;; answers of a type that compresses, of 1 KB or more, and for event streams that ask; proxies, how many proxies' hops
6
- ;; of X-Forwarded headers to believe; checks, run before handle. problem holds why listening failed.
6
+ ;; of X-Forwarded headers to believe; checks, run before handle. problem holds the failure when listening fails after
7
+ ;; http-serve! returns.
7
8
  ;; A request lasts until it is answered and its body has ended; on-done is called as it ends and must not trap. A trap
8
9
  ;; in a request's code answers 500, or ends the connection once the answer has begun, and goes to on-trap, the log, or
9
10
  ;; stderr.
@@ -39,7 +40,7 @@
39
40
  [on-end (fn ((ptr http-request)))]
40
41
  [on-gone (fn ((ptr http-request)))] [on-drained (fn ((ptr http-request)))] [body buffer] [limit u32]
41
42
  [taken (fn ((ptr http-request) (slice u8)))] [added buffer] [own-head http-head] [etag string] [modified f64]
42
- [ranges bool] [compressible bool] [events bool] [encoded bool] [gzip i32] [deflater (ptr deflater)] [crc u32]
43
+ [ranges bool] [compressible bool] [events bool] [encoded bool] [gzip zip-mode] [deflater (ptr deflater)] [crc u32]
43
44
  [size u32])
44
45
 
45
46
  ;; A check runs before the handler and returns #t when it has answered.
@@ -65,7 +66,7 @@
65
66
 
66
67
  (struct http-server [head event-head] [listener u32] [port u32] [host string] [secure bool] [most u32]
67
68
  [idle-timeout f64] [head-timeout f64] [request-timeout f64] [send-timeout f64] [body-limit u32] [compress i32]
68
- [proxies u32] [checks (slice http-check)] [problem string] [user (ptr void)] [connections (ptr http-connection)]
69
+ [proxies u32] [checks (slice http-check)] [problem error] [user (ptr void)] [connections (ptr http-connection)]
69
70
  [hooks (ptr http-hook)] [stopping bool] [stop-timer u32] [stopped (fn ((ptr http-server)))] [log (ptr logger)]
70
71
  [handle (fn ((ptr http-request)))] [listening (fn ((ptr http-server)))] [on-trap (fn ((ptr http-request) string))]
71
72
  [on-done (fn ((ptr http-request)))])
@@ -140,13 +141,11 @@
140
141
  (http-write-header! (& (-> r added)) name value))
141
142
 
142
143
  (define (new-request! [c (ptr http-connection)] [h (ptr http-head)]) -> (ptr http-request)
143
- (let ([r (cast (ptr http-request) (calloc 1 (sizeof http-request)))])
144
+ (let ([r (new 0 http-request)])
144
145
  (set! (-> r connection) (cast (ptr void) c))
145
146
  (set! (-> r server) (-> c server))
146
147
  (set! (-> r length) -1)
147
- (set! (-> r peer) (cast string (slice (cast (ptr u8) (arena-alloc (& (-> r arena)) (-> c peer length)))
148
- (-> c peer length))))
149
- (memcpy (-> r peer bytes) (-> c peer bytes) (-> c peer length))
148
+ (set! (-> r peer) (copy (& (-> r arena)) (-> c peer)))
150
149
  (http-head-copy! (& (-> r own-head)) h)
151
150
  (set! (-> r head) (& (-> r own-head)))
152
151
  (set! (-> r method) (-> r own-head method))
@@ -581,7 +580,7 @@
581
580
  [(== kind stream-failed) (drop! c)])))
582
581
 
583
582
  (define (new-connection! [s (ptr http-server)] [stream u32]) -> (ptr http-connection)
584
- (let ([c (cast (ptr http-connection) (calloc 1 (sizeof http-connection)))])
583
+ (let ([c (new 0 http-connection)])
585
584
  (set! (-> c head handle) connection-heard)
586
585
  (set! (-> c server) (cast (ptr void) s))
587
586
  (set! (-> c stream) stream)
@@ -612,10 +611,7 @@
612
611
  ;; ---- compression ----
613
612
 
614
613
  ;; How an answer is compressed: not, whole by http-send!, as each piece is written, or each event alone.
615
- (const zip-none i32 0)
616
- (const zip-whole i32 1)
617
- (const zip-pieces i32 2)
618
- (const zip-events i32 3)
614
+ (enum zip-mode zip-none zip-whole zip-pieces zip-events)
619
615
 
620
616
  ;; Bodies shorter than this go uncompressed.
621
617
  (const zip-least i64 1024)
@@ -763,7 +759,7 @@
763
759
  (when (and (== (-> r gzip) zip-none) (or (< (-> r length) 0) (>= (-> r length) zip-least)) (zip? r))
764
760
  (set! (-> r gzip) (if (-> r events) zip-events zip-pieces))
765
761
  (when (== (-> r gzip) zip-pieces)
766
- (set! (-> r deflater) (cast (ptr deflater) (malloc (sizeof deflater))))
762
+ (set! (-> r deflater) (new 0 deflater))
767
763
  (deflater-init! (-> r deflater) (-> s compress))))
768
764
  (when (and (has-length? r) (>= (-> r length) 0) (< (-> r gzip) zip-pieces))
769
765
  (http-write-number! b "content-length" (-> r length)))
@@ -911,7 +907,7 @@
911
907
  (begin
912
908
  (stream-close from)
913
909
  (http-finish! r))
914
- (let ([p (cast (ptr relay) (malloc (sizeof relay)))])
910
+ (let ([p (new 0 relay)])
915
911
  (http-write! r 0)
916
912
  (set! (-> p head handle) relay-heard)
917
913
  (set! (-> p from) from)
@@ -982,19 +978,21 @@
982
978
  (when (!= (-> s listening) 0) ((-> s listening) s))]
983
979
  [(== kind net-accept) (if (-> s stopping) (stream-close (cast u32 value)) (accept! s (cast u32 value)))]
984
980
  [(== kind net-failed)
985
- (set! (-> s port) 0)
986
- (set! (-> s problem) (events-take-text))
987
- (when (!= (-> s listening) 0) ((-> s listening) s))])))
981
+ (let ([why (events-take-text)])
982
+ (set! (-> s port) 0)
983
+ (error-fail! (& (-> s problem)) value why)
984
+ (free (-> why bytes))
985
+ (when (!= (-> s listening) 0) ((-> s listening) s)))])))
988
986
 
989
- (define (http-serve! [s (ptr http-server)] [port u32] [handle (fn ((ptr http-request)))]) -> u32
987
+ (define (http-serve! [s (ptr http-server)] [port u32] [handle (fn ((ptr http-request)))] [err (ptr error)]) -> bool
990
988
  (trap-handler! http-trapped)
991
989
  (set! (-> s head handle) server-heard)
992
990
  (set! (-> s handle) handle)
993
991
  (set! (-> s connections) 0)
994
992
  (set! (-> s port) 0)
995
- (set! (-> s problem) "")
996
- (set! (-> s listener) (net-listen port (-> s host) (-> s secure) (-> s most) (cast (ptr void) s) (& (-> s problem))))
997
- (-> s listener))
993
+ (error-free! (& (-> s problem)))
994
+ (set! (-> s problem code) 0)
995
+ (net-listen port (-> s host) (-> s secure) (-> s most) (cast (ptr void) s) (& (-> s listener)) err))
998
996
 
999
997
  ;; http-server-close! stops listening and drops every connection at once, and runs the hooks forced.
1000
998
  (define (http-server-close! [s (ptr http-server)])
@@ -1089,8 +1087,8 @@
1089
1087
  (set! (-> stopper server) s)
1090
1088
  (set! (-> stopper ms) ms)
1091
1089
  (set! (-> stopper done) done)
1092
- (set! (-> stopper term) (signal-listen "SIGTERM" (cast (ptr void) (& stopper)) 0))
1093
- (signal-listen "SIGINT" (cast (ptr void) (& stopper)) 0))
1090
+ (signal-listen "SIGTERM" (cast (ptr void) (& stopper)) (& (-> stopper term)) 0)
1091
+ (signal-listen "SIGINT" (cast (ptr void) (& stopper)) 0 0))
1094
1092
 
1095
1093
  ;; ---- connections without streams ----
1096
1094
 
@@ -51,10 +51,7 @@
51
51
  (set! ok (and (<= at (+ (now-seconds) 60)) (<= (cast i64 (- (now-seconds) at)) (max-age-of s)))))
52
52
  (set! (-> b length) 0)
53
53
  (set! ok (and ok (base64url-decode! (& b) (slice value 0 first))))
54
- (when ok
55
- (let ([p (cast (ptr u8) (arena-alloc (& (-> r arena)) (-> b length)))])
56
- (memcpy p (-> b bytes) (-> b length))
57
- (set! (@ into) (slice p (-> b length)))))
54
+ (when ok (set! (@ into) (copy (& (-> r arena)) (buffer-bytes (& b)))))
58
55
  (buffer-free! (& b))
59
56
  ok))
60
57
 
@@ -100,9 +97,9 @@
100
97
  (raise-syntax-error 'session "this type needs a json form, which (json T) defines: write it first" t))
101
98
  `(,#`(define (#,(derive-name t '"session-read") [s (ptr http-sessions)] [r (ptr http-request)] [v (ptr #,t)])
102
99
  -> bool
103
- (let ([bytes (slice u8)] [e json-error])
100
+ (let ([bytes (slice u8)])
104
101
  (and (http-session-read s r (& bytes))
105
- (#,(derive-name t '"json-read") (& (-> r arena)) (cast string bytes) v (& e)))))
102
+ (#,(derive-name t '"json-read") (& (-> r arena)) (cast string bytes) v 0))))
106
103
  ,#`(define (#,(derive-name t '"session-write!") [s (ptr http-sessions)] [r (ptr http-request)] [v (ptr #,t)])
107
104
  (let ([b (make-buffer)])
108
105
  (#,(derive-name t '"json-write!") (& b) v)
@@ -11,16 +11,11 @@
11
11
  (require-unqualified "http.sub")
12
12
 
13
13
  (provide sse-event sse-parser sse-parser-init! sse-parse! sse-parser-end! sse-parser-free! sse-client
14
- sse-client-open! sse-client-close! sse-ended sse-lost sse-refused)
14
+ sse-client-open! sse-client-close! sse-end sse-ended sse-lost sse-refused)
15
15
 
16
- (const sse-ended i32 0)
17
- (const sse-lost i32 1)
18
- (const sse-refused i32 2)
16
+ (enum sse-end sse-ended sse-lost sse-refused)
19
17
 
20
- (const state-closed i32 0)
21
- (const state-connecting i32 1)
22
- (const state-open i32 2)
23
- (const state-waiting i32 3)
18
+ (enum client-state state-closed state-connecting state-open state-waiting)
24
19
 
25
20
  (const default-retry f64 3000.0)
26
21
 
@@ -133,10 +128,10 @@
133
128
  ;; ---- the client ----
134
129
 
135
130
  (struct sse-client [head event-head] [user (ptr void)] [reconnect bool] [retry f64] [status i32] [error i32]
136
- [method string] [url string] [headers (slice string)] [body (slice u8)] [state i32] [fetch u32] [timer u32]
131
+ [method string] [url string] [headers (slice string)] [body (slice u8)] [state client-state] [fetch u32] [timer u32]
137
132
  [parser sse-parser] [chunk (array u8 16384)]
138
133
  [on-open (fn ((ptr sse-client)))] [on-event (fn ((ptr sse-client) (ptr sse-event)))]
139
- [on-closed (fn ((ptr sse-client) i32))])
134
+ [on-closed (fn ((ptr sse-client) sse-end))])
140
135
 
141
136
  (define (parsed [p (ptr sse-parser)] [e (ptr sse-event)])
142
137
  (let ([c (cast (ptr sse-client) (-> p user))])
@@ -144,7 +139,7 @@
144
139
 
145
140
  (define (connect! [c (ptr sse-client)])
146
141
  (let* ([n (-> c headers length)] [id (buffer-string (& (-> c parser id)))]
147
- [all (cast (ptr string) (malloc (* (+ n 4) (sizeof string))))] [k n])
142
+ [all (new 0 string (+ n 4))] [k n])
148
143
  (for ([i u32 0]) (< i n) (+= i 1) (set! (@ all i) (@ (-> c headers) i)))
149
144
  (set! (@ all k) "accept")
150
145
  (set! (@ all (+ k 1)) "text/event-stream")
@@ -154,8 +149,8 @@
154
149
  (set! (@ all (+ k 1)) id)
155
150
  (+= k 2))
156
151
  (set! (-> c state) state-connecting)
157
- (set! (-> c fetch) (fetch-start (-> c method) (-> c url) (slice all k) (-> c body) (cast (ptr void) c)))
158
- (free all)))
152
+ (set! (-> c fetch) (fetch-start (-> c method) (-> c url) (slice all 0 k) (-> c body) (cast (ptr void) c)))
153
+ (free (-> all items))))
159
154
 
160
155
  (define (stop! [c (ptr sse-client)])
161
156
  (unless (== (-> c fetch) 0) (stream-close (-> c fetch)))
@@ -164,7 +159,7 @@
164
159
  (set! (-> c timer) 0)
165
160
  (set! (-> c state) state-closed))
166
161
 
167
- (define (ended! [c (ptr sse-client)] [why i32])
162
+ (define (ended! [c (ptr sse-client)] [why sse-end])
168
163
  (stream-close (-> c fetch))
169
164
  (set! (-> c fetch) 0)
170
165
  (sse-parser-end! (& (-> c parser)))