gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit494d75d4494d75d4Nest caller slot styles in app-sidebarmre494d75d4/plugins/http1/native_http_server.hl

4.7 KB

  1. \* hl:http1 — the native HTTP/1.1 server. Subclass it (`inherit
  2. NativeHttpServer`) and answer `on request(req)` with a Response.
  3. AUTO-LISTEN: the file root IS the constructor and its last statement is
  4. listen(), so `new NativeHttpServer(port = 8888)` binds 8888 with no
  5. explicit call. *\
  6. /* Split from the former multi-class server.hl (mission 027). listen() is
  7. idempotent (the `listening` guard). A SUBCLASS does NOT inherit this run
  8. body — parent run bodies never run — so subclasses call listen() from
  9. their own root. No base `on request` handler here: the loop dispatches to
  10. the concrete instance's handlers, and old-form inheritance copies
  11. handlers — a base default would double-fire (mission 052). */
  12. \* One HTTP request reached this server — answer a Response (or hold
  13. req.respond for the streaming/deferred forms). *\
  14. emit request(
  15. Hybrid req \\ method, path, headers, body, bytes, params, respond
  16. )
  17. Number port = 8080 \\ the port bind() takes
  18. String host = "0.0.0.0" \\ the interface to bind
  19. String tls_cert = null \\ PEM certificate path — with tls_key, serve https
  20. String tls_key = null \\ PEM key path
  21. Number threads = 4 \\ I/O worker threads
  22. Number maxBody = 20971520 \\ largest request body read, bytes (default 20 MiB): a bigger Content-Length, or a chunked body growing past it, is answered 413 before it is read and the connection closed
  23. Boolean wsCheckOrigin = false \\ refuse a WebSocket upgrade whose Origin is foreign (not the Host, not in wsOrigins) with 403 before the 101; no Origin header = allowed
  24. String wsOrigins = "" \\ extra allowed WebSocket origins, comma-separated ('https://app.example.com' or bare 'app.example.com')
  25. Boolean listening = false \\ true after a successful bind
  26. Number #srcId = 0
  27. handleError(err) {
  28. console.log("Server error: " + err)
  29. }
  30. \* Bind the configured port (idempotent — an extra listen() is harmless).
  31. A FAILED BIND IS A REFUSAL, NOT A LINE OF PROSE (mission 286). This used
  32. to print `listen() failed: …` on stderr and return, so the process stayed
  33. alive with no socket and — because the event loop was still running for
  34. whatever else had registered — exited 0 or never exited at all. Every
  35. supervisor, deploy script and CI step was told the server started. A
  36. server that cannot serve says so and fails; the located form is below. *\
  37. listen() {
  38. if (listening == true) {
  39. return null
  40. }
  41. if (this.bind() != true) {
  42. 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)")
  43. }
  44. return null
  45. }
  46. \* The bind itself, without the idempotence guard: a subclass that answers
  47. for its own double-listen policy (hl:web refuses one) still needs the one
  48. place where the socket is actually opened.
  49. ANSWERS true/false RATHER THAN RAISING (mission 286), because the caller
  50. owns the wording AND the location: hl:web can point its refusal at the
  51. manifest member that chose the port, which this file cannot see. Every
  52. caller must treat false as fatal — there is no third answer. *\
  53. bind() {
  54. srv = __native("http1.create_server", port, host, tls_cert, tls_key, threads, maxBody, wsCheckOrigin, wsOrigins)
  55. if (srv == null) {
  56. return false
  57. }
  58. listening = true
  59. #srcId = __native("eventloop.register", srv, "request")
  60. return true
  61. }
  62. \* Turn ON the RFC 6455 WebSocket engine for the port this server already
  63. bound: an Upgrade is answered inline in the I/O worker and
  64. the raw stream arrives at your own `on wsEvent` — WebSockets on the same
  65. port, no second listener. Separate from bind() because a plain HTTP
  66. server has no reason to start the engine thread. *\
  67. sockets() {
  68. __native("eventloop.register", __native("http1.ws_events"), "wsEvent")
  69. }
  70. \* Stop this server: the event source is retired (the loop can end by
  71. itself) and the native side joins the workers and closes the listener —
  72. the port is immediately free for a new bind(). Answers false when the
  73. server was not listening.
  74. SAFE FROM INSIDE A HANDLER (mission 260), and immediate — never deferred.
  75. The request you are answering is untouched: its socket is already your
  76. `req.respond` handle, so returning a Response after close() still reaches
  77. the client. A request that was parsed but can no longer be dispatched
  78. (the source is gone) is refused `503 Service Unavailable`; a connection
  79. accepted but not yet parsed is simply closed. *\
  80. close() {
  81. if (listening == false) {
  82. return false
  83. }
  84. listening = false
  85. __native("eventloop.unregister", #srcId)
  86. return __native("http1.close", port)
  87. }
  88. // Run body: the constructor's last act — bind the configured port.
  89. listen()

Branches

Latest commits

  • 494d75d4Nest caller slot styles in app-sidebarmre
  • e5571680Align all plugin sources with Hybriel day38bmre
  • 5d204c46Re-vendor layouts to Hybriel set day38bmre
  • 17e0f03eNest html-prefixed theme component overridesmre
  • e54e735eFormat plain theme elements with nested rules and bare mediamre
  • 547f834eRecord LY7 deployment and live verificationmre
  • fa4600b1Link caller-slot style exceptions to hybriel#270mre
  • 413b11e1Record corrected LY7 nesting, exact CSS and Chrome proofmre
  • b810453etracker-demo: nest child and compound selectors in Stylemre
  • 0b5b1d61notes-demo: nest child and compound selectors in Stylemre
  • ab22bdc2mail-demo: nest child and compound selectors in Stylemre
  • fe2d8de9home: nest child and compound selectors in Stylemre
  • e631791buser-menu: nest child and compound selectors in Stylemre
  • 69604944three-pane: nest child and compound selectors in Stylemre
  • 3e389b83main-menu: nest child and compound selectors in Stylemre
  • 05413975list-first: nest child and compound selectors in Stylemre
  • c545b642content-first: nest child and compound selectors in Stylemre
  • eae2be7aapp-sidebar: nest child selectors and document caller-slot gapmre
  • 69c9f784Record LY7 slot nesting blocker and minimal repromre
  • b1d4f379app-header: nest child and compound selectors in Stylemre