lua-users home
lua-l archive

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



If I run the following lua web server, everything works fine except large strings are cut off around 17K. Can anyone see what I'm doing wrong, or could this be a problem with luasocket?

Steve

------------------------

local header =
[[HTTP/1.1 200 OK
Date: Fri, 19 Apr 2002 20:37:57 GMT
Server: Apache/1.3.23 (Darwin) mod_ssl/2.8.7 OpenSSL/0.9.6b
Cache-Control: max-age=60
Expires: Fri, 19 Apr 2002 20:38:57 GMT
Last-Modified: Tue, 16 Apr 2002 02:00:34 GMT
ETag: "3c57e-1e47-3cbb85c2"
Accept-Ranges: bytes
Content-Length: 7751
Connection: close
]]


WebServer =
{
 _port = 8080,
 _header = header,
}

function WebServer:run()
  self._server = bind("localhost", self._port)
  self._server:timeout(.01)
  self._clients = {}
  self._sendClients = {}
  print("WebServer running on port "..self._port)
  self:mainLoop()
end

function WebServer:lookForNewClients()
  local client = self._server:accept()
  if client then
    client:timeout(1)
    tinsert(self._clients, client)
  end
end

function WebServer:mainLoop()
  local clients = self._clients
  while 1 do
    self:lookForNewClients()

    local receivingClients, _, error = select(clients, nil, .01)
    if error and error ~= "timeout" then
      print("error = "..tostring(error))
    end

    for i, client in receivingClients do
      local data, error = client:receive()
      if error then
print("error: "..tostring(error).." on client "..tostring(client))
        tremove(clients, i)
      else
        local response = self._header..largeTestString()
        error, bytesSent = client:send(response)
        print("bytesSent = ".. bytesSent.." bytes = "..strlen(response))
        client:close()
        tremove(clients, i)
      end
    end
  end
end

function largeTestString()
  local s = "Content-Type: text/html\n\n"
  for i=1, 3000 do s=s..i.." " end
  return s
end

WebServer:run()