1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#lang racket
;; Single-line comment
#| Block comment on one line |#
#|Block comment.
(define x 3)
is not highlighted as the commented text.
|#
#|
Block comment.
(define x 3)
is not highlighted as the commented text.
|#
#| a |# 1 ; reads equal to 1
#| #| a |# 1 |# 2 ; reads equal to 2
#;1 2 ; reads equal to 2
#;(define x 1) (define x 2) ; reads equal to (define x 2)
#;(define x (f a b)) (define x 2) ; reads equal to (define x 2)
#!/bin/sh ; reads equal to nothing
#! /bin/sh ; reads equal to nothing
'symbol
`symbol
'(a quoted list)
`(a quasiquoted expr with ,unquasiquoted item)
#:keyword
-inf.f
+inf.f
-inf.0
+inf.0
-min.0
+max.0
-nan.0
+nan.0
(define (1-crazy-identifier-疯狂的标识符-τρελό-αναγνωριστικό x)
(add1 x))
(check-equal? (1-crazy-identifier-疯狂的标识符-τρελό-αναγνωριστικό 1) 2)
(require xml net/url
racket/control) ;; <<< new import
(define (serve port-no)
(define main-cust (make-custodian))
(parameterize ([current-custodian main-cust])
(define listener (tcp-listen port-no 5 #t))
(define (loop)
(accept-and-handle listener)
(loop))
(thread loop))
(λ ()
(custodian-shutdown-all main-cust)))
(define (accept-and-handle listener)
(define cust (make-custodian))
(custodian-limit-memory cust (* 50 1024 1024))
(parameterize ([current-custodian cust])
(define-values (in out) (tcp-accept listener))
(thread (λ ()
(handle in out)
(close-input-port in)
(close-output-port out))))
;; Watcher thread:
(thread (λ ()
(sleep 10)
(custodian-shutdown-all cust))))
(define (handle in out)
(define req
;; Match the first line to extract the request:
(regexp-match #rx"^GET (.+) HTTP/[0-9]+\\.[0-9]+"
(read-line in)))
(when req
;; Discard the rest of the header (up to blank line):
(regexp-match #rx"(\r\n|^)\r\n" in)
;; Dispatch:
(let ([xexpr (prompt (dispatch (list-ref req 1)))]) ;; <<< changed
;; Send reply:
(display "HTTP/1.0 200 Okay\r\n" out)
(display "Server: k\r\nContent-Type: text/html\r\n\r\n" out)
(display (xexpr->string xexpr) out))))
(define (dispatch str-path)
;; Parse the request as a URL:
(define url (string->url str-path))
;; Extract the path part:
(define path (map path/param-path (url-path url)))
;; Find a handler based on the path's first element:
(define h (hash-ref dispatch-table (car path) #f))
(if h
;; Call a handler:
(h (url-query url))
;; No handler found:
`(html (head (title "Error"))
(body
(font ((color "red"))
"Unknown page: "
,str-path)))))
(define dispatch-table (make-hash))
(hash-set! dispatch-table "hello"
(λ (query)
`(html (body "Hello, World!"))))
;; Highlight booleans
(define ht #true)
(define ht #t)
(define ht #T)
(define hf #false)
(define hf #f)
(define hf #F)
|