Created
November 9, 2021 22:23
-
-
Save samth/1d5a1e0a82b3f4e89fa3917c1bed54c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang rhombus | |
// Note: needs comment-out support in DrR | |
// Note: we should start building up this library now | |
import: | |
rhombus/macro: no_prefix | |
expr.rule '(loop: $body ...): | |
'(begin: | |
fun lp(): | |
$body ... | |
lp() | |
lp()) | |
expr.rule '(thread: $body ...): | |
'(r.thread(fun (): $body ...)) | |
import: | |
racket/tcp: | |
rename: | |
#{tcp-listen} ~to listen | |
#{tcp-accept} ~to accept | |
#{tcp-close} ~to close | |
racket/base: | |
prefix r | |
rename: | |
#{regexp-match} ~to regexp_match | |
#{close-input-port} ~to close_input_port | |
#{close-output-port} ~to close_output_port | |
fun go(): | |
'yep_it_works | |
fun serve(port :: Number): | |
val listener: tcp.listen(port, 5, #true) | |
val t: | |
thread: | |
loop: | |
accept_and_handle(listener) | |
fun(): r.#{kill-thread}(t) // want t.kill() | |
tcp.close(listener) | |
// what I want to write: | |
fun accept_and_handle_(listener): | |
val (in,out): listener.accept() | |
handle(in,out) | |
in.close(); out.close() | |
fun accept_and_handle(listener): | |
val (in,out): tcp.accept(listener) | |
thread: | |
handle(in,out) | |
r.close_input_port(in) | |
r.close_output_port(out) | |
fun handle(in,out): | |
// Discard the request header (up to blank line): | |
r.regexp_match(#{#rx"(\r\n|^)\r\n"}, in) | |
// Send reply | |
r.display("HTTP/1.0 200 Okay\r\n",out) | |
r.display("Server: k\r\nContent-Type: text/html\r\n\r\n",out) | |
r.display("<html><body>Hello, world!</body></html>", out) | |
// want to write: | |
// in.match(#{#rx"(\r\n|^)\r\n"}) | |
// out.display(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment