gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit514d7cbe514d7cbeRecords: mission 008 startedmre514d7cbe/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

  • 514d7cbeRecords: mission 008 startedmre
  • 61c40127Mission 008: #19 + #21mre
  • 6dcaeb46Records: resetKey planmre
  • c5dbf2ecRecords: R2 resultmre
  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre
  • deb8cef5Records: mission 006 startedmre
  • 4f0ad42fMission 006: jukebox#20mre
  • 800cae06Records: #21 planmre
  • 64bc7823Records: #21 causemre
  • 76639655Records: B1 questionmre
  • d7906b36Records: mission 005 startedmre
  • d3df773cMission 005: doc linemre
  • 6a5d840aMission 005: jukebox#22mre
  • 13e5e748Records: mission 004 startedmre
  • a8098271Mission 004: jukebox#21mre
  • df8f5205Records: jukebox#19-21, #11 closedmre
  • b7a4e692Records: hybriel#298mre
  • 9bcbb017Records: day38b waits for day39 fixmre
  • f80504e1Records: day38b repro sentmre