[racket] I did not expect this code to work but it did. So now I am a bit puzzled by how dispatch-rule work with K-URL.

From: J G Cho (gcho at fundingmatters.com)
Date: Sat Apr 23 11:32:28 EDT 2011

At the bottom of this email is a code that I did not think would work but it
did.  (The code is a combination of get-number and count-dot-com
tutorial. I first ask the user to type in a number. If it's 42, then
count-dot-com is evaluated. If not 42, it keeps asking for a number.
(To run it, you might have to change the
path to htdocs.))

Steps:

1) URL /secret presents a form.
2) The above step does recur until 42 is submitted.
3) Then it evaluates the code from count-dot-com example with 42 as
initial value.
4) + and - works as in the solo tutorial: At this point, URL looks
something like
http://localhost:8080/secret;((%22k%22%20.%20%22(3%20105%2097509303)%22))

At step 4, I expected the dispatch rule to intercept it and go into get-number
routine (i.e. Step 1) but it does not.

QUESTION: Is the dispatch rule called and it's smart enough to
hand it off?  Or because of K-URL, dispatch rule is bypassed entirely
by the server?

Thanks in advance.

jGc

The code:

#lang racket

(require web-server/servlet
        web-server/servlet-env
        web-server/dispatch)

(define-values (handle-if-url-match hanlder->url)
 (dispatch-rules
  [("") say-hello]
  [("secret") (make-guarded-dispatcher reveal-secret)]))

(define (say-hello req)
 (response/xexpr
  `(html (head (title "Hello world!"))
         (body (p "Hey out there!")))))

(define (reveal-secret req)
 (count-dot-com 42))

(define (count-dot-com i)
 (count-dot-com
 (send/suspend/dispatch
  (λ (embed/url)
    (response/xexpr
     `(html
       (head (title "Count!"))
       (body (h2 (a ([href ,(embed/url
                             (λ (req)
                               (sub1 i)))])
                    "-")
                 (form ([action ,(embed/url
                             (λ (req)
                               (count-dot-com (sub1 i))))]
                      [method "POST"])
                     (input ([type "submit"]
                             [value "-"]))))
             (h1 ,(number->string i))
             (h2 (a ([href ,(embed/url
                             (λ (req)
                               (add1 i)))])
                    "+")
                 (form ([action ,(embed/url
                             (λ (req)
                               (count-dot-com (add1 i))))]
                      [method "POST"])
                     (input ([type "submit"]
                             [value "+"])))))))))))


(define (make-guarded-dispatcher next-dispatcher)
 (define (guarded-dispatcher req)
   (define (get-number label)
     (define query
       (send/suspend

        (λ (k-url)
          (response/xexpr
           `(html (head (title "Enter a number"))
                  (body
                   (form ([action ,k-url])
                         ,label
                         (input ([name "number"]))
                         (input ([type "submit"])))))))))

     (string->number (extract-binding/single 'number
(request-bindings query))))
   (define n (get-number "secret number"))
   (if (= 42 n)
       (next-dispatcher req)
       (guarded-dispatcher req)))

 guarded-dispatcher)


(define (start request)
 (handle-if-url-match request))

(serve/servlet start
              #:command-line? #t
              #:launch-browser? #t
              #:quit? #t
              #:listen-ip #f
              #:port 8080
              #:log-file "log"
              #:extra-files-paths (list (build-path
"/Users/..../addhoc" "htdocs"))
              #:servlet-regexp #rx"")



Posted on the users mailing list.