[plt-scheme] Problems using post-pure-port and reading posted request

From: keydana at gmx.de (keydana at gmx.de)
Date: Fri Aug 28 15:32:47 EDT 2009

Hi all,

I'd be glad for some help with a problem I have reading in a raw POST  
request and/or the usage of post-pure-port.
I'm developing a small web service client, and I'm faking the  
corresponding server using the basic server from the More tutorial and  
adapting it just enough to send the required responses.

While this works fine for a GET request, I cannot make it work for a  
POST, and unfortunately I even do not know whether the problem is on  
the server or the client side...

 From the client, I use

(define store-events-to-ws
   (lambda (xexpr)
     (let ((payload (string->bytes/utf-8 (xexpr->string xexpr)))  
(request (format "http://~a:~a/calendar/events" *HOSTNAME* *PORT*)))
       (let ((response (post-pure-port (string->url request) payload)))
         (read-line response)))))

In the server, when it's a POST request, I try to parse the content,  
but I don't get at anything. With a regex like the one below, I get 2  
response headers and 2 empty strings:

POST content is: (#"Host: localhost:6666\r\nContent-Length: 517\r\n\r 
\n" #"Host: localhost:6666" #"Content-Length: 517" #"" #"")

When I try to make the regex longer, hoping for some POSTED content,  
the process hangs.

(define handle-request
   (lambda (in out)
      (let ((req (regexp-match #rx"^(.*) (.+) HTTP/[0-9]+\\. 
[0-9]+" (read-line in))))
       (when req
         (let ((method (second req)) (path (third req)))
           (let ((posted-content
                  (if (equal? method "POST")
                       (regexp-match (byte-regexp #"(.*?)\r\n(.*?)\r 
\n(.*?)\r\n(.*?)") in)
             (printf "POST content is: ~s~n" posted-content)
             (printf "Requested path is: ~s~n" path)
             (printf "Method is: ~s~n" method)
             (let ((xexpr (prompt (dispatch path method posted- 
               (display "HTTP/1.0 200 Okay\r\n" out)
               (display "Server: k\r\nContent-Type: text/html\r\n\r\n"  
               (display (xexpr->string xexpr) out)
               (printf "Request answered~n"))))))))

Now I am a bit clueless how to investigate the problem. Perhaps I am  
making mistakes with the regex, or the handling of the input port. Or  
perhaps the POST did not work and there was really nothing...

I'd be grateful for any advice in this. Of course I can at any time  
send more chunks of the code.

Best greetings,

Posted on the users mailing list.