Feature/web socket (#180)
* Websockets * Sending payloads * Lint fix * Remove example code * Disable web sockets * remove todo --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
d87337d389
commit
1cee5d61ab
11 files changed
+419
-5
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import "../WebSockets/WebSocketServer.bs"
|
||||
import "HttpConnection.bs"
|
||||
import "HttpConnectionState.bs"
|
||||
import "HttpRouter.bs"
|
||||
import "HttpSettings.bs"
|
||||
import "HttpUtils.bs"
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
|
||||
namespace Http
|
||||
|
||||
@@ -23,6 +25,7 @@ namespace Http
|
||||
public success as boolean
|
||||
public settings as Http.HttpSettings
|
||||
public task as object
|
||||
public websocket as Web.WebSocketServer
|
||||
|
||||
function new(settings as HttpSettings, task as object)
|
||||
m.settings = settings
|
||||
@@ -71,6 +74,11 @@ namespace Http
|
||||
m.sockin = sockin
|
||||
|
||||
LogInfo("listening on " + addrin.getAddress())
|
||||
|
||||
if m.settings.WebSocketsEnabled
|
||||
m.websocket = new Web.WebSocketServer(m)
|
||||
LogInfo("WebSocketServer created")
|
||||
end if
|
||||
return success
|
||||
end function
|
||||
|
||||
@@ -101,6 +109,10 @@ namespace Http
|
||||
LogError("invalid state")
|
||||
end if
|
||||
end for
|
||||
|
||||
if m.settings.WebSocketsEnabled
|
||||
m.websocket.Poll()
|
||||
end if
|
||||
end function
|
||||
|
||||
function PostWait()
|
||||
@@ -136,7 +148,7 @@ namespace Http
|
||||
if conn.close
|
||||
conn.socket.close()
|
||||
connections.delete(id)
|
||||
else if conn.ws_upgrade and m.websocket <> invalid
|
||||
else if conn.ws_upgrade and m.settings.WebSocketsEnabled
|
||||
LogInfo("upgrading connection to web socket")
|
||||
m.websocket.AddConnection(conn.socket)
|
||||
connections.delete(id)
|
||||
@@ -170,7 +182,7 @@ namespace Http
|
||||
conn = connections[id]
|
||||
conn.socket.close()
|
||||
end for
|
||||
if m.websocket <> invalid
|
||||
if m.settings.WebSocketsEnabled
|
||||
m.websocket.Close()
|
||||
end if
|
||||
' final stats
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Http
|
||||
public Port as integer = 8888
|
||||
public ServerName = "Roku Server"
|
||||
public TimeoutInMs = 60 * 1000
|
||||
public WebSocketsEnabled = true
|
||||
|
||||
public MessagePort = invalid
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ function WebServerLoop() as void
|
||||
settings.Port = m.top.port
|
||||
settings.ServerName = m.top.serverName
|
||||
settings.TimeoutInMs = m.top.timeoutInMs
|
||||
settings.WebSocketsEnabled = m.top.webSocketsEnabled
|
||||
|
||||
server = new Http.HttpServer(settings, m.top)
|
||||
if not server.success
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
<function name="StartServer" />
|
||||
<function name="StopServer" />
|
||||
|
||||
<!-- Web sockets disabled until it is implemented in the web app -->
|
||||
<field id="webSocketsEnabled" type="boolean" value="false" />
|
||||
<field id="webSocketBroadcast" type="string" alwaysNotify="true" />
|
||||
|
||||
<field id="shouldQuit" type="boolean" alwaysNotify="true" />
|
||||
</interface>
|
||||
<children>
|
||||
|
||||
</children>
|
||||
</component>
|
||||
@@ -0,0 +1,83 @@
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "WebSocketConnectionState.bs"
|
||||
import "WebSocketFrame.bs"
|
||||
|
||||
namespace Web
|
||||
|
||||
const BUFSIZE = 524288
|
||||
|
||||
class WebSocketConnection
|
||||
private server
|
||||
private socket
|
||||
private frame
|
||||
public id = invalid
|
||||
public state = invalid
|
||||
|
||||
function new(server as WebSocketServer, socket as object)
|
||||
m.server = server
|
||||
m.socket = socket
|
||||
m.socket.SetKeepAlive(true)
|
||||
m.socket.SetNoDelay(true)
|
||||
m.id = Stri(m.socket.getID()).trim()
|
||||
|
||||
m.buffer = CreateObject("roByteArray")
|
||||
m.buffer[Web.BUFSIZE] = 0
|
||||
|
||||
m.frame = new Web.WebSocketFrame()
|
||||
|
||||
m.state = Web.WebSocketConnectionState.OPEN
|
||||
end function
|
||||
|
||||
function Poll() as void
|
||||
if not m.socket.eOK()
|
||||
m.Close()
|
||||
return
|
||||
end if
|
||||
received = m.socket.receive(m.buffer, 0, Web.BUFSIZE)
|
||||
if received = 0
|
||||
m.Close()
|
||||
return
|
||||
else if received > 0
|
||||
if m.frame.ReadFromBuffer(m.buffer, 0, received)
|
||||
message = m.frame.GetPayloadText()
|
||||
LogInfo("received message:", message)
|
||||
|
||||
m.server.TriggerOnMessage(m, message)
|
||||
end if
|
||||
end if
|
||||
end function
|
||||
|
||||
function IsClosed() as boolean
|
||||
return m.state = Web.WebSocketConnectionState.CLOSED
|
||||
end function
|
||||
|
||||
function Close() as void
|
||||
if m.IsClosed()
|
||||
return
|
||||
end if
|
||||
m.socket.Close()
|
||||
m.state = Web.WebSocketConnectionState.CLOSED
|
||||
m.server.TriggerOnDisconnect(m)
|
||||
end function
|
||||
|
||||
function Send(text as string) as void
|
||||
if m.IsClosed()
|
||||
return
|
||||
end if
|
||||
m.frame.SetPayloadText(text)
|
||||
written_bytes = m.frame.WriteToBuffer(m.buffer, 0, Web.BUFSIZE)
|
||||
if written_bytes > 0
|
||||
total_sent = 0
|
||||
' TODO:P0 buffer instead of a while loop
|
||||
while total_sent < written_bytes
|
||||
sent = m.socket.Send(m.buffer, total_sent, written_bytes - total_sent)
|
||||
if sent > 0
|
||||
total_sent += sent
|
||||
LogInfo("Sent bytes:", total_sent, "out of:", written_bytes)
|
||||
end if
|
||||
end while
|
||||
end if
|
||||
end function
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Web
|
||||
|
||||
enum WebSocketConnectionState
|
||||
CONNECTING = 0 'We probably don't ever see this state, since we upgrade existing connections from web server
|
||||
OPEN = 1
|
||||
CLOSING = 2
|
||||
CLOSED = 3
|
||||
end enum
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,139 @@
|
||||
import "pkg:/components/Web/WebServer/WebSockets/WebSocketOpCode.bs"
|
||||
|
||||
namespace Web
|
||||
|
||||
class WebSocketFrame
|
||||
public fin
|
||||
public opcode
|
||||
public is_masked
|
||||
public payload_size
|
||||
public payload_size_buffer
|
||||
public mask
|
||||
public payload
|
||||
|
||||
private index
|
||||
private length
|
||||
|
||||
function new()
|
||||
m.payload = CreateObject("roByteArray")
|
||||
m.payload_size_buffer = CreateObject("roByteArray")
|
||||
end function
|
||||
|
||||
function ReadFromBuffer(buffer as object, index as integer, length as integer) as boolean
|
||||
m.buffer = buffer
|
||||
m.index = index
|
||||
m.length = length
|
||||
|
||||
firstByte = m.ReadByte()
|
||||
m.fin = (firstByte and &h80) <> 0
|
||||
m.opcode = firstByte and &hf
|
||||
|
||||
if not m.fin
|
||||
return false
|
||||
end if
|
||||
|
||||
if m.opcode <> Web.WebSocketOpCode.TEXT
|
||||
return false
|
||||
end if
|
||||
|
||||
secondByte = m.ReadByte()
|
||||
m.is_masked = (secondByte and &h80) <> 0
|
||||
if not m.is_masked
|
||||
return false
|
||||
end if
|
||||
|
||||
if not m.ReadPayloadSize(secondByte)
|
||||
return false
|
||||
end if
|
||||
|
||||
m.mask = [m.ReadByte(), m.ReadByte(), m.ReadByte(), m.ReadByte()]
|
||||
|
||||
m.payload.Clear()
|
||||
for i = 0 to m.payload_size - 1
|
||||
m.payload[i] = m.Xor(m.ReadByte(), m.mask[i mod 4])
|
||||
end for
|
||||
|
||||
return true
|
||||
end function
|
||||
|
||||
function ReadPayloadSize(secondByte as integer) as boolean
|
||||
payloadSize = secondByte and &h7F
|
||||
if payloadSize < 126
|
||||
m.payload_size = payloadSize
|
||||
return true
|
||||
else if payloadSize = 126
|
||||
payloadSize = (m.ReadByte() << 8) or m.ReadByte()
|
||||
m.payload_size = payloadSize
|
||||
return true
|
||||
else
|
||||
'TODO: Read size in 8 bytes
|
||||
return false
|
||||
end if
|
||||
end function
|
||||
|
||||
function WritePayloadSize(payloadSize as integer)
|
||||
if payloadSize < 126
|
||||
m.WriteByte(payloadSize)
|
||||
else if payloadSize < 65536
|
||||
m.WriteByte(126)
|
||||
m.WriteByte(payloadSize >> 8)
|
||||
m.WriteByte(payloadSize and &hFF)
|
||||
else
|
||||
m.WriteByte(127)
|
||||
for i = 7 to 0 step -1
|
||||
m.payload_size_buffer[i] = payloadSize and &hFF
|
||||
payloadSize = payloadSize >> 8
|
||||
next
|
||||
for i = 0 to 7
|
||||
m.WriteByte(m.payload_size_buffer[i])
|
||||
next
|
||||
end if
|
||||
end function
|
||||
|
||||
function WriteToBuffer(buffer as object, index as integer, length as integer) as integer
|
||||
m.buffer = buffer
|
||||
m.index = index
|
||||
m.length = length
|
||||
|
||||
m.WriteByte(m.fin or m.opcode)
|
||||
m.WritePayloadSize(m.payload_size)
|
||||
|
||||
for i = 0 to m.payload_size - 1
|
||||
m.buffer[m.index] = m.payload[i]
|
||||
m.index++
|
||||
end for
|
||||
|
||||
return m.index - index
|
||||
end function
|
||||
|
||||
function GetPayloadText() as string
|
||||
return m.payload.ToAsciiString()
|
||||
end function
|
||||
|
||||
function SetPayloadText(text as string)
|
||||
m.fin = &h80
|
||||
m.opcode = Web.WebSocketOpCode.TEXT
|
||||
m.is_masked = false
|
||||
m.mask = invalid
|
||||
m.payload.FromAsciiString(text)
|
||||
m.payload_size = m.payload.Count()
|
||||
end function
|
||||
|
||||
function ReadByte() as integer
|
||||
byte = m.buffer[m.index]
|
||||
m.index++
|
||||
return byte
|
||||
end function
|
||||
|
||||
function WriteByte(byte as integer) as integer
|
||||
m.buffer[m.index] = byte
|
||||
m.index++
|
||||
return byte
|
||||
end function
|
||||
|
||||
function Xor(a as integer, b as integer) as integer
|
||||
return (a or b) and not (a and b)
|
||||
end function
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,51 @@
|
||||
import "pkg:/source/utils/CryptoUtils.bs"
|
||||
|
||||
namespace Http
|
||||
|
||||
const WEB_SOCKET_MAGIC_STRING = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||
|
||||
class WebSocketMiddleware extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.Get("*", function(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
connection = context.connection
|
||||
|
||||
if LCase(ValidString(request.headers["Connection"])) <> "upgrade"
|
||||
return false
|
||||
end if
|
||||
|
||||
if LCase(ValidString(request.headers["Upgrade"])) <> "websocket"
|
||||
return false
|
||||
end if
|
||||
|
||||
webSocketKey = request.headers["Sec-WebSocket-Key"]
|
||||
if webSocketKey = invalid
|
||||
return false
|
||||
end if
|
||||
|
||||
sha1Hash = CryptoUtils.GetSha1(webSocketKey + Http.WEB_SOCKET_MAGIC_STRING)
|
||||
buffer = CreateObject("roByteArray")
|
||||
buffer.FromHexString(sha1Hash)
|
||||
webSocketAccept = StringUtils.ToBase64(buffer.ToAsciiString())
|
||||
|
||||
response.http_code = 101
|
||||
response.headers["Connection"] = "Upgrade"
|
||||
response.headers["Upgrade"] = "websocket"
|
||||
response.headers["Sec-WebSocket-Accept"] = webSocketAccept
|
||||
response.header_only = true
|
||||
|
||||
' We want this connection to be upgraded to a WebSocket connection
|
||||
' The connection will stop being handled by the HTTP server and will be handled by the WebSocket server
|
||||
connection.ws_upgrade = true
|
||||
|
||||
return true
|
||||
end function)
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Web
|
||||
|
||||
enum WebSocketOpCode
|
||||
CONTINUATION = 0
|
||||
TEXT = 1
|
||||
BINARY = 2
|
||||
CLOSE = 8
|
||||
PING = 9
|
||||
PONG = 10
|
||||
end enum
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,100 @@
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "WebSocketConnection.bs"
|
||||
|
||||
namespace Web
|
||||
|
||||
class WebSocketServer
|
||||
private webServer as object
|
||||
private connections = invalid
|
||||
private buffer = invalid
|
||||
private onConnectListeners
|
||||
private onDisconnectListeners
|
||||
private onMessageListeners
|
||||
|
||||
function new(webServer as object)
|
||||
m.connections = CreateObject("roAssociativeArray")
|
||||
m.onConnectListeners = []
|
||||
m.onDisconnectListeners = []
|
||||
m.onMessageListeners = []
|
||||
m.webServer = webServer
|
||||
end function
|
||||
|
||||
function OnConnect(callback as function)
|
||||
m.onConnectListeners.push(callback)
|
||||
end function
|
||||
|
||||
function OnDisconnect(callback as function)
|
||||
m.onDisconnectListeners.push(callback)
|
||||
end function
|
||||
|
||||
function OnMessage(callback as function)
|
||||
m.onMessageListeners.push(callback)
|
||||
end function
|
||||
|
||||
function AddConnection(socket as object)
|
||||
client = new Web.WebSocketConnection(m, socket)
|
||||
m.connections[client.id] = client
|
||||
m.TriggerOnConnect(client)
|
||||
end function
|
||||
|
||||
function TriggerOnConnect(connection as Web.WebSocketConnection)
|
||||
LogInfo("on connect")
|
||||
for each listener in m.onConnectListeners
|
||||
listener({
|
||||
server: m,
|
||||
connection: connection
|
||||
})
|
||||
end for
|
||||
end function
|
||||
|
||||
function TriggerOnDisconnect(connection as Web.WebSocketConnection)
|
||||
LogInfo("on disconnect")
|
||||
for each listener in m.onDisconnectListeners
|
||||
listener({
|
||||
server: m,
|
||||
connection: connection
|
||||
})
|
||||
end for
|
||||
end function
|
||||
|
||||
function TriggerOnMessage(connection as Web.WebSocketConnection, message as string)
|
||||
for each listener in m.onMessageListeners
|
||||
listener({
|
||||
server: m,
|
||||
connection: connection,
|
||||
message: message
|
||||
})
|
||||
end for
|
||||
end function
|
||||
|
||||
function Poll()
|
||||
connections = m.connections
|
||||
for each id in connections
|
||||
connection = connections[id]
|
||||
if connection.IsClosed()
|
||||
connections.delete(id)
|
||||
else
|
||||
connection.Poll()
|
||||
end if
|
||||
end for
|
||||
end function
|
||||
|
||||
function Close()
|
||||
connections = m.connections
|
||||
for each id in connections
|
||||
conn = connections[id]
|
||||
conn.Close()
|
||||
end for
|
||||
end function
|
||||
|
||||
function Send(text as string) as void
|
||||
connections = m.connections
|
||||
for each id in connections
|
||||
conn = connections[id]
|
||||
conn.Send(text)
|
||||
end for
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
Reference in new issue
Block a user