[racket] Problem with response/output

From: André Matheus (amatheus at mac.com)
Date: Fri Mar 6 18:48:35 EST 2015

Hi, starting a project of mine, I've setup a dispatch rule and a function to return the response.
To make things simple, I've used response/output, with a lambda writing to the output-port.
However, I've got the error:

response/output: contract violation
  expected: void?
  given: 11
  in: the range of
      the 1st argument of
      (->*
       ((-> output-port? void?))
       (#:code
        number?
        #:headers
        (listof header?)
        #:message
        bytes?
        #:mime-type
        (or/c bytes? #f)
        #:seconds
        number?)
       response?)
  contract from:
      <pkgs>/web-server-lib/web-server/http/response-structs.rkt
  blaming: /home/amatheus/Dropbox/focus/todagendas/teste.rkt
   (assuming the contract is correct)
  at: <pkgs>/web-server-lib/web-server/http/response-structs.rkt:41.2
  context...:
   /usr/share/racket/collects/racket/contract/private/blame.rkt:143:0: raise-blame-error16
   /usr/share/racket/pkgs/web-server-lib/web-server/http/response.rkt:115:12

So I've tried to use a response, with the same lambda, and it worked.
From the error message, I changed the lambda to return (void) and then it worked.

I think maybe the contract is wrong but frankly I don't understand much about contracts.

I've setup some code that exposes the problem. Navigating to "/working" and "/fixed" works
fine; navigating to "/not-working" exposes the problem.

#lang racket
(require web-server/dispatch
         web-server/servlet-env
         net/url
         web-server/http/request-structs
         web-server/http/response-structs)

(define (not-working req)
  (response/output (λ (op) (write-bytes #"Hello world" op))))

(define (working req)
  (response
   301 #"OK"
   (current-seconds) TEXT/HTML-MIME-TYPE
   empty
   (λ (op) (write-bytes #"Hello world" op))))

(define (fixed req)
  (response/output (λ (op) (write-bytes #"Hello world" op) (void))))

(define (url->request u)
    (make-request #"GET" (string->url u) empty
                  (delay empty) #f "1.2.3.4" 80 "4.3.2.1"))

(define-values (agenda-dispatch agenda-url)
  (dispatch-rules
   [("working") working]
   [("not-working")  not-working]
   [("fixed") fixed]))

(define (main)
  (serve/servlet agenda-dispatch
                 #:servlet-regexp #rx""
                 #:servlet-path ""))

(module+ main
  (main))

The contracts in response-structs.rkt:

(provide/contract
 [struct response
         ([code number?]
          [message bytes?]
          [seconds number?]
          [mime (or/c false/c bytes?)]
          [headers (listof header?)]
          [output (output-port? . -> . void)])]
 [response/full (-> number? bytes? number? (or/c false/c bytes?) (listof header?) (listof bytes?) response?)]
 [response/output (->* ((-> output-port? void?))
                       (#:code number?
                        #:message bytes?
                        #:seconds number?
                        #:mime-type (or/c bytes? #f)
                        #:headers (listof header?))
                       response?)]
 [TEXT/HTML-MIME-TYPE bytes?])

Is the contract wrong or am I doing something weird?

Thanks,

André
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150306/c961bda4/attachment.html>

Posted on the users mailing list.