gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit94db01c594db01c5Merge branch 'verdict-guard' into mainKunibert94db01c5/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

  • 94db01c5Merge branch 'verdict-guard' into mainKunibert
  • 8d3fb2e7verdict guard: unknown severity blocks; L2mre
  • 9cb7994fverdict guard: severity of older off-diff findings from review bodies; tolerant read; L2mre
  • fa2ba67c|||PR verdict guard: an open Blocker/Major thread holds request_changesmre
  • 9e89de3cSTATUS 11-24mre
  • 841ce528LOG: gitoria PR 25 round 2mre
  • 3c6c05feLOG: gitoria PR 25 reviewmre
  • 5a5c1accLOG: hycon PR 382 round 3mre
  • 0ab030b9LOG: gitoria PR 24 approvedmre
  • 0e7be144LOG: gitoria PR 24 round 4mre
  • 58075360LOG: hycon PR 382 clarificationmre
  • b5c8d9c5LOG: gitoria PR 24 round 3mre
  • d9c9857bLOG: gitoria PR 24 re-reviewmre
  • d678c1e3LOG: gitoria PR 24 reviewmre
  • bd3591c7conductor: gates run on the live day44 binarymre
  • 47f747a7records: jukebox #9 fix livemre
  • 0a3169fdMerge branch 'vendored-size' into mainKunibert
  • e447bb09Vendored size fix: keep renames with an app side whole; type the filter parameter (L10)mre
  • 99ccc742Vendored size fix: no renames in the app diff, filter the resumed delta toomre
  • 1a52e100|||PR Diff size limit counts only app files when vendored paths are setmre