def process_client(app)
      loop do
        client = nil
        begin
          make_interruptable do
            client = @listener.synchronize do
              begin
                @listener && @listener.accept()
              rescue => e
                return # this means we've been turned off, so exit the loop.
              end
            end
            if (!client)
              return # nil return means we're quitting, exit loop.
            end
          end
          
          req = Http::Parser.new()
          buf = ""
          chunked_read(client, @timeout) do |data|
            begin
              buf << data
              req.parse!(buf)
              if (req.done?)
                process_request(client, req, app)
                req = Http::Parser.new()
                if (@listener.closed?)
                  return # ignore any more requests from this client if we're shutting down.
                end
              end
            rescue Http::ParserError => e
              err_str = "<h2>#{e.code} #{e.message}</h2>"
              client.write("HTTP/1.1 #{e.code} #{e.message}\r\n")
              client.write("Connection: close\r\n")
              client.write("Content-Length: #{err_str.length}\r\n")
              client.write("Content-Type: text/html\r\n")
              client.write("\r\n")
              client.write(err_str)
              client.close_write            
            end
          end
        rescue Errno::EPIPE
          # do nothing, just let the connection close.
        rescue SystemExit, GracefulExit
          raise # pass it on.  
        rescue Object => e
          $stderr.puts("Unhandled error #{e}:")
          e.backtrace.each do |line|
            $stderr.puts(line)
          end
        ensure
          client.close if (client && !client.closed?)
        end
      end
    end
def process_client(app)
      loop do
        client = nil
        begin
          make_interruptable do
            client = @listener.synchronize do
              begin
                @listener && @listener.accept()
              rescue => e
                return # this means we've been turned off, so exit the loop.
              end
            end
            if (!client)
              return # nil return means we're quitting, exit loop.
            end
          end
          
          req = Http::Parser.new()
          buf = ""
          chunked_read(client, @timeout) do |data|
            begin
              buf << data
              req.parse!(buf)
              if (req.done?)
                process_request(client, req, app)
                req = Http::Parser.new()
                if (@listener.closed?)
                  return # ignore any more requests from this client if we're shutting down.
                end
              end
            rescue Http::ParserError => e
              err_str = "<h2>#{e.code} #{e.message}</h2>"
              client.write("HTTP/1.1 #{e.code} #{e.message}\r\n")
              client.write("Connection: close\r\n")
              client.write("Content-Length: #{err_str.length}\r\n")
              client.write("Content-Type: text/html\r\n")
              client.write("\r\n")
              client.write(err_str)
              client.close_write            
            end
          end
        rescue Errno::EPIPE
          # do nothing, just let the connection close.
        rescue SystemExit, GracefulExit
          raise # pass it on.  
        rescue Object => e
          $stderr.puts("Unhandled error #{e}:")
          e.backtrace.each do |line|
            $stderr.puts(line)
          end
        ensure
          client.close if (client && !client.closed?)
        end
      end
    end
Accepts a connection from a client and handles requests on it until
 the connection closes.