[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to read http PUT or POST data from luasocket server
- From: Francisco Olarte <folarte@...>
- Date: Tue, 1 Oct 2013 13:54:08 +0200
Hi Benjamin
> I've implemented a ReST server based on the listener.lua sample for the
> luasocket library. Everything works fine as long as I only handle GET
> requests. When I do
> l, e = c:receive()
> I get something like
> <PUT|POST> <url> HTTP1.0
> as the response but not the pushed or posted data.
HTTP is a little more complex than just receiving, as you can assert
by skimmin through, say, RFC 2616. The request consist of the method
line, plus some header lines, plus an empty line to signal end of
headers, plus an optional comment.
If you do a receive with a simple socket you are possibly getting only
the first line of the request, which is APARENTLY fine for GET, as
most of the info you need is there, but not with put or post.
> Has anyone experience with this kind of requests? I didn't found
> something in the official docs.
I think this library has HTTP modules. Use one of them. Anyway I will
recomend, as education, to install any server and point your browser /
web client to it and dump the traffic. If you have access to netcat a
very easy way to do it is 'nc -l localhost 3456' and point your client
to it. If I do this in my machine and instruct firefox to go to
'http://localhost:3456/' I get:
>>>>>
GET / HTTP/1.1
Host: localhost:3456
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0)
Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
<<<< ( note empty line at end ).
And firefox hangs, until I killed netcat in which moment it dutifully
displayed an error page.
If instead of this I do a post using
curl -i -X POST -d "value":"30" http://localhost:3456/
I get:
>>>>>>>
POST / HTTP/1.1
User-Agent: curl/7.29.0
Host: localhost:3456
Accept: */*
Content-Length: 8
Content-Type: application/x-www-form-urlencoded
value:30
<<<<<<<<<<<<<<< NO NEWLINE AT END!
Note the blank line signallinig end of headers and the 8 bytes payload
( sized by the content-length header ).
As I say before, not so simple, you'll be better of searching for some
server sample as reference, as completely parsing an HTTP request is
not athing you can properly do in a couple of pages of code.
Regards.
Francisco Olarte.