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
#1
I have an "old" piece of code that generates a html response for clients that connects to my lm:
http://192.168.x.x:19001/getdata/test

Results:
Chrome : ERR_INVALID_HTTP_RESPONSE
IOS Safari : cancelling load from .... because it is using HTTP/0.9
Internet explorer and Edge works great.

Anyone a solution?


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

 -- 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
     sock:send(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

-- 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>"
Reply
#2
Have you considered using .lp scripts with the main web server, or is the URL fixed on clients?
Reply
#3
(20.11.2017, 10:47)admin Wrote: Have you considered using .lp scripts with the main web server, or is the URL fixed on clients?

Its a fixed url
Reply
#4
Can i change something to have working code?
Reply
#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
#6
thanks
Reply


Forum Jump: