LM as webserver - ERR_INVALID_HTTP_RESPONSE - gjniewenhuijse - 20.11.2017
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>"
RE: LM as webserver - ERR_INVALID_HTTP_RESPONSE - admin - 20.11.2017
Have you considered using .lp scripts with the main web server, or is the URL fixed on clients?
RE: LM as webserver - ERR_INVALID_HTTP_RESPONSE - gjniewenhuijse - 20.11.2017
(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
RE: LM as webserver - ERR_INVALID_HTTP_RESPONSE - gjniewenhuijse - 21.11.2017
Can i change something to have working code?
RE: LM as webserver - ERR_INVALID_HTTP_RESPONSE - admin - 21.11.2017
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
RE: LM as webserver - ERR_INVALID_HTTP_RESPONSE - gjniewenhuijse - 21.11.2017
thanks
|