[racket] Help needed in writing Macro to transform (lambda (req) ....) to be used in web-server/servlet using auth cookies

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sat Jun 25 01:20:56 EDT 2011

On 06/24/2011 10:43 PM, J G Cho wrote:
> Hello again,
>
> I am guessing my problem calls for macro (which is "beyond my pay
> scale") and I am hoping this is the right place.
>
> Anyway, after reading this
> http://docs.racket-lang.org/web-server/faq.html#(part._.What_special_considerations_are_there_for_security_with_the_.Web_.Server_)
>
> I am led to believe that I will be writing lots of code like this:
>
> (define (some-sensitive-content req)
>
>     (if (user-is-legit req) ;check auth cookie
>        (...what have you ...)
>        (do-login-and-then-maybe-handle req)))
>
> [...]
>
>
> What I would really like, however, is
>
> (guard (lambda (req) ...)) to transformed to:
>
> (lambda (req)
>     (if (user-is-legit req)
>        (...what have you ...)
>        (do-login-and-then-maybe-handle  req)))


I don't think you need any macros. Just make 'guard' a higher-order 
function, like this:

;; guard : (request -> any) -> (request -> any)
;; Wraps a request-handler with an authorization check.
(define (guard proc)
   (lambda (req)
     (if (user-is-legit? req)
         (proc req)
         (do-login-and-then-maybe-handle proc req))))

You can even concentrate control of the wrapping into the 
'send/suspend/...' function instead of having to wrap every handler 
yourself:

;; send/suspend/dispatch/guard
;;    : ((request -> any) -> string) -> any
(define (send/suspend/dispatch/guard make-page)
   (send/suspend/dispatch
    (lambda (embed/url)
      (make-page (lambda (proc) (embed/url (guard proc)))))))

(Warning: neither tested nor proven correct.)

Ryan


Posted on the users mailing list.