This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

LM as webserver - ERR_INVALID_HTTP_RESPONSE
#5
This should work. Another thing is that you probably don't need local UDP server here. If you want to send some dynamic data you can use storage.

Code:
-- init server handler
if not ready then
  require('copas')

  -- all dynamic pages
  htmlbody =
  "<html>\n"..
  "\n"..
  "<head>\n"..
  "<title>Web Communication</title>\n"..
  "</head>\n"..
  "\n"..
  "<body>\n"..
  "Web Communication \n"..
  "<h3>Test</h3>\n"..
  "<pre style='white-space:normal;'>"..
  "</pre>"..
  "<br>"..
  "</body>\n"..
  "\n"..
  "</html>"

  function httpsend(sock, body)
    local resp = 'HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-type: text/html\r\nConnection: close\r\n\r\n%s'
    local data = string.format(resp, #body, body)
    return sock:send(data)
  end

  -- list of client sockets
  clients = {}

  -- incoming data handler
  function datahandler(sock, data)
    local ip, port
    ip, port = sock:getpeername()

    -- send reply
    if string.find(data,"GET /getdata/test") then
      httpsend(sock, htmlbody)
    --else
        --log(string.format('[server] data from %s:%d - %s', ip, port, data))
    end
  end

  -- connection handler
  function connhandler(sock)
    -- enable keep-alive to check for disconnect events
    --sock:setoption('keepalive', false)

    local ip, port, data, err, id

    -- get ip and port from socket
    ip, port = sock:getpeername()

    -- client id
    id = string.format('%s:%d', ip, port)
    --log(string.format('[server] connection from %s', id))

    -- save socket reference
    clients[ id ] = sock

    -- main reader loop
    while true do
      -- wait for single line of data (until \n, \r is ignored)
      data, err = copas.receive(sock, '*l')
      -- error while receiving
      if err then
        --log(string.format('[server] closed connection from %s:%d', ip, port))
        -- remove socket reference
        clients[ id ] = nil
        return
      end
      -- handle data frame
      datahandler(sock, data)
      if data == "" then
        --log("stop")
        break
      end
    end
  end

  -- bind to port 10001
  tcpserver = socket.bind('*', 19001)

  -- error while binding, try again later
  if not tcpserver then
    os.sleep(5)
    log('[server] error: cannot bind')
  end

  -- set server connection handler
  copas.addserver(tcpserver, connhandler)

  -- create udp server on port 10002
  udpserver = socket.udp()
  udpserver:setsockname('*', 19002)
  udpserver:settimeout(0.1)

  ready = true
end

-- perform server tasks for one second (5 x (0.1 + 0.1))
for i = 1, 5 do
  message = udpserver:receive()

  -- got message from udp, send to all active clients
  if message then
    --log("message: " .. message .. " : " .. chartobyte(message))
    for id, sock in pairs(clients) do
      --log("client id: " .. id)
      sock:send(message .. '\r')
    end
  end

  copas.step(0.1)
end
Reply


Messages In This Thread
RE: LM as webserver - ERR_INVALID_HTTP_RESPONSE - by admin - 21.11.2017, 07:29

Forum Jump: