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

From: keydana at gmx.de (keydana at gmx.de)
Date: Sat Aug 29 14:03:02 EDT 2009


thanks a lot for your advice! I tried read-bytes and it worked - I see  
the posted request, it is there!

So now it only remains for me to find out what I did wrong with regex,  
just for fun/curiosity/learning :-;

BTW just as an aside, I was also planning to use the web server as a  
last resort, just it was easier for me to extend the code from the  
More tutorial as I have worked through this not-too-long-ago and of  
course again, I was curious to experiment  with this basic & raw  
server implementation :-;

So thanks again, this saved me quite some hours!

Am 28.08.2009 um 22:03 schrieb YC:

> It seems the first thing to do is to isolate the problem you are  
> having.  A way to do so is to use either a known working server (so  
> you can test the client) or a known working client (so you can test  
> the server).
> If you use PLT web-server, you should be able to access the post  
> data with request-post-data/raw.  See http://docs.plt-scheme.org/web-server/http.html#(def._((lib._web-server/ 
> http/request-structs..ss)._request))
> It is unclear what you mean by you "cannot make it work for a POST"  
> - does it throw an error or hangs?
> post-pure-port returns only the data (without the headers), and your  
> usage appears correct below.
> Another way of finding out what's posted to the server is to read in  
> the whole request at once - you can use read-bytes and pass a large  
> enough number that you'll for sure the request will not exceed.
> Above are what I thought of immediately.  Cheers,
> yc
> On Fri, Aug 28, 2009 at 12:32 PM, keydana at gmx.de <keydana at gmx.de>  
> wrote:
> 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)
>                     #f)))
>            (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- 
> content))))
>              (display "HTTP/1.0 200 Okay\r\n" out)
>              (display "Server: k\r\nContent-Type: text/html\r\n\r\n"  
> out)
>              (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,
> Sigrid
>        _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090829/8f221f18/attachment.html>

Posted on the users mailing list.