gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7480a98d7480a98dkunibert.service: SIGTERM stop is a clean exitKunibert7480a98d/plugins/http1/server.hl

3.2 KB

  1. \* hl:http1 — module exports beside the class surface (NativeHttpServer,
  2. NativeWebSocketServer, Response, WsClient in their own files). *\
  3. \* Bind a plain HTTP/1.1 listener; answers the request event source. *\
  4. listen(
  5. Number port \\ the port to bind
  6. ) {
  7. return __native("http1.listen", port)
  8. }
  9. \* The full-featured constructor: interface, TLS pair, worker count. *\
  10. create_server(
  11. Number port \\ the port to bind
  12. String host = '0.0.0.0' \\ the interface
  13. String cert = null \\ PEM certificate path — with key, serve https
  14. String key = null \\ PEM key path
  15. Number threads = 4 \\ I/O worker threads
  16. Number maxBody = 20971520 \\ largest request body read, bytes; over it: 413
  17. Boolean wsCheckOrigin = false \\ 403 a WebSocket upgrade with a foreign Origin
  18. String wsOrigins = "" \\ extra allowed origins, comma-separated
  19. ) {
  20. return __native("http1.create_server", port, host, cert, key, threads, maxBody, wsCheckOrigin, wsOrigins)
  21. }
  22. \* A cookie-grade random token: n lowercase hex characters read from the
  23. KERNEL's CSPRNG, never a seeded PRNG — a session id is the only thing
  24. between a stranger and someone else's session (hl:math's random() is a
  25. clock-seeded xoshiro whose state a handful of outputs reveal). Lives here
  26. because the session cookie is an HTTP artifact. *\
  27. randomToken(
  28. Number n \\ how many hex characters
  29. ) {
  30. return __native("http1.random_token", n)
  31. }
  32. \* SERVER-SENT EVENTS (ticket #144) — the push channel, imported by name like any
  33. other export: import { sseStart, sseSend } from 'hl:http1'. A static or a method
  34. calls them; the browser build of a file that imports them binds each to a refusal
  35. that throws only if the browser CALLS it (hl:http1 ships no browser half).
  36. let conn = sseStart(req.respond) \\ the GET is now a text/event-stream
  37. sseSend(conn, JSON.stringify(row)) \\ one `data:` frame to that client
  38. A stream lives until its client goes: hl:http1 reaps a closed one at once (FIN) or
  39. at the next ":" heartbeat (every 5 s), and a push to it answers false. Plain HTTP
  40. only — on hl:http1's own TLS, sseStart answers null (end TLS in a proxy). *\
  41. \* Turn this request into a Server-Sent Events stream: writes the 200
  42. text/event-stream head and keeps the socket open. Answers the CONNECTION ID the
  43. other sse calls address, or null when the request was already answered (or is TLS). *\
  44. sseStart(
  45. Hybrid respond \\ the request's respond handle: req.respond
  46. ) {
  47. return respond.call("sse_start")
  48. }
  49. \* One frame to one stream: `data: <text>` and the blank line. A text with
  50. newlines continues the frame (a line after the first is its own field, e.g.
  51. 'id: 42'). Answers true when it was written, false when that stream is gone. *\
  52. sseSend(
  53. Number conn \\ the id sseStart answered
  54. String text \\ the frame's data, usually JSON.stringify(...)
  55. ) {
  56. return __native("http1.sse_send", conn, text) == 1
  57. }
  58. \* The same frame to every open stream; answers how many got it. *\
  59. sseBroadcast(
  60. String text \\ the frame's data
  61. ) {
  62. return __native("http1.sse_broadcast", text)
  63. }
  64. \* Is the stream still open? Asks the registry, pushes nothing. *\
  65. sseAlive(
  66. Number conn \\ the id sseStart answered
  67. ) {
  68. return __native("http1.sse_alive", conn) == 1
  69. }
  70. \* How many streams are open now. *\
  71. sseCount() {
  72. return __native("http1.sse_count")
  73. }

Branches

Latest commits

  • 7480a98dkunibert.service: SIGTERM stop is a clean exitKunibert
  • 3a8d93f9gitoria client: parse only JSON answers, accept a plain-text errorKunibert
  • c9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibert
  • a41ba15fKunibert: event intake, git cache, review runner, posting, linked tickets, deploy filesKunibert