gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit50093aaf50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre50093aaf/plugins/http1/native_web_socket_server.hl

4.4 KB

  1. \* hl:http1 — the native WebSocket server. Subclass it (`inherit
  2. NativeWebSocketServer`) and answer the declared events below; each
  3. connected peer arrives as a WsClient (send/ping/close). AUTO-LISTEN: the
  4. file root IS the constructor and ends with listen(), so
  5. `new NativeWebSocketServer(port = 8888)` serves immediately. *\
  6. /* Mission 064 (decision D9): the h1 plugin performs the RFC 6455 Upgrade/101
  7. handshake and framing (shared core plugins/http/ws_common.zig); this
  8. wrapper maps the native event stream onto on/emit, mirroring
  9. NativeHttpServer. A SUBCLASS does not inherit this run body and calls
  10. listen() from its own root. */
  11. \* A peer completed the upgrade and is connected. *\
  12. emit connect(
  13. WsClient client \\ the new peer — send/ping/close
  14. )
  15. \* A complete text (or binary) message arrived from a peer. *\
  16. emit message(
  17. WsClient client \\ who sent it
  18. String data \\ the message payload
  19. )
  20. \* The peer answered a client.ping(). *\
  21. emit pong(
  22. WsClient client \\ who answered
  23. )
  24. \* A connection ended — the peer is already unregistered. *\
  25. emit close(
  26. WsClient client \\ who left
  27. )
  28. \* OPTIONAL: plain HTTP answered on the same port, exactly as
  29. NativeHttpServer's request. *\
  30. emit request(
  31. Hybrid req \\ method, path, headers, body, bytes, params, respond
  32. )
  33. Number port = 8080 \\ the port listen() binds
  34. String host = "0.0.0.0" \\ the interface to bind
  35. Number threads = 4 \\ I/O worker threads
  36. Number maxBody = 20971520 \\ largest request body read, bytes (default 20 MiB); over it: 413, connection closed
  37. Boolean wsCheckOrigin = false \\ refuse an upgrade whose Origin is foreign (not the Host, not in wsOrigins) with 403 before the 101; no Origin = allowed
  38. String wsOrigins = "" \\ extra allowed origins, comma-separated
  39. Boolean listening = false \\ true after a successful bind
  40. Hybrid clients = {} \\ live WsClients by connection id
  41. Number #srcId = 0
  42. Number #wsSrcId = 0
  43. \* Bind the configured port and start the WebSocket engine (idempotent). *\
  44. listen() {
  45. if (listening == true) {
  46. return null
  47. }
  48. srv = __native("http1.create_server", port, host, "", "", threads, maxBody, wsCheckOrigin, wsOrigins)
  49. if (srv == null) {
  50. // A REFUSAL, NOT PROSE (mission 286) — same reason as NativeHttpServer's.
  51. hlError("hl:http1: listen() failed: could not bind port " + port + " on " + host + " — the port is already taken. The line just above, 'bind() failed on port " + port + "', names the process holding it: stop that process, or start this app on another port (--port N, or HL_PORT=N)")
  52. }
  53. wsev = __native("http1.ws_events")
  54. listening = true
  55. #srcId = __native("eventloop.register", srv, "request")
  56. #wsSrcId = __native("eventloop.register", wsev, "wsEvent")
  57. }
  58. \* One text frame to EVERY connected peer; answers how many received it. *\
  59. broadcast(
  60. String text \\ the frame to send
  61. ) {
  62. return __native("http1.ws_broadcast", text)
  63. }
  64. \* Stop this server: both event sources retired, workers joined, listener
  65. closed — the port is free again. Answers false when not listening. *\
  66. close() {
  67. if (listening == false) {
  68. return false
  69. }
  70. listening = false
  71. __native("eventloop.unregister", #srcId)
  72. __native("eventloop.unregister", #wsSrcId)
  73. return __native("http1.close", port)
  74. }
  75. // Raw native events ({ kind, id, data, binary, code }) re-emitted as the
  76. // documented surface — emit is the universal primitive.
  77. on wsEvent(ev) {
  78. key = "c" + ev.id
  79. if (ev.kind == "connect") {
  80. client = new WsClient(ev.id, ev.cookie, ev.host != '' ? ev.host : null, headerHash(ev.headers), ev.ip != '' ? ev.ip : null, ev.secure) // the upgrade's cookie, host and headers ride with the connection (creator, 2026-09-12; tickets #74, #105)
  81. clients[key] = client
  82. emit connect(client)
  83. }
  84. if (ev.kind == "message") {
  85. client = clients[key]
  86. if (client != null) {
  87. emit message(client, ev.data)
  88. }
  89. }
  90. if (ev.kind == "pong") {
  91. client = clients[key]
  92. if (client != null) {
  93. emit pong(client)
  94. }
  95. }
  96. if (ev.kind == "close") {
  97. client = clients[key]
  98. clients[key] = null
  99. if (client != null) {
  100. emit close(client)
  101. }
  102. }
  103. }
  104. // The upgrade's headers arrive as `name: value` lines (names already lowercase);
  105. // a connection is handed them as a hash (ticket #105).
  106. headerHash(lines) {
  107. let nl = "
  108. "
  109. let out = {}
  110. if (lines == null || lines == '') { return out }
  111. for (line of lines.split(nl)) {
  112. let colon = line.indexOf(': ')
  113. if (colon > 0) { out[line.slice(0, colon)] = line.slice(colon + 2) }
  114. }
  115. return out
  116. }
  117. // Run body: bind the configured port (idempotent, like NativeHttpServer).
  118. listen()

Branches

Latest commits

  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre
  • 9f502db1gitoria#46: W46b briefmre
  • 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
  • bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
  • 772c401fgitoria#45: W45 report, logmre
  • b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
  • fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
  • f7198131gitoria#45: tests/pr-page.mjs gatemre
  • 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
  • e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
  • 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
  • 5de7f578gitoria#47: rate limits per app (429, Retry-After), status read route, statuses and required on the pull, one JSON check for every write routemre
  • 19edc1f5gitoria#44: contract v1.3.1, what outdated meansmre
  • 7c2277ccgitoria#38: worker reports W40–W46, logmre
  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre