Example Code using lua-httpd

Here are some simple samples of code demonstrating how the library may be used. For more details on the specific functions available and their accepted parameters please see the API documentation.

The library includes a couple of small samples of its own which have better error handling, and more complete functionality. However these samples are simpler for demonstration purposes.

Simple Server

The following code implements a simple single-threaded "echo server", which will accept connections upon a port and merely echo back everything it recieves to the connected client.

#!/usr/bin/lua50

-- Load the library
socket = require( "libhttpd" );

-- Start listening upon a socket
listener = socket.bind( 9999 );

-- Show instructions!
print( "Echo server running on port 9999" );

-- Loop waiting for connections
while true do

   -- Accept a new connection.
   client,ip = socket.accept( listener );

   -- Read from the client.
   length, data = socket.read(client);
   
   while( length > 0 ) do 
       -- Echo data back to client.
       socket.write( client, data );

       length, data = socket.read( client );
   end

   -- Now close the socket.
   socket.close( client );

end

(You may download example-server.lua.)

Simple Client

The following implements a simple client, connecting to http://localhost/ and retrieving the header and contents of a page request.

  -- Load the library
  socket = require( "libhttpd" );

  -- Make a connection to http://localhost/
  sock = socket.connect( "localhost", 80 );

  -- Send the request.
  socket.write( sock, "GET / HTTP/1.0\n\n" );

  -- Read the response and print it
  repeat
     len,line = socket.read( sock )
     print( line );
  until len <= 0

  -- Close up
  socket.close( sock );

(You may download example-client.lua.)

Working With Files

The following example prints out a recursive tree of your current directory:

#!/usr/bin/lua50

-- Load the library
socket = require( "libhttpd" );




function showPath( dir, level )
    --  Read directory entries.
    local  entries = socket.readdir( dir );

    -- For each entry
    for i=0,table.getn(entries) do
        -- The sub-entry
        item = entries[i];

        -- If it is a file then show it.
        if ( socket.is_file(  dir .. "/" .. item ) ) then
             -- Print filename with indentation
             ind   = level;
	     txt = "";
             while( ind > 0 ) do 
                 txt = txt .. "  " ; 
                 ind = ind - 1;
             end
             print( txt .. item );
        end
 
    end


    -- Now do the same for subdirectories
   for i=0,table.getn(entries) do
       -- The sub-entry
       item = entries[i];

       -- Make sure we have a valid directory which isn't '.', or '..'.
       if ( ( item ~= nil ) and  ( item ~= "." ) and ( item ~= ".." ) ) then
           if ( socket.is_dir( dir .. "/" .. item ) ) then
               -- Print it out.
               print ( dir .. "/" .. item .. "/" );
               -- Now recurse:
 	       showPath( dir .. "/" .. item, (level + 1) );
           end
       end
   end
end

-- Now show the current directory recursively
showPath( ".", 1 );

(You may download example-tree.lua.)

Sample usage looks like this:

skx@lappy:~/cvs/lua-httpd$ lua ./docs/example-tree.lua  | head
  Makefile
  README
  client.lua
  libhttpd.c
  default.lua
  .cvsignore
  httpd.lua
./CVS/
    Root
    Repository