lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Feb 8, 2011, at 9:45 AM, Michal Kottman wrote:

>>> While we are at it, here are my very own 500 lines worth of dinky HTTP server... and it doesn't even need LuaSocket :P
>> 
>> Man, that is such cool code that I don't understand how it actually
>> works ;)  In particular, how does it do the basic networking layer?
> 
> Reading the source, it looks like it uses by default io.stdin and
> io.stdout, or anything that supports the :read() and :write() methods
> (i.e. sockets). 

Right, HTTP.lua works in term of a subset of the file io API [1][2] so it doesn't concern itself with networking per se. Just with the HTTP protocol itself.

So it can be run directly from the terminal, avoiding the entire network stack altogether. Handy for development/debugging. 

Or over a tcp wrapper such as D.J. Bernstein's tcpserver [3], leaving network management to these who know best :P

For example, this is how the online demo of Nanoki [4] runs:

exec bin/tcpserver -c 100 -qRHlOD 0 3388 bin/timelimit 60 bin/lua Nanoki.lua .

And of course, it can use LuaSocket as well [5] :)

For example:

HTTP[ '/' ] = function() return 'Hello world' end TCPServer( '127.0.0.1', 1080 )( HTTP )

The embedded server can be used directly, or in conjunction with something like nginx [6] to provide a pool of long running application instances.

> Does that mean that it can work cleanly with FCGI as a "sub-server"?

Perhaps. Never tried :)


[1] http://www.lua.org/manual/5.1/manual.html#pdf-file:read
[2] http://www.lua.org/manual/5.1/manual.html#pdf-file:write
[3] http://cr.yp.to/ucspi-tcp/tcpserver.html
[4] http://svr225.stepx.com:3388/nanoki
[5] http://dev.alt.textdrive.com/browser/HTTP/TCPServer.lua
[6] http://wiki.nginx.org/Main