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

From: YC (yinso.chen at gmail.com)
Date: Fri Aug 28 16:03:41 EDT 2009

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))<http://docs.plt-scheme.org/web-server/http.html#%28def._%28%28lib._web-server/http/request-structs..ss%29._request%29%29>

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/20090828/04eb5ac0/attachment.html>

Posted on the users mailing list.