Awesome! That helped a lot. Thanks!<br><br><div class="gmail_quote">On Wed, Mar 21, 2012 at 6:28 PM, Jay McCarthy <span dir="ltr"><<a href="mailto:jay.mccarthy@gmail.com">jay.mccarthy@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Since you did not tell us what error you had, I assume the error was:<br>
<br>
serve/servlet: contract violation, expected: can-be-response?, given: #<promise><br>
<br>
When I tried your code, I also had the error:<br>
<br>
current-directory: `exists' access denied for /home/jay/Downloads/<br>
<br>
because you need to give the sandbox permission to read, like:<br>
<br>
#:allow-read (list "/")<br>
<br>
But I assume that you know that error doesn't have to do with the Web server.<br>
<br>
Returning to the serve/servlet error...<br>
<br>
If you change your 'start' to:<br>
<br>
(define (start request)<br>
(define prm ((lazy-eval 'lazy-handler) request))<br>
(printf "~a ~a\n" prm (promise? prm))<br>
(define ans (force prm))<br>
(printf "~a\n" ans (promise-forced? prm))<br>
ans)<br>
<br>
You will see the output:<br>
<br>
#<promise> #f<br>
Servlet (@ /main) exception:<br>
promise-forced?: expected argument of type <promise>; given: #<promise><br>
<br>
This indicates the thing returned by your handler is not a promise and<br>
therefore cannot be forced.<br>
<br>
This is called structure generativity: every instantiation of a module<br>
has different structures, and sandboxes lead to multiple<br>
instantiations of the same module---racket/promise in this case. Thus,<br>
different 'promise' data structures.<br>
<br>
Fixing this is what 'sandbox-namespace-specs' is for.<br>
<br>
If you wrap your call to 'make-evaluator' in:<br>
<br>
(parameterize ([sandbox-namespace-specs<br>
(list sandbox-make-namespace<br>
'racket/promise<br>
'web-server/http)])<br>
.....)<br>
<br>
Then it will work. This shares the modules 'racket/promise' (to get<br>
the right promise data structure) and 'web-server/http' (to get the<br>
right request and response structures) between the evaluator and the<br>
host Racket program.<br>
<br>
In case you don't realize, using an evaluator is totally unnecessary.<br>
You can just write another module in the lazy language and require it<br>
in the strict program and call force normally. For example:<br>
<br>
#lang racket/load<br>
<br>
(module lazy-handler lazy<br>
(require web-server/http/bindings<br>
web-server/http/response-structs<br>
web-server/http/xexpr)<br>
<div class="im"> (define (lazy-handler request)<br>
(let ((bindings (request-bindings request)))<br>
(if (exists-binding? 'hi bindings)<br>
(response/xexpr "Hi!")<br>
</div> (response/xexpr ""))))<br>
(provide lazy-handler))<br>
<br>
(module the-server racket<br>
(require web-server/servlet-env<br>
web-server/http/bindings<br>
web-server/http/response-structs<br>
web-server/http/xexpr<br>
'lazy-handler)<br>
<br>
(define (start request)<br>
(define prm (lazy-handler request))<br>
(printf "~a ~a\n" prm (promise? prm))<br>
(define ans (force prm))<br>
(printf "~a ~a\n" ans (promise-forced? prm))<br>
ans)<br>
<br>
(serve/servlet start<br>
#:launch-browser? #t<br>
<div class="im"> #:quit? #f<br>
#:listen-ip #f<br>
</div> #:servlet-path "/main"<br>
<div class="im"> #:port 8080<br>
#:servlet-regexp #rx"main.*"<br>
#:extra-files-paths<br>
</div> (list (build-path (current-directory)))))<br>
<br>
(require 'the-server)<br>
<br>
Hope this helps,<br>
<br>
Jay<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
On Wed, Mar 21, 2012 at 11:44 AM, Nathan Breit <<a href="mailto:nabreit@gmail.com">nabreit@gmail.com</a>> wrote:<br>
> I'm trying to write a servlet that plugs into the existing server. Here's an<br>
> example:<br>
><br>
> #lang racket<br>
> (require web-server/servlet-env)<br>
> (require web-server/http/bindings)<br>
> (require web-server/http/response-structs)<br>
> (require web-server/http/xexpr)<br>
><br>
> (require racket/sandbox)<br>
> (define lazy-eval (make-evaluator 'lazy))<br>
><br>
> (map lazy-eval<br>
> '(<br>
> (require web-server/http/bindings)<br>
> (require web-server/http/response-structs)<br>
> (require web-server/http/xexpr)<br>
> (require racket/promise)<br>
> (define (lazy-handler request)<br>
> (let ((bindings (request-bindings request)))<br>
> (if (exists-binding? 'hi bindings)<br>
> (response/xexpr "Hi!")<br>
> "")))<br>
> ))<br>
><br>
> (define (start request)<br>
> (force ((lazy-eval 'lazy-handler)<br>
> request)))<br>
><br>
> (serve/servlet start<br>
> #:launch-browser? #f<br>
> #:quit? #f<br>
> #:listen-ip #f<br>
> #:servlet-path ""<br>
> #:port 8080<br>
> #:servlet-regexp #rx"main.*"<br>
> #:extra-files-paths<br>
> (list (build-path (current-directory) "extraFiles")))<br>
><br>
> On Wed, Mar 21, 2012 at 6:22 AM, Matthias Felleisen <<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>><br>
> wrote:<br>
>><br>
>><br>
>><br>
>> On Mar 21, 2012, at 5:18 AM, Nathan Breit wrote:<br>
>><br>
>> > Hi,<br>
>> > I'm trying to implement a Racket web-server handler that does lazy<br>
>> > evaluation. My approach so far as been to try making a evaluator/namespace<br>
>> > that uses the lazy racket, then evaluate a function in it that returns a<br>
>> > lazy request handler. However, I'm running into problems getting the handler<br>
>> > to read the request object. My last resort will be to make a request<br>
>> > serializer and pass serialized requests into the handler, but is there a<br>
>> > better way?<br>
>> > Thanks,<br>
>> > -Nathan<br>
>> > ____________________<br>
>> > Racket Users list:<br>
>> > <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
>><br>
>><br>
>><br>
>> Are you trying to write a web server in Lazy or are you trying to write a<br>
>> servlet in Lazy and plug it into the existing strict server? Perhaps you<br>
>> want to post a code snippet that shows where things fail. -- Matthias<br>
>><br>
><br>
><br>
> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
<br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Jay McCarthy <<a href="mailto:jay@cs.byu.edu">jay@cs.byu.edu</a>><br>
Assistant Professor / Brigham Young University<br>
<a href="http://faculty.cs.byu.edu/~jay" target="_blank">http://faculty.cs.byu.edu/~jay</a><br>
<br>
"The glory of God is Intelligence" - D&C 93<br>
</font></span></blockquote></div><br>