gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd69665f6d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmred69665f6/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

  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
  • 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
  • dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
  • 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
  • 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
  • ddc7d135Merge branch 'f7-hooks' into mainIdent
  • eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
  • ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
  • 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
  • 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
  • 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
  • 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
  • 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
  • e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
  • fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre